]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Rework dns_test_makezone()
authorMichał Kępień <michal@isc.org>
Tue, 13 Mar 2018 13:06:04 +0000 (14:06 +0100)
committerEvan Hunt <each@isc.org>
Thu, 10 May 2018 16:20:31 +0000 (09:20 -0700)
The dns_test_makezone() helper function always assigns the created zone
to some view, which is not always necessary and complicates cleanup of
non-managed zones as they are required not to be assigned to any view.

Rework dns_test_makezone() in order to make it easier to use in unit
tests operating on non-managed zones.  Use dns_name_fromstring() instead
of dns_name_fromtext() to simplify code.  Do not use the CHECK() macro
and add comments to make code flow simpler to follow.  Use
dns_test_makeview() instead of dns_view_create().

Adjust existing unit tests using this function so that they still pass.

(cherry picked from commit bfbeef3609dba1929e3df4ab980291a56387006a)
(cherry picked from commit f70c02d2c2a7fe23e045dabe61ff964170ed2f1a)
(cherry picked from commit d45ee39baed24e3d55dfaeb41f26866b2f5d61f1)

lib/dns/tests/dnstest.c
lib/dns/tests/dnstest.h
lib/dns/tests/zt_test.c

index faf582b382337940524fea87dad7bd7cb8d741cd..bb17dc834e4dd2f628edbb8228b0cf29b13faf2b 100644 (file)
@@ -210,58 +210,69 @@ dns_test_makeview(const char *name, dns_view_t **viewp) {
        return (result);
 }
 
-/*
- * Create a zone with origin 'name', return a pointer to the zone object in
- * 'zonep'.  If 'view' is set, add the zone to that view; otherwise, create
- * a new view for the purpose.
- *
- * If the created view is going to be needed by the caller subsequently,
- * then 'keepview' should be set to true; this will prevent the view
- * from being detached.  In this case, the caller is responsible for
- * detaching the view.
- */
 isc_result_t
 dns_test_makezone(const char *name, dns_zone_t **zonep, dns_view_t *view,
-                 isc_boolean_t keepview)
+                 isc_boolean_t createview)
 {
-       isc_result_t result;
+       dns_fixedname_t fixed_origin;
        dns_zone_t *zone = NULL;
-       isc_buffer_t buffer;
-       dns_fixedname_t fixorigin;
+       isc_result_t result;
        dns_name_t *origin;
 
-       if (view == NULL)
-               CHECK(dns_view_create(mctx, dns_rdataclass_in, "view", &view));
-       else if (!keepview)
-               keepview = ISC_TRUE;
-
-       zone = *zonep;
-       if (zone == NULL)
-               CHECK(dns_zone_create(&zone, mctx));
-
-       isc_buffer_constinit(&buffer, name, strlen(name));
-       isc_buffer_add(&buffer, strlen(name));
-       dns_fixedname_init(&fixorigin);
-       origin = dns_fixedname_name(&fixorigin);
-       CHECK(dns_name_fromtext(origin, &buffer, dns_rootname, 0, NULL));
-       CHECK(dns_zone_setorigin(zone, origin));
-       dns_zone_setview(zone, view);
+       REQUIRE(view == NULL || !createview);
+
+       /*
+        * Create the zone structure.
+        */
+       result = dns_zone_create(&zone, mctx);
+       if (result != ISC_R_SUCCESS) {
+               return (result);
+       }
+
+       /*
+        * Set zone type and origin.
+        */
        dns_zone_settype(zone, dns_zone_master);
-       dns_zone_setclass(zone, view->rdclass);
-       dns_view_addzone(view, zone);
+       dns_fixedname_init(&fixed_origin);
+       origin = dns_fixedname_name(&fixed_origin);
+       result = dns_name_fromstring(origin, name, 0, NULL);
+       if (result != ISC_R_SUCCESS) {
+               goto detach_zone;
+       }
+       result = dns_zone_setorigin(zone, origin);
+       if (result != ISC_R_SUCCESS) {
+               goto detach_zone;
+       }
 
-       if (!keepview)
-               dns_view_detach(&view);
+       /*
+        * If requested, create a view.
+        */
+       if (createview) {
+               result = dns_test_makeview("view", &view);
+               if (result != ISC_R_SUCCESS) {
+                       goto detach_zone;
+               }
+       }
+
+       /*
+        * If a view was passed as an argument or created above, attach the
+        * created zone to it.  Otherwise, set the zone's class to IN.
+        */
+       if (view != NULL) {
+               dns_zone_setview(zone, view);
+               dns_zone_setclass(zone, view->rdclass);
+               dns_view_addzone(view, zone);
+       } else {
+               dns_zone_setclass(zone, dns_rdataclass_in);
+       }
 
        *zonep = zone;
 
        return (ISC_R_SUCCESS);
 
-  cleanup:
-       if (zone != NULL)
-               dns_zone_detach(&zone);
-       if (view != NULL)
-               dns_view_detach(&view);
+ detach_zone:
+       dns_zone_detach(&zone);
+
        return (result);
 }
 
index 918bddd4c2fa0f66d83b3ff19ad0a52a702da565..c4e154ceea1d62c62313713e07dd022ca208a409 100644 (file)
@@ -59,9 +59,25 @@ dns_test_end(void);
 isc_result_t
 dns_test_makeview(const char *name, dns_view_t **viewp);
 
+/*%
+ * Create a zone with origin 'name', return a pointer to the zone object in
+ * 'zonep'.
+ *
+ * If 'view' is set, the returned zone will be assigned to the passed view.
+ * 'createview' must be set to false when 'view' is non-NULL.
+ *
+ * If 'view' is not set and 'createview' is true, a new view is also created
+ * and the returned zone is assigned to it.  This imposes two requirements on
+ * the caller: 1) the returned zone has to be subsequently assigned to a zone
+ * manager, otherwise its cleanup will fail, 2) the created view has to be
+ * cleaned up by the caller.
+ *
+ * If 'view' is not set and 'createview' is false, the returned zone will not
+ * be assigned to any view.
+ */
 isc_result_t
 dns_test_makezone(const char *name, dns_zone_t **zonep, dns_view_t *view,
-                                 isc_boolean_t keepview);
+                 isc_boolean_t createview);
 
 isc_result_t
 dns_test_setupzonemgr(void);
index b390e4002b71e589da4a79e85032e877db788bc4..551783ff682fa8558b756844619f712936d762e2 100644 (file)
@@ -14,8 +14,6 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id$ */
-
 /*! \file */
 
 #include <config.h>
@@ -224,12 +222,12 @@ ATF_TC_BODY(asyncload_zt, tc) {
        dns_zone_setfile(zone1, "testdata/zt/zone1.db");
        view = dns_zone_getview(zone1);
 
-       result = dns_test_makezone("bar", &zone2, view, ISC_TRUE);
+       result = dns_test_makezone("bar", &zone2, view, ISC_FALSE);
        ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
        dns_zone_setfile(zone2, "testdata/zt/zone1.db");
 
        /* This one will fail to load */
-       result = dns_test_makezone("fake", &zone3, view, ISC_TRUE);
+       result = dns_test_makezone("fake", &zone3, view, ISC_FALSE);
        ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
        dns_zone_setfile(zone3, "testdata/zt/nonexistent.db");