1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package loader
import (
"path/filepath"
"strings"
"testing"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
func TestRestrictionNone(t *testing.T) {
fSys := filesys.MakeFsInMemory()
root := filesys.ConfirmedDir("irrelevant")
path := "whatever"
p, err := RestrictionNone(fSys, root, path)
if err != nil {
t.Fatal(err)
}
if p != path {
t.Fatalf("expected '%s', got '%s'", path, p)
}
}
func TestRestrictionRootOnly(t *testing.T) {
fSys := filesys.MakeFsInMemory()
root := filesys.ConfirmedDir(
filesys.Separator + filepath.Join("tmp", "foo"))
path := filepath.Join(string(root), "whatever", "beans")
fSys.Create(path)
p, err := RestrictionRootOnly(fSys, root, path)
if err != nil {
t.Fatal(err)
}
if p != path {
t.Fatalf("expected '%s', got '%s'", path, p)
}
// Legal.
path = filepath.Join(
string(root), "whatever", "..", "..", "foo", "whatever", "beans")
p, err = RestrictionRootOnly(fSys, root, path)
if err != nil {
t.Fatal(err)
}
path = filepath.Join(
string(root), "whatever", "beans")
if p != path {
t.Fatalf("expected '%s', got '%s'", path, p)
}
// 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")
}
if !strings.Contains(
err.Error(),
"file '/tmp/illegal' is not in or below '/tmp/foo'") {
t.Fatalf("unexpected err: %s", err)
}
}
|