More tests, better errors.

This commit is contained in:
jregan
2019-11-26 18:13:15 -08:00
parent a2b84fce86
commit f3e735153f
22 changed files with 112 additions and 59 deletions

View File

@@ -93,20 +93,18 @@ type fileLoader struct {
cleaner func() error
}
const CWD = "."
// NewFileLoaderAtCwd returns a loader that loads from ".".
// NewFileLoaderAtCwd returns a loader that loads from PWD.
// A convenience for kustomize edit commands.
func NewFileLoaderAtCwd(fSys filesys.FileSystem) *fileLoader {
return newLoaderOrDie(
RestrictionRootOnly, fSys, CWD)
RestrictionRootOnly, fSys, filesys.SelfDir)
}
// NewFileLoaderAtRoot returns a loader that loads from "/".
// A convenience for tests.
func NewFileLoaderAtRoot(fSys filesys.FileSystem) *fileLoader {
return newLoaderOrDie(
RestrictionRootOnly, fSys, string(filepath.Separator))
RestrictionRootOnly, fSys, filesys.Separator)
}
// Root returns the absolute path that is prepended to any

View File

@@ -131,7 +131,7 @@ func TestLoaderBadRelative(t *testing.T) {
}
// It's not okay to stay at the same place.
l2, err = l1.New(".")
l2, err = l1.New(filesys.SelfDir)
if err == nil {
t.Fatalf("expected err, but got root %s", l2.Root())
}

View File

@@ -19,7 +19,7 @@ func RestrictionRootOnly(
return "", err
}
if f == "" {
return "", fmt.Errorf("'%s' must be a file", path)
return "", fmt.Errorf("'%s' must resolve to a file", path)
}
if !d.HasPrefix(root) {
return "", fmt.Errorf(

View File

@@ -4,6 +4,7 @@
package loader
import (
"path/filepath"
"strings"
"testing"
@@ -25,9 +26,11 @@ func TestRestrictionNone(t *testing.T) {
func TestRestrictionRootOnly(t *testing.T) {
fSys := filesys.MakeFsInMemory()
root := filesys.ConfirmedDir("/tmp/foo")
root := filesys.ConfirmedDir(
filesys.Separator + filepath.Join("tmp", "foo"))
path := filepath.Join(string(root), "whatever", "beans")
path := "/tmp/foo/whatever/beans"
fSys.Create(path)
p, err := RestrictionRootOnly(fSys, root, path)
if err != nil {
t.Fatal(err)
@@ -37,18 +40,21 @@ func TestRestrictionRootOnly(t *testing.T) {
}
// Legal.
path = "/tmp/foo/whatever/../../foo/whatever"
path = filepath.Join(
string(root), "whatever", "..", "..", "foo", "whatever", "beans")
p, err = RestrictionRootOnly(fSys, root, path)
if err != nil {
t.Fatal(err)
}
path = "/tmp/foo/whatever"
path = filepath.Join(
string(root), "whatever", "beans")
if p != path {
t.Fatalf("expected '%s', got '%s'", path, p)
}
// Illegal.
path = "/tmp/illegal"
// Illegal; file exists but is out of bounds.
path = filepath.Join(filesys.Separator+"tmp", "illegal")
fSys.Create(path)
_, err = RestrictionRootOnly(fSys, root, path)
if err == nil {
t.Fatal("should have an error")