| 1 |
package geoip |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnsserver" |
| 7 |
"github.com/oschwald/geoip2-golang/v2" |
| 8 |
) |
| 9 |
|
| 10 |
func TestSetup(t *testing.T) { |
| 11 |
testcases := []struct { |
| 12 |
input string |
| 13 |
exp *Geoip |
| 14 |
}{ |
| 15 |
{ |
| 16 |
`geoip { |
| 17 |
city testdata/GeoIPCity.dat |
| 18 |
}`, |
| 19 |
&Geoip{City: new(geoip2.Reader)}, |
| 20 |
}, |
| 21 |
{ |
| 22 |
`geoip { |
| 23 |
city testdata/GeoIPCity.dat testdata/GeoIPCity.dat |
| 24 |
}`, |
| 25 |
&Geoip{City: new(geoip2.Reader), City6: new(geoip2.Reader)}, |
| 26 |
}, |
| 27 |
} |
| 28 |
for i, tc := range testcases { |
| 29 |
geoip := new(Geoip) |
| 30 |
co := dnsserver.NewTestController(tc.input) |
| 31 |
err := geoip.Setup(co) |
| 32 |
if err != nil { |
| 33 |
t.Fatal(err) |
| 34 |
} |
| 35 |
if geoip.City == nil { |
| 36 |
t.Errorf("test %d, city, expected non-nil, got nil", i) |
| 37 |
} |
| 38 |
if tc.exp.City == nil && geoip.City != nil { |
| 39 |
t.Errorf("test %d, city, expected nil, got %v", i, tc.exp.City) |
| 40 |
} |
| 41 |
if tc.exp.City6 == nil && geoip.City6 != nil { |
| 42 |
t.Errorf("test %d, city, expected nil, got %v", i, tc.exp.City6) |
| 43 |
} |
| 44 |
} |
| 45 |
} |