forked from NiceLabs/geoip-seeker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_seeker_test.go
66 lines (58 loc) · 1.14 KB
/
ip_seeker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package qqwry
import (
"fmt"
"io/ioutil"
"math/rand"
"net"
"testing"
)
var seeker *Seeker
func init() {
data, _ := ioutil.ReadFile("../testdata/qqwry.dat")
seeker, _ = New(data)
}
func TestSeeker_Init(t *testing.T) {
_, _ = New(nil)
}
func TestSeeker_Metadata(t *testing.T) {
t.Log(seeker.String())
t.Log(seeker.BuildTime())
t.Log(seeker.RecordCount())
}
func TestSeeker_InvalidLookup(t *testing.T) {
_, _ = seeker.LookupByIP(nil)
}
func TestSeeker_LookupByIP(t *testing.T) {
cases := []net.IP{
{0, 0, 0, 0},
{127, 0, 0, 1},
{172, 16, 0, 0},
{192, 168, 0, 0},
{100, 64, 0, 0},
{156, 154, 114, 35},
{157, 160, 206, 174},
{220, 174, 130, 251},
{255, 0, 0, 0},
{255, 255, 255, 255},
}
for index, unit := range cases {
record, err := seeker.LookupByIP(unit)
if err != nil {
t.Fatal(index, err)
}
fmt.Println(record, record.BeginIP, record.EndIP)
}
}
func BenchmarkIPSeeker_LookupByIP(b *testing.B) {
var ips []net.IP
for i := 0; i < b.N; i++ {
ip := make(net.IP, 4)
_, _ = rand.Read(ip)
ips = append(ips, ip)
}
b.ReportAllocs()
b.ResetTimer()
for _, ip := range ips {
_, _ = seeker.LookupByIP(ip)
}
}