[lxc-devel] [distrobuilder/master] Update getChecksum function

monstermunchkin on Github lxc-bot at linuxcontainers.org
Fri Oct 11 19:07:59 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 310 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20191011/9cbb09b0/attachment.bin>
-------------- next part --------------
From 4dd7dd704a687496bac8a831f351583767a48085 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Fri, 11 Oct 2019 21:02:08 +0200
Subject: [PATCH 1/2] shared: Update getChecksum function

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/net.go  | 12 +++---------
 shared/util.go | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/shared/net.go b/shared/net.go
index aa16210..993edf7 100644
--- a/shared/net.go
+++ b/shared/net.go
@@ -1,7 +1,6 @@
 package shared
 
 import (
-	"bufio"
 	"fmt"
 	"hash"
 	"io"
@@ -9,7 +8,6 @@ import (
 	"net/http"
 	"os"
 	"path/filepath"
-	"regexp"
 	"strings"
 
 	lxd "github.com/lxc/lxd/shared"
@@ -135,13 +133,9 @@ func downloadChecksum(targetDir string, URL string, fname string, hashFunc hash.
 
 	tempFile.Seek(0, 0)
 
-	scanner := bufio.NewScanner(tempFile)
-	for scanner.Scan() {
-		s := strings.Split(scanner.Text(), " ")
-		matched, _ := regexp.MatchString(fmt.Sprintf(".*%s", filepath.Base(fname)), s[len(s)-1])
-		if matched && (hashLen == 0 || hashLen == len(strings.TrimSpace(s[0]))) {
-			return s[0], nil
-		}
+	checksum := getChecksum(filepath.Base(fname), hashLen, tempFile)
+	if checksum != "" {
+		return checksum, nil
 	}
 
 	return "", fmt.Errorf("Could not find checksum")
diff --git a/shared/util.go b/shared/util.go
index f8873b5..04a3937 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -1,6 +1,7 @@
 package shared
 
 import (
+	"bufio"
 	"bytes"
 	"fmt"
 	"io"
@@ -365,3 +366,26 @@ func GetTargetDir(def DefinitionImage) string {
 
 	return targetDir
 }
+
+func getChecksum(fname string, hashLen int, r io.Reader) string {
+	scanner := bufio.NewScanner(r)
+
+	for scanner.Scan() {
+		if !strings.Contains(scanner.Text(), fname) {
+			continue
+		}
+
+		for _, s := range strings.Split(scanner.Text(), " ") {
+			m, _ := regexp.MatchString("[[:xdigit:]]+", s)
+			if !m {
+				continue
+			}
+
+			if hashLen == 0 || hashLen == len(strings.TrimSpace(s)) {
+				return s
+			}
+		}
+	}
+
+	return ""
+}

From d10fe3ba36de434d880508172422a641994346ed Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Fri, 11 Oct 2019 21:02:28 +0200
Subject: [PATCH 2/2] shared: Add test for getChecksum

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/util_test.go | 88 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)

diff --git a/shared/util_test.go b/shared/util_test.go
index a964728..994eae3 100644
--- a/shared/util_test.go
+++ b/shared/util_test.go
@@ -1,6 +1,8 @@
 package shared
 
 import (
+	"bytes"
+	"io"
 	"log"
 	"os"
 	"path"
@@ -213,3 +215,89 @@ func TestSetEnvVariables(t *testing.T) {
 	require.False(t, set, "Expected 'BAR' to be unset")
 	require.Empty(t, val)
 }
+
+func Test_getChecksum(t *testing.T) {
+	type args struct {
+		fname   string
+		hashLen int
+		r       io.Reader
+	}
+	tests := []struct {
+		name string
+		args args
+		want string
+	}{
+		{
+			"CentOS-8-x86_64-1905-dvd1.iso",
+			args{
+				"CentOS-8-x86_64-1905-dvd1.iso",
+				64,
+				bytes.NewBufferString(`-----BEGIN PGP SIGNED MESSAGE-----
+Hash: SHA256
+
+# CentOS-8-x86_64-1905-boot.iso: 559939584 bytes
+SHA256 (CentOS-8-x86_64-1905-boot.iso) = a7993a0d4b7fef2433e0d4f53530b63c715d3aadbe91f152ee5c3621139a2cbc
+# CentOS-8-x86_64-1905-dvd1.iso: 7135559680 bytes
+SHA256 (CentOS-8-x86_64-1905-dvd1.iso) = ea17ef71e0df3f6bf1d4bf1fc25bec1a76d1f211c115d39618fe688be34503e8
+-----BEGIN PGP SIGNATURE-----
+Version: GnuPG v1
+
+iQIVAwUBXYirdQW1VbOEg8ZdAQigchAAj+LbZtV7BQTnfB3i+fzECuomjsTZE8Ki
+zUs9fLA67aayBL1KiavIzURMgjqj/+dXWr73Kv49pELngrznPlEPOclCaPkAKSe0
+V2Nj56AUhT/tHGcBoNvD0UrC0nCObMLx6PI2FDEozEELyQR32Syjtb0y5CDnxRvX
+6JeGWPWQsf+jXdZS/GUUh39XR5va5YAwues0qLfqNf7nfUk07tmU0pcCG+vRN13H
+45av+1/49zbxn4Y/Km2AaAbmqX8LlQpppVYE2K5V73YsG3o6eSU1DwjDijQHYPOK
+ZUixjbhh5xkOzvhv5HUETvPncbnOez+xLwDPFAMFz/jX/4BgLWpA1/PM/3xcFFij
+qXBlZh+QLWm1Z8UCBftDc+RqoktI460cqL/SsnOyHmQ+95QLt20yR46hi3oZ6/Cv
+cUdXaql3iCNWZUvi27Dr8bExqaVaJn0zeDCItPWUA7NwxXP2TlGs2VXC4E37HQhZ
+SyuCQZMrwGmDJl7gMOE7kZ/BifKvrycAlvTPvhq0jrUwLvokX8QhoTmAwRdzwGSk
+9nS+BkoK7xW5lSATuVYEcCkb2fL+qDKuSBJMuKhQNhPs6rN5OEZL3gU54so7Jyz9
+OmR+r+1+/hELjOIsPcR4IiyauJQXXgtJ28G7swMsrl07PYHOU+awvB/N9GyUzNAM
+RP3G/3Z1T3c=
+=HgZm
+-----END PGP SIGNATURE-----`),
+			},
+			"ea17ef71e0df3f6bf1d4bf1fc25bec1a76d1f211c115d39618fe688be34503e8",
+		},
+
+		{
+			"CentOS-7-x86_64-Minimal-1908.iso",
+			args{
+				"CentOS-7-x86_64-Minimal-1908.iso",
+				64,
+				bytes.NewBufferString(`-----BEGIN PGP SIGNED MESSAGE-----
+Hash: SHA256
+
+9bba3da2876cb9fcf6c28fb636bcbd01832fe6d84cd7445fa58e44e569b3b4fe  CentOS-7-x86_64-DVD-1908.iso
+bd5e6ca18386e8a8e0b5a9e906297b5610095e375e4d02342f07f32022b13acf  CentOS-7-x86_64-Everything-1908.iso
+ba827210d4eb9313fc19120b9b85e7baef234c7f81bc55847a336114ddac20cb  CentOS-7-x86_64-LiveGNOME-1908.iso
+0ef3310d13f7fc140ec5180dc05369d2f473e802577466825205d17e46ef5a9b  CentOS-7-x86_64-LiveKDE-1908.iso
+9a2c47d97b9975452f7d582264e9fc16d108ed8252ac6816239a3b58cef5c53d  CentOS-7-x86_64-Minimal-1908.iso
+6ffa7ad44e8716e4cd6a5c3a85ba5675a935fc0448c260f43b12311356ba85ad  CentOS-7-x86_64-NetInstall-1908.iso
+-----BEGIN PGP SIGNATURE-----
+Version: GnuPG v1
+
+iQIVAwUBXYDRPyTGqKf0qA61AQhHcg/+LvGu95Y825HoUpS9JPFIb7axkIj8fx5/
+Qw2fN+BQtd7W7jcUNmofaajjWyqP5b5Q0iCyNrbhAT6CO4lVVY1z+OxCefAk/Wve
+go1fSY5cRn7LRtvDuKrkDHJE+nYCVBg8ksWRBm2Xwx2sy4AxP2PAs7Oh3QvkK+9V
+199YPLAQ+m4cFdBTTR3Dl78OEKVgjp5O351n4q0pKp72jxhjCZ+tk+dWGg9JEBSb
+53nMkwnqTWZzFYpLqGc3fOfscc38oIvet0y3gVbZLNsE25AwwMxqjlC/Z2TqXwc5
+1JoZI7XkKggWH6fA4BuzcOtezGMPMPDaqnNhfAWzYq3CsQAA8aQuQaCnGoG2dNN/
+fdhGRrbXdpAFbKhfQ/dbKSvDGNvZTFfRfD9m5AJ/ddUAv7DFr4VeVur1KMTqtVO2
+NvcLRn7BnkN7ZRqvqdT4kDyndWgQCABahqI6OcC8mmc449JecloQK4U1zGhKMRor
+33OtMEW/KhnSOu9pK6+CRnPykyIk2yxUCJ11YFXCKNKfX2cmdFf0puUsmefB6O7E
+1nVE3n0aZVSVmebl3sjVJvstT2oyVNynnSQ/Fw3NBAiHe5FvgUnVqHQKyg1nnTet
+hsfTg6egTQUGOB2fVgt7n3p1HIvCjXAjKo6Wa3R8+aoapQ74Gcok3I3rNoL1jWbW
+Z4iksZrx82g=
+=L746
+-----END PGP SIGNATURE-----`),
+			},
+			"9a2c47d97b9975452f7d582264e9fc16d108ed8252ac6816239a3b58cef5c53d",
+		},
+	}
+
+	for _, tt := range tests {
+		got := getChecksum(tt.args.fname, tt.args.hashLen, tt.args.r)
+		require.Equal(t, tt.want, got)
+	}
+}


More information about the lxc-devel mailing list