Skip to content

Commit 0721741

Browse files
authored
Merge branch 'master' into phillip/implement-rdns
2 parents 1d31925 + 7b80105 commit 0721741

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
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

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,29 @@ Either way, this will install the `zannotate` binary in your `$GOPATH/bin` direc
3131
Check that it was installed correctly with:
3232

3333
```shell
34-
zannotate --version
34+
zannotate --help
35+
```
36+
37+
# Acquiring Datasets
38+
39+
> [!NOTE]
40+
> URLs and instructions may change over time. These are up-to-date as of September 2025.
41+
42+
Below are instructions for getting datasets from the below providers.
43+
44+
### GeoLite2-ASN
45+
1. [Sign-up form](https://www.maxmind.com/en/geolite2/signup) for MaxMind GeoLite Access
46+
2. Login to your account
47+
3. Go to the "GeoIP / GeoLite" > "Download files" section where you should see a list of available databases
48+
4. Download the `.mmdb` files for GeoLite ASN
49+
5. Unzip the downloaded file and test with:
50+
51+
```shell
52+
echo "1.1.1.1" | zannotate --geoasn --geoasn-database=/path-to-downloaded-file/GeoLite2-ASN_20250923/GeoLite2-ASN.mmdb
53+
```
54+
55+
```shell
56+
{"ip":"1.1.1.1","geoasn":{"asn":13335,"org":"CLOUDFLARENET"}}
3557
```
3658

3759
# Input/Output

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)