Skip to content

Commit

Permalink
fix: Fixing the broken file tests, add dirhash test.
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Glastra <[email protected]>
  • Loading branch information
matglas authored and jkjell committed May 2, 2024
1 parent 746bc8a commit 556f23d
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions attestation/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path/filepath"
"testing"

"github.com/gobwas/glob"
"github.com/in-toto/go-witness/cryptoutil"
"github.com/stretchr/testify/require"
)
Expand All @@ -38,13 +39,15 @@ func TestBrokenSymlink(t *testing.T) {
symTestDir := filepath.Join(dir, "symTestDir")
require.NoError(t, os.Symlink(testDir, symTestDir))

_, err := RecordArtifacts(dir, map[string]cryptoutil.DigestSet{}, []cryptoutil.DigestValue{{Hash: crypto.SHA256}}, map[string]struct{}{})
dirHash := make([]glob.Glob, 0)

_, err := RecordArtifacts(dir, map[string]cryptoutil.DigestSet{}, []cryptoutil.DigestValue{{Hash: crypto.SHA256}}, map[string]struct{}{}, dirHash)
require.NoError(t, err)

// remove the symlinks and make sure we don't get an error back
require.NoError(t, os.RemoveAll(testDir))
require.NoError(t, os.RemoveAll(testFile))
_, err = RecordArtifacts(dir, map[string]cryptoutil.DigestSet{}, []cryptoutil.DigestValue{{Hash: crypto.SHA256}}, map[string]struct{}{})
_, err = RecordArtifacts(dir, map[string]cryptoutil.DigestSet{}, []cryptoutil.DigestValue{{Hash: crypto.SHA256}}, map[string]struct{}{}, dirHash)
require.NoError(t, err)
}

Expand All @@ -57,7 +60,40 @@ func TestSymlinkCycle(t *testing.T) {
symTestDir := filepath.Join(dir, "symTestDir")
require.NoError(t, os.Symlink(dir, symTestDir))

dirHash := make([]glob.Glob, 0)

// if a symlink cycle weren't properly handled this would be an infinite loop
_, err := RecordArtifacts(dir, map[string]cryptoutil.DigestSet{}, []cryptoutil.DigestValue{{Hash: crypto.SHA256}}, map[string]struct{}{})
_, err := RecordArtifacts(dir, map[string]cryptoutil.DigestSet{}, []cryptoutil.DigestValue{{Hash: crypto.SHA256}}, map[string]struct{}{}, dirHash)
require.NoError(t, err)
}

func TestDirHash(t *testing.T) {
dir := t.TempDir()
testFile := filepath.Join(dir, "testfile")
require.NoError(t, os.WriteFile(testFile, []byte("some dummy data"), os.ModePerm))
testDir := filepath.Join(dir, "testdir")
require.NoError(t, os.Mkdir(testDir, os.ModePerm))
testFile2 := filepath.Join(testDir, "testfile2")
require.NoError(t, os.WriteFile(testFile2, []byte("more dummy data"), os.ModePerm))

dirHashGlobs := make([]glob.Glob, 0)

dirHash := "testdir"
dirHashGlobItem, _ := glob.Compile(dirHash)
dirHashGlobs = append(dirHashGlobs, dirHashGlobItem)

artifacts, err := RecordArtifacts(dir, map[string]cryptoutil.DigestSet{}, []cryptoutil.DigestValue{{Hash: crypto.SHA256}}, map[string]struct{}{}, dirHashGlobs)
require.NoError(t, err)

// Below command is example usage on the above created scenario for testdir.
// find . -type f | cut -c3- | LC_ALL=C sort | xargs -r sha256sum | sha256sum
dirHashSha256 := "ba9842eac063209c5f67c5a202b2b3a710f8f845f1d064f54af56763645b895b"

require.Len(t, artifacts, 2)

dirDigestSet := artifacts["testdir/"]
dirDigestSetMap, err := dirDigestSet.ToNameMap()
require.NoError(t, err)

require.Equal(t, dirDigestSetMap["sha256"], dirHashSha256)
}

0 comments on commit 556f23d

Please sign in to comment.