Skip to content

Commit c393a1e

Browse files
author
XavierTrump
committed
test(edge): 补充 security 覆盖率用例
1 parent 6b5d0d7 commit c393a1e

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

edge-server/internal/security/origin_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,59 @@ func TestIsTrustedLocalOrigin(t *testing.T) {
6060
}
6161
}
6262

63+
func TestIsTrustedLocalHost(t *testing.T) {
64+
tests := []struct {
65+
name string
66+
host string
67+
want bool
68+
}{
69+
{"localhost", "localhost", true},
70+
{"localhost with port", "localhost:3210", true},
71+
{"ipv4 loopback", "127.0.0.1:3210", true},
72+
{"ipv6 loopback", "[::1]:3210", true},
73+
{"tauri localhost", "tauri.localhost", true},
74+
{"uppercase localhost", "LOCALHOST:3210", true},
75+
{"lan ip", "192.168.1.20:3210", false},
76+
{"remote hostname", "edge.example.com:3210", false},
77+
{"empty host", "", false},
78+
}
79+
80+
for _, tt := range tests {
81+
t.Run(tt.name, func(t *testing.T) {
82+
got := IsTrustedLocalHost(tt.host)
83+
if got != tt.want {
84+
t.Fatalf("IsTrustedLocalHost(%q) = %v, want %v", tt.host, got, tt.want)
85+
}
86+
})
87+
}
88+
}
89+
90+
func TestIsTrustedOriginRemoteMode(t *testing.T) {
91+
tests := []struct {
92+
name string
93+
origin string
94+
want bool
95+
}{
96+
{"https remote", "https://edge.example.com", true},
97+
{"http remote", "http://edge.example.com:3210", true},
98+
{"localhost still allowed", "http://localhost:5173", true},
99+
{"empty origin rejected", "", false},
100+
{"invalid url rejected", "://bad", false},
101+
{"file scheme rejected", "file:///tmp/index.html", false},
102+
{"extension scheme rejected", "chrome-extension://abc", false},
103+
{"tauri remote host rejected", "tauri://edge.example.com", false},
104+
}
105+
106+
for _, tt := range tests {
107+
t.Run(tt.name, func(t *testing.T) {
108+
got := IsTrustedOrigin(tt.origin, true)
109+
if got != tt.want {
110+
t.Fatalf("IsTrustedOrigin(%q, true) = %v, want %v", tt.origin, got, tt.want)
111+
}
112+
})
113+
}
114+
}
115+
63116
func TestValidateLocalListenAddr(t *testing.T) {
64117
tests := []struct {
65118
name string
@@ -96,3 +149,36 @@ func TestValidateLocalListenAddr(t *testing.T) {
96149
})
97150
}
98151
}
152+
153+
func TestValidateRemoteListenAddr(t *testing.T) {
154+
tests := []struct {
155+
name string
156+
addr string
157+
wantErr bool
158+
errSnippet string
159+
}{
160+
{"wildcard host", ":3210", false, ""},
161+
{"ipv4 wildcard", "0.0.0.0:3210", false, ""},
162+
{"ipv6 wildcard", "[::]:3210", false, ""},
163+
{"lan ip", "192.168.1.10:3210", false, ""},
164+
{"remote hostname", "edge.example.com:3210", false, ""},
165+
{"loopback remains valid", "127.0.0.1:3210", false, ""},
166+
{"empty addr", "", true, "required"},
167+
{"missing port", "edge.example.com", true, "host:port"},
168+
}
169+
170+
for _, tt := range tests {
171+
t.Run(tt.name, func(t *testing.T) {
172+
err := ValidateRemoteListenAddr(tt.addr)
173+
if tt.wantErr && err == nil {
174+
t.Fatalf("ValidateRemoteListenAddr(%q) returned nil error", tt.addr)
175+
}
176+
if tt.errSnippet != "" && (err == nil || !strings.Contains(err.Error(), tt.errSnippet)) {
177+
t.Fatalf("ValidateRemoteListenAddr(%q) error = %v, want snippet %q", tt.addr, err, tt.errSnippet)
178+
}
179+
if !tt.wantErr && err != nil {
180+
t.Fatalf("ValidateRemoteListenAddr(%q) returned error: %v", tt.addr, err)
181+
}
182+
})
183+
}
184+
}

0 commit comments

Comments
 (0)