| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 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 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package paths |
| 15 |
|
| 16 |
import ( |
| 17 |
"path/filepath" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
) |
| 22 |
|
| 23 |
func TestMakePathRelative(t *testing.T) { |
| 24 |
type test struct { |
| 25 |
inPath, path1, path2, output string |
| 26 |
} |
| 27 |
|
| 28 |
data := []test{ |
| 29 |
{"/abc/bcd/ab.css", "/abc/bcd", "/bbc/bcd", "/ab.css"}, |
| 30 |
{"/abc/bcd/ab.css", "/abcd/bcd", "/abc/bcd", "/ab.css"}, |
| 31 |
} |
| 32 |
|
| 33 |
for i, d := range data { |
| 34 |
output, _ := makePathRelative(d.inPath, d.path1, d.path2) |
| 35 |
if d.output != output { |
| 36 |
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output) |
| 37 |
} |
| 38 |
} |
| 39 |
_, error := makePathRelative("a/b/c.ss", "/a/c", "/d/c", "/e/f") |
| 40 |
|
| 41 |
if error == nil { |
| 42 |
t.Errorf("Test failed, expected error") |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestExtNoDelimiter(t *testing.T) { |
| 47 |
c := qt.New(t) |
| 48 |
c.Assert(ExtNoDelimiter(filepath.FromSlash("/my/data.json")), qt.Equals, "json") |
| 49 |
} |
| 50 |
|
| 51 |
func TestFilename(t *testing.T) { |
| 52 |
type test struct { |
| 53 |
input, expected string |
| 54 |
} |
| 55 |
data := []test{ |
| 56 |
{"index.html", "index"}, |
| 57 |
{"./index.html", "index"}, |
| 58 |
{"/index.html", "index"}, |
| 59 |
{"index", "index"}, |
| 60 |
{"/tmp/index.html", "index"}, |
| 61 |
{"./filename-no-ext", "filename-no-ext"}, |
| 62 |
{"/filename-no-ext", "filename-no-ext"}, |
| 63 |
{"filename-no-ext", "filename-no-ext"}, |
| 64 |
{"directory/", ""}, // no filename case?? |
| 65 |
{"directory/.hidden.ext", ".hidden"}, |
| 66 |
{"./directory/../~/banana/gold.fish", "gold"}, |
| 67 |
{"../directory/banana.man", "banana"}, |
| 68 |
{"~/mydir/filename.ext", "filename"}, |
| 69 |
{"./directory//tmp/filename.ext", "filename"}, |
| 70 |
} |
| 71 |
|
| 72 |
for i, d := range data { |
| 73 |
output := Filename(filepath.FromSlash(d.input)) |
| 74 |
if d.expected != output { |
| 75 |
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestFileAndExt(t *testing.T) { |
| 81 |
type test struct { |
| 82 |
input, expectedFile, expectedExt string |
| 83 |
} |
| 84 |
data := []test{ |
| 85 |
{"index.html", "index", ".html"}, |
| 86 |
{"./index.html", "index", ".html"}, |
| 87 |
{"/index.html", "index", ".html"}, |
| 88 |
{"index", "index", ""}, |
| 89 |
{"/tmp/index.html", "index", ".html"}, |
| 90 |
{"./filename-no-ext", "filename-no-ext", ""}, |
| 91 |
{"/filename-no-ext", "filename-no-ext", ""}, |
| 92 |
{"filename-no-ext", "filename-no-ext", ""}, |
| 93 |
{"directory/", "", ""}, // no filename case?? |
| 94 |
{"directory/.hidden.ext", ".hidden", ".ext"}, |
| 95 |
{"./directory/../~/banana/gold.fish", "gold", ".fish"}, |
| 96 |
{"../directory/banana.man", "banana", ".man"}, |
| 97 |
{"~/mydir/filename.ext", "filename", ".ext"}, |
| 98 |
{"./directory//tmp/filename.ext", "filename", ".ext"}, |
| 99 |
} |
| 100 |
|
| 101 |
for i, d := range data { |
| 102 |
file, ext := fileAndExt(filepath.FromSlash(d.input), fpb) |
| 103 |
if d.expectedFile != file { |
| 104 |
t.Errorf("Test %d failed. Expected filename %q got %q.", i, d.expectedFile, file) |
| 105 |
} |
| 106 |
if d.expectedExt != ext { |
| 107 |
t.Errorf("Test %d failed. Expected extension %q got %q.", i, d.expectedExt, ext) |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestSanitize(t *testing.T) { |
| 113 |
c := qt.New(t) |
| 114 |
tests := []struct { |
| 115 |
input string |
| 116 |
expected string |
| 117 |
}{ |
| 118 |
{" Foo bar ", "Foo-bar"}, |
| 119 |
{"Foo.Bar/foo_Bar-Foo", "Foo.Bar/foo_Bar-Foo"}, |
| 120 |
{"fOO,bar:foobAR", "fOObarfoobAR"}, |
| 121 |
{"FOo/BaR.html", "FOo/BaR.html"}, |
| 122 |
{"FOo/Ba---R.html", "FOo/Ba---R.html"}, /// See #10104 |
| 123 |
{"FOo/Ba R.html", "FOo/Ba-R.html"}, |
| 124 |
{"трям/трям", "трям/трям"}, |
| 125 |
{"은행", "은행"}, |
| 126 |
{"Банковский кассир", "Банковский-кассир"}, |
| 127 |
// Issue #1488 |
| 128 |
{"संस्कृत", "संस्कृत"}, |
| 129 |
{"a%C3%B1ame", "a%C3%B1ame"}, // Issue #1292 |
| 130 |
{"this+is+a+test", "this+is+a+test"}, // Issue #1290 |
| 131 |
{"~foo", "~foo"}, // Issue #2177 |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
for _, test := range tests { |
| 136 |
c.Assert(Sanitize(test.input), qt.Equals, test.expected) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func BenchmarkSanitize(b *testing.B) { |
| 141 |
const ( |
| 142 |
allAlowedPath = "foo/bar" |
| 143 |
spacePath = "foo bar" |
| 144 |
) |
| 145 |
|
| 146 |
// This should not allocate any memory. |
| 147 |
b.Run("All allowed", func(b *testing.B) { |
| 148 |
for b.Loop() { |
| 149 |
got := Sanitize(allAlowedPath) |
| 150 |
if got != allAlowedPath { |
| 151 |
b.Fatal(got) |
| 152 |
} |
| 153 |
} |
| 154 |
}) |
| 155 |
|
| 156 |
// This will allocate some memory. |
| 157 |
b.Run("Spaces", func(b *testing.B) { |
| 158 |
for b.Loop() { |
| 159 |
got := Sanitize(spacePath) |
| 160 |
if got != "foo-bar" { |
| 161 |
b.Fatal(got) |
| 162 |
} |
| 163 |
} |
| 164 |
}) |
| 165 |
} |
| 166 |
|
| 167 |
func TestDir(t *testing.T) { |
| 168 |
c := qt.New(t) |
| 169 |
c.Assert(Dir("/a/b/c/d"), qt.Equals, "/a/b/c") |
| 170 |
c.Assert(Dir("/a"), qt.Equals, "/") |
| 171 |
c.Assert(Dir("/"), qt.Equals, "/") |
| 172 |
c.Assert(Dir(""), qt.Equals, "") |
| 173 |
} |
| 174 |
|
| 175 |
func TestFieldsSlash(t *testing.T) { |
| 176 |
c := qt.New(t) |
| 177 |
|
| 178 |
c.Assert(FieldsSlash("a/b/c"), qt.DeepEquals, []string{"a", "b", "c"}) |
| 179 |
c.Assert(FieldsSlash("/a/b/c"), qt.DeepEquals, []string{"a", "b", "c"}) |
| 180 |
c.Assert(FieldsSlash("/a/b/c/"), qt.DeepEquals, []string{"a", "b", "c"}) |
| 181 |
c.Assert(FieldsSlash("a/b/c/"), qt.DeepEquals, []string{"a", "b", "c"}) |
| 182 |
c.Assert(FieldsSlash("/"), qt.DeepEquals, []string{}) |
| 183 |
c.Assert(FieldsSlash(""), qt.DeepEquals, []string{}) |
| 184 |
} |
| 185 |
|
| 186 |
func TestIsSameFilePath(t *testing.T) { |
| 187 |
c := qt.New(t) |
| 188 |
|
| 189 |
for _, this := range []struct { |
| 190 |
a, b string |
| 191 |
expected bool |
| 192 |
}{ |
| 193 |
{"/a/b/c", "/a/b/c", true}, |
| 194 |
{"/a/b/c", "/a/b/c/", true}, |
| 195 |
{"/a/b/c", "/a/b/d", false}, |
| 196 |
{"/a/b/c", "/a/b", false}, |
| 197 |
{"/a/b/c", "/a/b/c/d", false}, |
| 198 |
{"/a/b/c", "/a/b/cd", false}, |
| 199 |
{"/a/b/c", "/a/b/cc", false}, |
| 200 |
{"/a/b/c", "/a/b/c/", true}, |
| 201 |
{"/a/b/c", "/a/b/c//", true}, |
| 202 |
{"/a/b/c", "/a/b/c/.", true}, |
| 203 |
{"/a/b/c", "/a/b/c/./", true}, |
| 204 |
{"/a/b/c", "/a/b/c/./.", true}, |
| 205 |
{"/a/b/c", "/a/b/c/././", true}, |
| 206 |
{"/a/b/c", "/a/b/c/././.", true}, |
| 207 |
{"/a/b/c", "/a/b/c/./././", true}, |
| 208 |
{"/a/b/c", "/a/b/c/./././.", true}, |
| 209 |
{"/a/b/c", "/a/b/c/././././", true}, |
| 210 |
} { |
| 211 |
c.Assert(IsSameFilePath(filepath.FromSlash(this.a), filepath.FromSlash(this.b)), qt.Equals, this.expected, qt.Commentf("a: %s b: %s", this.a, this.b)) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func BenchmarkAddLeadingSlash(b *testing.B) { |
| 216 |
const ( |
| 217 |
noLeadingSlash = "a/b/c" |
| 218 |
withLeadingSlash = "/a/b/c" |
| 219 |
) |
| 220 |
|
| 221 |
// This should not allocate any memory. |
| 222 |
b.Run("With leading slash", func(b *testing.B) { |
| 223 |
for b.Loop() { |
| 224 |
got := AddLeadingSlash(withLeadingSlash) |
| 225 |
if got != withLeadingSlash { |
| 226 |
b.Fatal(got) |
| 227 |
} |
| 228 |
} |
| 229 |
}) |
| 230 |
|
| 231 |
// This will allocate some memory. |
| 232 |
b.Run("Without leading slash", func(b *testing.B) { |
| 233 |
for b.Loop() { |
| 234 |
got := AddLeadingSlash(noLeadingSlash) |
| 235 |
if got != "/a/b/c" { |
| 236 |
b.Fatal(got) |
| 237 |
} |
| 238 |
} |
| 239 |
}) |
| 240 |
|
| 241 |
b.Run("Blank string", func(b *testing.B) { |
| 242 |
for b.Loop() { |
| 243 |
got := AddLeadingSlash("") |
| 244 |
if got != "/" { |
| 245 |
b.Fatal(got) |
| 246 |
} |
| 247 |
} |
| 248 |
}) |
| 249 |
} |
| 250 |
|
| 251 |
func TestPathEscape(t *testing.T) { |
| 252 |
c := qt.New(t) |
| 253 |
|
| 254 |
for _, this := range []struct { |
| 255 |
input string |
| 256 |
expected string |
| 257 |
}{ |
| 258 |
{"/tags/欢迎", "/tags/%E6%AC%A2%E8%BF%8E"}, |
| 259 |
{"/path with spaces", "/path%20with%20spaces"}, |
| 260 |
{"/simple-path", "/simple-path"}, |
| 261 |
{"/path/with/slash", "/path/with/slash"}, |
| 262 |
{"/path/with special&chars", "/path/with%20special&chars"}, |
| 263 |
} { |
| 264 |
in := this.input |
| 265 |
for range 2 { |
| 266 |
result := PathEscape(in) |
| 267 |
c.Assert(result, qt.Equals, this.expected, qt.Commentf("input: %q", this.input)) |
| 268 |
in = result // test idempotency |
| 269 |
} |
| 270 |
} |
| 271 |
} |