00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <atf-c.h>
00024
00025 #include <unistd.h>
00026
00027 #include <isc/types.h>
00028
00029 #include <dns/geoip.h>
00030
00031 #include "dnstest.h"
00032
00033 #ifdef HAVE_GEOIP
00034 #include <GeoIP.h>
00035
00036
00037 #define TEST_GEOIP_DATA "../../../bin/tests/system/geoip/data"
00038
00039
00040
00041
00042
00043 static dns_geoip_databases_t geoip = {
00044 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
00045 };
00046
00047 static void
00048 init_geoip_db(GeoIP **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback,
00049 GeoIPOptions method, const char *name)
00050 {
00051 char *info;
00052 GeoIP *db;
00053
00054 REQUIRE(dbp != NULL);
00055
00056 db = *dbp;
00057
00058 if (db != NULL) {
00059 GeoIP_delete(db);
00060 db = *dbp = NULL;
00061 }
00062
00063 if (! GeoIP_db_avail(edition)) {
00064 fprintf(stderr, "GeoIP %s (type %d) DB not available\n",
00065 name, edition);
00066 goto fail;
00067 }
00068
00069 fprintf(stderr, "initializing GeoIP %s (type %d) DB\n",
00070 name, edition);
00071
00072 db = GeoIP_open_type(edition, method);
00073 if (db == NULL) {
00074 fprintf(stderr,
00075 "failed to initialize GeoIP %s (type %d) DB%s\n",
00076 name, edition, fallback == 0
00077 ? "; geoip matches using this database will fail"
00078 : "");
00079 goto fail;
00080 }
00081
00082 info = GeoIP_database_info(db);
00083 if (info != NULL)
00084 fprintf(stderr, "%s\n", info);
00085
00086 *dbp = db;
00087 return;
00088
00089 fail:
00090 if (fallback != 0)
00091 init_geoip_db(dbp, fallback, 0, method, name);
00092 }
00093
00094 static void
00095 load_geoip(const char *dir) {
00096 GeoIPOptions method;
00097
00098 #ifdef _WIN32
00099 method = GEOIP_STANDARD;
00100 #else
00101 method = GEOIP_MMAP_CACHE;
00102 #endif
00103
00104 if (dir != NULL) {
00105 char *p;
00106 DE_CONST(dir, p);
00107 GeoIP_setup_custom_directory(p);
00108 }
00109
00110 init_geoip_db(&geoip.country_v4, GEOIP_COUNTRY_EDITION, 0,
00111 method, "Country (IPv4)");
00112 #ifdef HAVE_GEOIP_V6
00113 init_geoip_db(&geoip.country_v6, GEOIP_COUNTRY_EDITION_V6, 0,
00114 method, "Country (IPv6)");
00115 #endif
00116
00117 init_geoip_db(&geoip.city_v4, GEOIP_CITY_EDITION_REV1,
00118 GEOIP_CITY_EDITION_REV0, method, "City (IPv4)");
00119 #if defined(HAVE_GEOIP_V6) && defined(HAVE_GEOIP_CITY_V6)
00120 init_geoip_db(&geoip.city_v6, GEOIP_CITY_EDITION_REV1_V6,
00121 GEOIP_CITY_EDITION_REV0_V6, method, "City (IPv6)");
00122 #endif
00123
00124 init_geoip_db(&geoip.region, GEOIP_REGION_EDITION_REV1,
00125 GEOIP_REGION_EDITION_REV0, method, "Region");
00126 init_geoip_db(&geoip.isp, GEOIP_ISP_EDITION, 0,
00127 method, "ISP");
00128 init_geoip_db(&geoip.org, GEOIP_ORG_EDITION, 0,
00129 method, "Org");
00130 init_geoip_db(&geoip.as, GEOIP_ASNUM_EDITION, 0,
00131 method, "AS");
00132 init_geoip_db(&geoip.domain, GEOIP_DOMAIN_EDITION, 0,
00133 method, "Domain");
00134 init_geoip_db(&geoip.netspeed, GEOIP_NETSPEED_EDITION, 0,
00135 method, "NetSpeed");
00136 }
00137
00138 static isc_boolean_t
00139 do_lookup_string(const char *addr, isc_uint8_t *scope,
00140 dns_geoip_subtype_t subtype, const char *string)
00141 {
00142 dns_geoip_elem_t elt;
00143 struct in_addr in4;
00144 isc_netaddr_t na;
00145
00146 inet_pton(AF_INET, addr, &in4);
00147 isc_netaddr_fromin(&na, &in4);
00148
00149 elt.subtype = subtype;
00150 strcpy(elt.as_string, string);
00151
00152 return (dns_geoip_match(&na, scope, &geoip, &elt));
00153 }
00154
00155 static isc_boolean_t
00156 do_lookup_string_v6(const char *addr, isc_uint8_t *scope,
00157 dns_geoip_subtype_t subtype, const char *string)
00158 {
00159 dns_geoip_elem_t elt;
00160 struct in6_addr in6;
00161 isc_netaddr_t na;
00162
00163 inet_pton(AF_INET6, addr, &in6);
00164 isc_netaddr_fromin6(&na, &in6);
00165
00166 elt.subtype = subtype;
00167 strcpy(elt.as_string, string);
00168
00169 return (dns_geoip_match(&na, scope, &geoip, &elt));
00170 }
00171
00172 static isc_boolean_t
00173 do_lookup_int(const char *addr, isc_uint8_t *scope,
00174 dns_geoip_subtype_t subtype, int id)
00175 {
00176 dns_geoip_elem_t elt;
00177 struct in_addr in4;
00178 isc_netaddr_t na;
00179
00180 inet_pton(AF_INET, addr, &in4);
00181 isc_netaddr_fromin(&na, &in4);
00182
00183 elt.subtype = subtype;
00184 elt.as_int = id;
00185
00186 return (dns_geoip_match(&na, scope, &geoip, &elt));
00187 }
00188
00189
00190
00191
00192
00193
00194 ATF_TC(country);
00195 ATF_TC_HEAD(country, tc) {
00196 atf_tc_set_md_var(tc, "descr", "test country database matching");
00197 }
00198 ATF_TC_BODY(country, tc) {
00199 isc_result_t result;
00200 isc_boolean_t match;
00201 isc_uint8_t scope;
00202
00203 UNUSED(tc);
00204
00205 result = dns_test_begin(NULL, ISC_TRUE);
00206 ATF_REQUIRE(result == ISC_R_SUCCESS);
00207
00208
00209 load_geoip(TEST_GEOIP_DATA);
00210
00211 if (geoip.country_v4 == NULL) {
00212 dns_test_end();
00213 atf_tc_skip("Database not available");
00214 }
00215
00216 match = do_lookup_string("10.53.0.1", &scope,
00217 dns_geoip_country_code, "AU");
00218 ATF_CHECK(match);
00219 ATF_CHECK_EQ(scope, 32);
00220
00221 match = do_lookup_string("10.53.0.1", &scope,
00222 dns_geoip_country_code3, "AUS");
00223 ATF_CHECK(match);
00224 ATF_CHECK_EQ(scope, 32);
00225
00226 match = do_lookup_string("10.53.0.1", &scope,
00227 dns_geoip_country_name, "Australia");
00228 ATF_CHECK(match);
00229 ATF_CHECK_EQ(scope, 32);
00230
00231 match = do_lookup_string("192.0.2.128", &scope,
00232 dns_geoip_country_code, "O1");
00233 ATF_CHECK(match);
00234 ATF_CHECK_EQ(scope, 24);
00235
00236 match = do_lookup_string("192.0.2.128", &scope,
00237 dns_geoip_country_name, "Other");
00238 ATF_CHECK(match);
00239 ATF_CHECK_EQ(scope, 24);
00240
00241 dns_test_end();
00242 }
00243
00244
00245 ATF_TC(country_v6);
00246 ATF_TC_HEAD(country_v6, tc) {
00247 atf_tc_set_md_var(tc, "descr", "test country (ipv6) database matching");
00248 }
00249 ATF_TC_BODY(country_v6, tc) {
00250 isc_result_t result;
00251 isc_boolean_t match;
00252 isc_uint8_t scope;
00253
00254 UNUSED(tc);
00255
00256 result = dns_test_begin(NULL, ISC_TRUE);
00257 ATF_REQUIRE(result == ISC_R_SUCCESS);
00258
00259
00260 load_geoip(TEST_GEOIP_DATA);
00261
00262 if (geoip.country_v6 == NULL) {
00263 dns_test_end();
00264 atf_tc_skip("Database not available");
00265 }
00266
00267 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", &scope,
00268 dns_geoip_country_code, "AU");
00269 ATF_CHECK(match);
00270 ATF_CHECK_EQ(scope, 128);
00271
00272 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", &scope,
00273 dns_geoip_country_code3, "AUS");
00274 ATF_CHECK(match);
00275 ATF_CHECK_EQ(scope, 128);
00276
00277 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", &scope,
00278 dns_geoip_country_name, "Australia");
00279 ATF_CHECK(match);
00280 ATF_CHECK_EQ(scope, 128);
00281
00282 dns_test_end();
00283 }
00284
00285
00286 ATF_TC(city);
00287 ATF_TC_HEAD(city, tc) {
00288 atf_tc_set_md_var(tc, "descr", "test city database matching");
00289 }
00290 ATF_TC_BODY(city, tc) {
00291 isc_result_t result;
00292 isc_boolean_t match;
00293
00294 UNUSED(tc);
00295
00296 result = dns_test_begin(NULL, ISC_TRUE);
00297 ATF_REQUIRE(result == ISC_R_SUCCESS);
00298
00299
00300 load_geoip(TEST_GEOIP_DATA);
00301
00302 if (geoip.city_v4 == NULL) {
00303 dns_test_end();
00304 atf_tc_skip("Database not available");
00305 }
00306
00307 match = do_lookup_string("10.53.0.1", NULL,
00308 dns_geoip_city_continentcode, "NA");
00309 ATF_CHECK(match);
00310
00311 match = do_lookup_string("10.53.0.1", NULL,
00312 dns_geoip_city_countrycode, "US");
00313 ATF_CHECK(match);
00314
00315 match = do_lookup_string("10.53.0.1", NULL,
00316 dns_geoip_city_countrycode3, "USA");
00317 ATF_CHECK(match);
00318
00319 match = do_lookup_string("10.53.0.1", NULL,
00320 dns_geoip_city_countryname, "United States");
00321 ATF_CHECK(match);
00322
00323 match = do_lookup_string("10.53.0.1", NULL,
00324 dns_geoip_city_region, "CA");
00325 ATF_CHECK(match);
00326
00327 match = do_lookup_string("10.53.0.1", NULL,
00328 dns_geoip_city_regionname, "California");
00329 ATF_CHECK(match);
00330
00331 match = do_lookup_string("10.53.0.1", NULL,
00332 dns_geoip_city_name, "Redwood City");
00333 ATF_CHECK(match);
00334
00335 match = do_lookup_string("10.53.0.1", NULL,
00336 dns_geoip_city_postalcode, "94063");
00337 ATF_CHECK(match);
00338
00339 match = do_lookup_int("10.53.0.1", NULL, dns_geoip_city_areacode, 650);
00340 ATF_CHECK(match);
00341
00342 match = do_lookup_int("10.53.0.1", NULL, dns_geoip_city_metrocode, 807);
00343 ATF_CHECK(match);
00344
00345 dns_test_end();
00346 }
00347
00348
00349 ATF_TC(city_v6);
00350 ATF_TC_HEAD(city_v6, tc) {
00351 atf_tc_set_md_var(tc, "descr", "test city (ipv6) database matching");
00352 }
00353 ATF_TC_BODY(city_v6, tc) {
00354 isc_result_t result;
00355 isc_boolean_t match;
00356
00357 UNUSED(tc);
00358
00359 result = dns_test_begin(NULL, ISC_TRUE);
00360 ATF_REQUIRE(result == ISC_R_SUCCESS);
00361
00362
00363 load_geoip(TEST_GEOIP_DATA);
00364
00365 if (geoip.city_v6 == NULL) {
00366 dns_test_end();
00367 atf_tc_skip("Database not available");
00368 }
00369
00370 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", NULL,
00371 dns_geoip_city_continentcode, "NA");
00372 ATF_CHECK(match);
00373
00374 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", NULL,
00375 dns_geoip_city_countrycode, "US");
00376 ATF_CHECK(match);
00377
00378 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", NULL,
00379 dns_geoip_city_countrycode3, "USA");
00380 ATF_CHECK(match);
00381
00382 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", NULL,
00383 dns_geoip_city_countryname,
00384 "United States");
00385 ATF_CHECK(match);
00386
00387 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", NULL,
00388 dns_geoip_city_region, "CA");
00389 ATF_CHECK(match);
00390
00391 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", NULL,
00392 dns_geoip_city_regionname, "California");
00393 ATF_CHECK(match);
00394
00395 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", NULL,
00396 dns_geoip_city_name, "Redwood City");
00397 ATF_CHECK(match);
00398
00399 match = do_lookup_string_v6("fd92:7065:b8e:ffff::1", NULL,
00400 dns_geoip_city_postalcode, "94063");
00401 ATF_CHECK(match);
00402
00403 dns_test_end();
00404 }
00405
00406
00407
00408 ATF_TC(region);
00409 ATF_TC_HEAD(region, tc) {
00410 atf_tc_set_md_var(tc, "descr", "test region database matching");
00411 }
00412 ATF_TC_BODY(region, tc) {
00413 isc_result_t result;
00414 isc_boolean_t match;
00415
00416 UNUSED(tc);
00417
00418 result = dns_test_begin(NULL, ISC_TRUE);
00419 ATF_REQUIRE(result == ISC_R_SUCCESS);
00420
00421
00422 load_geoip(TEST_GEOIP_DATA);
00423
00424 if (geoip.region == NULL) {
00425 dns_test_end();
00426 atf_tc_skip("Database not available");
00427 }
00428
00429 match = do_lookup_string("10.53.0.1", NULL,
00430 dns_geoip_region_code, "CA");
00431 ATF_CHECK(match);
00432
00433 match = do_lookup_string("10.53.0.1", NULL,
00434 dns_geoip_region_name, "California");
00435 ATF_CHECK(match);
00436
00437 match = do_lookup_string("10.53.0.1", NULL,
00438 dns_geoip_region_countrycode, "US");
00439 ATF_CHECK(match);
00440
00441 dns_test_end();
00442 }
00443
00444
00445
00446
00447
00448
00449
00450 ATF_TC(best);
00451 ATF_TC_HEAD(best, tc) {
00452 atf_tc_set_md_var(tc, "descr", "test best database matching");
00453 }
00454 ATF_TC_BODY(best, tc) {
00455 isc_result_t result;
00456 isc_boolean_t match;
00457
00458 UNUSED(tc);
00459
00460 result = dns_test_begin(NULL, ISC_TRUE);
00461 ATF_REQUIRE(result == ISC_R_SUCCESS);
00462
00463
00464 load_geoip(TEST_GEOIP_DATA);
00465
00466 if (geoip.region == NULL) {
00467 dns_test_end();
00468 atf_tc_skip("Database not available");
00469 }
00470
00471 match = do_lookup_string("10.53.0.4", NULL,
00472 dns_geoip_countrycode, "US");
00473 ATF_CHECK(match);
00474
00475 match = do_lookup_string("10.53.0.4", NULL,
00476 dns_geoip_countrycode3, "USA");
00477 ATF_CHECK(match);
00478
00479 match = do_lookup_string("10.53.0.4", NULL,
00480 dns_geoip_countryname, "United States");
00481 ATF_CHECK(match);
00482
00483 match = do_lookup_string("10.53.0.4", NULL,
00484 dns_geoip_regionname, "Virginia");
00485 ATF_CHECK(match);
00486
00487 match = do_lookup_string("10.53.0.4", NULL,
00488 dns_geoip_region, "VA");
00489 ATF_CHECK(match);
00490
00491 GeoIP_delete(geoip.city_v4);
00492 geoip.city_v4 = NULL;
00493
00494 match = do_lookup_string("10.53.0.4", NULL,
00495 dns_geoip_countrycode, "AU");
00496 ATF_CHECK(match);
00497
00498
00499
00500
00501
00502 match = do_lookup_string("10.53.0.4", NULL,
00503 dns_geoip_countrycode3, "CAN");
00504 ATF_CHECK(match);
00505
00506 match = do_lookup_string("10.53.0.4", NULL,
00507 dns_geoip_countryname, "Canada");
00508 ATF_CHECK(match);
00509
00510 GeoIP_delete(geoip.region);
00511 geoip.region = NULL;
00512
00513 match = do_lookup_string("10.53.0.4", NULL,
00514 dns_geoip_countrycode, "CA");
00515 ATF_CHECK(match);
00516
00517 match = do_lookup_string("10.53.0.4", NULL,
00518 dns_geoip_countrycode3, "CAN");
00519 ATF_CHECK(match);
00520
00521 match = do_lookup_string("10.53.0.4", NULL,
00522 dns_geoip_countryname, "Canada");
00523 ATF_CHECK(match);
00524
00525 dns_test_end();
00526 }
00527
00528
00529
00530 ATF_TC(asnum);
00531 ATF_TC_HEAD(asnum, tc) {
00532 atf_tc_set_md_var(tc, "descr", "test asnum database matching");
00533 }
00534 ATF_TC_BODY(asnum, tc) {
00535 isc_result_t result;
00536 isc_boolean_t match;
00537
00538 UNUSED(tc);
00539
00540 result = dns_test_begin(NULL, ISC_TRUE);
00541 ATF_REQUIRE(result == ISC_R_SUCCESS);
00542
00543
00544 load_geoip(TEST_GEOIP_DATA);
00545
00546 if (geoip.as == NULL) {
00547 dns_test_end();
00548 atf_tc_skip("Database not available");
00549 }
00550
00551
00552 match = do_lookup_string("10.53.0.3", NULL, dns_geoip_as_asnum,
00553 "AS100003 Three Network Labs");
00554 ATF_CHECK(match);
00555
00556 dns_test_end();
00557 }
00558
00559
00560 ATF_TC(isp);
00561 ATF_TC_HEAD(isp, tc) {
00562 atf_tc_set_md_var(tc, "descr", "test isp database matching");
00563 }
00564 ATF_TC_BODY(isp, tc) {
00565 isc_result_t result;
00566 isc_boolean_t match;
00567
00568 UNUSED(tc);
00569
00570 result = dns_test_begin(NULL, ISC_TRUE);
00571 ATF_REQUIRE(result == ISC_R_SUCCESS);
00572
00573
00574 load_geoip(TEST_GEOIP_DATA);
00575
00576 if (geoip.isp == NULL) {
00577 dns_test_end();
00578 atf_tc_skip("Database not available");
00579 }
00580
00581 match = do_lookup_string("10.53.0.1", NULL, dns_geoip_isp_name,
00582 "One Systems, Inc.");
00583 ATF_CHECK(match);
00584
00585 dns_test_end();
00586 }
00587
00588
00589 ATF_TC(org);
00590 ATF_TC_HEAD(org, tc) {
00591 atf_tc_set_md_var(tc, "descr", "test org database matching");
00592 }
00593 ATF_TC_BODY(org, tc) {
00594 isc_result_t result;
00595 isc_boolean_t match;
00596
00597 UNUSED(tc);
00598
00599 result = dns_test_begin(NULL, ISC_TRUE);
00600 ATF_REQUIRE(result == ISC_R_SUCCESS);
00601
00602
00603 load_geoip(TEST_GEOIP_DATA);
00604
00605 if (geoip.org == NULL) {
00606 dns_test_end();
00607 atf_tc_skip("Database not available");
00608 }
00609
00610 match = do_lookup_string("10.53.0.2", NULL, dns_geoip_org_name,
00611 "Two Technology Ltd.");
00612 ATF_CHECK(match);
00613
00614 dns_test_end();
00615 }
00616
00617
00618 ATF_TC(domain);
00619 ATF_TC_HEAD(domain, tc) {
00620 atf_tc_set_md_var(tc, "descr", "test domain database matching");
00621 }
00622 ATF_TC_BODY(domain, tc) {
00623 isc_result_t result;
00624 isc_boolean_t match;
00625
00626 UNUSED(tc);
00627
00628 result = dns_test_begin(NULL, ISC_TRUE);
00629 ATF_REQUIRE(result == ISC_R_SUCCESS);
00630
00631
00632 load_geoip(TEST_GEOIP_DATA);
00633
00634 if (geoip.domain == NULL) {
00635 dns_test_end();
00636 atf_tc_skip("Database not available");
00637 }
00638
00639 match = do_lookup_string("10.53.0.4", NULL,
00640 dns_geoip_domain_name, "four.com");
00641 ATF_CHECK(match);
00642
00643 dns_test_end();
00644 }
00645
00646
00647 ATF_TC(netspeed);
00648 ATF_TC_HEAD(netspeed, tc) {
00649 atf_tc_set_md_var(tc, "descr", "test netspeed database matching");
00650 }
00651 ATF_TC_BODY(netspeed, tc) {
00652 isc_result_t result;
00653 isc_boolean_t match;
00654
00655 UNUSED(tc);
00656
00657 result = dns_test_begin(NULL, ISC_TRUE);
00658 ATF_REQUIRE(result == ISC_R_SUCCESS);
00659
00660
00661 load_geoip(TEST_GEOIP_DATA);
00662
00663 if (geoip.netspeed == NULL) {
00664 dns_test_end();
00665 atf_tc_skip("Database not available");
00666 }
00667
00668 match = do_lookup_int("10.53.0.1", NULL, dns_geoip_netspeed_id, 0);
00669 ATF_CHECK(match);
00670
00671 match = do_lookup_int("10.53.0.2", NULL, dns_geoip_netspeed_id, 1);
00672 ATF_CHECK(match);
00673
00674 match = do_lookup_int("10.53.0.3", NULL, dns_geoip_netspeed_id, 2);
00675 ATF_CHECK(match);
00676
00677 match = do_lookup_int("10.53.0.4", NULL, dns_geoip_netspeed_id, 3);
00678 ATF_CHECK(match);
00679
00680 dns_test_end();
00681 }
00682 #else
00683 ATF_TC(untested);
00684 ATF_TC_HEAD(untested, tc) {
00685 atf_tc_set_md_var(tc, "descr", "skipping geoip test");
00686 }
00687 ATF_TC_BODY(untested, tc) {
00688 UNUSED(tc);
00689 atf_tc_skip("GeoIP not available");
00690 }
00691 #endif
00692
00693
00694
00695
00696 ATF_TP_ADD_TCS(tp) {
00697 #ifdef HAVE_GEOIP
00698 ATF_TP_ADD_TC(tp, country);
00699 ATF_TP_ADD_TC(tp, country_v6);
00700 ATF_TP_ADD_TC(tp, city);
00701 ATF_TP_ADD_TC(tp, city_v6);
00702 ATF_TP_ADD_TC(tp, region);
00703 ATF_TP_ADD_TC(tp, best);
00704 ATF_TP_ADD_TC(tp, asnum);
00705 ATF_TP_ADD_TC(tp, isp);
00706 ATF_TP_ADD_TC(tp, org);
00707 ATF_TP_ADD_TC(tp, domain);
00708 ATF_TP_ADD_TC(tp, netspeed);
00709 #else
00710 ATF_TP_ADD_TC(tp, untested);
00711 #endif
00712
00713 return (atf_no_error());
00714 }
00715