| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package network |
| 5 |
|
| 6 |
import ( |
| 7 |
"net" |
| 8 |
"reflect" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
) |
| 13 |
|
| 14 |
type CityDb struct { |
| 15 |
City struct { |
| 16 |
Names struct { |
| 17 |
En string |
| 18 |
} |
| 19 |
} |
| 20 |
Country struct { |
| 21 |
Iso_Code string |
| 22 |
Names struct { |
| 23 |
En string |
| 24 |
} |
| 25 |
} |
| 26 |
Continent struct { |
| 27 |
Code string |
| 28 |
} |
| 29 |
Location struct { |
| 30 |
Latitude float64 |
| 31 |
Longitude float64 |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
type ASNDb struct { |
| 36 |
Autonomous_system_number uint |
| 37 |
Autonomous_system_organization string |
| 38 |
} |
| 39 |
|
| 40 |
func TestNewGeoIP(t *testing.T) { |
| 41 |
g := NewGeoIP() |
| 42 |
if g == nil { |
| 43 |
t.Fatalf("Expected valid pointer, got nil") |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func TestGeoIP_GetRecord(t *testing.T) { |
| 48 |
g := NewGeoIP() |
| 49 |
|
| 50 |
mockcity := &geoipDB{ |
| 51 |
filename: "city.mmdb", |
| 52 |
modTime: time.Now(), |
| 53 |
db: &GeoIPMockCity{}, |
| 54 |
} |
| 55 |
|
| 56 |
mockasn := &geoipDB{ |
| 57 |
filename: "asn.mmdb", |
| 58 |
modTime: time.Now(), |
| 59 |
db: &GeoIPMockASN{}, |
| 60 |
} |
| 61 |
|
| 62 |
g.city = mockcity |
| 63 |
g.asn = mockasn |
| 64 |
|
| 65 |
/* city */ |
| 66 |
r := g.GetRecord("127.0.0.1") |
| 67 |
if r.City != "test1" { |
| 68 |
t.Fatalf("Invalid response got %s, expected test1", r.City) |
| 69 |
} |
| 70 |
if r.CountryCode != "test2" { |
| 71 |
t.Fatalf("Invalid response got %s, expected test2", r.CountryCode) |
| 72 |
} |
| 73 |
if r.Country != "test3" { |
| 74 |
t.Fatalf("Invalid response got %s, expected test3", r.Country) |
| 75 |
} |
| 76 |
if r.ContinentCode != "test4" { |
| 77 |
t.Fatalf("Invalid response got %s, expected test4", r.ContinentCode) |
| 78 |
} |
| 79 |
if r.Latitude != 24 { |
| 80 |
t.Fatalf("Invalid response got %f, expected 24", r.Latitude) |
| 81 |
} |
| 82 |
if r.Longitude != 42 { |
| 83 |
t.Fatalf("Invalid response got %f, expected 42", r.Longitude) |
| 84 |
} |
| 85 |
if r.ASNum != 42 { |
| 86 |
t.Fatalf("Invalid response got %d, expected 42", r.ASNum) |
| 87 |
} |
| 88 |
if r.ASName != "forty two" { |
| 89 |
t.Fatalf("Invalid response got %s, expected forty two", r.ASName) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestIsIPv6(t *testing.T) { |
| 94 |
g := NewGeoIP() |
| 95 |
if g.IsIPv6("192.168.0.1") == true { |
| 96 |
t.Fatalf("Expected ipv4, got ipv6") |
| 97 |
} |
| 98 |
if g.IsIPv6("::1") == false { |
| 99 |
t.Fatalf("Expected ipv6, got ipv4") |
| 100 |
} |
| 101 |
if g.IsIPv6("fe80::801a:2cff:fe80:315c") == false { |
| 102 |
t.Fatalf("Expected ipv6, got ipv4") |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func TestGeoIPRecord_IsValid(t *testing.T) { |
| 107 |
var r GeoIPRecord |
| 108 |
|
| 109 |
if r.IsValid() == true { |
| 110 |
t.Fatalf("Expected false, got true") |
| 111 |
} |
| 112 |
|
| 113 |
r = GeoIPRecord{ |
| 114 |
CountryCode: "FR", |
| 115 |
} |
| 116 |
|
| 117 |
if r.IsValid() == false { |
| 118 |
t.Fatalf("Expected true, got false") |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/* MOCK */ |
| 123 |
|
| 124 |
type GeoIPMockCity struct { |
| 125 |
} |
| 126 |
|
| 127 |
func (g *GeoIPMockCity) Lookup(ipAddress net.IP, result any) error { |
| 128 |
var citydb CityDb |
| 129 |
citydb.City.Names.En = "test1" |
| 130 |
citydb.Country.Iso_Code = "test2" |
| 131 |
citydb.Country.Names.En = "test3" |
| 132 |
citydb.Continent.Code = "test4" |
| 133 |
citydb.Location.Latitude = 24 |
| 134 |
citydb.Location.Longitude = 42 |
| 135 |
|
| 136 |
CopyStruct(&citydb, result) |
| 137 |
|
| 138 |
return nil |
| 139 |
} |
| 140 |
|
| 141 |
type GeoIPMockASN struct { |
| 142 |
} |
| 143 |
|
| 144 |
func (g *GeoIPMockASN) Lookup(ipAddress net.IP, result any) error { |
| 145 |
var asnDb ASNDb |
| 146 |
asnDb.Autonomous_system_number = 42 |
| 147 |
asnDb.Autonomous_system_organization = "forty two" |
| 148 |
|
| 149 |
CopyStruct(&asnDb, result) |
| 150 |
|
| 151 |
return nil |
| 152 |
} |
| 153 |
|
| 154 |
func CopyStruct(src any, dst any) { |
| 155 |
s := reflect.Indirect(reflect.ValueOf(src)) |
| 156 |
d := reflect.Indirect(reflect.ValueOf(dst)) |
| 157 |
CopyStructRec(s, d) |
| 158 |
} |
| 159 |
|
| 160 |
func CopyStructRec(s, d reflect.Value) { |
| 161 |
st := s.Type() |
| 162 |
dt := d.Type() |
| 163 |
|
| 164 |
typeOft1 := s.Type() |
| 165 |
typeOft2 := d.Type() |
| 166 |
|
| 167 |
for i := 0; i < s.NumField(); i++ { |
| 168 |
sf := s.Field(i) |
| 169 |
|
| 170 |
if st.Field(i).Type.Kind() == reflect.Struct { |
| 171 |
for j := 0; j < d.NumField(); j++ { |
| 172 |
if typeOft1.Field(i).Name == typeOft2.Field(j).Name { |
| 173 |
CopyStructRec(s.Field(i), d.Field(j)) |
| 174 |
goto cont |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
for j := 0; j < d.NumField(); j++ { |
| 180 |
df := d.Field(j) |
| 181 |
dtf := dt.Field(j) |
| 182 |
dsttag := dtf.Tag.Get("maxminddb") |
| 183 |
|
| 184 |
if strings.ToLower(typeOft1.Field(i).Name) == strings.ToLower(dsttag) { |
| 185 |
df.Set(reflect.Value(sf)) |
| 186 |
break |
| 187 |
} |
| 188 |
} |
| 189 |
cont: |
| 190 |
} |
| 191 |
} |