-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
98 lines (79 loc) · 2.45 KB
/
Copy pathmain_test.go
File metadata and controls
98 lines (79 loc) · 2.45 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
package main
import (
"net"
"reflect"
"testing"
"github.com/oschwald/geoip2-golang/v2"
)
func TestExtractField_CountryRecord(t *testing.T) {
// Test geoip2.Country structure with correct field paths for v2
country := &geoip2.Country{
Country: geoip2.CountryRecord{ISOCode: "US", Names: geoip2.Names{English: "United States"}},
}
v := reflect.ValueOf(country)
got := extractField(v, "Country.ISOCode")
if got != "US" {
t.Fatalf("expected Country.ISOCode 'US', got '%s'", got)
}
got = extractField(v, "Country.Names.English")
if got != "United States" {
t.Fatalf("expected Country.Names.English 'United States', got '%s'", got)
}
}
func TestExtractField_CityAndASN(t *testing.T) {
city := &geoip2.City{
City: geoip2.CityRecord{Names: geoip2.Names{English: "San Francisco"}},
Subdivisions: []geoip2.CitySubdivision{{ISOCode: "CA", Names: geoip2.Names{German: "Kalifornien"}}},
}
v := reflect.ValueOf(city)
got := extractField(v, "City.Names.English")
if got != "San Francisco" {
t.Fatalf("expected City.Names.English 'San Francisco', got '%s'", got)
}
got = extractField(v, "Subdivisions[0].Names.German")
if got != "Kalifornien" {
t.Fatalf("expected Subdivisions[0].Names.German 'Kalifornien', got '%s'", got)
}
asn := &geoip2.ASN{AutonomousSystemNumber: 64496, AutonomousSystemOrganization: "Example ASN Org"}
v = reflect.ValueOf(asn)
got = extractField(v, "AutonomousSystemNumber")
if got != "64496" {
t.Fatalf("expected AutonomousSystemNumber '64496', got '%s'", got)
}
got = extractField(v, "AutonomousSystemOrganization")
if got != "Example ASN Org" {
t.Fatalf("expected AutonomousSystemOrganization 'Example ASN Org', got '%s'", got)
}
}
func TestExtractField_NilPointerSafely(t *testing.T) {
type Inner struct {
Value string
}
type Outer struct {
Ptr *Inner
}
o := &Outer{Ptr: nil}
v := reflect.ValueOf(o)
got := extractField(v, "Ptr.Value")
if got != "" {
t.Fatalf("expected empty string for nil pointer path, got '%s'", got)
}
}
func TestIsAllowed(t *testing.T) {
// prepare allowedNets
allowedNets = nil
cidrs := []string{"127.0.0.0/8", "192.168.0.0/16"}
for _, c := range cidrs {
_, n, err := net.ParseCIDR(c)
if err != nil {
t.Fatalf("failed to parse cidr %s: %v", c, err)
}
allowedNets = append(allowedNets, n)
}
if !isAllowed(net.ParseIP("127.0.0.1")) {
t.Fatalf("127.0.0.1 should be allowed")
}
if isAllowed(net.ParseIP("8.8.8.8")) {
t.Fatalf("8.8.8.8 should NOT be allowed")
}
}