| 1 | package exitpolicy |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | _ "embed" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | var countrySource []byte |
| 13 | |
| 14 | func 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 | |
| 43 | func 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 | |
| 53 | func 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 | |
| 70 | func 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 | } |