Debian Codegraph @

tests/dns/geoip_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #define UNIT_TESTING
25 #include <cmocka.h>
26 #include <maxminddb.h>
27
28 #include <isc/dir.h>
29 #include <isc/string.h>
30 #include <isc/types.h>
31 #include <isc/util.h>
32
33 #include <dns/geoip.h>
34
35 #include "geoip2.c"
36
37 #include <tests/dns.h>
38
39 static dns_geoip_databases_t geoip;
40
41 static MMDB_s geoip_country, geoip_city, geoip_as, geoip_isp, geoip_domain;
42
43 static void
44 load_geoip(const char *dir);
45 static void
46 close_geoip(void);
47
48 static int
49 setup_test(void **state) {
50 UNUSED(state);
51
52 /* Use databases from the geoip system test */
53 load_geoip(TEST_GEOIP_DATA);
54
55 return 0;
56 }
57
58 static int
59 teardown_test(void **state) {
60 UNUSED(state);
61
62 close_geoip();
63
64 return 0;
65 }
66
67 static MMDB_s *
68 open_geoip2(const char *dir, const char *dbfile, MMDB_s *mmdb) {
69 char pathbuf[PATH_MAX];
70 int ret;
71
72 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dir, dbfile);
73 ret = MMDB_open(pathbuf, MMDB_MODE_MMAP, mmdb);
74 if (ret == MMDB_SUCCESS) {
75 return mmdb;
76 }
77
78 return NULL;
79 }
80
81 static void
82 load_geoip(const char *dir) {
83 geoip.country = open_geoip2(dir, "GeoIP2-Country.mmdb", &geoip_country);
84 geoip.city = open_geoip2(dir, "GeoIP2-City.mmdb", &geoip_city);
85 geoip.as = open_geoip2(dir, "GeoLite2-ASN.mmdb", &geoip_as);
86 geoip.isp = open_geoip2(dir, "GeoIP2-ISP.mmdb", &geoip_isp);
87 geoip.domain = open_geoip2(dir, "GeoIP2-Domain.mmdb", &geoip_domain);
88 }
89
90 static void
91 close_geoip(void) {
92 MMDB_close(&geoip_country);
93 MMDB_close(&geoip_city);
94 MMDB_close(&geoip_as);
95 MMDB_close(&geoip_isp);
96 MMDB_close(&geoip_domain);
97 }
98
99 static bool
100 /* Check if an MMDB entry of a given subtype exists for the given IP */
101 entry_exists(dns_geoip_subtype_t subtype, const char *addr) {
102 struct in6_addr in6;
103 struct in_addr in4;
104 isc_netaddr_t na;
105 MMDB_s *db;
106
107 if (inet_pton(AF_INET6, addr, &in6) == 1) {
108 isc_netaddr_fromin6(&na, &in6);
109 } else if (inet_pton(AF_INET, addr, &in4) == 1) {
110 isc_netaddr_fromin(&na, &in4);
111 } else {
112 UNREACHABLE();
113 }
114
115 db = geoip2_database(&geoip, fix_subtype(&geoip, subtype));
116
117 return db != NULL && get_entry_for(db, &na) != NULL;
118 }
119
120 /*
121 * Baseline test - check if get_entry_for() works as expected, i.e. that its
122 * return values are consistent with the contents of the test MMDBs found in
123 * bin/tests/system/geoip2/data/ (10.53.0.1 and fd92:7065:b8e:ffff::1 should be
124 * present in all databases, 192.0.2.128 should only be present in the country
125 * database, ::1 should be absent from all databases).
126 */
127 ISC_RUN_TEST_IMPL(baseline) {
128 dns_geoip_subtype_t subtype;
129
130 UNUSED(state);
131
132 subtype = dns_geoip_city_name;
133
134 assert_true(entry_exists(subtype, "10.53.0.1"));
135 assert_false(entry_exists(subtype, "192.0.2.128"));
136 assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
137 assert_false(entry_exists(subtype, "::1"));
138
139 subtype = dns_geoip_country_name;
140
141 assert_true(entry_exists(subtype, "10.53.0.1"));
142 assert_true(entry_exists(subtype, "192.0.2.128"));
143 assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
144 assert_false(entry_exists(subtype, "::1"));
145
146 subtype = dns_geoip_domain_name;
147
148 assert_true(entry_exists(subtype, "10.53.0.1"));
149 assert_false(entry_exists(subtype, "192.0.2.128"));
150 assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
151 assert_false(entry_exists(subtype, "::1"));
152
153 subtype = dns_geoip_isp_name;
154
155 assert_true(entry_exists(subtype, "10.53.0.1"));
156 assert_false(entry_exists(subtype, "192.0.2.128"));
157 assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
158 assert_false(entry_exists(subtype, "::1"));
159
160 subtype = dns_geoip_as_asnum;
161
162 assert_true(entry_exists(subtype, "10.53.0.1"));
163 assert_false(entry_exists(subtype, "192.0.2.128"));
164 assert_true(entry_exists(subtype, "fd92:7065:b8e:ffff::1"));
165 assert_false(entry_exists(subtype, "::1"));
166 }
167
168 static bool
169 do_lookup_string(const char *addr, dns_geoip_subtype_t subtype,
170 const char *string) {
171 dns_geoip_elem_t elt;
172 struct in_addr in4;
173 isc_netaddr_t na;
174 int n;
175
176 n = inet_pton(AF_INET, addr, &in4);
177 assert_int_equal(n, 1);
178 isc_netaddr_fromin(&na, &in4);
179
180 elt.subtype = subtype;
181 strlcpy(elt.as_string, string, sizeof(elt.as_string));
182
183 return dns_geoip_match(&na, &geoip, &elt);
184 }
185
186 static bool
187 do_lookup_string_v6(const char *addr, dns_geoip_subtype_t subtype,
188 const char *string) {
189 dns_geoip_elem_t elt;
190 struct in6_addr in6;
191 isc_netaddr_t na;
192 int n;
193
194 n = inet_pton(AF_INET6, addr, &in6);
195 assert_int_equal(n, 1);
196 isc_netaddr_fromin6(&na, &in6);
197
198 elt.subtype = subtype;
199 strlcpy(elt.as_string, string, sizeof(elt.as_string));
200
201 return dns_geoip_match(&na, &geoip, &elt);
202 }
203
204 /* GeoIP country matching */
205 ISC_RUN_TEST_IMPL(country) {
206 bool match;
207
208 UNUSED(state);
209
210 if (geoip.country == NULL) {
211 skip();
212 }
213
214 match = do_lookup_string("10.53.0.1", dns_geoip_country_code, "AU");
215 assert_true(match);
216
217 match = do_lookup_string("10.53.0.1", dns_geoip_country_name,
218 "Australia");
219 assert_true(match);
220
221 match = do_lookup_string("192.0.2.128", dns_geoip_country_code, "O1");
222 assert_true(match);
223
224 match = do_lookup_string("192.0.2.128", dns_geoip_country_name,
225 "Other");
226 assert_true(match);
227 }
228
229 /* GeoIP country (ipv6) matching */
230 ISC_RUN_TEST_IMPL(country_v6) {
231 bool match;
232
233 UNUSED(state);
234
235 if (geoip.country == NULL) {
236 skip();
237 }
238
239 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
240 dns_geoip_country_code, "AU");
241 assert_true(match);
242
243 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
244 dns_geoip_country_name, "Australia");
245 assert_true(match);
246 }
247
248 /* GeoIP city (ipv4) matching */
249 ISC_RUN_TEST_IMPL(city) {
250 bool match;
251
252 UNUSED(state);
253
254 if (geoip.city == NULL) {
255 skip();
256 }
257
258 match = do_lookup_string("10.53.0.1", dns_geoip_city_continentcode,
259 "NA");
260 assert_true(match);
261
262 match = do_lookup_string("10.53.0.1", dns_geoip_city_countrycode, "US");
263 assert_true(match);
264
265 match = do_lookup_string("10.53.0.1", dns_geoip_city_countryname,
266 "United States");
267 assert_true(match);
268
269 match = do_lookup_string("10.53.0.1", dns_geoip_city_region, "CA");
270 assert_true(match);
271
272 match = do_lookup_string("10.53.0.1", dns_geoip_city_regionname,
273 "California");
274 assert_true(match);
275
276 match = do_lookup_string("10.53.0.1", dns_geoip_city_name,
277 "Redwood City");
278 assert_true(match);
279
280 match = do_lookup_string("10.53.0.1", dns_geoip_city_postalcode,
281 "94063");
282 assert_true(match);
283 }
284
285 /* GeoIP city (ipv6) matching */
286 ISC_RUN_TEST_IMPL(city_v6) {
287 bool match;
288
289 UNUSED(state);
290
291 if (geoip.city == NULL) {
292 skip();
293 }
294
295 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
296 dns_geoip_city_continentcode, "NA");
297 assert_true(match);
298
299 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
300 dns_geoip_city_countrycode, "US");
301 assert_true(match);
302
303 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
304 dns_geoip_city_countryname,
305 "United States");
306 assert_true(match);
307
308 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
309 dns_geoip_city_region, "CA");
310 assert_true(match);
311
312 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
313 dns_geoip_city_regionname, "California");
314 assert_true(match);
315
316 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
317 dns_geoip_city_name, "Redwood City");
318 assert_true(match);
319
320 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1",
321 dns_geoip_city_postalcode, "94063");
322 assert_true(match);
323 }
324
325 /* GeoIP asnum matching */
326 ISC_RUN_TEST_IMPL(asnum) {
327 bool match;
328
329 UNUSED(state);
330
331 if (geoip.as == NULL) {
332 skip();
333 }
334
335 match = do_lookup_string("10.53.0.3", dns_geoip_as_asnum, "AS100003");
336 assert_true(match);
337 }
338
339 /* GeoIP isp matching */
340 ISC_RUN_TEST_IMPL(isp) {
341 bool match;
342
343 UNUSED(state);
344
345 if (geoip.isp == NULL) {
346 skip();
347 }
348
349 match = do_lookup_string("10.53.0.1", dns_geoip_isp_name,
350 "One Systems, Inc.");
351 assert_true(match);
352 }
353
354 /* GeoIP org matching */
355 ISC_RUN_TEST_IMPL(org) {
356 bool match;
357
358 UNUSED(state);
359
360 if (geoip.as == NULL) {
361 skip();
362 }
363
364 match = do_lookup_string("10.53.0.2", dns_geoip_org_name,
365 "Two Technology Ltd.");
366 assert_true(match);
367 }
368
369 /* GeoIP domain matching */
370 ISC_RUN_TEST_IMPL(domain) {
371 bool match;
372
373 UNUSED(state);
374
375 if (geoip.domain == NULL) {
376 skip();
377 }
378
379 match = do_lookup_string("10.53.0.5", dns_geoip_domain_name, "five.es");
380 assert_true(match);
381 }
382
383 ISC_TEST_LIST_START
384 ISC_TEST_ENTRY_CUSTOM(baseline, setup_test, teardown_test)
385 ISC_TEST_ENTRY_CUSTOM(country, setup_test, teardown_test)
386 ISC_TEST_ENTRY_CUSTOM(country_v6, setup_test, teardown_test)
387 ISC_TEST_ENTRY_CUSTOM(city, setup_test, teardown_test)
388 ISC_TEST_ENTRY_CUSTOM(city_v6, setup_test, teardown_test)
389 ISC_TEST_ENTRY_CUSTOM(asnum, setup_test, teardown_test)
390 ISC_TEST_ENTRY_CUSTOM(isp, setup_test, teardown_test)
391 ISC_TEST_ENTRY_CUSTOM(org, setup_test, teardown_test)
392 ISC_TEST_ENTRY_CUSTOM(domain, setup_test, teardown_test)
393 ISC_TEST_LIST_END
394
395 ISC_TEST_MAIN