From 18827a74f407cb4c0264086a72d7b6870e92a95f Mon Sep 17 00:00:00 2001 From: brendan-coughlan Date: Thu, 6 Feb 2025 02:38:30 -0800 Subject: [PATCH] shell out for the sha256sum cli as well --- pkg/snapshot/snapshot_test.go | 59 ++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/pkg/snapshot/snapshot_test.go b/pkg/snapshot/snapshot_test.go index aa2f56f6..b1666a6e 100644 --- a/pkg/snapshot/snapshot_test.go +++ b/pkg/snapshot/snapshot_test.go @@ -2,10 +2,12 @@ package snapshot import ( "crypto/sha256" + "encoding/hex" "fmt" "net/http" "net/http/httptest" "os" + "os/exec" "path/filepath" "strings" "testing" @@ -90,9 +92,17 @@ func TestValidateRestoreConfig(t *testing.T) { err := os.WriteFile(snapshotFile, content, 0644) assert.NoError(t, err, "Writing to snapshot file should not fail") - // Correct the hash to match the expected value and include the file name - expectedHash := "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72" - hashFileContent := fmt.Sprintf("%s %s\n", expectedHash, filepath.Base(snapshotFile)) + // Generate the hash using the CLI + cmd := exec.Command("shasum", "-a", "256", snapshotFile) + output, err := cmd.Output() + assert.NoError(t, err, "Generating hash with CLI should not fail") + + // Extract the hash from the CLI output + hashParts := strings.Fields(string(output)) + if len(hashParts) < 1 { + t.Fatal("Failed to parse hash from CLI output") + } + hashFileContent := fmt.Sprintf("%s %s\n", hashParts[0], filepath.Base(snapshotFile)) err = os.WriteFile(snapshotHashFile, []byte(hashFileContent), 0644) assert.NoError(t, err, "Writing to hash file should not fail") @@ -172,6 +182,15 @@ func TestSaveOutputFileHash(t *testing.T) { assert.Equal(t, 2, len(hashParts), "Output hash file should contain two parts: hash and filename") assert.Equal(t, filepath.Base(outputFile), hashParts[1], "Output hash file should contain the correct filename") assert.Equal(t, sha256.Size*2, len(hashParts[0]), "Output hash file should have the correct hash length") + + // Compare the hash with the one generated by sha256sum + cmd := exec.Command("sha256sum", outputFile) + sha256sumOutput, err := cmd.Output() + assert.NoError(t, err, "Executing sha256sum should not fail") + + sha256sumParts := strings.Fields(string(sha256sumOutput)) + assert.Equal(t, 2, len(sha256sumParts), "sha256sum output should contain two parts: hash and filename") + assert.Equal(t, sha256sumParts[0], hashParts[0], "Hashes should match between sha256sum and saveOutputFileHash") } func TestSaveOutputFileHashCompatibilityWithSha256sum(t *testing.T) { @@ -188,6 +207,15 @@ func TestSaveOutputFileHashCompatibilityWithSha256sum(t *testing.T) { err = saveOutputFileHash(outputFile, outputHashFile) assert.NoError(t, err, "Saving output file hash should not fail") + // Shell out to sha256sum to verify the hash + cmd := exec.Command("sha256sum", outputFile) + sha256sumOutput, err := cmd.Output() + assert.NoError(t, err, "Executing sha256sum should not fail") + + // Extract the hash from the sha256sum output + sha256sumParts := strings.Fields(string(sha256sumOutput)) + assert.Equal(t, 2, len(sha256sumParts), "sha256sum output should contain two parts: hash and filename") + // Read the generated hash file hashContent, err := os.ReadFile(outputHashFile) assert.NoError(t, err, "Reading output hash file should not fail") @@ -198,8 +226,8 @@ func TestSaveOutputFileHashCompatibilityWithSha256sum(t *testing.T) { assert.Equal(t, filepath.Base(outputFile), hashParts[1], "Output hash file should contain the correct filename") assert.Equal(t, sha256.Size*2, len(hashParts[0]), "Output hash file should have the correct hash length") - // Optionally, you can print the hash content to manually verify with sha256sum - t.Logf("Generated hash file content: %s", string(hashContent)) + // Compare the hash from sha256sum with the one generated by saveOutputFileHash + assert.Equal(t, sha256sumParts[0], hashParts[0], "Hashes should match between sha256sum and saveOutputFileHash") } func TestCleanupTempFiles(t *testing.T) { @@ -229,9 +257,14 @@ func TestValidateInputFileHash(t *testing.T) { err := os.WriteFile(inputFile, content, 0644) assert.NoError(t, err, "Writing to input file should not fail") - // Use the saveOutputFileHash function to create the hash file - err = saveOutputFileHash(inputFile, hashFile) - assert.NoError(t, err, "Saving input file hash should not fail") + // Shell out to sha256sum to create the hash file + cmd := exec.Command("sha256sum", inputFile) + sha256sumOutput, err := cmd.Output() + assert.NoError(t, err, "Executing sha256sum should not fail") + + // Write the sha256sum output to the hash file + err = os.WriteFile(hashFile, sha256sumOutput, 0644) + assert.NoError(t, err, "Writing sha256sum output to hash file should not fail") err = validateInputFileHash(inputFile, hashFile) assert.NoError(t, err, "Input file hash should be valid") @@ -328,6 +361,16 @@ func TestCreateAndRestoreSnapshot(t *testing.T) { assert.NoError(t, err, "Snapshot file should be created") assert.Greater(t, fileInfo.Size(), int64(4096), "Snapshot file size should be greater than 4KB") + // Verify the SHA256 hash of the created snapshot file + hash, err := getFileHash(dumpFile) + assert.NoError(t, err, "Getting file hash should not fail") + + expectedHash, err := exec.Command("sha256sum", dumpFile).Output() + assert.NoError(t, err, "Executing sha256sum command should not fail") + + expectedHashStr := strings.Fields(string(expectedHash))[0] + assert.Equal(t, expectedHashStr, hex.EncodeToString(hash), "File hash should match expected hash from sha256sum") + t.Cleanup(func() { postgres.TeardownTestDatabase(dbName, cfg, dbGrm, l) })