forked from upstream/filebrowser
+10








3c02eedca6
Co-authored-by: Kevin B. <kurami32_kbm@proton.me> Co-authored-by: Gabriel Górski <me@glaeqen.eu> Co-authored-by: Joel Godfrey <82655889+BaccanoMob@users.noreply.github.com> Co-authored-by: Shane Chiu <71911511+pwn2ooown@users.noreply.github.com> Co-authored-by: carloda <35763669+carloda@users.noreply.github.com> Co-authored-by: Taufik Hidayat <taufikdev88@gmail.com> Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com> Co-authored-by: SIDDHU SINGH <siddhu3116@gmail.com> Co-authored-by: Gaurav Kalal <117557391+Gorupa@users.noreply.github.com> Co-authored-by: Nebula <40148908+nebula-it@users.noreply.github.com> Co-authored-by: Juan Soler <juansoler@gmail.com> Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com> Co-authored-by: Anatolii Prylutskyi <anpryl@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Graham Steffaniak <graham@debian-lake.ghome.net> Co-authored-by: Neil <kylelzzz02@icloud.com> Co-authored-by: Graham <info@quantumx-apps.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
183 lines
4.7 KiB
Go
183 lines
4.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestIndexPathFromNormalized_String(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
isDir bool
|
|
expected string
|
|
}{
|
|
{"root slash", "/", true, "/"},
|
|
{"empty", "", true, "/"},
|
|
{"hidden at root", "/.test", true, "/.test/"},
|
|
{"trailing slash dir", "/test/", true, "/test/"},
|
|
{"nested dir", "/nested/path", true, "/nested/path/"},
|
|
{"file no trailing", "/test/file.txt", false, "/test/file.txt"},
|
|
{"single file segment", "file.txt", false, "/file.txt"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := IndexPathFromNormalized(tt.input, tt.isDir).String()
|
|
if got != tt.expected {
|
|
t.Errorf("IndexPathFromNormalized(%q, %v).String() = %q, want %q", tt.input, tt.isDir, got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseIndexPath_consecutiveSlashes(t *testing.T) {
|
|
got := IndexPathFromNormalized("/a//b/", true)
|
|
if got.String() != "/a/b/" {
|
|
t.Errorf("got %q want /a/b/", got.String())
|
|
}
|
|
if len(got.Parts) != 2 {
|
|
t.Errorf("Parts = %v, want [a b]", got.Parts)
|
|
}
|
|
}
|
|
|
|
func TestSanitizePath_indexPaths(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{"root slash", "/", "/", false},
|
|
{"nested", "/foo/bar", "/foo/bar", false},
|
|
{"traversal segment", "..", "", true},
|
|
{"resolved traversal", "/../secret", "/secret", false},
|
|
{"empty", "", "", true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := SanitizePath(tt.input)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("SanitizePath(%q) expected error", tt.input)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("SanitizePath(%q) unexpected error: %v", tt.input, err)
|
|
}
|
|
if strings.Contains(got, `\`) {
|
|
t.Errorf("SanitizePath(%q) = %q contains backslash", tt.input, got)
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("SanitizePath(%q) = %q, want %q", tt.input, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseSanitizedIndexPath_root(t *testing.T) {
|
|
got, err := ParseSanitizedIndexPath("/", true)
|
|
if err != nil {
|
|
t.Fatalf("ParseSanitizedIndexPath(/): %v", err)
|
|
}
|
|
if got.String() != "/" {
|
|
t.Errorf("got %q want /", got.String())
|
|
}
|
|
}
|
|
|
|
func TestParseSanitizedIndexPath(t *testing.T) {
|
|
_, err := ParseSanitizedIndexPath("..", true)
|
|
if err == nil {
|
|
t.Fatal("expected error for .. path")
|
|
}
|
|
|
|
got, err := ParseSanitizedIndexPath("/valid/path/", true)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got.String() != "/valid/path/" {
|
|
t.Errorf("got %q want /valid/path/", got.String())
|
|
}
|
|
|
|
// SanitizePath resolves traversal; result is a normal index path
|
|
got, err = ParseSanitizedIndexPath("/../secret", true)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got.String() != "/secret/" {
|
|
t.Errorf("got %q want /secret/", got.String())
|
|
}
|
|
}
|
|
|
|
func TestIndexPath_Parent(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
path IndexPath
|
|
expected string
|
|
}{
|
|
{"root parent", IndexPathFromNormalized("/", true), "/"},
|
|
{"one level", IndexPathFromNormalized("/a/", true), "/"},
|
|
{"two levels", IndexPathFromNormalized("/a/b/", true), "/a/"},
|
|
{"file parent is dir", IndexPathFromNormalized("/a/b.txt", false), "/a/"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := tt.path.Parent().String()
|
|
if got != tt.expected {
|
|
t.Errorf("Parent().String() = %q, want %q", got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIndexPath_Join(t *testing.T) {
|
|
root := IndexPathFromNormalized("/", true)
|
|
child := root.Join("test", true)
|
|
if child.String() != "/test/" {
|
|
t.Errorf("Join dir got %q, want /test/", child.String())
|
|
}
|
|
file := root.Join("readme.txt", false)
|
|
if file.String() != "/readme.txt" {
|
|
t.Errorf("Join file got %q, want /readme.txt", file.String())
|
|
}
|
|
}
|
|
|
|
func TestIndexPath_RuleKey(t *testing.T) {
|
|
file := IndexPathFromNormalized("/foo/bar.txt", false)
|
|
if file.RuleKey() != "/foo/bar.txt/" {
|
|
t.Errorf("RuleKey() = %q, want /foo/bar.txt/", file.RuleKey())
|
|
}
|
|
dir := IndexPathFromNormalized("/foo/", true)
|
|
if dir.RuleKey() != "/foo/" {
|
|
t.Errorf("RuleKey() = %q, want /foo/", dir.RuleKey())
|
|
}
|
|
}
|
|
|
|
func TestAddTrailingSlashIfNotExists(t *testing.T) {
|
|
if AddTrailingSlashIfNotExists("/foo") != "/foo/" {
|
|
t.Errorf("expected /foo/")
|
|
}
|
|
if AddTrailingSlashIfNotExists("/") != "/" {
|
|
t.Errorf("root should stay /")
|
|
}
|
|
}
|
|
|
|
func BenchmarkIndexPathFromNormalized(b *testing.B) {
|
|
const input = "/nested/path/to/resource/"
|
|
b.ReportAllocs()
|
|
for b.Loop() {
|
|
_ = IndexPathFromNormalized(input, true)
|
|
}
|
|
}
|
|
|
|
func BenchmarkParseSanitizedIndexPath(b *testing.B) {
|
|
const input = "/nested/path/to/resource/"
|
|
b.ReportAllocs()
|
|
for b.Loop() {
|
|
_, _ = ParseSanitizedIndexPath(input, true)
|
|
}
|
|
}
|