]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Fix the memory leaks in GeoIP unit test
authorOndřej Surý <ondrej@sury.org>
Tue, 16 Jul 2019 13:28:20 +0000 (15:28 +0200)
committerEvan Hunt <each@isc.org>
Tue, 16 Jul 2019 22:39:56 +0000 (15:39 -0700)
Each individual test opened GeoIP databases but the database handles were never
closed.  This commit moves the open/close from the individual unit tests into
the _setup and _teardown methods where they really belong.

(cherry picked from commit d1c7b791835282d8c9b8031afa47327bac8d2b7f)

lib/dns/tests/geoip_test.c

index 4e9e63a4ab411ec60aca6c3925b2e5384a021f92..727c649f900c7c8443413b59d744ad02223e8157 100644 (file)
@@ -47,6 +47,9 @@
 #endif
 
 #if defined(HAVE_GEOIP) || defined(HAVE_GEOIP2)
+static void load_geoip(const char *dir);
+static void close_geoip(void);
+
 static int
 _setup(void **state) {
        isc_result_t result;
@@ -56,6 +59,9 @@ _setup(void **state) {
        result = dns_test_begin(NULL, false);
        assert_int_equal(result, ISC_R_SUCCESS);
 
+       /* Use databases from the geoip system test */
+       load_geoip(TEST_GEOIP_DATA);
+
        return (0);
 }
 
@@ -63,6 +69,8 @@ static int
 _teardown(void **state) {
        UNUSED(state);
 
+       close_geoip();
+
        dns_test_end();
 
        return (0);
@@ -97,6 +105,16 @@ load_geoip(const char *dir) {
        geoip.isp = open_geoip2(dir, "GeoIP2-ISP.mmdb", &geoip_isp);
        geoip.domain = open_geoip2(dir, "GeoIP2-Domain.mmdb", &geoip_domain);
 }
+
+static void
+close_geoip(void) {
+       MMDB_close(&geoip_country);
+       MMDB_close(&geoip_city);
+       MMDB_close(&geoip_as);
+       MMDB_close(&geoip_isp);
+       MMDB_close(&geoip_domain);
+}
+
 #elif defined(HAVE_GEOIP)
 /*
  * Helper functions (mostly copied from bin/named/geoip.c)
@@ -178,6 +196,11 @@ load_geoip(const char *dir) {
                      method, "NetSpeed");
 }
 
+static void
+close_geoip(void) {
+       GeoIP_cleanup();
+}
+
 static bool
 do_lookup_int(const char *addr, dns_geoip_subtype_t subtype, int id) {
        dns_geoip_elem_t elt;
@@ -237,9 +260,6 @@ country(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
 #ifdef HAVE_GEOIP2
        if (geoip.country == NULL) {
                skip();
@@ -272,9 +292,6 @@ country_v6(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
 #ifdef HAVE_GEOIP2
        if (geoip.country == NULL) {
                skip();
@@ -301,9 +318,6 @@ city(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
 #ifdef HAVE_GEOIP2
        if (geoip.city == NULL) {
                skip();
@@ -358,9 +372,6 @@ city_v6(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
 #ifdef HAVE_GEOIP2
        if (geoip.city == NULL) {
                skip();
@@ -408,14 +419,10 @@ asnum(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.as == NULL) {
                skip();
        }
 
-
        match = do_lookup_string("10.53.0.3", dns_geoip_as_asnum, "AS100003");
        assert_true(match);
 }
@@ -427,9 +434,6 @@ isp(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.isp == NULL) {
                skip();
        }
@@ -446,9 +450,6 @@ org(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
 #ifdef HAVE_GEOIP2
        if (geoip.as == NULL) {
                skip();
@@ -471,9 +472,6 @@ domain(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.domain == NULL) {
                skip();
        }