github.com/dtroyer-salad/og2/v2@v2.0.0-20240412154159-c47231610877/content/file/utils_test.go (about) 1 /* 2 Copyright The ORAS Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package file 17 18 import ( 19 "os" 20 "path/filepath" 21 "testing" 22 ) 23 24 func Test_ensureBasePath(t *testing.T) { 25 root := t.TempDir() 26 if err := os.MkdirAll(filepath.Join(root, "hello world", "foo", "bar"), 0700); err != nil { 27 t.Fatal("failed to create temp folders:", err) 28 } 29 base := "hello world/foo" 30 31 tests := []struct { 32 name string 33 target string 34 want string 35 wantErr bool 36 }{ 37 { 38 name: "valid case (depth 0)", 39 target: "hello world/foo", 40 want: ".", 41 }, 42 { 43 name: "valid case (depth 1)", 44 target: "hello world/foo/bar", 45 want: "bar", 46 }, 47 { 48 name: "valid case (depth 2)", 49 target: "hello world/foo/bar/fun", 50 want: filepath.Join("bar", "fun"), 51 }, 52 { 53 name: "invalid prefix", 54 target: "hello world/fun", 55 wantErr: true, 56 }, 57 { 58 name: "invalid prefix", 59 target: "hello/foo", 60 wantErr: true, 61 }, 62 { 63 name: "bad traversal", 64 target: "hello world/foo/..", 65 wantErr: true, 66 }, 67 { 68 name: "valid traversal", 69 target: "hello world/foo/../foo/bar/../bar", 70 want: "bar", 71 }, 72 { 73 name: "complex traversal", 74 target: "hello world/foo/../foo/bar/../..", 75 wantErr: true, 76 }, 77 } 78 for _, tt := range tests { 79 t.Run(tt.name, func(t *testing.T) { 80 got, err := ensureBasePath(root, base, tt.target) 81 if (err != nil) != tt.wantErr { 82 t.Errorf("ensureBasePath() error = %v, wantErr %v", err, tt.wantErr) 83 return 84 } 85 if got != tt.want { 86 t.Errorf("ensureBasePath() = %v, want %v", got, tt.want) 87 } 88 }) 89 } 90 } 91 92 func Test_ensureLinkPath(t *testing.T) { 93 root := t.TempDir() 94 if err := os.MkdirAll(filepath.Join(root, "hello world", "foo", "bar"), 0700); err != nil { 95 t.Fatal("failed to create temp folders:", err) 96 } 97 base := "hello world/foo" 98 99 tests := []struct { 100 name string 101 link string 102 target string 103 want string 104 wantErr bool 105 }{ 106 { 107 name: "valid case (depth 1)", 108 link: "hello world/foo/bar", 109 target: "fun", 110 want: "fun", 111 }, 112 { 113 name: "valid case (depth 2)", 114 link: "hello world/foo/bar/fun", 115 target: "../fun", 116 want: "../fun", 117 }, 118 { 119 name: "invalid prefix", 120 link: "hello world/foo", 121 target: "fun", 122 wantErr: true, 123 }, 124 { 125 name: "bad traversal", 126 link: "hello world/foo/bar", 127 target: "../fun", 128 wantErr: true, 129 }, 130 { 131 name: "valid traversal", 132 link: "hello world/foo/../foo/bar/../bar", // hello world/foo/bar 133 target: "../foo/../foo/fun", 134 want: "../foo/../foo/fun", 135 }, 136 { 137 name: "complex traversal", 138 link: "hello world/foo/bar", 139 target: "../foo/../../fun", 140 wantErr: true, 141 }, 142 } 143 for _, tt := range tests { 144 t.Run(tt.name, func(t *testing.T) { 145 got, err := ensureLinkPath(root, base, tt.link, tt.target) 146 if (err != nil) != tt.wantErr { 147 t.Errorf("ensureLinkPath() error = %v, wantErr %v", err, tt.wantErr) 148 return 149 } 150 if got != tt.want { 151 t.Errorf("ensureLinkPath() = %v, want %v", got, tt.want) 152 } 153 }) 154 } 155 }