Skip to content

Commit 759ef62

Browse files
authored
test: compare sha of gzip files between source and client server images (#14)
1 parent e609c12 commit 759ef62

File tree

1 file changed

+101
-3
lines changed

1 file changed

+101
-3
lines changed

test/acceptance/client_server_test.go

+101-3
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,41 @@ package acceptance
22

33
import (
44
"context"
5+
"crypto/sha256"
56
"debug/elf"
67
"debug/macho"
78
"debug/pe"
89
"fmt"
10+
"io"
11+
"log"
912
"os"
1013
"path/filepath"
14+
"strings"
1115
"time"
1216

1317
. "github.com/onsi/ginkgo/v2"
1418
. "github.com/onsi/gomega"
1519
"github.com/securesign/structural-tests/test/support"
1620
)
1721

18-
const cliServerPathMask = "/var/www/html/clients/%s/%s-%s.gz"
22+
const (
23+
cliServerFileMask = "%s-%s.gz"
24+
cliServerPathMask = "/var/www/html/clients/%s/" + cliServerFileMask
25+
26+
cliImageBasePath = "/usr/local/bin"
27+
cliImageFileMask = "%s_cli_%s_%s.gz"
28+
)
1929

2030
var _ = Describe("Client server", Ordered, func() {
2131

2232
var clientServerImage string
33+
var snapshotImages support.SnapshotMap
2334
var tmpDir string
2435

2536
Describe("client-server image", func() {
2637
It("snapshot.json", func() {
27-
snapshotImages, err := support.ParseSnapshotImages()
38+
var err error
39+
snapshotImages, err = support.ParseSnapshotImages()
2840
Expect(err).NotTo(HaveOccurred())
2941

3042
clientServerImage = snapshotImages["client-server-image"]
@@ -35,6 +47,7 @@ var _ = Describe("Client server", Ordered, func() {
3547
var err error
3648
tmpDir, err = os.MkdirTemp("", "client-server")
3749
Expect(err).NotTo(HaveOccurred())
50+
log.Printf("TEMP directory: %s", tmpDir)
3851
})
3952
})
4053

@@ -49,6 +62,22 @@ var _ = Describe("Client server", Ordered, func() {
4962
func(cli string, matrix support.OSArchMatrix) {
5063
for osName, archs := range matrix {
5164
for _, arch := range archs {
65+
var image string
66+
var gzipServerSHA []byte
67+
68+
It("init", func() {
69+
switch cli {
70+
case "createtree", "updatetree":
71+
image = snapshotImages[cli+"-image"]
72+
case "tuftool":
73+
image = snapshotImages["tuf-tool-image"]
74+
case "rekor-cli":
75+
image = snapshotImages["rekor-cli-image"]
76+
default:
77+
image = snapshotImages[cli+"-cli-image"]
78+
}
79+
})
80+
5281
It(fmt.Sprintf("verify %s-%s executable", osName, arch), func() {
5382
osPath := filepath.Join(tmpDir, osName)
5483
Expect(os.MkdirAll(osPath, 0755)).To(Succeed())
@@ -59,13 +88,66 @@ var _ = Describe("Client server", Ordered, func() {
5988
Expect(support.FileFromImage(ctx, clientServerImage, fmt.Sprintf(cliServerPathMask, osName, cli, arch), osPath)).To(Succeed())
6089

6190
By("decompress gzip")
62-
gzipPath := filepath.Join(osPath, cli+"-"+arch+".gz")
91+
gzipPath := filepath.Join(osPath, fmt.Sprintf(cliServerFileMask, cli, arch))
6392
targetPath := filepath.Join(osPath, cli+"-"+arch)
6493
Expect(support.DecompressGzipFile(gzipPath, targetPath)).To(Succeed())
6594

6695
By("verify executable OS and arch")
6796
executable := filepath.Join(tmpDir, osName, cli+"-"+arch)
6897
Expect(verifyExecutable(executable, osName, arch)).To(Succeed())
98+
99+
By("checksums of gzip file")
100+
var err error
101+
gzipServerSHA, err = checksumFile(filepath.Join(osPath, fmt.Sprintf(cliServerFileMask, cli, arch)))
102+
Expect(err).NotTo(HaveOccurred())
103+
})
104+
105+
It(fmt.Sprintf("compare checkum of %s-%s with source image", osName, arch), func() {
106+
var (
107+
targetPath = tmpDir
108+
fileName string
109+
filePath string
110+
)
111+
112+
switch cli {
113+
case "tuftool":
114+
Skip("`tuftool` do not have gzip in source image")
115+
case "ec":
116+
Skip("`ec` source image is not part of handover")
117+
case "cosign", "updatetree", "createtree":
118+
if osName == "windows" { //nolint:goconst
119+
fileName = fmt.Sprintf("%s-%s-%s.exe.gz", cli, osName, arch)
120+
} else {
121+
fileName = fmt.Sprintf("%s-%s-%s.gz", cli, osName, arch)
122+
}
123+
case "rekor-cli", "fetch-tsa-certs":
124+
ncli := strings.ReplaceAll(cli, "-", "_")
125+
if osName == "windows" {
126+
fileName = fmt.Sprintf("%s_%s_%s.exe.gz", ncli, osName, arch)
127+
} else {
128+
fileName = fmt.Sprintf("%s_%s_%s.gz", ncli, osName, arch)
129+
}
130+
default:
131+
if osName == "windows" {
132+
fileName = fmt.Sprintf(cliImageFileMask, cli, osName, arch+".exe")
133+
} else {
134+
fileName = fmt.Sprintf(cliImageFileMask, cli, osName, arch)
135+
}
136+
}
137+
filePath = filepath.Join(cliImageBasePath, fileName)
138+
139+
By("get gzip file from container image")
140+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
141+
defer cancel()
142+
143+
Expect(support.FileFromImage(ctx, image, filePath, targetPath)).To(Succeed())
144+
145+
By("checksums of gzip file")
146+
gzipImageSHA, err := checksumFile(filepath.Join(targetPath, fileName))
147+
Expect(err).NotTo(HaveOccurred())
148+
149+
By("compare checksum with client server file")
150+
Expect(gzipImageSHA).To(Equal(gzipServerSHA))
69151
})
70152
}
71153
}
@@ -165,3 +247,19 @@ func getMachOCpuType(arch string) macho.Cpu {
165247
return 0 // Unsupported architecture
166248
}
167249
}
250+
251+
// checksumFile computes the SHA256 checksum of a given file.
252+
func checksumFile(filePath string) ([]byte, error) {
253+
file, err := os.Open(filePath)
254+
if err != nil {
255+
return nil, fmt.Errorf("failed to open file %s: %w", filePath, err)
256+
}
257+
defer func() { _ = file.Close() }()
258+
259+
hasher := sha256.New()
260+
if _, err := io.Copy(hasher, file); err != nil {
261+
return nil, fmt.Errorf("failed to hash file %s: %w", filePath, err)
262+
}
263+
264+
return hasher.Sum(nil), nil
265+
}

0 commit comments

Comments
 (0)