Files
whatismyip/models/geo_test.go
T
dcarrillo 49ffcadb0a Fix data races, nil-record panics, scanner SSRF, log date layout, shutdown timeouts, and enable stricter linters (#54)
High (behavioral bugs):
- service.Geo: add RLock on lookups, handle Reload error, keep old DBs on failure
- models.GeoDB.Reload: open new DBs before closing old ones (no reader gap)
- router: guard nil geo records (404 instead of 500) in geo.go, generic.go, dns.go
- router/port_scanner: reject undetermined client IPs (nil->localhost dial)
- internal/httputils: fix access log date layout (02/Jan/2006, not literal Nov)
- server/quic: log.Printf instead of log.Fatal in request handler

Medium:
- server/*: 10s shutdown timeout; SIGTERM drains servers before closing geo DBs
- httputils: Clone() request headers instead of mutating the live map; strings.Builder
- setting/app: handle all os.Stat errors; fix "truster" typos; use errors.New;
  remove dead Token field
- cmd: errors.Is for sentinel checks; os.Stderr; log.Fatal instead of panic;
  pass http.Handler by value (not pointer-to-interface)
- resolver: validate config at startup (pre-parse RRs, validate IPs); check WriteMsg
  errors; Setup returns error

Low:
- .golangci.yaml: enable errcheck, govet, errorlint; fix all resulting findings
- server/tls: explicit TLSConfig{MinVersion: tls.VersionTLS12}
- router/dns: label-exact vhost suffix check, strip port; template.Must
- models: errors.Join instead of %s on []error
- service/geo_test: race-regression test (concurrent lookups during Reload)
- README: Go >= 1.25; fix latent test bug (store.Add->Set in dns_test.go)
2026-07-19 12:09:52 +02:00

82 lines
2.1 KiB
Go

package models
import (
"fmt"
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestModels(t *testing.T) {
expectedCity := &GeoRecord{
Country: struct {
ISOCode string "maxminddb:\"iso_code\""
Names map[string]string "maxminddb:\"names\""
}{
ISOCode: "GB",
Names: map[string]string{
"de": "Vereinigtes Königreich",
"en": "United Kingdom",
"es": "Reino Unido",
"fr": "Royaume-Uni",
"ja": "イギリス",
"pt-BR": "Reino Unido",
"ru": "Великобритания",
"zh-CN": "英国",
},
},
City: struct {
Names map[string]string "maxminddb:\"names\""
}{
Names: map[string]string{
"de": "London",
"en": "London",
"es": "Londres",
"fr": "Londres",
"ja": "ロンドン",
"pt-BR": "Londres",
"ru": "Лондон",
},
},
Location: struct {
Latitude float64 "maxminddb:\"latitude\""
Longitude float64 "maxminddb:\"longitude\""
TimeZone string "maxminddb:\"time_zone\""
}{
Latitude: 51.5142,
Longitude: -0.0931,
TimeZone: "Europe/London",
},
Postal: struct {
Code string "maxminddb:\"code\""
}{
Code: "",
},
}
expectedASN := &ASNRecord{
AutonomousSystemNumber: 12552,
AutonomousSystemOrganization: "IP-Only",
}
db, err := Setup("../test/GeoIP2-City-Test.mmdb", "../test/GeoLite2-ASN-Test.mmdb")
require.NoError(t, err, fmt.Sprintf("Error setting up db: %s", err))
t.Cleanup(func() { assert.NoError(t, db.CloseDBs()) })
assert.NotNil(t, db.ASN)
assert.NotNil(t, db.City)
cityRecord, err := db.LookupCity(net.ParseIP("81.2.69.192"))
require.NoError(t, err, fmt.Sprintf("Error looking up city: %s", err))
assert.Equal(t, expectedCity, cityRecord)
_, err = db.LookupCity(net.ParseIP("error"))
assert.Error(t, err)
asnRecord, err := db.LookupASN(net.ParseIP("82.99.17.64"))
require.NoError(t, err, fmt.Sprintf("Error looking up asn: %s", err))
assert.Equal(t, expectedASN, asnRecord)
_, err = db.LookupASN(net.ParseIP("error"))
assert.Error(t, err)
}