Skip to content

Commit 5e68a3a

Browse files
Added a unit test for GeoIPASN module
1 parent eee4c43 commit 5e68a3a

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.idea
22
*.mmdb
3+
# Ignore all mmdb files in data-snapshots directory, used for unit-testing
4+
!data-snapshots/*.mmdb
35
**/venv/*
46
**/__pycache__/*
57
# zannotate binary

data-snapshots/geolite2_asn.mmdb

10.4 MB
Binary file not shown.

geoipasn_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* ZAnnotate Copyright 2025 Regents of the University of Michigan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy
6+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11+
* implied. See the License for the specific language governing
12+
* permissions and limitations under the License.
13+
*/
14+
15+
package zannotate
16+
17+
import (
18+
"net"
19+
"reflect"
20+
"testing"
21+
)
22+
23+
func TestGeoIPASNAnnotator(t *testing.T) {
24+
tests := []struct {
25+
testName string
26+
ipAddr net.IP
27+
expectedResult *GeoIPASNOutput
28+
}{
29+
{
30+
testName: "Positive Test Case, IPv4",
31+
ipAddr: net.ParseIP("1.1.1.1"),
32+
expectedResult: &GeoIPASNOutput{
33+
ASN: 13335,
34+
ASNOrg: "CLOUDFLARENET",
35+
},
36+
}, {
37+
testName: "Positive Test Case, IPv6",
38+
ipAddr: net.ParseIP("2606:4700:4700::1111"),
39+
expectedResult: &GeoIPASNOutput{
40+
ASN: 13335,
41+
ASNOrg: "CLOUDFLARENET",
42+
},
43+
}, {
44+
testName: "Negative Test Case, Invalid IP",
45+
ipAddr: net.ParseIP("999.999.999.999"),
46+
expectedResult: &GeoIPASNOutput{},
47+
}, {
48+
testName: "Negative Test Case, Private IP",
49+
ipAddr: net.ParseIP("127.0.0.1"),
50+
expectedResult: &GeoIPASNOutput{},
51+
},
52+
}
53+
factory := &GeoIPASNAnnotatorFactory{
54+
Path: "./data-snapshots/geolite2_asn.mmdb",
55+
Mode: "mmap",
56+
}
57+
err := factory.Initialize(nil)
58+
if err != nil {
59+
t.Fatalf("Failed to initialize factory: %v", err)
60+
}
61+
for _, tt := range tests {
62+
t.Run(tt.testName, func(t *testing.T) {
63+
annotator := factory.MakeAnnotator(0).(*GeoIPASNAnnotator)
64+
err = annotator.Initialize()
65+
if err != nil {
66+
t.Fatalf("Failed to initialize annotator: %v", err)
67+
}
68+
result := annotator.Annotate(tt.ipAddr)
69+
if tt.expectedResult == nil && result == nil {
70+
return // pass
71+
}
72+
if !reflect.DeepEqual(result, tt.expectedResult) {
73+
t.Errorf("Annotating IP %s gave = %v; expected: %v", tt.ipAddr, result, tt.expectedResult)
74+
}
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)