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 committed May 2, 2024
1 parent 0c8aed0 commit 8aa9302
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion attestation/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func RecordArtifacts(basePath string, baseArtifacts map[string]cryptoutil.Digest
return err
}

artifacts[relPath + string(os.PathSeparator)] = dir
artifacts[relPath+string(os.PathSeparator)] = dir
return filepath.SkipDir
}

Expand Down
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)
}
2 changes: 1 addition & 1 deletion cryptoutil/dirhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ func DirhHashSha256(files []string, open func(string) (io.ReadCloser, error)) (s
fmt.Fprintf(h, "%x %s\n", hf.Sum(nil), file)
}
return hex.EncodeToString(h.Sum(nil)), nil
}
}

0 comments on commit 8aa9302

Please sign in to comment.