|
| 1 | +package pathutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCleanAbs(t *testing.T) { |
| 11 | + got, err := CleanAbs("/foo/bar/../baz") |
| 12 | + if err != nil { |
| 13 | + t.Fatalf("CleanAbs error: %v", err) |
| 14 | + } |
| 15 | + if want := filepath.Clean("/foo/baz"); got != want { |
| 16 | + t.Errorf("CleanAbs = %q, want %q", got, want) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func TestResolveDirSymlinks_LeavesFinalComponent(t *testing.T) { |
| 21 | + if runtime.GOOS == "windows" { |
| 22 | + t.Skip("symlink tests skipped on windows") |
| 23 | + } |
| 24 | + dir := t.TempDir() |
| 25 | + target := filepath.Join(dir, "target") |
| 26 | + if err := os.Mkdir(target, 0755); err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + link := filepath.Join(dir, "link") |
| 30 | + if err := os.Symlink(target, link); err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + } |
| 33 | + |
| 34 | + // link/file: directory symlink resolved, final component untouched. |
| 35 | + resolved := ResolveDirSymlinks(filepath.Join(link, "file")) |
| 36 | + want := filepath.Join(target, "file") |
| 37 | + // On macOS the temp dir itself may be reached through a symlink (/var -> |
| 38 | + // /private/var), so resolve the target directory before appending the final |
| 39 | + // component. |
| 40 | + if r, err := filepath.EvalSymlinks(target); err == nil { |
| 41 | + want = filepath.Join(r, "file") |
| 42 | + } |
| 43 | + if resolved != want { |
| 44 | + t.Errorf("ResolveDirSymlinks = %q, want %q", resolved, want) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestResolveDirSymlinks_FallsBackWhenParentMissing(t *testing.T) { |
| 49 | + dir := t.TempDir() |
| 50 | + missing := filepath.Join(dir, "missing", "file") |
| 51 | + got := ResolveDirSymlinks(missing) |
| 52 | + want, err := CleanAbs(missing) |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + if got != want { |
| 57 | + t.Errorf("ResolveDirSymlinks missing-parent = %q, want %q", got, want) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestWithinRoot_Lexical(t *testing.T) { |
| 62 | + if !WithinRoot("/workspace", "/workspace/extra") { |
| 63 | + t.Error("expected /workspace/extra to be under /workspace") |
| 64 | + } |
| 65 | + if WithinRoot("/workspace", "/tmp") { |
| 66 | + t.Error("expected /tmp not to be under /workspace") |
| 67 | + } |
| 68 | + // Separator-aware: /workspacefoo must not match /workspace prefix. |
| 69 | + if WithinRoot("/workspace", "/workspacefoo") { |
| 70 | + t.Error("expected /workspacefoo not to be under /workspace") |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestWithinRoot_SymlinkEscape(t *testing.T) { |
| 75 | + if runtime.GOOS == "windows" { |
| 76 | + t.Skip("symlink tests skipped on windows") |
| 77 | + } |
| 78 | + workdir := t.TempDir() |
| 79 | + outside := t.TempDir() |
| 80 | + if err := os.Symlink(outside, filepath.Join(workdir, "link")); err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + |
| 84 | + // workdir/link/secret resolves outside workdir. |
| 85 | + if WithinRoot(workdir, filepath.Join(workdir, "link", "secret")) { |
| 86 | + t.Error("expected symlinked directory escape to be rejected") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestWithinRoot_MissingRootFallsBack(t *testing.T) { |
| 91 | + // The root does not exist. The comparison should fall back to lexical |
| 92 | + // paths so legitimate under-root candidates are still accepted. |
| 93 | + if !WithinRoot("/home/alice/project", "/home/alice/project/data") { |
| 94 | + t.Error("expected under-root candidate to be accepted when root does not exist") |
| 95 | + } |
| 96 | + if WithinRoot("/home/alice/project", "/tmp") { |
| 97 | + t.Error("expected outside candidate to be rejected when root does not exist") |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestWithinRoot_Equality(t *testing.T) { |
| 102 | + if !WithinRoot("/workspace", "/workspace") { |
| 103 | + t.Error("expected root to be considered inside itself") |
| 104 | + } |
| 105 | +} |
0 commit comments