Skip to content

Commit c8aa31d

Browse files
Added tests for Plus/Core, fixed ptr bug which resulted in bool fields being filled out incorrectly
1 parent 6109491 commit c8aa31d

File tree

2 files changed

+122
-13
lines changed

2 files changed

+122
-13
lines changed

ipinfo.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type IPInfoMMDBOutput struct {
5050
Longitude string `maxminddb:"longitude"`
5151
Timezone string `maxminddb:"timezone"`
5252
PostalCode string `maxminddb:"postal_code"`
53+
DMACode string `maxminddb:"dma_code"`
5354
GeonameID string `maxminddb:"geoname_id"` // GeoNames database identifier (if available).
5455
Radius string `maxminddb:"radius"` // Accuracy radius in kilometers (if available).
5556
GeoChanged string `maxminddb:"geo_changed"` // Timestamp or flag indicating when the geolocation last changed (if available).
@@ -86,6 +87,7 @@ type IPInfoModuleOutput struct {
8687
Longitude float64 `json:"longitude,omitempty"`
8788
Timezone string `json:"timezone,omitempty"`
8889
PostalCode string `json:"postal_code,omitempty"`
90+
DMACode string `json:"dma_code,omitempty"` // Nielsen Designated Market Area code (if available).
8991
GeonameID uint64 `json:"geoname_id,omitempty"` // GeoNames database identifier (if available).
9092
Radius uint64 `json:"radius,omitempty"` // Accuracy radius in kilometers (if available).
9193
GeoChanged string `json:"geo_changed,omitempty"` // Timestamp or flag indicating when the geolocation last changed (if available).
@@ -147,31 +149,40 @@ func (in *IPInfoMMDBOutput) ToModuleOutput() *IPInfoModuleOutput {
147149
}
148150
var temp bool
149151
if temp, err = strconv.ParseBool(in.IsProxy); err == nil {
150-
out.IsProxy = &temp
152+
t := temp // avoid taking address of a short-lived variable
153+
out.IsProxy = &t
151154
}
152155
if temp, err = strconv.ParseBool(in.IsRelay); err == nil {
153-
out.IsRelay = &temp
156+
t := temp // avoid taking address of a short-lived variable
157+
out.IsRelay = &t
154158
}
155159
if temp, err = strconv.ParseBool(in.IsTOR); err == nil {
156-
out.IsTOR = &temp
160+
t := temp // avoid taking address of a short-lived variable
161+
out.IsTOR = &t
157162
}
158163
if temp, err = strconv.ParseBool(in.IsVPN); err == nil {
159-
out.IsVPN = &temp
164+
t := temp // avoid taking address of a short-lived variable
165+
out.IsVPN = &t
160166
}
161167
if temp, err = strconv.ParseBool(in.IsAnonymous); err == nil {
162-
out.IsAnonymous = &temp
168+
t := temp // avoid taking address of a short-lived variable
169+
out.IsAnonymous = &t
163170
}
164171
if temp, err = strconv.ParseBool(in.IsAnycast); err == nil {
165-
out.IsAnycast = &temp
172+
t := temp // avoid taking address of a short-lived variable
173+
out.IsAnycast = &t
166174
}
167175
if temp, err = strconv.ParseBool(in.IsHosting); err == nil {
168-
out.IsHosting = &temp
176+
t := temp // avoid taking address of a short-lived variable
177+
out.IsHosting = &t
169178
}
170179
if temp, err = strconv.ParseBool(in.IsMobile); err == nil {
171-
out.IsMobile = &temp
180+
t := temp // avoid taking address of a short-lived variable
181+
out.IsMobile = &t
172182
}
173183
if temp, err = strconv.ParseBool(in.IsSatellite); err == nil {
174-
out.IsSatellite = &temp
184+
t := temp // avoid taking address of a short-lived variable
185+
out.IsSatellite = &t
175186
}
176187
return out
177188
}

ipinfo_test.go

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ import (
2222

2323
// TestIPInfoAnnotator tests the IPInfoAnnotator with known IP addresses against a constant MMDB file in ./data-snapshots
2424
// Unfortunately, I only have a free account for IPInfo, so can only test parsing for the Lite api with a real DB file.
25-
func TestIPInfoAnnotator(t *testing.T) {
25+
func TestIPInfoAnnotatorLite(t *testing.T) {
2626
tests := []struct {
2727
testName string
2828
ipAddr net.IP
29-
expectedResult *IPInfoOutput
29+
expectedResult *IPInfoModuleOutput
3030
}{
3131
{
3232
testName: "Positive Test Case, IPv4",
3333
ipAddr: net.ParseIP("1.1.1.1"),
34-
expectedResult: &IPInfoOutput{
34+
expectedResult: &IPInfoModuleOutput{
3535
ASN: "AS13335",
3636
ASName: "Cloudflare, Inc.",
3737
ASDomain: "cloudflare.com",
@@ -43,7 +43,7 @@ func TestIPInfoAnnotator(t *testing.T) {
4343
}, {
4444
testName: "Positive Test Case, IPv6",
4545
ipAddr: net.ParseIP("2606:4700:4700::1111"),
46-
expectedResult: &IPInfoOutput{
46+
expectedResult: &IPInfoModuleOutput{
4747
ASN: "AS13335",
4848
ASName: "Cloudflare, Inc.",
4949
ASDomain: "cloudflare.com",
@@ -86,3 +86,101 @@ func TestIPInfoAnnotator(t *testing.T) {
8686
})
8787
}
8888
}
89+
90+
func TestIPInfoAnnotatorCore(t *testing.T) {
91+
expectedResult := &IPInfoModuleOutput{
92+
City: "Brisbane",
93+
Region: "Queensland",
94+
RegionCode: "QLD",
95+
Country: "Australia",
96+
CountryCode: "AU",
97+
Continent: "Oceania",
98+
ContinentCode: "OC",
99+
Latitude: -27.48159,
100+
Longitude: 153.0175,
101+
Timezone: "Australia/Brisbane",
102+
PostalCode: "4101",
103+
ASN: "AS13335",
104+
ASName: "Cloudflare, Inc.",
105+
ASDomain: "cloudflare.com",
106+
ASType: "hosting",
107+
IsAnonymous: &[]bool{true}[0],
108+
IsAnycast: &[]bool{true}[0],
109+
IsHosting: &[]bool{true}[0],
110+
IsMobile: &[]bool{false}[0],
111+
IsSatellite: &[]bool{false}[0],
112+
}
113+
inputIP := net.ParseIP("1.0.0.1")
114+
factory := &IPInfoAnnotatorFactory{
115+
DatabaseFilePath: "./data-snapshots/ipinfo_core_sample.mmdb",
116+
}
117+
err := factory.Initialize(nil)
118+
if err != nil {
119+
t.Fatalf("Failed to initialize IPInfoAnnotatorFactory: %v", err)
120+
}
121+
annotator := factory.MakeAnnotator(0).(*IPInfoAnnotator)
122+
err = annotator.Initialize()
123+
if err != nil {
124+
t.Fatalf("Failed to initialize IPInfoAnnotator: %v", err)
125+
}
126+
result := annotator.Annotate(inputIP)
127+
if expectedResult == nil && result == nil {
128+
return // pass
129+
}
130+
if !reflect.DeepEqual(result, expectedResult) {
131+
t.Errorf("Annotating IP %s gave = \n%v\nexpected: \n%v", inputIP, result, expectedResult)
132+
}
133+
}
134+
135+
func TestIPInfoAnnotatorPlus(t *testing.T) {
136+
expectedResult := &IPInfoModuleOutput{
137+
City: "Brisbane",
138+
Region: "Queensland",
139+
RegionCode: "QLD",
140+
Country: "Australia",
141+
CountryCode: "AU",
142+
Continent: "Oceania",
143+
ContinentCode: "OC",
144+
Latitude: -27.48159,
145+
Longitude: 153.0175,
146+
Timezone: "Australia/Brisbane",
147+
PostalCode: "4101",
148+
GeonameID: 2174003,
149+
Radius: 200,
150+
ASN: "AS13335",
151+
ASName: "Cloudflare, Inc.",
152+
ASDomain: "cloudflare.com",
153+
ASType: "hosting",
154+
ASChanged: "2025-09-28",
155+
GeoChanged: "2024-02-18",
156+
IsAnonymous: &[]bool{true}[0],
157+
IsAnycast: &[]bool{true}[0],
158+
IsHosting: &[]bool{true}[0],
159+
IsMobile: &[]bool{false}[0],
160+
IsSatellite: &[]bool{false}[0],
161+
IsProxy: &[]bool{false}[0],
162+
IsRelay: &[]bool{false}[0],
163+
IsTOR: &[]bool{false}[0],
164+
IsVPN: &[]bool{true}[0],
165+
}
166+
inputIP := net.ParseIP("1.0.0.1")
167+
factory := &IPInfoAnnotatorFactory{
168+
DatabaseFilePath: "./data-snapshots/ipinfo_plus_sample.mmdb",
169+
}
170+
err := factory.Initialize(nil)
171+
if err != nil {
172+
t.Fatalf("Failed to initialize IPInfoAnnotatorFactory: %v", err)
173+
}
174+
annotator := factory.MakeAnnotator(0).(*IPInfoAnnotator)
175+
err = annotator.Initialize()
176+
if err != nil {
177+
t.Fatalf("Failed to initialize IPInfoAnnotator: %v", err)
178+
}
179+
result := annotator.Annotate(inputIP)
180+
if expectedResult == nil && result == nil {
181+
return // pass
182+
}
183+
if !reflect.DeepEqual(result, expectedResult) {
184+
t.Errorf("Annotating IP %s gave = \n%v\nexpected: \n%v", inputIP, result, expectedResult)
185+
}
186+
}

0 commit comments

Comments
 (0)