Skip to content

Commit

Permalink
expand link test
Browse files Browse the repository at this point in the history
  • Loading branch information
lkingland committed Feb 16, 2024
1 parent 71b5887 commit 1bdd09b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
8 changes: 3 additions & 5 deletions pkg/oci/containerize.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func newDataTarball(root, target string, ignored []string, verbose bool) error {

lnk := "" // if link, this will be used as the target
if info.Mode()&fs.ModeSymlink != 0 {
if lnk, err = validateLink(root, path, info); err != nil {
if lnk, err = validatedLinkTarget(root, path, info); err != nil {
return err
}
}
Expand Down Expand Up @@ -191,11 +191,9 @@ func newDataTarball(root, target string, ignored []string, verbose bool) error {
})
}

// validateLink returns the target of a given link and an error if
// validatedLinkTarget returns the target of a given link or an error if
// that target is either absolute or outside the given project root.
// or referrs to a target outside of the given root.
// For conven
func validateLink(root, path string, info os.FileInfo) (tgt string, err error) {
func validatedLinkTarget(root, path string, info os.FileInfo) (tgt string, err error) {
// tgt is the raw target of the link.
// This path is either absolute or relative to the link's location.
tgt, err = os.Readlink(path)
Expand Down
25 changes: 20 additions & 5 deletions pkg/oci/containerize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"testing"
)

// Test_validateLink ensures that the validateLink function disallows
// links which are absolute or refer to targets outside the given root.
func Test_validateLink(t *testing.T) {
// Test_validatedLinkTaarget ensures that the function disallows
// links which are absolute or refer to targets outside the given root, in
// addition to the basic job of returning the value of reading the link.
func Test_validatedLinkTarget(t *testing.T) {
root := "testdata/test-links"

tests := []struct {
Expand All @@ -34,7 +35,7 @@ func Test_validateLink(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = validateLink(root, path, info)
_, err = validatedLinkTarget(root, path, info)
if err == nil != tt.valid {
t.Fatalf("expected %v, got %v", tt.valid, err)
}
Expand All @@ -48,9 +49,23 @@ func Test_validateLink(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = validateLink(root, path, info)
_, err = validatedLinkTarget(root, path, info)
if err == nil {
t.Fatal("absolute path should be invalid on windows")
}
}

// Spot-check the base case of being a decorator for os.ReadLink
path := "testdata/test-links/a.lnk"
info, err := os.Lstat(path)
if err != nil {
t.Fatal(err)
}
tgt, err := validatedLinkTarget(root, path, info)
if err != nil {
t.Fatal(err)
}
if tgt != "./a.txt" {
t.Fatalf("expected target of 'a/lnk' to be 'a.txt', got '%v'", tgt)
}
}

0 comments on commit 1bdd09b

Please sign in to comment.