Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve all the error in unit tests #333

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions in_toto/attestations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
94 changes: 66 additions & 28 deletions in_toto/runlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
Expand Down Expand Up @@ -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": {
Expand All @@ -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)
Expand All @@ -193,45 +219,51 @@ 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
// not work correctly on Windows
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)
Expand All @@ -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)
}
Expand Down
Loading