package geojs_test import ( "context" "encoding/json" "net/http" "net/http/httptest" "net/netip" "slices" "strings" "sync" "testing" "testing/synctest" "time" "sneak.berlin/go/smallwebwaf/internal/geojs" ) const ( // germany is where the stand-in for GeoJS places every address but // unplaced, and asNumber, kept as asn, and asName the AS it gives them. germany = "DE" asNumber = 64496 asn = "AS64496" asName = "Example Net" // unplaced is the address it cannot place, for which it gives the AS // number 64512 and the AS name Unknown, as GeoJS does. unplaced = "192.0.2.1" // leftOut is the address it leaves out of its answer when // answeringWithoutLeftOut. leftOut = "203.0.113.7" // wait is how long a lookup waits for a new client's answer. wait = time.Second // requestTimeout is how long a request to GeoJS may take before it is // abandoned. requestTimeout = 10 * time.Second // lateAnswer is how long the stand-in takes to answer when // answeringLate. lateAnswer = 5 * time.Second // week is how long an answer is kept. week = 7 * 24 * time.Hour ) // The tests that have GeoJS asked run in a synctest bubble, where the time // package runs on a clock of the test's own: a wait lasts exactly as long // as it should, however slowly the test process runs, and synctest.Wait // returns once g has done all it can before time passes. The stand-in for // GeoJS answers without the network, since a request waiting on the // network would keep that clock from moving on. func TestKeptAnswerIsUsedFor7DaysThenAskedAgain(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, clock, g := start() placed := netip.MustParsePrefix("203.0.113.9/32") notPlaced := netip.MustParsePrefix(unplaced + "/32") wantCountry(t, g, placed, germany) wantCountry(t, g, notPlaced, "") wantRequests(t, service, 2) // An answer without a country is kept too. clock.advance(week - time.Second) wantCountry(t, g, placed, germany) wantCountry(t, g, notPlaced, "") wantRequests(t, service, 2) clock.advance(time.Second) wantCountry(t, g, placed, germany) wantRequests(t, service, 3) wantAsked(t, service, 2, "203.0.113.9") }) } func TestLookUpWithoutWaitingAsksGeoJSAndGoesOnAtOnce(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, clock, g := start() client := netip.MustParsePrefix("203.0.113.9/32") // GeoJS does not answer. service.set(hanging) began := time.Now() answer := g.LookUpWithoutWaiting(t.Context(), client) if waited := time.Since(began); waited != 0 || answer != (geojs.Answer{}) { t.Errorf( "got %+v after %s, want no answer at once", answer, waited, ) } waitForRequests(t, service, 1) wantAsked(t, service, 0, "203.0.113.9") // That request is abandoned, and has failed once this returns. GeoJS // answers when it is asked again, and the answer is kept. time.Sleep(requestTimeout) synctest.Wait() service.set(answering) waitForCountry(t, g, clock, client, germany) if got := g.LookUpWithoutWaiting(t.Context(), client).Country; got != germany { t.Errorf( "the kept answer gives the country %q, want %q", got, germany, ) } }) } func TestNewClientWaitsAsLongAsItsCallerSaysThenCountsAsNotFound( t *testing.T, ) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, clock, g := start() client := netip.MustParsePrefix("203.0.113.9/32") // The client comes while GeoJS is asked about an earlier client, which // it answers most of a second later. It is then asked about the client // and does not answer: that request is abandoned ten seconds after it // began, well after the client's wait is over. service.set(answeringSlowly) var earlier sync.WaitGroup earlier.Go(func() { g.LookUp( t.Context(), netip.MustParsePrefix("203.0.113.1/32"), wait, ) }) defer earlier.Wait() waitForRequests(t, service, 1) service.set(hanging) began := time.Now() wantCountry(t, g, client, "") took := time.Since(began) if took != wait { t.Errorf("waited %s for the answer, want %s", took, wait) } // Its next lookup does not wait. began = time.Now() wantCountry(t, g, client, "") took = time.Since(began) if took != 0 { t.Errorf("waited %s again, want no wait", took) } // Once GeoJS answers, the client is asked about again in the // background, and has its country. service.set(answering) waitForCountry(t, g, clock, client, germany) }) } func TestAnswerAfterTheWaitIsKeptAndIsNoFailure(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, _, g, reports := startWithReports() client := netip.MustParsePrefix("203.0.113.9/32") // GeoJS answers after the lookup's wait is over. service.set(answeringLate) wantCountry(t, g, client, "") time.Sleep(lateAnswer - wait) synctest.Wait() // The answer is kept, and the next lookup returns it without asking // GeoJS again. answer, found := g.Kept(client) if !found || answer.Country != germany { t.Errorf( "kept %+v, %t; want the answer in %s", answer, found, germany, ) } wantCountry(t, g, client, germany) wantRequests(t, service, 1) reports.wantFailures(t) // GeoJS is not left alone: a new client is asked about at once. service.set(answering) wantCountry(t, g, netip.MustParsePrefix("203.0.113.10/32"), germany) wantRequests(t, service, 2) }) } func TestAnswerAfter5SecondsIsReadFromTheCache6SecondsAfterTheFirstLookUp( t *testing.T, ) { t.Parallel() synctest.Test(t, func(t *testing.T) { const shortWait = 800 * time.Millisecond service, _, g := start() client := netip.MustParsePrefix("203.0.113.9/32") service.set(answeringLate) began := time.Now() answer := g.LookUp(t.Context(), client, shortWait) if took := time.Since(began); took != shortWait || answer != (geojs.Answer{}) { t.Errorf( "got %+v after %s, want no answer after %s", answer, took, shortWait, ) } time.Sleep(6*time.Second - shortWait) began = time.Now() answer = g.LookUp(t.Context(), client, shortWait) if took := time.Since(began); took != 0 || answer.Country != germany { t.Errorf( "got %+v after %s, want the answer in %s at once", answer, took, germany, ) } wantRequests(t, service, 1) }) } func TestTenLookUpsAtOnceOfOneClientCauseOneRequest(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, _, g := start() client := netip.MustParsePrefix("203.0.113.9/32") service.set(answeringSlowly) var lookUps sync.WaitGroup for range 10 { lookUps.Go(func() { wantCountry(t, g, client, germany) }) } lookUps.Wait() wantRequests(t, service, 1) wantAsked(t, service, 0, "203.0.113.9") }) } func TestCallerWhoseContextEndsEarlyDoesNotStopTheRequest(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { const early = 100 * time.Millisecond service, _, g, reports := startWithReports() client := netip.MustParsePrefix("203.0.113.9/32") service.set(answeringLate) ctx, cancel := context.WithTimeout(t.Context(), early) defer cancel() began := time.Now() answer := g.LookUp(ctx, client, wait) if took := time.Since(began); took != early || answer != (geojs.Answer{}) { t.Errorf( "got %+v after %s, want no answer after %s", answer, took, early, ) } time.Sleep(lateAnswer) synctest.Wait() answer, found := g.Kept(client) if !found || answer.Country != germany { t.Errorf( "kept %+v, %t; want the answer in %s", answer, found, germany, ) } wantRequests(t, service, 1) reports.wantFailures(t) }) } func TestRequestOver10SecondsIsAFailure(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, _, g, reports := startWithReports() // GeoJS does not answer. service.set(hanging) wantCountry(t, g, netip.MustParsePrefix("203.0.113.9/32"), "") // A moment before 10 seconds are up, the request has not failed. time.Sleep(requestTimeout - wait - time.Millisecond) synctest.Wait() reports.wantFailures(t) // As they run out, it is abandoned, which is a failure. The error // does not name the address asked about. time.Sleep(time.Millisecond) synctest.Wait() reports.wantFailures(t, failure{ err: "ask GeoJS: context deadline exceeded", askingAgainIn: time.Second, }) // GeoJS is then left alone, so a new client is not asked about. wantCountry(t, g, netip.MustParsePrefix("203.0.113.10/32"), "") wantRequests(t, service, 1) }) } func TestFailureIsReportedWithHowLongGeoJSIsLeftAlone(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, clock, g, reports := startWithReports() clients := newClients() service.set(failing) wantCountry(t, g, clients(), "") clock.advance(time.Second) wantCountry(t, g, clients(), "") reports.wantFailures(t, failure{ err: "GeoJS answered 503 Service Unavailable", askingAgainIn: time.Second, }, failure{ err: "GeoJS answered 503 Service Unavailable", askingAgainIn: 2 * time.Second, }, ) }) } func TestAddressLeftOutOfAnAnswerIsAskedAboutAgain(t *testing.T) { t.Parallel() for _, tc := range []struct { name string answers int // named is whether the answer names the other client asked about. named bool }{ {"null", answeringNull, false}, {"empty list", answeringEmptyList, false}, {"list without " + leftOut, answeringWithoutLeftOut, true}, } { t.Run(tc.name, func(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, clock, g := start() other := netip.MustParsePrefix("203.0.113.1/32") client := netip.MustParsePrefix(leftOut + "/32") // GeoJS fails, and is left alone for a second while the client // comes too, so that the next request asks about both. service.set(failing) wantCountry(t, g, other, "") wantCountry(t, g, client, "") service.set(tc.answers) clock.advance(time.Second) wantCountry(t, g, other, "") waitForRequests(t, service, 2) // The answer counts as a failure, and the client is asked about // again, with the other client only if the answer left it out too. service.set(answering) waitForCountry(t, g, clock, client, germany) wantCountry(t, g, other, germany) wantRequests(t, service, 3) if tc.named { wantAsked(t, service, 2, leftOut) } else { wantAsked(t, service, 2, leftOut, "203.0.113.1") } }) }) } } func TestRedirectCountsAsFailure(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, _, g := start() service.set(redirecting) wantCountry(t, g, netip.MustParsePrefix("203.0.113.9/32"), "") wantRequests(t, service, 1) }) } func TestCountryIsKeptInCapitals(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, _, g := start() service.set(answeringInLowerCase) wantCountry(t, g, netip.MustParsePrefix("203.0.113.9/32"), germany) }) } func TestAnswerHoldsTheASNumberTheASNameAndTheCountry(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { _, clock, g := start() placed := netip.MustParsePrefix("203.0.113.9/32") notPlaced := netip.MustParsePrefix(unplaced + "/32") now := clock.Now() // For the client it cannot place, GeoJS gives the AS number 64512 // and the AS name Unknown, which count as unknown. for client, want := range map[netip.Prefix]geojs.Answer{ placed: { Client: placed, ASN: asn, ASName: asName, Country: germany, Answered: now, Used: now, }, notPlaced: {Client: notPlaced, Answered: now, Used: now}, } { got := g.LookUp(t.Context(), client, wait) if got != want { t.Errorf("answer for %s\n%+v\nwant\n%+v", client, got, want) } } }) } func TestEachAnswerIsGivenToAnsweredOnceKept(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { var ( mu sync.Mutex given []geojs.Answer ) service := &standIn{answers: answeringSlowly} clock := newClock() g, reports := newGeoJS(service, geojs.Params{ Answered: func(answer geojs.Answer) { mu.Lock() defer mu.Unlock() given = append(given, answer) }, Now: clock.Now, }) client := netip.MustParsePrefix("203.0.113.9/32") // GeoJS answers about the client most of a second later. if got := g.LookUpWithoutWaiting(t.Context(), client); got != (geojs.Answer{}) { t.Errorf("got %+v, want no answer", got) } waitForRequests(t, service, 1) wantAsked(t, service, 0, "203.0.113.9") time.Sleep(wait) synctest.Wait() now := clock.Now() want := geojs.Answer{ Client: client, ASN: asn, ASName: asName, Country: germany, Answered: now, Used: now, } mu.Lock() if !slices.Equal(given, []geojs.Answer{want}) { t.Errorf("answers given %+v, want only %+v", given, want) } mu.Unlock() wantCountry(t, g, client, germany) reports.wantWithoutAnswer(t, 0) }) } func TestWaitingClientsAreAskedAboutInOneRequest(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, clock, g := start() // GeoJS fails, and is then left alone for a second, while three more // clients come. An IPv6 client is a /64, and GeoJS is asked about its // first address. service.set(failing) wantCountry(t, g, netip.MustParsePrefix("203.0.113.1/32"), "") wantCountry(t, g, netip.MustParsePrefix("203.0.113.2/32"), "") wantCountry(t, g, netip.MustParsePrefix("2001:db8:1:2::/64"), "") wantRequests(t, service, 1) service.set(answering) clock.advance(time.Second) wantCountry(t, g, netip.MustParsePrefix("203.0.113.3/32"), germany) wantRequests(t, service, 2) wantAsked( t, service, 1, "203.0.113.1", "203.0.113.2", "2001:db8:1:2::", "203.0.113.3", ) }) } func TestKeptAnswersUnaffectedWhileGeoJSFailsAndAskedAgainWithBackoff( t *testing.T, ) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, clock, g := start() clients := newClients() kept := clients() wantCountry(t, g, kept, germany) service.set(failing) wantCountry(t, g, kept, germany) wantRequests(t, service, 1) // Each failure leaves GeoJS alone twice as long as the one before, up // to five minutes. New clients meanwhile count as not found, and the // client with a kept answer still gets its country, without GeoJS being // asked. requests := 1 for _, delay := range []time.Duration{ time.Second, 2 * time.Second, 4 * time.Second, 8 * time.Second, 16 * time.Second, 32 * time.Second, 64 * time.Second, 128 * time.Second, 256 * time.Second, 5 * time.Minute, 5 * time.Minute, } { wantCountry(t, g, clients(), "") requests++ wantRequests(t, service, requests) clock.advance(delay - time.Millisecond) wantCountry(t, g, clients(), "") wantCountry(t, g, kept, germany) wantRequests(t, service, requests) clock.advance(time.Millisecond) } // Once GeoJS answers again, it is asked about every client waiting. service.set(answering) wantCountry(t, g, clients(), germany) wantRequests(t, service, requests+1) asked := waitForRequests(t, service, requests+1) if len(asked[requests]) != 23 { t.Errorf( "GeoJS was asked about %d clients, want 23", len(asked[requests]), ) } }) } func TestAtMost200AddressesInOneRequest(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, clock, g := start() clients := newClients() first := clients() // 201 clients wait while GeoJS is left alone after a failure. service.set(failing) wantCountry(t, g, first, "") for range 200 { wantCountry(t, g, clients(), "") } // The first one's next lookup has GeoJS asked again. service.set(answering) clock.advance(time.Second) wantCountry(t, g, first, "") asked := waitForRequests(t, service, 3) if len(asked[1]) != 200 || len(asked[2]) != 1 { t.Errorf( "GeoJS was asked about %d and then %d clients, want 200 and 1", len(asked[1]), len(asked[2]), ) } }) } func TestAtMost10000ClientsWait(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, clock, g := start() clients := newClients() first := clients() // 10,000 clients wait while GeoJS is left alone after a failure, and // one more cannot join them. service.set(failing) wantCountry(t, g, first, "") for range 9999 { wantCountry(t, g, clients(), "") } extra := clients() wantCountry(t, g, extra, "") // The first one's next lookup has GeoJS asked about the 10,000, 200 // at a time, and not about the one more. service.set(answering) clock.advance(time.Second) wantCountry(t, g, first, "") asked := waitForRequests(t, service, 51) for i, request := range asked { if slices.Contains(request, extra.Addr().String()) { t.Errorf("request %d asked about %s", i, extra.Addr()) } } // With room among those waiting, it is asked about. wantCountry(t, g, extra, germany) }) } func TestClientsWithoutAnAnswerAreCounted(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { service, _, g, reports := startWithReports() clients := newClients() service.set(failing) // GeoJS fails, so the first client goes without an answer, and GeoJS // is left alone for a second, which does not pass in this test. wantCountry(t, g, clients(), "") reports.wantWithoutAnswer(t, 1) // Meanwhile each new client goes without one at once, while there is // room for it among the 10,000 that may wait. for range 9999 { wantCountry(t, g, clients(), "") } reports.wantWithoutAnswer(t, 10000) // One more, for which there is no room, goes without one too. wantCountry(t, g, clients(), "") reports.wantWithoutAnswer(t, 10001) }) } // How the stand-in for GeoJS answers. const ( answering = iota answeringSlowly // most of a second later answeringLate // lateAnswer later answeringInLowerCase // with each country in lower case answeringWithoutLeftOut // with a list that leaves leftOut out answeringEmptyList // with [] answeringNull // with null failing // with 503 hanging // not at all, until the request is abandoned redirecting // with a redirect to itself ) // standIn is a stand-in for GeoJS. It notes the addresses each request // asks about. type standIn struct { mu sync.Mutex answers int requests [][]string } // RoundTrip has the stand-in answer req, in place of the network. A request // abandoned before the stand-in answers fails, as over the network. func (s *standIn) RoundTrip(req *http.Request) (*http.Response, error) { answer := httptest.NewRecorder() s.ServeHTTP(answer, req) err := req.Context().Err() if err != nil { return nil, err } return answer.Result(), nil } // ServeHTTP answers a request about the addresses in its ip parameter. func (s *standIn) ServeHTTP(w http.ResponseWriter, r *http.Request) { addrs := strings.Split(r.URL.Query().Get("ip"), ",") s.mu.Lock() s.requests = append(s.requests, addrs) answers := s.answers s.mu.Unlock() switch answers { case failing: w.WriteHeader(http.StatusServiceUnavailable) return case hanging: <-r.Context().Done() return case redirecting: http.Redirect(w, r, "/", http.StatusFound) return case answeringSlowly, answeringLate: select { case <-time.After(delay(answers)): case <-r.Context().Done(): return } } list := make([]map[string]any, 0, len(addrs)) for _, addr := range addrs { item := map[string]any{ "ip": addr, "asn": asNumber, "organization_name": asName, "country_code": germany, } switch { case addr == unplaced: item = map[string]any{ "ip": addr, "asn": 64512, "organization_name": "Unknown", } case addr == leftOut && answers == answeringWithoutLeftOut: continue case answers == answeringInLowerCase: item["country_code"] = strings.ToLower(germany) } list = append(list, item) } var answer any = list switch answers { case answeringEmptyList: answer = []string{} case answeringNull: answer = nil } err := json.NewEncoder(w).Encode(answer) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // delay returns how long the stand-in takes to answer when answering // slowly or late. func delay(answers int) time.Duration { if answers == answeringLate { return lateAnswer } return wait * 4 / 5 } // set sets how the stand-in answers. func (s *standIn) set(answers int) { s.mu.Lock() defer s.mu.Unlock() s.answers = answers } // asked returns the addresses each request has asked about so far. func (s *standIn) asked() [][]string { s.mu.Lock() defer s.mu.Unlock() return slices.Clone(s.requests) } // reports are what a GeoJS reported through Failed and WentWithoutAnswer. type reports struct { mu sync.Mutex failures []failure withoutAnswer int } // failure is one report of Failed: the error, and how long GeoJS is left // alone. type failure struct { err string askingAgainIn time.Duration } // wantFailures checks the failures reported so far. func (r *reports) wantFailures(t *testing.T, want ...failure) { t.Helper() r.mu.Lock() defer r.mu.Unlock() if !slices.Equal(r.failures, want) { t.Errorf("failures %+v, want %+v", r.failures, want) } } // wantWithoutAnswer checks how many times a caller has gone without an // answer. func (r *reports) wantWithoutAnswer(t *testing.T, want int) { t.Helper() r.mu.Lock() defer r.mu.Unlock() if r.withoutAnswer != want { t.Errorf("%d lookups went without an answer, want %d", r.withoutAnswer, want) } } // testClock is a clock the test sets. GeoJS tells the time by it, while // waits run on the bubble's clock. type testClock struct { mu sync.Mutex now time.Time } // Now tells the time. func (c *testClock) Now() time.Time { c.mu.Lock() defer c.mu.Unlock() return c.now } // advance moves the clock on by d. func (c *testClock) advance(d time.Duration) { c.mu.Lock() defer c.mu.Unlock() c.now = c.now.Add(d) } // start returns a stand-in for GeoJS that answers, a clock, and a GeoJS // asking the stand-in by that clock. func start() (*standIn, *testClock, *geojs.GeoJS) { service, clock, g, _ := startWithReports() return service, clock, g } // startWithReports is start, and returns what the GeoJS reports as well. func startWithReports() (*standIn, *testClock, *geojs.GeoJS, *reports) { service := &standIn{} clock := newClock() g, reports := newGeoJS(service, geojs.Params{Now: clock.Now}) return service, clock, g, reports } // newGeoJS returns a GeoJS that asks service, made with params and with // what it reports noted in the reports returned. func newGeoJS( service *standIn, params geojs.Params, ) (*geojs.GeoJS, *reports) { r := &reports{} params.URL = geojs.URL params.Asked = func() {} params.Failed = func(err error, askingAgainIn time.Duration) { r.mu.Lock() defer r.mu.Unlock() r.failures = append(r.failures, failure{err.Error(), askingAgainIn}) } params.WentWithoutAnswer = func() { r.mu.Lock() defer r.mu.Unlock() r.withoutAnswer++ } g := geojs.New(params) g.SetTransport(service) return g, r } // newClock returns a clock set to the start of a day. func newClock() *testClock { return &testClock{now: time.Date(2026, 10, 4, 0, 0, 0, 0, time.UTC)} } // newClients returns what returns a new IPv4 client each time it is // called. func newClients() func() netip.Prefix { addr := netip.MustParseAddr("10.0.0.0") return func() netip.Prefix { addr = addr.Next() return netip.PrefixFrom(addr, addr.BitLen()) } } // wantCountry checks the country g gives client, waiting up to wait for // it. func wantCountry( t *testing.T, g *geojs.GeoJS, client netip.Prefix, want string, ) { t.Helper() got := g.LookUp(t.Context(), client, wait).Country if got != want { t.Errorf("%s is in %q, want %q", client, got, want) } } // wantRequests checks how many requests GeoJS has had. func wantRequests(t *testing.T, service *standIn, want int) { t.Helper() got := len(service.asked()) if got != want { t.Errorf("GeoJS had %d requests, want %d", got, want) } } // wantAsked checks the addresses request i asked about, in any order. func wantAsked(t *testing.T, service *standIn, i int, want ...string) { t.Helper() asked := service.asked() if len(asked) <= i { t.Fatalf("GeoJS had %d requests, want more than %d", len(asked), i) } got := slices.Sorted(slices.Values(asked[i])) slices.Sort(want) if !slices.Equal(got, want) { t.Errorf("request %d asked about %v, want %v", i, got, want) } } // waitForRequests waits until g has done all it can before time passes, // checks that GeoJS has had count requests, and returns the addresses each // asked about. func waitForRequests(t *testing.T, service *standIn, count int) [][]string { t.Helper() synctest.Wait() asked := service.asked() if len(asked) != count { t.Fatalf("GeoJS had %d requests, want %d", len(asked), count) } return asked } // waitForCountry lets a request to GeoJS under way be abandoned, and moves // the clock on a minute, so that GeoJS may be asked again after a failure. // It then checks that client's next lookup does not wait but has it asked // about again in the background, after which g gives it the country want. func waitForCountry( t *testing.T, g *geojs.GeoJS, clock *testClock, client netip.Prefix, want string, ) { t.Helper() time.Sleep(requestTimeout) clock.advance(time.Minute) wantCountry(t, g, client, "") synctest.Wait() wantCountry(t, g, client, want) }