1+ const std = @import ("std" );
2+
13const reader = @import ("reader.zig" );
24const decoder = @import ("decoder.zig" );
35const net = @import ("net.zig" );
@@ -10,5 +12,150 @@ pub const Metadata = reader.Metadata;
1012pub const Network = net .Network ;
1113
1214test {
13- @import ("std" ).testing .refAllDecls (@This ());
15+ std .testing .refAllDecls (@This ());
16+ }
17+
18+ fn expectEqualMaps (
19+ map : geolite2.StringMap ,
20+ keys : []const []const u8 ,
21+ values : []const []const u8 ,
22+ ) ! void {
23+ try std .testing .expectEqual (map .count (), keys .len );
24+
25+ for (keys , values ) | key , want_value | {
26+ const got_value = map .get (key ) orelse {
27+ std .debug .print ("map key=\" {s}\" was not found\n " , .{key });
28+ return error .MapKeyNotFound ;
29+ };
30+ try std .testing .expectEqualStrings (want_value , got_value );
31+ }
32+ }
33+
34+ const expectEqual = std .testing .expectEqual ;
35+ const expectEqualStrings = std .testing .expectEqualStrings ;
36+
37+ test "GeoLite2 Country" {
38+ var db = try Reader .open_mmap (
39+ std .testing .allocator ,
40+ "test-data/test-data/GeoLite2-Country-Test.mmdb" ,
41+ );
42+ defer db .close ();
43+
44+ const ip = try std .net .Address .parseIp ("89.160.20.128" , 0 );
45+ const got = try db .lookup (geolite2 .Country , & ip );
46+ defer got .deinit ();
47+
48+ try expectEqualStrings ("EU" , got .continent .code );
49+ try expectEqual (6255148 , got .continent .geoname_id );
50+ try expectEqualMaps (
51+ got .continent .names .? ,
52+ &.{ "de" , "en" , "es" , "fr" , "ja" , "pt-BR" , "ru" , "zh-CN" },
53+ &.{ "Europa" , "Europe" , "Europa" , "Europe" , "ヨーロッパ" , "Europa" , "Европа" , "欧洲" },
54+ );
55+
56+ try expectEqual (2661886 , got .country .geoname_id );
57+ try expectEqual (true , got .country .is_in_european_union );
58+ try expectEqualStrings ("SE" , got .country .iso_code );
59+ try expectEqualMaps (
60+ got .country .names .? ,
61+ &.{ "de" , "en" , "es" , "fr" , "ja" , "pt-BR" , "ru" , "zh-CN" },
62+ &.{ "Schweden" , "Sweden" , "Suecia" , "Suède" , "スウェーデン王国" , "Suécia" , "Швеция" , "瑞典" },
63+ );
64+
65+ try expectEqual (2921044 , got .registered_country .geoname_id );
66+ try expectEqual (true , got .registered_country .is_in_european_union );
67+ try expectEqualStrings ("DE" , got .registered_country .iso_code );
68+ try expectEqualMaps (
69+ got .registered_country .names .? ,
70+ &.{ "de" , "en" , "es" , "fr" , "ja" , "pt-BR" , "ru" , "zh-CN" },
71+ &.{ "Deutschland" , "Germany" , "Alemania" , "Allemagne" , "ドイツ連邦共和国" , "Alemanha" , "Германия" , "德国" },
72+ );
73+
74+ try std .testing .expectEqualDeep (geolite2.Country.RepresentedCountry {}, got .represented_country );
75+ }
76+
77+ test "GeoLite2 City" {
78+ var db = try Reader .open_mmap (
79+ std .testing .allocator ,
80+ "test-data/test-data/GeoLite2-City-Test.mmdb" ,
81+ );
82+ defer db .close ();
83+
84+ const ip = try std .net .Address .parseIp ("89.160.20.128" , 0 );
85+ const got = try db .lookup (geolite2 .City , & ip );
86+ defer got .deinit ();
87+
88+ try expectEqual (2694762 , got .city .geoname_id );
89+ try expectEqualMaps (
90+ got .city .names .? ,
91+ &.{ "de" , "en" , "fr" , "ja" , "zh-CN" },
92+ &.{ "Linköping" , "Linköping" , "Linköping" , "リンシェーピング" , "林雪平" },
93+ );
94+
95+ try expectEqualStrings ("EU" , got .continent .code );
96+ try expectEqual (6255148 , got .continent .geoname_id );
97+ try expectEqualMaps (
98+ got .continent .names .? ,
99+ &.{ "de" , "en" , "es" , "fr" , "ja" , "pt-BR" , "ru" , "zh-CN" },
100+ &.{ "Europa" , "Europe" , "Europa" , "Europe" , "ヨーロッパ" , "Europa" , "Европа" , "欧洲" },
101+ );
102+
103+ try expectEqual (2661886 , got .country .geoname_id );
104+ try expectEqual (true , got .country .is_in_european_union );
105+ try expectEqualStrings ("SE" , got .country .iso_code );
106+ try expectEqualMaps (
107+ got .country .names .? ,
108+ &.{ "de" , "en" , "es" , "fr" , "ja" , "pt-BR" , "ru" , "zh-CN" },
109+ &.{ "Schweden" , "Sweden" , "Suecia" , "Suède" , "スウェーデン王国" , "Suécia" , "Швеция" , "瑞典" },
110+ );
111+
112+ try std .testing .expectEqualDeep (
113+ geolite2.City.Location {
114+ .accuracy_radius = 76 ,
115+ .latitude = 58.4167 ,
116+ .longitude = 15.6167 ,
117+ .time_zone = "Europe/Stockholm" ,
118+ },
119+ got .location ,
120+ );
121+
122+ try std .testing .expectEqualDeep (geolite2.City.Postal {}, got .postal );
123+
124+ try expectEqual (2921044 , got .registered_country .geoname_id );
125+ try expectEqual (true , got .registered_country .is_in_european_union );
126+ try expectEqualStrings ("DE" , got .registered_country .iso_code );
127+ try expectEqualMaps (
128+ got .registered_country .names .? ,
129+ &.{ "de" , "en" , "es" , "fr" , "ja" , "pt-BR" , "ru" , "zh-CN" },
130+ &.{ "Deutschland" , "Germany" , "Alemania" , "Allemagne" , "ドイツ連邦共和国" , "Alemanha" , "Германия" , "德国" },
131+ );
132+
133+ try std .testing .expectEqualDeep (geolite2.Country.RepresentedCountry {}, got .represented_country );
134+
135+ try expectEqual (1 , got .subdivisions .? .items .len );
136+ const sub = got .subdivisions .? .getLast ();
137+ try expectEqual (2685867 , sub .geoname_id );
138+ try expectEqualStrings ("E" , sub .iso_code );
139+ try expectEqualMaps (
140+ sub .names .? ,
141+ &.{ "en" , "fr" },
142+ &.{ "Östergötland County" , "Comté d'Östergötland" },
143+ );
144+ }
145+
146+ test "GeoLite2 ASN" {
147+ var db = try Reader .open_mmap (
148+ std .testing .allocator ,
149+ "test-data/test-data/GeoLite2-ASN-Test.mmdb" ,
150+ );
151+ defer db .close ();
152+
153+ const ip = try std .net .Address .parseIp ("89.160.20.128" , 0 );
154+ const got = try db .lookup (geolite2 .ASN , & ip );
155+
156+ const want = geolite2.ASN {
157+ .autonomous_system_number = 29518 ,
158+ .autonomous_system_organization = "Bredband2 AB" ,
159+ };
160+ try std .testing .expectEqualDeep (want , got );
14161}
0 commit comments