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