Look up IP addresses by prefix at each mask length (closes #48)
check / check (push) Successful in 3m13s
check / check (push) Successful in 3m13s
Looking up an address on /ip/ and /api/v1/ip/ read every IPv6 live route and about half of the IPv4 range index, so on a day-sized database it passed the 30-second request timeout. The lookup is now one function for both families: from the longest mask length down, it looks up the address's network prefix on the existing prefix index, and the first live route wins. The feed sends IPv6 withdrawals uncompressed, so they never matched the stored compressed prefix and never removed a route. Prefixes are now stored in the text form net/netip prints, so IPv6 withdrawals take effect. ip_start, ip_end and GetASInfoForIP are removed; nothing read them. A database from before this change must be deleted. Model: opus-5-5
This commit was merged in pull request #51.
This commit is contained in:
@@ -3,13 +3,15 @@ package database
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"net"
|
||||
"errors"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.eeqj.de/sneak/routewatch/internal/config"
|
||||
"git.eeqj.de/sneak/routewatch/internal/logger"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// tempStoreMemory is the PRAGMA temp_store value meaning "hold temp B-trees in
|
||||
@@ -20,6 +22,9 @@ const tempStoreMemory = 2
|
||||
// once so each is a distinct SQLite connection that parsed the DSN.
|
||||
const heldConnections = 5
|
||||
|
||||
// testPeerIP is the peer every route in the IP lookup test is learned from.
|
||||
const testPeerIP = "192.0.2.254"
|
||||
|
||||
// Parameters for the checkpoint-contention regression test.
|
||||
const (
|
||||
// contentionIterations is how many batch writes race the checkpoint loop.
|
||||
@@ -32,281 +37,92 @@ const (
|
||||
asnSecondBand = 100
|
||||
)
|
||||
|
||||
func TestIPToUint32(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ip string
|
||||
expected uint32
|
||||
}{
|
||||
{
|
||||
name: "Simple IP",
|
||||
ip: "192.168.1.1",
|
||||
expected: 3232235777, // 192<<24 + 168<<16 + 1<<8 + 1
|
||||
},
|
||||
{
|
||||
name: "Minimum IP",
|
||||
ip: "0.0.0.0",
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "Maximum IP",
|
||||
ip: "255.255.255.255",
|
||||
expected: 4294967295,
|
||||
},
|
||||
{
|
||||
name: "10.0.0.0",
|
||||
ip: "10.0.0.0",
|
||||
expected: 167772160,
|
||||
},
|
||||
{
|
||||
name: "172.16.0.0",
|
||||
ip: "172.16.0.0",
|
||||
expected: 2886729728,
|
||||
},
|
||||
{
|
||||
name: "8.8.8.8",
|
||||
ip: "8.8.8.8",
|
||||
expected: 134744072,
|
||||
},
|
||||
{
|
||||
name: "1.2.3.4",
|
||||
ip: "1.2.3.4",
|
||||
expected: 16909060,
|
||||
},
|
||||
// TestGetIPInfoFindsMostSpecificLiveRoute stores nested live prefixes for both
|
||||
// families and checks that a lookup returns the most specific one covering the
|
||||
// address, ErrNoRoute when none covers it, and the next less specific prefix
|
||||
// once the only route of the most specific one is withdrawn.
|
||||
func TestGetIPInfoFindsMostSpecificLiveRoute(t *testing.T) {
|
||||
cfg := &config.Config{StateDir: t.TempDir()}
|
||||
|
||||
db, err := New(cfg, logger.New())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create database: %v", err)
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ip := net.ParseIP(tt.ip)
|
||||
if ip == nil {
|
||||
t.Fatalf("Failed to parse IP: %s", tt.ip)
|
||||
}
|
||||
|
||||
result := ipToUint32(ip)
|
||||
if result != tt.expected {
|
||||
t.Errorf("ipToUint32(%s) = %d, want %d", tt.ip, result, tt.expected)
|
||||
}
|
||||
|
||||
// Test with IPv4-mapped IPv6 address
|
||||
ip6 := net.ParseIP(tt.ip).To16()
|
||||
if ip6 != nil {
|
||||
result6 := ipToUint32(ip6)
|
||||
if result6 != tt.expected {
|
||||
t.Errorf("ipToUint32(%s as IPv6) = %d, want %d", tt.ip, result6, tt.expected)
|
||||
}
|
||||
}
|
||||
// Nested live prefixes, each originated by its own AS.
|
||||
origins := map[string]int{
|
||||
"10.0.0.0/8": 64500,
|
||||
"10.1.0.0/16": 64501,
|
||||
"10.1.2.0/24": 64502,
|
||||
"2001:db8::/32": 64500,
|
||||
"2001:db8:1::/48": 64501,
|
||||
"2001:db8:1:2::/64": 64502,
|
||||
}
|
||||
ts := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
routes := make([]*LiveRoute, 0, len(origins))
|
||||
for prefix, asn := range origins {
|
||||
routes = append(routes, &LiveRoute{
|
||||
ID: uuid.New(),
|
||||
Prefix: prefix,
|
||||
MaskLength: netip.MustParsePrefix(prefix).Bits(),
|
||||
IPVersion: detectIPVersion(prefix),
|
||||
OriginASN: asn,
|
||||
PeerIP: testPeerIP,
|
||||
ASPath: []int{asn},
|
||||
NextHop: testPeerIP,
|
||||
LastUpdated: ts,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateIPv4Range(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cidr string
|
||||
wantStart uint32
|
||||
wantEnd uint32
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Single IP /32",
|
||||
cidr: "192.168.1.1/32",
|
||||
wantStart: 3232235777,
|
||||
wantEnd: 3232235777,
|
||||
},
|
||||
{
|
||||
name: "Class C /24",
|
||||
cidr: "192.168.1.0/24",
|
||||
wantStart: 3232235776, // 192.168.1.0
|
||||
wantEnd: 3232236031, // 192.168.1.255
|
||||
},
|
||||
{
|
||||
name: "Class B /16",
|
||||
cidr: "192.168.0.0/16",
|
||||
wantStart: 3232235520, // 192.168.0.0
|
||||
wantEnd: 3232301055, // 192.168.255.255
|
||||
},
|
||||
{
|
||||
name: "Class A /8",
|
||||
cidr: "10.0.0.0/8",
|
||||
wantStart: 167772160, // 10.0.0.0
|
||||
wantEnd: 184549375, // 10.255.255.255
|
||||
},
|
||||
{
|
||||
name: "Entire IPv4 space /0",
|
||||
cidr: "0.0.0.0/0",
|
||||
wantStart: 0,
|
||||
wantEnd: 4294967295,
|
||||
},
|
||||
{
|
||||
name: "Small subnet /30",
|
||||
cidr: "192.168.1.0/30",
|
||||
wantStart: 3232235776, // 192.168.1.0
|
||||
wantEnd: 3232235779, // 192.168.1.3
|
||||
},
|
||||
{
|
||||
name: "Medium subnet /20",
|
||||
cidr: "172.16.0.0/20",
|
||||
wantStart: 2886729728, // 172.16.0.0
|
||||
wantEnd: 2886733823, // 172.16.15.255
|
||||
},
|
||||
{
|
||||
name: "Private range 172.16/12",
|
||||
cidr: "172.16.0.0/12",
|
||||
wantStart: 2886729728, // 172.16.0.0
|
||||
wantEnd: 2887778303, // 172.31.255.255
|
||||
},
|
||||
{
|
||||
name: "Google DNS /29",
|
||||
cidr: "8.8.8.8/29",
|
||||
wantStart: 134744072, // 8.8.8.8 (network is actually 8.8.8.8 with /29)
|
||||
wantEnd: 134744079, // 8.8.8.15
|
||||
},
|
||||
{
|
||||
name: "Non-zero host bits",
|
||||
cidr: "192.168.1.5/24",
|
||||
wantStart: 3232235776, // 192.168.1.0 (network address)
|
||||
wantEnd: 3232236031, // 192.168.1.255
|
||||
},
|
||||
{
|
||||
name: "Invalid CIDR",
|
||||
cidr: "192.168.1.1/33",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Invalid IP",
|
||||
cidr: "256.256.256.256/24",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "IPv6 CIDR",
|
||||
cidr: "2001:db8::/32",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Empty CIDR",
|
||||
cidr: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Missing mask",
|
||||
cidr: "192.168.1.1",
|
||||
wantErr: true,
|
||||
},
|
||||
if err := db.UpsertLiveRouteBatch(routes); err != nil {
|
||||
t.Fatalf("UpsertLiveRouteBatch: %v", err)
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
start, end, err := CalculateIPv4Range(tt.cidr)
|
||||
// lookup checks that ip resolves to the live prefix want, or to ErrNoRoute
|
||||
// when want is empty.
|
||||
lookup := func(ip, want string) {
|
||||
t.Helper()
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Errorf("CalculateIPv4Range(%s) expected error, got nil", tt.cidr)
|
||||
}
|
||||
return
|
||||
info, err := db.GetIPInfo(ip)
|
||||
if want == "" {
|
||||
if !errors.Is(err, ErrNoRoute) {
|
||||
t.Errorf("GetIPInfo(%s) = %+v, %v; want ErrNoRoute", ip, info, err)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("CalculateIPv4Range(%s) unexpected error: %v", tt.cidr, err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("GetIPInfo(%s): %v", ip, err)
|
||||
|
||||
if start != tt.wantStart {
|
||||
t.Errorf("CalculateIPv4Range(%s) start = %d, want %d", tt.cidr, start, tt.wantStart)
|
||||
}
|
||||
|
||||
if end != tt.wantEnd {
|
||||
t.Errorf("CalculateIPv4Range(%s) end = %d, want %d", tt.cidr, end, tt.wantEnd)
|
||||
}
|
||||
|
||||
// Verify that start <= end
|
||||
if start > end {
|
||||
t.Errorf("CalculateIPv4Range(%s) start (%d) > end (%d)", tt.cidr, start, end)
|
||||
}
|
||||
|
||||
// Verify the range size matches the CIDR mask
|
||||
if !tt.wantErr && tt.cidr != "" {
|
||||
_, ipNet, _ := net.ParseCIDR(tt.cidr)
|
||||
if ipNet != nil {
|
||||
ones, bits := ipNet.Mask.Size()
|
||||
expectedSize := uint32(1) << uint(bits-ones)
|
||||
actualSize := end - start + 1
|
||||
if actualSize != expectedSize {
|
||||
t.Errorf("CalculateIPv4Range(%s) range size = %d, want %d", tt.cidr, actualSize, expectedSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPv4RangeIntegration(t *testing.T) {
|
||||
// Test that our functions work correctly together
|
||||
tests := []struct {
|
||||
name string
|
||||
cidr string
|
||||
testIPs []string
|
||||
shouldContain []bool
|
||||
}{
|
||||
{
|
||||
name: "192.168.1.0/24",
|
||||
cidr: "192.168.1.0/24",
|
||||
testIPs: []string{
|
||||
"192.168.1.0",
|
||||
"192.168.1.1",
|
||||
"192.168.1.255",
|
||||
"192.168.0.255",
|
||||
"192.168.2.0",
|
||||
},
|
||||
shouldContain: []bool{true, true, true, false, false},
|
||||
},
|
||||
{
|
||||
name: "10.0.0.0/8",
|
||||
cidr: "10.0.0.0/8",
|
||||
testIPs: []string{
|
||||
"10.0.0.0",
|
||||
"10.255.255.255",
|
||||
"10.1.2.3",
|
||||
"9.255.255.255",
|
||||
"11.0.0.0",
|
||||
},
|
||||
shouldContain: []bool{true, true, true, false, false},
|
||||
},
|
||||
{
|
||||
name: "172.16.0.0/12",
|
||||
cidr: "172.16.0.0/12",
|
||||
testIPs: []string{
|
||||
"172.16.0.0",
|
||||
"172.31.255.255",
|
||||
"172.20.1.1",
|
||||
"172.15.255.255",
|
||||
"172.32.0.0",
|
||||
},
|
||||
shouldContain: []bool{true, true, true, false, false},
|
||||
},
|
||||
return
|
||||
}
|
||||
if info.Netblock != want || info.MaskLength != netip.MustParsePrefix(want).Bits() ||
|
||||
info.ASN != origins[want] {
|
||||
t.Errorf("GetIPInfo(%s) = %s (mask %d) AS%d, want %s AS%d",
|
||||
ip, info.Netblock, info.MaskLength, info.ASN, want, origins[want])
|
||||
}
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
start, end, err := CalculateIPv4Range(tt.cidr)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to calculate range for %s: %v", tt.cidr, err)
|
||||
}
|
||||
lookup("10.1.2.3", "10.1.2.0/24")
|
||||
lookup("::ffff:10.1.2.3", "10.1.2.0/24")
|
||||
lookup("10.1.3.4", "10.1.0.0/16")
|
||||
lookup("10.2.0.1", "10.0.0.0/8")
|
||||
lookup("192.0.2.1", "")
|
||||
lookup("2001:db8:1:2::3", "2001:db8:1:2::/64")
|
||||
lookup("2001:db8:1:3::4", "2001:db8:1::/48")
|
||||
lookup("2001:db8:2::1", "2001:db8::/32")
|
||||
lookup("2001:db9::1", "")
|
||||
|
||||
for i, testIP := range tt.testIPs {
|
||||
ip := net.ParseIP(testIP)
|
||||
if ip == nil {
|
||||
t.Fatalf("Failed to parse test IP: %s", testIP)
|
||||
}
|
||||
|
||||
ipUint := ipToUint32(ip)
|
||||
contained := ipUint >= start && ipUint <= end
|
||||
|
||||
if contained != tt.shouldContain[i] {
|
||||
t.Errorf("IP %s in range %s: got %v, want %v", testIP, tt.cidr, contained, tt.shouldContain[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
err = db.DeleteLiveRouteBatch([]LiveRouteDeletion{
|
||||
{Prefix: "10.1.2.0/24", OriginASN: 64502, PeerIP: testPeerIP, IPVersion: ipVersionV4},
|
||||
{Prefix: "2001:db8:1:2::/64", OriginASN: 64502, PeerIP: testPeerIP, IPVersion: ipVersionV6},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteLiveRouteBatch: %v", err)
|
||||
}
|
||||
|
||||
lookup("10.1.2.3", "10.1.0.0/16")
|
||||
lookup("2001:db8:1:2::3", "2001:db8:1::/48")
|
||||
}
|
||||
|
||||
// TestConnectionPoolPragmas holds several pooled connections open at once and
|
||||
@@ -424,21 +240,3 @@ func TestBatchWriteDuringCheckpoint(t *testing.T) {
|
||||
cancel()
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func BenchmarkIPToUint32(b *testing.B) {
|
||||
ip := net.ParseIP("192.168.1.1")
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = ipToUint32(ip)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCalculateIPv4Range(b *testing.B) {
|
||||
cidr := "192.168.0.0/16"
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _, _ = CalculateIPv4Range(cidr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user