| 1 | package cage |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestExitPolicyStaticAndGeographicChanges(t *testing.T) { |
| 6 | base := ExitLocation{IP: "8.8.8.8", CountryCode: "US", Region: "California", City: "Los Angeles"} |
| 7 | city := ExitPolicy{Mode: "dynamic", Scope: "city"}.BindMissing(base) |
| 8 | country := city |
| 9 | country.Scope = "country" |
| 10 | region := city |
| 11 | region.Scope, region.City = "region", "" |
| 12 | static := ExitPolicy{Mode: "static"}.BindMissing(base) |
| 13 | for _, tc := range []struct { |
| 14 | name string |
| 15 | policy ExitPolicy |
| 16 | location ExitLocation |
| 17 | want CheckState |
| 18 | }{ |
| 19 | {"same IP", static, base, CheckPassed}, |
| 20 | {"static changes", static, ExitLocation{IP: "1.1.1.1", CountryCode: "US"}, CheckFailed}, |
| 21 | {"same city new IP", city, ExitLocation{IP: "1.1.1.1", CountryCode: "US", Region: "california", City: "Los Angeles"}, CheckPassed}, |
| 22 | {"city moves", city, ExitLocation{IP: "1.1.1.1", CountryCode: "US", Region: "California", City: "San Jose"}, CheckFailed}, |
| 23 | {"same name different state", city, ExitLocation{IP: "1.1.1.1", CountryCode: "US", Region: "Texas", City: "Los Angeles"}, CheckFailed}, |
| 24 | {"country allows city change", country, ExitLocation{IP: "1.1.1.1", CountryCode: "US", Region: "New York", City: "New York"}, CheckPassed}, |
| 25 | {"country moves", country, ExitLocation{IP: "1.1.1.1", CountryCode: "JP", Region: "Tokyo", City: "Tokyo"}, CheckFailed}, |
| 26 | {"missing city", city, ExitLocation{IP: "1.1.1.1", CountryCode: "US", Region: "California"}, CheckUnknown}, |
| 27 | {"region allows city change", region, ExitLocation{IP: "1.1.1.1", CountryCode: "US", Region: "california", City: "San Jose"}, CheckPassed}, |
| 28 | {"region does not require city", region, ExitLocation{IP: "1.1.1.1", CountryCode: "US", Region: "California"}, CheckPassed}, |
| 29 | {"region changes", region, ExitLocation{IP: "1.1.1.1", CountryCode: "US", Region: "Texas"}, CheckFailed}, |
| 30 | {"region country changes", region, ExitLocation{IP: "1.1.1.1", CountryCode: "CA", Region: "California"}, CheckFailed}, |
| 31 | {"region evidence missing", region, ExitLocation{IP: "1.1.1.1", CountryCode: "US"}, CheckUnknown}, |
| 32 | } { |
| 33 | t.Run(tc.name, func(t *testing.T) { |
| 34 | got, _ := tc.policy.Evaluate(tc.location.IP, &tc.location) |
| 35 | if got != tc.want { |
| 36 | t.Fatalf("got %s, want %s", got, tc.want) |
| 37 | } |
| 38 | }) |
| 39 | } |
| 40 | if got, _ := city.Evaluate("1.1.1.1", &base); got != CheckUnknown { |
| 41 | t.Fatal("location for another IP was accepted") |
| 42 | } |
| 43 | if got, _ := country.Evaluate("1.1.1.1", nil); got != CheckUnknown { |
| 44 | t.Fatal("missing geography was accepted") |
| 45 | } |
| 46 | if got, _ := (ExitPolicy{Mode: "dynamic", Scope: "any"}).Evaluate("1.1.1.1", nil); got != CheckUnknown { |
| 47 | t.Fatal("unrestricted dynamic mode bypassed mandatory country evidence") |
| 48 | } |
| 49 | if changed := static.BindMissing(ExitLocation{IP: "1.1.1.1"}); changed.IP != static.IP { |
| 50 | t.Fatal("recheck silently rebound static IP") |
| 51 | } |
| 52 | if changed := city.BindMissing(ExitLocation{CountryCode: "JP", Region: "Tokyo", City: "Tokyo"}); changed != city { |
| 53 | t.Fatal("recheck silently rebound city") |
| 54 | } |
| 55 | boundRegion := (ExitPolicy{Mode: "dynamic", Scope: "region"}).BindMissing(base) |
| 56 | if boundRegion.NeedsBinding() || boundRegion.Region != base.Region || boundRegion.City != "" { |
| 57 | t.Fatalf("region binding: %+v", boundRegion) |
| 58 | } |
| 59 | if changed := boundRegion.BindMissing(ExitLocation{CountryCode: "US", Region: "Texas"}); changed != boundRegion { |
| 60 | t.Fatal("recheck silently rebound region") |
| 61 | } |
| 62 | if !(ExitPolicy{Mode: "dynamic", Scope: "region", CountryCode: "JP"}).NeedsBinding() { |
| 63 | t.Fatal("region without an anchor accepted") |
| 64 | } |
| 65 | } |