-
Notifications
You must be signed in to change notification settings - Fork 0
/
gibloc_test.go
189 lines (150 loc) · 4.77 KB
/
gibloc_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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2023 Yawning Angel
//
// SPDX-License-Identifier: LGPL-2.1-only
package gibloc
import (
"bytes"
"context"
"io"
"net"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ulikunitz/xz"
"gitlab.com/yawning/gibloc/internal/db"
)
func TestGibloc(t *testing.T) {
t.Run("Updater", testGiblocUpdater)
db, err := LoadFile(getDBPath(t))
require.NoError(t, err, "LoadFile")
sigOk := db.SignatureValid(nil)
require.True(t, sigOk, "SignatureValid")
t.Logf("db: %s", db)
for _, addr := range []string{
// RFC 5737/ RFC 3849
"192.0.2.1", // 192.0.2.0/24 (TEST-NET-1)
// "198.51.100.69", // 198.51.100.0/24 (TEST-NET-2) Missing :(
"203.0.113.23", // 203.0.113.0/24 (TEST-NET-3)
"2001:DB8::1", // 2001:DB8::/32
// Google Public DNS
"8.8.8.8",
"8.8.4.4",
"2001:4860:4860::8888",
"2001:4860:4860::8844",
} {
t.Run("QueryIP/"+addr, func(t *testing.T) {
ip := net.ParseIP(addr)
require.NotNil(t, ip, "net.ParseIP")
result := db.QueryIP(ip)
require.NotNil(t, result, "QueryIP")
t.Logf("%+v", result)
t.Logf("%s", db.Country(result.CountryCode))
t.Logf("%s", db.AutonomousSystem(result.ASN))
})
}
for _, ipNetStr := range []string{
// RFC 5737/ RFC 3849
"192.0.2.0/24", // (TEST-NET-1)
// "198.51.100.0/24", (TEST-NET-2) Missing :(
"203.0.113.0/24", // (TEST-NET-3)
"2001:DB8::/32",
} {
t.Run("QueryIPNet/"+strings.ReplaceAll(ipNetStr, "/", "_"), func(t *testing.T) {
_, ipNet, err := net.ParseCIDR(ipNetStr)
require.NoError(t, err, "net.ParseCIDR")
result := db.QueryIPNet(ipNet)
require.NotNil(t, result, "QueryIPNet")
t.Logf("%+v", result)
t.Logf("%s", db.Country(result.CountryCode))
t.Logf("%s", db.AutonomousSystem(result.ASN))
})
}
}
func testGiblocUpdater(t *testing.T) {
// Note: These tests requires networking, and GetLatestDBVersion is
// somewhat flaky due to DNS timeouts.
t.Run("GetLatestDBVersion", func(t *testing.T) {
if !runNetworkTests() {
t.Skip("skipping DNS lookup version test")
}
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()
version, err := GetLatestDBVersion(ctx)
require.NoError(t, err, "GetLatestDBVersion")
t.Logf("Latest version: %v", version)
})
t.Run("GetLatestDB/File", func(t *testing.T) {
if !runNetworkTests() {
t.Skip("skipping database download test")
}
tmpPath := t.TempDir()
fn := filepath.Join(tmpPath, "location.db")
t.Logf("%s", fn)
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancelFn()
db, err := GetLatestDB(ctx, fn, time.Time{})
require.NoError(t, err, "GetLatestDB")
require.NotNil(t, db, "GetLatestDB - have a database")
sigOk := db.SignatureValid(nil)
require.True(t, sigOk, "SignatureValid")
_, err = GetLatestDB(ctx, fn, time.Now())
require.ErrorIs(t, ErrNotModified, err, "GetLatestDB - refetch")
// Ugh. This wastes bandwidth, but the test is disabled by default. Sorry!
db, err = GetLatestDB(ctx, "", time.Time{})
require.NoError(t, err, "GetLatestDB")
require.NotNil(t, db, "GetLatestDB - have a database")
})
t.Run("GetLatestDB/Memory", func(t *testing.T) {
if !runNetworkTests() {
t.Skip("skipping database download test")
}
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancelFn()
db, err := GetLatestDB(ctx, "", time.Time{})
require.NoError(t, err, "GetLatestDB")
require.NotNil(t, db, "GetLatestDB - have a database")
sigOk := db.SignatureValid(nil)
require.True(t, sigOk, "SignatureValid")
_, err = GetLatestDB(ctx, "", time.Now())
require.ErrorIs(t, ErrNotModified, err, "GetLatestDB - refetch")
// Ugh. This wastes bandwidth, but the test is disabled by default. Sorry!
db, err = GetLatestDB(ctx, "", time.Time{})
require.NoError(t, err, "GetLatestDB")
require.NotNil(t, db, "GetLatestDB - have a database")
})
}
func TestDebugDataErrors(t *testing.T) {
if testing.Short() {
t.Skip("skipping debug database inconsistency test")
}
// This is mostly so I can update the internal fixup table.
raw, err := os.ReadFile(getDBPath(t))
require.NoError(t, err, "os.ReadFile")
if bytes.HasPrefix(raw, xzMagic) {
rd := bytes.NewReader(raw)
xzr, err := xz.NewReader(rd)
require.NoError(t, err, "xz.NewReader")
raw, err = io.ReadAll(xzr)
require.NoError(t, err, "io.ReadAll")
}
db, err := db.New(raw, true)
require.NoError(t, err, "db.New")
vec := db.DebugGetDataErrors()
for _, s := range vec {
t.Log(s)
}
}
func getDBPath(t *testing.T) string {
f := os.Getenv("LIBLOC_DB_PATH")
if f == "" {
// The datafile is 36 MiB. I'm not going to include it.
t.Skip("No database available, set `LIBLOC_DB_PATH`")
}
return f
}
func runNetworkTests() bool {
return os.Getenv("GIBLOC_RUN_NETWORK_TESTS") != ""
}