跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

country_test.go

94 行 · 3.3 KB历史下载
1package exitpolicy
2
3import (
4 "crypto/sha256"
5 _ "embed"
6 "encoding/hex"
7 "encoding/json"
8 "testing"
9)
10
11//go:embed data/source.json
12var countrySource []byte
13
14func TestBundledMainlandListSourceAndCoverage(t *testing.T) {
15 var source struct {
16 SHA256 string `json:"sha256"`
17 Prefixes int `json:"ipv4_prefixes"`
18 }
19 if err := json.Unmarshal(countrySource, &source); err != nil {
20 t.Fatal(err)
21 }
22 digest := sha256.Sum256([]byte(mainlandIPv4))
23 if mainlandListError != nil || source.Prefixes != len(mainlandPrefixes) || source.SHA256 != hex.EncodeToString(digest[:]) {
24 t.Fatal("bundled address list does not match its source metadata", mainlandListError)
25 }
26 for _, ip := range []string{"114.114.114.114", "180.76.76.76", "223.5.5.5"} {
27 if state, _ := CheckExitCountry(ip, nil); state != Failed {
28 t.Errorf("mainland address %s accepted", ip)
29 }
30 }
31 for _, ip := range []string{"8.8.8.8", "1.1.1.1"} {
32 if state, _ := CheckExitCountry(ip, &Location{IP: ip, CountryCode: "US"}); state != Passed {
33 t.Errorf("non-mainland address %s rejected", ip)
34 }
35 }
36 for _, invalid := range []string{"", "0.0.0.0/0", "1.0.1.1/24", "not-a-prefix", "::/0"} {
37 if _, err := parseMainlandPrefixes(invalid); err == nil {
38 t.Errorf("invalid list accepted: %q", invalid)
39 }
40 }
41}
42
43func TestUnsafeExitCannotBecomeInitialAnchor(t *testing.T) {
44 for _, policy := range []Policy{{Mode: "static"}, {Mode: "dynamic", Scope: "country"}} {
45 for _, location := range []Location{{IP: "223.5.5.5", CountryCode: "US"}, {IP: "8.8.8.8", CountryCode: "CN"}, {IP: "8.8.8.8"}} {
46 if got := policy.BindMissing(location); got != policy.Normalized() {
47 t.Fatalf("unsafe exit became an anchor: %+v", got)
48 }
49 }
50 }
51}
52
53func TestMainlandExitIsDeniedInEveryMode(t *testing.T) {
54 policies := []Policy{
55 {Mode: "static", IP: "223.5.5.5"},
56 {Mode: "dynamic", Scope: "any"},
57 {Mode: "dynamic", Scope: "country", CountryCode: "CN"},
58 }
59 for _, policy := range policies {
60 for _, country := range []string{"", "CN", "US"} {
61 location := &Location{IP: "223.5.5.5", CountryCode: country}
62 state, message := policy.Evaluate(location.IP, location)
63 if state != Failed {
64 t.Errorf("mode=%s scope=%s country=%s: got %s (%s), want denied", policy.Mode, policy.Scope, country, state, message)
65 }
66 }
67 }
68}
69
70func TestAllModesRequireMatchingNonMainlandCountryEvidence(t *testing.T) {
71 for _, policy := range []Policy{{Mode: "static", IP: "8.8.8.8"}, {Mode: "dynamic", Scope: "any"}} {
72 for _, tc := range []struct {
73 name string
74 location *Location
75 want State
76 }{
77 {"missing", nil, Unknown},
78 {"missing country", &Location{IP: "8.8.8.8"}, Unknown},
79 {"wrong address", &Location{IP: "1.1.1.1", CountryCode: "US"}, Unknown},
80 {"invalid country", &Location{IP: "8.8.8.8", CountryCode: "??"}, Unknown},
81 {"mainland geolocation", &Location{IP: "8.8.8.8", CountryCode: "cn"}, Failed},
82 {"United States", &Location{IP: "8.8.8.8", CountryCode: "US"}, Passed},
83 {"Hong Kong", &Location{IP: "8.8.8.8", CountryCode: "HK"}, Passed},
84 {"Macao", &Location{IP: "8.8.8.8", CountryCode: "MO"}, Passed},
85 {"Taiwan", &Location{IP: "8.8.8.8", CountryCode: "TW"}, Passed},
86 } {
87 t.Run(policy.Mode+"/"+tc.name, func(t *testing.T) {
88 if got, message := policy.Evaluate("8.8.8.8", tc.location); got != tc.want {
89 t.Fatalf("got %s (%s), want %s", got, message, tc.want)
90 }
91 })
92 }
93 }
94}