]> 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 23:27:05 +0000 (16:27 -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)
(cherry picked from commit aad88d2c007e8c7ea54f629ddee95f079f357eaf)

lib/dns/tests/geoip_test.c

index 7d45941b9cbe047a756786f93b9ad30c2a9b2a79..3323945443bd51672a929ca7624d29af8dc8e615 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)
@@ -179,6 +197,11 @@ load_geoip(const char *dir) {
                      method, "NetSpeed");
 }
 
+static void
+close_geoip(void) {
+       GeoIP_cleanup();
+}
+
 static bool
 do_lookup_int(const char *addr, uint8_t *scope,
              dns_geoip_subtype_t subtype, int id)
@@ -241,9 +264,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();
@@ -283,9 +303,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();
@@ -314,9 +331,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();
@@ -371,9 +385,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();
@@ -421,14 +432,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", NULL,
                                 dns_geoip_as_asnum, "AS100003");
        assert_true(match);
@@ -441,9 +448,6 @@ isp(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.isp == NULL) {
                skip();
        }
@@ -460,9 +464,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();
@@ -485,9 +486,6 @@ domain(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.domain == NULL) {
                skip();
        }