| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "math/rand/v2" |
| 6 | "net" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/geo" |
| 11 | ) |
| 12 | |
| 13 | type fakeLocator map[string]geo.Point |
| 14 | |
| 15 | func (f fakeLocator) Locate(ip net.IP) (geo.Point, bool) { |
| 16 | p, ok := f[ip.String()] |
| 17 | return p, ok |
| 18 | } |
| 19 | |
| 20 | func allowAll(string) bool { return true } |
| 21 | |
| 22 | func cand(code, aws string, eligible bool) regionCandidate { |
| 23 | return regionCandidate{Code: code, Home: code == "", AWSRegion: aws, |
| 24 | Reachable: true, Hosting: true, KeyOK: eligible, Eligible: eligible} |
| 25 | } |
| 26 | |
| 27 | var ( |
| 28 | cHome = cand("", "eu-west-1", true) |
| 29 | cUK = cand("uk", "eu-west-2", true) |
| 30 | cAU = cand("au", "ap-southeast-2", true) |
| 31 | ) |
| 32 | |
| 33 | func TestGuessRegionOrder(t *testing.T) { |
| 34 | loc := fakeLocator{"81.2.69.142": {Lat: -33.9, Lon: 151.2}} |
| 35 | cands := []regionCandidate{cHome, cUK, cAU} |
| 36 | cases := []struct { |
| 37 | name string |
| 38 | in guessInput |
| 39 | want guessResult |
| 40 | }{ |
| 41 | {"timezone first", guessInput{TZ: "Europe/London", IP: net.ParseIP("81.2.69.142")}, guessResult{"uk", "timezone"}}, |
| 42 | {"UTC falls to IP", guessInput{TZ: "Etc/UTC", IP: net.ParseIP("81.2.69.142")}, guessResult{"au", "ip"}}, |
| 43 | {"nothing maps", guessInput{TZ: "Etc/UTC"}, guessResult{"", "default"}}, |
| 44 | } |
| 45 | for _, c := range cases { |
| 46 | if got, ok := guessRegion(cands, allowAll, c.in, loc); !ok || got != c.want { |
| 47 | t.Errorf("%s: got (%+v, %v), want %+v", c.name, got, ok, c.want) |
| 48 | } |
| 49 | } |
| 50 | got, _ := guessRegion([]regionCandidate{cHome, cUK, cand("au", "ap-southeast-2", false)}, allowAll, guessInput{TZ: "Australia/Sydney"}, nil) |
| 51 | if got.Code == "au" { |
| 52 | t.Fatal("picked an ineligible region") |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // spec §6.4, codex R3-4. |
| 57 | func TestGuessRegionNeverReturnsADisallowedHome(t *testing.T) { |
| 58 | noPointHome := cand("", "", true) |
| 59 | onlyUK := func(code string) bool { return instanceRegionAllowed([]string{"uk"}, code) } |
| 60 | cands := []regionCandidate{noPointHome, cUK} |
| 61 | if got, ok := guessRegion(cands, onlyUK, guessInput{TZ: "Europe/London"}, nil); !ok || got.Code != "uk" { |
| 62 | t.Fatalf("mapped hint = (%+v, %v), want uk", got, ok) |
| 63 | } |
| 64 | if got, ok := guessRegion(cands, onlyUK, guessInput{TZ: "Etc/UTC"}, nil); ok { |
| 65 | t.Fatalf("unmappable hint = %+v, want no guess", got) |
| 66 | } |
| 67 | if got, ok := guessRegion(homeOnlyCandidates(), onlyUK, guessInput{TZ: "Europe/London"}, nil); ok { |
| 68 | t.Fatalf("edges-read failure = %+v, want no guess", got) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // The invariant over generated MANIFESTS, using the door's own predicate |
| 73 | // on the normalized code (spec §16, codex plan-R1 10): |
| 74 | // |
| 75 | // 1. every non-null guess passes instanceRegionAllowed and is an eligible |
| 76 | // candidate; |
| 77 | // 2. a guess exists EXACTLY when the order of §6.4 can reach one: the home |
| 78 | // is a filtered candidate, or the timezone maps and some filtered |
| 79 | // candidate has a point. |
| 80 | func TestGuessRegionInvariantProperty(t *testing.T) { |
| 81 | codes := []string{"", "uk", "au", "us", "jp"} |
| 82 | listable := []string{"", "ie", "uk", "au", "us", "jp"} |
| 83 | regions := []string{"eu-west-1", "eu-west-2", "ap-southeast-2", "us-east-1", "", "xx-nowhere-1"} |
| 84 | zones := []string{"Europe/London", "Australia/Sydney", "America/New_York", "Etc/UTC", "", "Asia/Tokyo"} |
| 85 | r := rand.New(rand.NewPCG(1, 2)) |
| 86 | for i := 0; i < 20000; i++ { |
| 87 | var cands []regionCandidate |
| 88 | for _, code := range codes { |
| 89 | if code != "" && r.IntN(3) == 0 { |
| 90 | continue |
| 91 | } |
| 92 | cands = append(cands, cand(code, regions[r.IntN(len(regions))], code == "" || r.IntN(4) != 0)) |
| 93 | } |
| 94 | var list []string |
| 95 | for _, c := range listable { |
| 96 | if r.IntN(3) == 0 { |
| 97 | list = append(list, c) |
| 98 | } |
| 99 | } |
| 100 | allows := func(code string) bool { return instanceRegionAllowed(list, code) } |
| 101 | tz := zones[r.IntN(len(zones))] |
| 102 | got, ok := guessRegion(cands, allows, guessInput{TZ: tz}, nil) |
| 103 | |
| 104 | homeIn, anyPoint := false, false |
| 105 | for _, c := range cands { |
| 106 | if !c.Eligible || !allows(c.Code) { |
| 107 | continue |
| 108 | } |
| 109 | homeIn = homeIn || c.Home |
| 110 | if _, has := geo.LookupAWSRegion(c.AWSRegion); has { |
| 111 | anyPoint = true |
| 112 | } |
| 113 | } |
| 114 | _, tzMaps := geo.ZonePoint(tz) |
| 115 | want := homeIn || (tzMaps && anyPoint) |
| 116 | if ok != want { |
| 117 | t.Fatalf("case %d: guess existence %v, want %v (list %v, tz %q, cands %+v)", i, ok, want, list, tz, cands) |
| 118 | } |
| 119 | if !ok { |
| 120 | continue |
| 121 | } |
| 122 | if !allows(got.Code) { |
| 123 | t.Fatalf("case %d: guess %+v fails the manifest %v", i, got, list) |
| 124 | } |
| 125 | if c, found := findCandidate(cands, got.Code); !found || !c.Eligible { |
| 126 | t.Fatalf("case %d: guess %+v is not an eligible candidate", i, got) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Review Focus 4's core: a malformed hint is absent. |
| 132 | func TestGuessRegionToleratesGarbageHints(t *testing.T) { |
| 133 | got, ok := guessRegion([]regionCandidate{cHome, cUK}, allowAll, guessInput{TZ: strings.Repeat("a", 10<<10)}, fakeLocator{}) |
| 134 | if !ok || got != (guessResult{"", "default"}) { |
| 135 | t.Fatalf("garbage tz = (%+v, %v), want the home default", got, ok) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // codex plan-R3 4: a region whose boxes only collectively have every fact |
| 140 | // is never guessed, however near the caller is (Task 10's splitCapability). |
| 141 | func TestGuessRegionSkipsSplitCapabilityRegions(t *testing.T) { |
| 142 | for name, boxes := range splitCapability { |
| 143 | cands := regionCandidates(views(t, homeBox, boxes[0], boxes[1]), "ie", testKeyFP) |
| 144 | got, ok := guessRegion(cands, allowAll, guessInput{TZ: "Europe/London"}, nil) |
| 145 | if !ok || got.Code != "" { |
| 146 | t.Errorf("%s: guess = %+v %v, want the home", name, got, ok) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Controller ruling O7: the home has no "left out" diagnostic. A home |
| 152 | // without a point yields "default" (spec §6.3), so the log line would |
| 153 | // misdescribe it. A coded region without a point still logs. |
| 154 | func TestRegionCandidatesHomeWithNoPointLogsNoO7(t *testing.T) { |
| 155 | var buf bytes.Buffer |
| 156 | restore := captureLogTo(&buf) |
| 157 | defer restore() |
| 158 | noPointSaid.Delete("") |
| 159 | noPointSaid.Delete("odd") |
| 160 | noRegionHome := edgeJSON("ie-box", "5.6.7.8", "", "", eligibleBox) |
| 161 | odd := edgeJSON("odd-box", "3.3.3.3", "xx-nowhere-1", "odd", eligibleBox) |
| 162 | regionCandidates(views(t, noRegionHome), "ie", testKeyFP) |
| 163 | if buf.Len() != 0 { |
| 164 | t.Fatalf("a home with no AWS region logged: %s", buf.String()) |
| 165 | } |
| 166 | regionCandidates(views(t, noRegionHome, odd), "ie", testKeyFP) |
| 167 | if !strings.Contains(buf.String(), `region "odd"`) || strings.Contains(buf.String(), `region ""`) { |
| 168 | t.Fatalf("log = %q, want the coded region only", buf.String()) |
| 169 | } |
| 170 | } |
| 171 | |