Skip to content

Commit

Permalink
shell out for the sha256sum cli as well
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanCoughlan5 committed Feb 6, 2025
1 parent 4bd2504 commit 18827a7
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions pkg/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package snapshot

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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) {
Expand All @@ -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")
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit 18827a7

Please sign in to comment.