-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrectness_test.go
More file actions
159 lines (139 loc) · 4.28 KB
/
correctness_test.go
File metadata and controls
159 lines (139 loc) · 4.28 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package test
import (
"encoding/binary"
"fmt"
"net/netip"
"reflect"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/pg9182/ip2x"
)
func TestCorrectness(t *testing.T) {
for _, a := range ips {
t.Run(a.String(), func(t *testing.T) {
if err := testCorrectness(a); err != nil {
t.Fatal(err.Error())
}
})
}
}
func TestCorrectnessIPv425bit(t *testing.T) {
testCorrectnessIPv4Bits(t, 25)
}
func testCorrectnessIPv4Bits(t *testing.T, bits int) {
if bits < 1 || bits > 32 {
panic("invalid bits")
}
upper := uint32(1)<<uint32(bits) - 1
t.Logf("bits: %d (%d addresses)", bits, bits)
const maxErrors = 100
var numErrors uint64
var wg sync.WaitGroup
threads := runtime.NumCPU()
t.Logf("threads: %d", threads)
const progressBits = 5 // emit progress 2^progressBits times
var done uint32
start := time.Now()
for thread := 0; thread < threads; thread++ {
thread := thread
wg.Add(1)
go func() {
defer wg.Done()
runtime.LockOSThread()
for i, u := uint32(0), uint32(1)<<uint32(bits)-1; i <= upper; i++ {
if atomic.LoadUint64(&numErrors) >= maxErrors {
return
}
if i%uint32(threads) != uint32(thread) {
continue
}
n := i << (32 - bits)
if done := atomic.AddUint32(&done, 1); n<<progressBits == 0 {
pct := 100 * float64(i>>(bits-progressBits)) / float64((uint32(1)<<progressBits - 1))
rem := u - i
if i == 0 {
t.Logf("[%3.0f%%] %d remaining", pct, rem)
} else {
nps := float64(done) / time.Since(start).Seconds()
npt := time.Since(start) / time.Duration(done) * time.Duration(rem)
t.Logf("[%3.0f%%] %d remaining, %s (%.0f/sec)", pct, rem, npt, nps)
}
}
if bits < 32 {
n += 1 // so we aren't just .0 IPs
}
var b [4]byte
binary.BigEndian.PutUint32(b[:], n)
a := netip.AddrFrom4(b)
if err := testCorrectness(a); err != nil {
t.Errorf("%s: %v", a, err)
atomic.AddUint64(&numErrors, 1)
}
}
}()
}
wg.Wait()
if atomic.LoadUint64(&numErrors) >= maxErrors {
t.Fatalf("too many errors")
}
}
func testCorrectness(a netip.Addr) error {
fmap := map[ip2x.DBField]string{ // map[ip2x.DBField]ip2location.Record.*
ip2x.CountryCode: "Country_short",
ip2x.CountryName: "Country_long",
ip2x.Region: "Region",
ip2x.City: "City",
ip2x.ISP: "Isp",
ip2x.Latitude: "Latitude",
ip2x.Longitude: "Longitude",
ip2x.Domain: "Domain",
ip2x.Zipcode: "Zipcode",
ip2x.Timezone: "Timezone",
ip2x.NetSpeed: "Netspeed",
ip2x.IDDCode: "Iddcode",
ip2x.AreaCode: "Areacode",
ip2x.WeatherStationCode: "Weatherstationcode",
ip2x.WeatherStationName: "Weatherstationname",
ip2x.MCC: "Mcc",
ip2x.MNC: "Mnc",
ip2x.MobileBrand: "Mobilebrand",
ip2x.Elevation: "Elevation",
ip2x.UsageType: "Usagetype",
ip2x.AddressType: "Addresstype",
ip2x.Category: "Category",
}
r1, err := IP2LocationV9_DB.Get_all(a.String())
if err != nil {
return fmt.Errorf("ip2location/v9 lookup error: %w", err)
}
if r1.Country_short == ip2locationv9_invalid_address {
return fmt.Errorf("ip2location/v9 thinks a valid address is invalid...")
}
r2, err := IP2x_DB.Lookup(a)
if err != nil {
return fmt.Errorf("ip2x lookup error: %w", err)
}
for x, y := range fmap {
switch v := reflect.ValueOf(r1).FieldByName(y).Interface().(type) {
case string:
if exp, act := v != ip2locationv9_not_supported, IP2x_DB.Has(x); exp != act {
return fmt.Errorf("ip2location/v9 thinks field %s (ip2x.%s) should exist=%t (value %q), but ip2x thinks %t", y, x, exp, v, act)
}
if act, _ := r2.GetString(x); v != act && !(v == ip2locationv9_not_supported && act == "") {
return fmt.Errorf("ip2location/v9 thinks field %s (ip2x.%s) should be %#v, but ip2x thinks %#v", y, x, v, act)
}
case float32:
if act, _ := r2.GetFloat32(x); v != act {
return fmt.Errorf("ip2location/v9 thinks field %s (ip2x.%s) should be %#v, but ip2x thinks %#v", y, x, v, act)
}
case nil:
panic("invalid ip2location field name " + y + " for mapping of ip2x." + x.GoString())
default:
panic("unhandled ip2location type " + reflect.ValueOf(v).Type().String())
}
}
return nil
}