返回 DeepSeek-Reasonix
open_local_path_test.go
根目录 / desktop / open_local_path_test.go
1 package main
2
3 import (
4 "errors"
5 "os"
6 "path/filepath"
7 "runtime"
8 "strings"
9 "testing"
10 )
11
12 func TestNormalizeLocalOpenPath(t *testing.T) {
13 abs := filepath.Join(t.TempDir(), "report.md")
14
15 tests := []struct {
16 name string
17 in string
18 want string
19 }{
20 {"empty", "", ""},
21 {"blank", " ", ""},
22 {"plain absolute", abs, abs},
23 {"file URL", "file:///" + filepath.ToSlash(abs), abs},
24 }
25 for _, tc := range tests {
26 t.Run(tc.name, func(t *testing.T) {
27 got, err := normalizeLocalOpenPath(tc.in)
28 if tc.want == "" {
29 if err == nil {
30 t.Fatalf("normalizeLocalOpenPath(%q) = %q, want error", tc.in, got)
31 }
32 return
33 }
34 if err != nil {
35 t.Fatalf("normalizeLocalOpenPath(%q): %v", tc.in, err)
36 }
37 if got != tc.want {
38 t.Fatalf("normalizeLocalOpenPath(%q) = %q, want %q", tc.in, got, tc.want)
39 }
40 })
41 }
42 }
43
44 func TestNormalizeLocalOpenPathRejectsRelative(t *testing.T) {
45 for _, in := range []string{"report.md", "dir/report.md", "file:///report.md", "file:///dir/report.md"} {
46 if _, err := normalizeLocalOpenPath(in); err == nil || !strings.Contains(err.Error(), "not absolute") {
47 t.Fatalf("normalizeLocalOpenPath(%q) err = %v, want not-absolute error", in, err)
48 }
49 }
50 }
51
52 func TestNormalizeLocalOpenPathWindowsUNC(t *testing.T) {
53 if runtime.GOOS != "windows" {
54 t.Skip("UNC paths are Windows-only")
55 }
56 got, err := normalizeLocalOpenPath(`\\server\share\docs\report.md`)
57 if err != nil {
58 t.Fatalf("UNC path rejected: %v", err)
59 }
60 if got != `\\server\share\docs\report.md` {
61 t.Fatalf("UNC path = %q, want unchanged", got)
62 }
63 }
64
65 func TestOpenLocalPathRejectsMissingFile(t *testing.T) {
66 a := &App{}
67 missing := filepath.Join(t.TempDir(), "does-not-exist.md")
68 if err := a.OpenLocalPath(missing); err == nil {
69 t.Fatal("OpenLocalPath on a missing path succeeded, want error")
70 }
71 }
72
73 func TestOpenLocalPathRejectsRelativeAndEmpty(t *testing.T) {
74 a := &App{}
75 if err := a.OpenLocalPath(""); !errors.Is(err, os.ErrInvalid) {
76 t.Fatalf("OpenLocalPath(\"\") err = %v, want os.ErrInvalid", err)
77 }
78 if err := a.OpenLocalPath("relative.md"); err == nil {
79 t.Fatal("OpenLocalPath(relative) succeeded, want error")
80 }
81 }
82
83 func TestOpenTargetAllowed(t *testing.T) {
84 if !openTargetAllowed(filepath.Join("C:", "docs", "report.md"), false) {
85 t.Fatal("markdown document should be openable")
86 }
87 if !openTargetAllowed(filepath.Join("C:", "docs"), true) {
88 t.Fatal("directory should be openable")
89 }
90 for _, name := range []string{"evil.bat", "evil.cmd", "evil.exe", "evil.ps1", "evil.lnk", "evil.url", "evil.msi", "run.BAT", "EVIL.Scr"} {
91 if openTargetAllowed(filepath.Join("C:", "temp", name), false) {
92 t.Fatalf("executable target %q should be refused", name)
93 }
94 }
95 }
96
97 func TestOpenLocalPathRejectsExecutable(t *testing.T) {
98 a := &App{}
99 dir := t.TempDir()
100 script := filepath.Join(dir, "clicked.bat")
101 if err := os.WriteFile(script, []byte("@echo off"), 0o644); err != nil {
102 t.Fatal(err)
103 }
104 if err := a.OpenLocalPath(script); err == nil || !strings.Contains(err.Error(), "executable") {
105 t.Fatalf("OpenLocalPath(.bat) err = %v, want executable refusal", err)
106 }
107 }
108
108 lines GO