78 lines
3.0 KiB
Go
78 lines
3.0 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
"testing/fstest"
|
|
)
|
|
|
|
func TestLiteralImportSelectorCoordinates(t *testing.T) {
|
|
t.Parallel()
|
|
anchor := canonicalTestPath(t, t.TempDir())
|
|
external := canonicalTestPath(t, t.TempDir())
|
|
host, err := openHostAccess("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer host.close()
|
|
bounded, err := openHostAccess(anchor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer bounded.close()
|
|
native := newNativeAccess(fstest.MapFS{}, ".", &namespaceToken{})
|
|
nativeBounded := newNativeAccess(fstest.MapFS{}, "allowed", &namespaceToken{})
|
|
for _, entry := range []struct {
|
|
name string
|
|
access *filesystemAccess
|
|
base, selector, want string
|
|
outside bool
|
|
}{
|
|
{name: "host absolute", access: host, base: anchor, selector: filepath.Join(external, "part.yml"), want: filepath.Join(external, "part.yml")},
|
|
{name: "host parent", access: host, base: filepath.Join(anchor, "nested"), selector: "../part.yml", want: filepath.Join(anchor, "part.yml")},
|
|
{name: "host bounded", access: bounded, base: anchor, selector: filepath.Join(external, "part.yml"), outside: true},
|
|
{name: "host literal", access: host, base: anchor, selector: "literal*.yml", want: filepath.Join(anchor, "literal*.yml")},
|
|
{name: "native root", access: native, base: "app", selector: "/shared/part.yml", want: "shared/part.yml"},
|
|
{name: "native parent", access: native, base: "app", selector: "../shared/part.yml", want: "shared/part.yml"},
|
|
{name: "native escape", access: native, base: "app", selector: "../../part.yml", outside: true},
|
|
{name: "native bounded root", access: nativeBounded, base: "allowed/app", selector: "/allowed/part.yml", want: "allowed/part.yml"},
|
|
{name: "native bounded escape", access: nativeBounded, base: "allowed/app", selector: "/shared/part.yml", outside: true},
|
|
} {
|
|
t.Run(entry.name, func(t *testing.T) {
|
|
got, err := resolveLiteralImportSelector(entry.access, entry.base, entry.selector)
|
|
if entry.outside {
|
|
if !errors.Is(err, errImportPathOutsideRoot) {
|
|
t.Fatalf("outside error = %v", err)
|
|
}
|
|
return
|
|
}
|
|
if err != nil || got != entry.want {
|
|
t.Fatalf("resolved = %q, %v; want %q", got, err, entry.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestImportSelectorRootGrammar(t *testing.T) {
|
|
t.Parallel()
|
|
for _, pattern := range []string{"/shared/*.yml", "../shared/*.yml", "/shared/literal\\*.yml"} {
|
|
if _, ok := validGlobPattern(pattern); !ok {
|
|
t.Errorf("rejected %q", pattern)
|
|
}
|
|
}
|
|
for _, pattern := range []string{"", "https://example.test/*.yml", "/shared/**/*.yml", "/shared//*.yml", "/shared/[broken"} {
|
|
if _, ok := validGlobPattern(pattern); ok {
|
|
t.Errorf("accepted %q", pattern)
|
|
}
|
|
}
|
|
root, rest := splitImportSelectorRoot("/shared/*/../part.yml")
|
|
if root != "/" || rest != "shared/*/../part.yml" {
|
|
t.Fatalf("root=%q, rest=%q", root, rest)
|
|
}
|
|
literal, patterned := globLiteralSegment("literal\\*.yml")
|
|
if patterned || literal != "literal*.yml" {
|
|
t.Fatalf("escaped literal=%q, patterned=%v", literal, patterned)
|
|
}
|
|
}
|