diff --git a/in_toto/attestations_test.go b/in_toto/attestations_test.go index d3953c3b..d02f371f 100644 --- a/in_toto/attestations_test.go +++ b/in_toto/attestations_test.go @@ -122,7 +122,9 @@ func TestDecodeProvenanceStatementSLSA02(t *testing.T) { } func TestEncodeProvenanceStatementSLSA02(t *testing.T) { - var testTime = time.Unix(1597826280, 0) + // Ensure the time is in UTC + var testTime = time.Unix(1597826280, 0).UTC() + var p = ProvenanceStatement{ StatementHeader: StatementHeader{ Type: StatementInTotoV01, @@ -181,6 +183,7 @@ func TestEncodeProvenanceStatementSLSA02(t *testing.T) { }, }, } + var want = `{"_type":"https://in-toto.io/Statement/v0.1","predicateType":"https://slsa.dev/provenance/v0.2","subject":[{"name":"curl-7.72.0.tar.bz2","digest":{"sha256":"ad91970864102a59765e20ce16216efc9d6ad381471f7accceceab7d905703ef"}},{"name":"curl-7.72.0.tar.gz","digest":{"sha256":"d4d5899a3868fbb6ae1856c3e55a32ce35913de3956d1973caccd37bd0174fa2"}}],"predicate":{"builder":{"id":"https://github.com/Attestations/GitHubHostedActions@v1"},"buildType":"https://github.com/Attestations/GitHubActionsWorkflow@v1","invocation":{"configSource":{"uri":"git+https://github.com/curl/curl-docker@master","digest":{"sha1":"d6525c840a62b398424a78d792f457477135d0cf"},"entryPoint":"build.yaml:maketgz"}},"metadata":{"buildStartedOn":"2020-08-19T08:38:00Z","buildFinishedOn":"2020-08-19T08:38:00Z","completeness":{"parameters":true,"environment":false,"materials":true},"reproducible":false},"materials":[{"uri":"git+https://github.com/curl/curl-docker@master","digest":{"sha1":"d6525c840a62b398424a78d792f457477135d0cf"}},{"uri":"github_hosted_vm:ubuntu-18.04:20210123.1"},{"uri":"git+https://github.com/curl/"}]}}` b, err := json.Marshal(&p) @@ -291,7 +294,7 @@ func TestDecodeProvenanceStatementSLSA01(t *testing.T) { } func TestEncodeProvenanceStatementSLSA01(t *testing.T) { - var testTime = time.Unix(1597826280, 0) + var testTime = time.Unix(1597826280, 0).UTC() var p = ProvenanceStatementSLSA01{ StatementHeader: StatementHeader{ Type: StatementInTotoV01, diff --git a/in_toto/runlib_test.go b/in_toto/runlib_test.go index 6036f52f..4e9f829c 100644 --- a/in_toto/runlib_test.go +++ b/in_toto/runlib_test.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "os" - "path" "path/filepath" "reflect" "runtime" @@ -125,9 +124,22 @@ func TestGitPathSpec(t *testing.T) { // We will only calculate the hash for for.tar.gz // The symlink will not be added to the list right now, nor will we calculate a checksum for it. func TestSymlinkToFile(t *testing.T) { - if err := os.Symlink("foo.tar.gz", "foo.tar.gz.sym"); err != nil { - t.Errorf("could not create a symlink: %s", err) + // Create a dummy file to link to + if _, err := os.Create("foo.tar.gz"); err != nil { + t.Fatalf("could not create dummy file: %s", err) } + defer os.Remove("foo.tar.gz") + + // Attempt to create a symlink + err := os.Symlink("foo.tar.gz", "foo.tar.gz.sym") + if err != nil { + if testOSisWindows() { + t.Skip("skipping test; requires symlink creation privilege on Windows") + } else { + t.Fatalf("could not create a symlink: %s", err) + } + } + defer os.Remove("foo.tar.gz.sym") expected := map[string]HashObj{ "foo.tar.gz.sym": { @@ -149,30 +161,44 @@ func TestSymlinkToFile(t *testing.T) { // symTestA/linkToB -> symTestB and symTestB/linkToA -> symTestA func TestIndirectSymlinkCycles(t *testing.T) { if err := os.Mkdir("symTestA", 0700); err != nil { - t.Errorf("could not create tmpdir: %s", err) + t.Fatalf("could not create tmpdir: %s", err) } + defer os.RemoveAll("symTestA") + if err := os.Mkdir("symTestB", 0700); err != nil { - t.Errorf("could not create tmpdir: %s", err) + t.Fatalf("could not create tmpdir: %s", err) } + defer os.RemoveAll("symTestB") // we need to get the current working directory here, otherwise // os.Symlink() will create a wrong symlink dir, err := os.Getwd() if err != nil { - t.Error(err) + t.Fatalf("could not get current working directory: %s", err) } linkB := filepath.FromSlash("symTestA/linkToB.sym") linkA := filepath.FromSlash("symTestB/linkToA.sym") - if err := os.Symlink(dir+"/symTestA", linkA); err != nil { - t.Errorf("could not create a symlink: %s", err) + if err := os.Symlink(filepath.Join(dir, "symTestA"), linkA); err != nil { + if testOSisWindows() { + t.Skip("skipping test; requires symlink creation privilege on Windows") + } else { + t.Fatalf("could not create a symlink: %s", err) + } } - if err := os.Symlink(dir+"/symTestB", linkB); err != nil { - t.Errorf("could not create a symlink: %s", err) + defer os.Remove(linkA) + + if err := os.Symlink(filepath.Join(dir, "symTestB"), linkB); err != nil { + if testOSisWindows() { + t.Skip("skipping test; requires symlink creation privilege on Windows") + } else { + t.Fatalf("could not create a symlink: %s", err) + } } + defer os.Remove(linkB) - // provoke "symlink cycle detected" error + // Provoke "symlink cycle detected" error _, err = RecordArtifacts([]string{"symTestA/linkToB.sym", "symTestB/linkToA.sym", "foo.tar.gz"}, []string{"sha256"}, nil, nil, testOSisWindows(), true) if !errors.Is(err, ErrSymCycle) { t.Errorf("we expected: %s, we got: %s", ErrSymCycle, err) @@ -193,19 +219,25 @@ func TestIndirectSymlinkCycles(t *testing.T) { if err := os.Remove("symTestB"); err != nil { t.Errorf("could not remove path: %s", err) + t.Errorf("expected error: %s, got: %s", ErrSymCycle, err) } - } // TestSymlinkToFolder checks if we are successfully following symlinks to folders func TestSymlinkToFolder(t *testing.T) { if err := os.MkdirAll("symTest/symTest2", 0700); err != nil { - t.Errorf("could not create tmpdir: %s", err) + t.Fatalf("could not create tmpdir: %s", err) } + defer os.RemoveAll("symTest") if err := os.Symlink("symTest/symTest2", "symTmpfile.sym"); err != nil { - t.Errorf("could not create a symlink: %s", err) + if testOSisWindows() { + t.Skip("skipping test; requires symlink creation privilege on Windows") + } else { + t.Fatalf("could not create a symlink: %s", err) + } } + defer os.Remove("symTmpfile.sym") // create a filepath from slash, because otherwise // our tests are going to fail, because the path matching will @@ -213,25 +245,25 @@ func TestSymlinkToFolder(t *testing.T) { p := filepath.FromSlash("symTest/symTest2/symTmpfile") if err := os.WriteFile(p, []byte("abc"), 0400); err != nil { - t.Errorf("could not write symTmpfile: %s", err) + t.Fatalf("could not write symTmpfile: %s", err) } + defer os.Remove(p) result, err := RecordArtifacts([]string{"symTmpfile.sym"}, []string{"sha256"}, nil, nil, testOSisWindows(), true) if err != nil { - t.Error(err) + t.Fatalf("RecordArtifacts error: %s", err) } expected := map[string]HashObj{ - path.Join("symTmpfile.sym", "symTmpfile"): { + filepath.Join("symTmpfile.sym", "symTmpfile"): { "sha256": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", }, } if !reflect.DeepEqual(result, expected) { - t.Errorf("RecordArtifacts returned '(%s, %s)', expected '(%s, nil)'", + t.Errorf("RecordArtifacts returned '(%v, %s)', expected '(%v, nil)'", result, err, expected) } - // make sure to clean up everything if err := os.Remove("symTest/symTest2/symTmpfile"); err != nil { t.Errorf("could not remove path symTest/symTest2/symTmpfile: %s", err) @@ -252,27 +284,33 @@ func TestSymlinkToFolder(t *testing.T) { // This test provokes a symlink cycle func TestSymlinkCycle(t *testing.T) { - if err := os.Mkdir("symlinkCycle/", 0700); err != nil { - t.Errorf("could not create tmpdir: %s", err) + if err := os.Mkdir("symlinkCycle", 0700); err != nil { + t.Fatalf("could not create tmpdir: %s", err) } + defer os.RemoveAll("symlinkCycle") - // we need to get the current working directory here, otherwise - // os.Symlink() will create a wrong symlink + // get the current working directory dir, err := os.Getwd() if err != nil { - t.Error(err) + t.Fatal(err) } + // create a cycle ./symlinkCycle/symCycle.sym -> ./symlinkCycle/ - if err := os.Symlink(dir+"/symlinkCycle", "symlinkCycle/symCycle.sym"); err != nil { - t.Errorf("could not create a symlink: %s", err) + symlinkPath := filepath.Join("symlinkCycle", "symCycle.sym") + if err := os.Symlink(filepath.Join(dir, "symlinkCycle"), symlinkPath); err != nil { + if testOSisWindows() { + t.Skip("skipping test; requires symlink creation privilege on Windows") + } else { + t.Fatalf("could not create a symlink: %s", err) + } } + defer os.Remove(symlinkPath) // provoke "symlink cycle detected" error - _, err = RecordArtifacts([]string{"symlinkCycle/symCycle.sym", "foo.tar.gz"}, []string{"sha256"}, nil, nil, testOSisWindows(), true) + _, err = RecordArtifacts([]string{symlinkPath, "foo.tar.gz"}, []string{"sha256"}, nil, nil, testOSisWindows(), true) if !errors.Is(err, ErrSymCycle) { t.Errorf("we expected: %s, we got: %s", ErrSymCycle, err) } - if err := os.Remove("symlinkCycle/symCycle.sym"); err != nil { t.Errorf("could not remove path symlinkCycle/symCycle.sym: %s", err) }