]> 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)
committerOndřej Surý <ondrej@sury.org>
Tue, 16 Jul 2019 13:35:34 +0000 (15:35 +0200)
Each individual test opened GeoIP databased 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.

lib/dns/tests/geoip_test.c

index 2ede4fda012b8aaccb28450863b646aebcbcce44..a09f66e552694ae5f8200cfb4dcc65fb4e99b49d 100644 (file)
@@ -42,6 +42,9 @@ static dns_geoip_databases_t geoip;
 
 static MMDB_s geoip_country, geoip_city, geoip_as, geoip_isp, geoip_domain;
 
+static void load_geoip(const char *dir);
+static void close_geoip(void);
+
 static int
 _setup(void **state) {
        isc_result_t result;
@@ -51,6 +54,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);
 }
 
@@ -58,6 +64,8 @@ static int
 _teardown(void **state) {
        UNUSED(state);
 
+       close_geoip();
+
        dns_test_end();
 
        return (0);
@@ -87,6 +95,15 @@ load_geoip(const char *dir) {
        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);
+}
+
 static bool
 do_lookup_string(const char *addr, dns_geoip_subtype_t subtype,
                 const char *string)
@@ -128,9 +145,6 @@ country(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.country == NULL) {
                skip();
        }
@@ -157,9 +171,6 @@ country_v6(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.country == NULL) {
                skip();
        }
@@ -180,9 +191,6 @@ city(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.city == NULL) {
                skip();
        }
@@ -223,9 +231,6 @@ city_v6(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.city == NULL) {
                skip();
        }
@@ -267,14 +272,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);
 }
@@ -286,9 +287,6 @@ isp(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.isp == NULL) {
                skip();
        }
@@ -305,9 +303,6 @@ org(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.as == NULL) {
                skip();
        }
@@ -324,9 +319,6 @@ domain(void **state) {
 
        UNUSED(state);
 
-       /* Use databases from the geoip system test */
-       load_geoip(TEST_GEOIP_DATA);
-
        if (geoip.domain == NULL) {
                skip();
        }