2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-30 14:07:59 +00:00

convert name_test

This commit is contained in:
Joey
2018-11-14 20:19:50 +08:00
committed by Evan Hunt
parent 4ba08bb119
commit 1b48e710d5

View File

@@ -9,17 +9,22 @@
* information regarding copyright ownership. * information regarding copyright ownership.
*/ */
/*! \file */
#include <config.h> #include <config.h>
#if HAVE_CMOCKA
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <atf-c.h> #define UNIT_TESTING
#include <cmocka.h>
#include <isc/buffer.h> #include <isc/buffer.h>
#include <isc/commandline.h> #include <isc/commandline.h>
@@ -35,15 +40,30 @@
#include "dnstest.h" #include "dnstest.h"
/* static int
* Individual unit tests _setup(void **state) {
*/ isc_result_t result;
ATF_TC(fullcompare); UNUSED(state);
ATF_TC_HEAD(fullcompare, tc) {
atf_tc_set_md_var(tc, "descr", "dns_name_fullcompare test"); result = dns_test_begin(NULL, false);
assert_int_equal(result, ISC_R_SUCCESS);
return (0);
} }
ATF_TC_BODY(fullcompare, tc) {
static int
_teardown(void **state) {
UNUSED(state);
dns_test_end();
return (0);
}
/* dns_name_fullcompare test */
static void
fullcompare_test(void **state) {
dns_fixedname_t fixed1; dns_fixedname_t fixed1;
dns_fixedname_t fixed2; dns_fixedname_t fixed2;
dns_name_t *name1; dns_name_t *name1;
@@ -84,7 +104,7 @@ ATF_TC_BODY(fullcompare, tc) {
{ NULL, NULL, dns_namereln_none, 0, 0 } { NULL, NULL, dns_namereln_none, 0, 0 }
}; };
UNUSED(tc); UNUSED(state);
name1 = dns_fixedname_initname(&fixed1); name1 = dns_fixedname_initname(&fixed1);
name2 = dns_fixedname_initname(&fixed2); name2 = dns_fixedname_initname(&fixed2);
@@ -97,28 +117,28 @@ ATF_TC_BODY(fullcompare, tc) {
} else { } else {
result = dns_name_fromstring2(name1, data[i].name1, result = dns_name_fromstring2(name1, data[i].name1,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
} }
if (data[i].name2[0] == 0) { if (data[i].name2[0] == 0) {
dns_fixedname_init(&fixed2); dns_fixedname_init(&fixed2);
} else { } else {
result = dns_name_fromstring2(name2, data[i].name2, result = dns_name_fromstring2(name2, data[i].name2,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
} }
relation = dns_name_fullcompare(name1, name1, &order, &nlabels); relation = dns_name_fullcompare(name1, name1, &order, &nlabels);
ATF_REQUIRE_EQ(relation, dns_namereln_equal); assert_int_equal(relation, dns_namereln_equal);
ATF_REQUIRE_EQ(order, 0); assert_int_equal(order, 0);
ATF_REQUIRE_EQ(nlabels, name1->labels); assert_int_equal(nlabels, name1->labels);
/* Some random initializer */ /* Some random initializer */
order = 3001; order = 3001;
nlabels = 3001; nlabels = 3001;
relation = dns_name_fullcompare(name1, name2, &order, &nlabels); relation = dns_name_fullcompare(name1, name2, &order, &nlabels);
ATF_REQUIRE_EQ(relation, data[i].relation); assert_int_equal(relation, data[i].relation);
ATF_REQUIRE_EQ(order, data[i].order); assert_int_equal(order, data[i].order);
ATF_REQUIRE_EQ(nlabels, data[i].nlabels); assert_int_equal(nlabels, data[i].nlabels);
} }
} }
@@ -136,11 +156,11 @@ compress_test(dns_name_t *name1, dns_name_t *name2, dns_name_t *name3,
isc_buffer_init(&source, buf1, sizeof(buf1)); isc_buffer_init(&source, buf1, sizeof(buf1));
isc_buffer_init(&target, buf2, sizeof(buf2)); isc_buffer_init(&target, buf2, sizeof(buf2));
ATF_REQUIRE_EQ(dns_name_towire(name1, cctx, &source), ISC_R_SUCCESS); assert_int_equal(dns_name_towire(name1, cctx, &source), ISC_R_SUCCESS);
ATF_CHECK_EQ(dns_name_towire(name2, cctx, &source), ISC_R_SUCCESS); assert_int_equal(dns_name_towire(name2, cctx, &source), ISC_R_SUCCESS);
ATF_CHECK_EQ(dns_name_towire(name2, cctx, &source), ISC_R_SUCCESS); assert_int_equal(dns_name_towire(name2, cctx, &source), ISC_R_SUCCESS);
ATF_CHECK_EQ(dns_name_towire(name3, cctx, &source), ISC_R_SUCCESS); assert_int_equal(dns_name_towire(name3, cctx, &source), ISC_R_SUCCESS);
isc_buffer_setactive(&source, source.used); isc_buffer_setactive(&source, source.used);
@@ -155,15 +175,13 @@ compress_test(dns_name_t *name1, dns_name_t *name2, dns_name_t *name3,
&target) == ISC_R_SUCCESS); &target) == ISC_R_SUCCESS);
dns_decompress_invalidate(dctx); dns_decompress_invalidate(dctx);
ATF_CHECK_EQ(target.used, length); assert_int_equal(target.used, length);
ATF_CHECK(memcmp(target.base, expected, target.used) == 0); assert_true(memcmp(target.base, expected, target.used) == 0);
} }
ATF_TC(compression); /* name compression test */
ATF_TC_HEAD(compression, tc) { static void
atf_tc_set_md_var(tc, "descr", "name compression test"); compression_test(void **state) {
}
ATF_TC_BODY(compression, tc) {
unsigned int allowed; unsigned int allowed;
dns_compress_t cctx; dns_compress_t cctx;
dns_decompress_t dctx; dns_decompress_t dctx;
@@ -177,7 +195,7 @@ ATF_TC_BODY(compression, tc) {
unsigned char plain[] = "\003yyy\003foo\0\003bar\003yyy\003foo\0\003" unsigned char plain[] = "\003yyy\003foo\0\003bar\003yyy\003foo\0\003"
"bar\003yyy\003foo\0\003xxx\003bar\003foo"; "bar\003yyy\003foo\0\003xxx\003bar\003foo";
ATF_REQUIRE_EQ(dns_test_begin(NULL, false), ISC_R_SUCCESS);; UNUSED(state);
dns_name_init(&name1, NULL); dns_name_init(&name1, NULL);
r.base = plain1; r.base = plain1;
@@ -196,7 +214,7 @@ ATF_TC_BODY(compression, tc) {
/* Test 1: NONE */ /* Test 1: NONE */
allowed = DNS_COMPRESS_NONE; allowed = DNS_COMPRESS_NONE;
ATF_REQUIRE_EQ(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS); assert_int_equal(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS);
dns_compress_setmethods(&cctx, allowed); dns_compress_setmethods(&cctx, allowed);
dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT); dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT);
dns_decompress_setmethods(&dctx, allowed); dns_decompress_setmethods(&dctx, allowed);
@@ -209,7 +227,7 @@ ATF_TC_BODY(compression, tc) {
/* Test2: GLOBAL14 */ /* Test2: GLOBAL14 */
allowed = DNS_COMPRESS_GLOBAL14; allowed = DNS_COMPRESS_GLOBAL14;
ATF_REQUIRE_EQ(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS); assert_int_equal(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS);
dns_compress_setmethods(&cctx, allowed); dns_compress_setmethods(&cctx, allowed);
dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT); dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT);
dns_decompress_setmethods(&dctx, allowed); dns_decompress_setmethods(&dctx, allowed);
@@ -222,7 +240,7 @@ ATF_TC_BODY(compression, tc) {
/* Test3: ALL */ /* Test3: ALL */
allowed = DNS_COMPRESS_ALL; allowed = DNS_COMPRESS_ALL;
ATF_REQUIRE_EQ(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS); assert_int_equal(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS);
dns_compress_setmethods(&cctx, allowed); dns_compress_setmethods(&cctx, allowed);
dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT); dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT);
dns_decompress_setmethods(&dctx, allowed); dns_decompress_setmethods(&dctx, allowed);
@@ -235,7 +253,7 @@ ATF_TC_BODY(compression, tc) {
/* Test4: NONE disabled */ /* Test4: NONE disabled */
allowed = DNS_COMPRESS_NONE; allowed = DNS_COMPRESS_NONE;
ATF_REQUIRE_EQ(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS); assert_int_equal(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS);
dns_compress_setmethods(&cctx, allowed); dns_compress_setmethods(&cctx, allowed);
dns_compress_disable(&cctx); dns_compress_disable(&cctx);
dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT); dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT);
@@ -249,7 +267,7 @@ ATF_TC_BODY(compression, tc) {
/* Test5: GLOBAL14 disabled */ /* Test5: GLOBAL14 disabled */
allowed = DNS_COMPRESS_GLOBAL14; allowed = DNS_COMPRESS_GLOBAL14;
ATF_REQUIRE_EQ(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS); assert_int_equal(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS);
dns_compress_setmethods(&cctx, allowed); dns_compress_setmethods(&cctx, allowed);
dns_compress_disable(&cctx); dns_compress_disable(&cctx);
dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT); dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT);
@@ -263,7 +281,7 @@ ATF_TC_BODY(compression, tc) {
/* Test6: ALL disabled */ /* Test6: ALL disabled */
allowed = DNS_COMPRESS_ALL; allowed = DNS_COMPRESS_ALL;
ATF_REQUIRE_EQ(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS); assert_int_equal(dns_compress_init(&cctx, -1, mctx), ISC_R_SUCCESS);
dns_compress_setmethods(&cctx, allowed); dns_compress_setmethods(&cctx, allowed);
dns_compress_disable(&cctx); dns_compress_disable(&cctx);
dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT); dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT);
@@ -274,15 +292,11 @@ ATF_TC_BODY(compression, tc) {
dns_compress_rollback(&cctx, 0); dns_compress_rollback(&cctx, 0);
dns_compress_invalidate(&cctx); dns_compress_invalidate(&cctx);
dns_test_end();
} }
ATF_TC(istat); /* is trust-anchor-telemetry test */
ATF_TC_HEAD(istat, tc) { static void
atf_tc_set_md_var(tc, "descr", "is trust-anchor-telemetry test"); istat_test(void **state) {
}
ATF_TC_BODY(istat, tc) {
dns_fixedname_t fixed; dns_fixedname_t fixed;
dns_name_t *name; dns_name_t *name;
isc_result_t result; isc_result_t result;
@@ -315,74 +329,73 @@ ATF_TC_BODY(istat, tc) {
{ "_ta-1234-abcf-EcD89.example", false } { "_ta-1234-abcf-EcD89.example", false }
}; };
UNUSED(state);
name = dns_fixedname_initname(&fixed); name = dns_fixedname_initname(&fixed);
for (i = 0; i < sizeof(data)/sizeof(data[0]); i++) { for (i = 0; i < (sizeof(data) / sizeof(data[0])); i++) {
result = dns_name_fromstring(name, data[i].name, 0, NULL); result = dns_name_fromstring(name, data[i].name, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
ATF_CHECK_EQ_MSG(dns_name_istat(name), data[i].istat, assert_int_equal(dns_name_istat(name), data[i].istat);
"testing %s - expected %u", data[i].name, data[i].istat);
} }
} }
ATF_TC(init); /* dns_nane_init */
ATF_TC_HEAD(init, tc) { static void
atf_tc_set_md_var(tc, "descr", "dns_nane_init"); init_test(void **state) {
}
ATF_TC_BODY(init, tc) {
dns_name_t name; dns_name_t name;
unsigned char offsets[1]; unsigned char offsets[1];
UNUSED(state);
dns_name_init(&name, offsets); dns_name_init(&name, offsets);
ATF_CHECK_EQ(name.ndata, NULL); assert_null(name.ndata);
ATF_CHECK_EQ(name.length, 0); assert_int_equal(name.length, 0);
ATF_CHECK_EQ(name.labels, 0); assert_int_equal(name.labels, 0);
ATF_CHECK_EQ(name.attributes, 0); assert_int_equal(name.attributes, 0);
ATF_CHECK_EQ(name.offsets, offsets); assert_int_equal(name.offsets, offsets);
ATF_CHECK_EQ(name.buffer, NULL); assert_null(name.buffer);
} }
ATF_TC(invalidate); /* dns_nane_invalidate */
ATF_TC_HEAD(invalidate, tc) { static void
atf_tc_set_md_var(tc, "descr", "dns_nane_invalidate"); invalidate_test(void **state) {
}
ATF_TC_BODY(invalidate, tc) {
dns_name_t name; dns_name_t name;
unsigned char offsets[1]; unsigned char offsets[1];
UNUSED(state);
dns_name_init(&name, offsets); dns_name_init(&name, offsets);
dns_name_invalidate(&name); dns_name_invalidate(&name);
ATF_CHECK_EQ(name.ndata, NULL); assert_null(name.ndata);
ATF_CHECK_EQ(name.length, 0); assert_int_equal(name.length, 0);
ATF_CHECK_EQ(name.labels, 0); assert_int_equal(name.labels, 0);
ATF_CHECK_EQ(name.attributes, 0); assert_int_equal(name.attributes, 0);
ATF_CHECK_EQ(name.offsets, NULL); assert_null(name.offsets);
ATF_CHECK_EQ(name.buffer, NULL); assert_null(name.buffer);
} }
ATF_TC(buffer); /* dns_nane_setbuffer/hasbuffer */
ATF_TC_HEAD(buffer, tc) { static void
atf_tc_set_md_var(tc, "descr", "dns_nane_setbuffer/hasbuffer"); buffer_test(void **state) {
}
ATF_TC_BODY(buffer, tc) {
dns_name_t name; dns_name_t name;
unsigned char buf[BUFSIZ]; unsigned char buf[BUFSIZ];
isc_buffer_t b; isc_buffer_t b;
UNUSED(state);
isc_buffer_init(&b, buf, BUFSIZ); isc_buffer_init(&b, buf, BUFSIZ);
dns_name_init(&name, NULL); dns_name_init(&name, NULL);
dns_name_setbuffer(&name, &b); dns_name_setbuffer(&name, &b);
ATF_CHECK_EQ(name.buffer, &b); assert_int_equal(name.buffer, &b);
ATF_CHECK(dns_name_hasbuffer(&name)); assert_true(dns_name_hasbuffer(&name));
} }
ATF_TC(isabsolute); /* dns_nane_isabsolute */
ATF_TC_HEAD(isabsolute, tc) { static void
atf_tc_set_md_var(tc, "descr", "dns_nane_isabsolute"); isabsolute_test(void **state) {
}
ATF_TC_BODY(isabsolute, tc) {
struct { struct {
const char *namestr; const char *namestr;
bool expect; bool expect;
@@ -393,7 +406,9 @@ ATF_TC_BODY(isabsolute, tc) {
}; };
unsigned int i; unsigned int i;
for (i = 0; i < (sizeof(testcases)/sizeof(testcases[0])); i++) { UNUSED(state);
for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
isc_result_t result; isc_result_t result;
dns_name_t name; dns_name_t name;
unsigned char data[BUFSIZ]; unsigned char data[BUFSIZ];
@@ -408,17 +423,16 @@ ATF_TC_BODY(isabsolute, tc) {
isc_buffer_init(&nb, data, BUFSIZ); isc_buffer_init(&nb, data, BUFSIZ);
dns_name_setbuffer(&name, &nb); dns_name_setbuffer(&name, &nb);
result = dns_name_fromtext(&name, &b, NULL, 0, NULL); result = dns_name_fromtext(&name, &b, NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
ATF_CHECK_EQ(dns_name_isabsolute(&name), testcases[i].expect); assert_int_equal(dns_name_isabsolute(&name),
testcases[i].expect);
} }
} }
ATF_TC(hash); /* dns_nane_hash */
ATF_TC_HEAD(hash, tc) { static void
atf_tc_set_md_var(tc, "descr", "dns_nane_hash"); hash_test(void **state) {
}
ATF_TC_BODY(hash, tc) {
struct { struct {
const char *name1; const char *name1;
const char *name2; const char *name2;
@@ -434,7 +448,9 @@ ATF_TC_BODY(hash, tc) {
}; };
unsigned int i; unsigned int i;
for (i = 0; i < (sizeof(testcases)/sizeof(testcases[0])); i++) { UNUSED(state);
for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
isc_result_t result; isc_result_t result;
dns_fixedname_t f1, f2; dns_fixedname_t f1, f2;
dns_name_t *n1, *n2; dns_name_t *n1, *n2;
@@ -445,10 +461,10 @@ ATF_TC_BODY(hash, tc) {
result = dns_name_fromstring2(n1, testcases[i].name1, result = dns_name_fromstring2(n1, testcases[i].name1,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
result = dns_name_fromstring2(n2, testcases[i].name2, result = dns_name_fromstring2(n2, testcases[i].name2,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
/* Check case-insensitive hashing first */ /* Check case-insensitive hashing first */
h1 = dns_name_hash(n1, false); h1 = dns_name_hash(n1, false);
@@ -457,7 +473,7 @@ ATF_TC_BODY(hash, tc) {
printf("%s hashes to %u, %s to %u, case insensitive\n", printf("%s hashes to %u, %s to %u, case insensitive\n",
testcases[i].name1, h1, testcases[i].name2, h2); testcases[i].name1, h1, testcases[i].name2, h2);
ATF_REQUIRE_EQ((h1 == h2), testcases[i].expect); assert_int_equal((h1 == h2), testcases[i].expect);
/* Now case-sensitive */ /* Now case-sensitive */
h1 = dns_name_hash(n1, false); h1 = dns_name_hash(n1, false);
@@ -466,15 +482,13 @@ ATF_TC_BODY(hash, tc) {
printf("%s hashes to %u, %s to %u, case sensitive\n", printf("%s hashes to %u, %s to %u, case sensitive\n",
testcases[i].name1, h1, testcases[i].name2, h2); testcases[i].name1, h1, testcases[i].name2, h2);
ATF_REQUIRE_EQ((h1 == h2), testcases[i].expect); assert_int_equal((h1 == h2), testcases[i].expect);
} }
} }
ATF_TC(issubdomain); /* dns_nane_issubdomain */
ATF_TC_HEAD(issubdomain, tc) { static void
atf_tc_set_md_var(tc, "descr", "dns_nane_issubdomain"); issubdomain_test(void **state) {
}
ATF_TC_BODY(issubdomain, tc) {
struct { struct {
const char *name1; const char *name1;
const char *name2; const char *name2;
@@ -490,7 +504,9 @@ ATF_TC_BODY(issubdomain, tc) {
}; };
unsigned int i; unsigned int i;
for (i = 0; i < (sizeof(testcases)/sizeof(testcases[0])); i++) { UNUSED(state);
for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
isc_result_t result; isc_result_t result;
dns_fixedname_t f1, f2; dns_fixedname_t f1, f2;
dns_name_t *n1, *n2; dns_name_t *n1, *n2;
@@ -500,26 +516,24 @@ ATF_TC_BODY(issubdomain, tc) {
result = dns_name_fromstring2(n1, testcases[i].name1, result = dns_name_fromstring2(n1, testcases[i].name1,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
result = dns_name_fromstring2(n2, testcases[i].name2, result = dns_name_fromstring2(n2, testcases[i].name2,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
printf("check: %s %s a subdomain of %s\n", printf("check: %s %s a subdomain of %s\n",
testcases[i].name1, testcases[i].name1,
testcases[i].expect ? "is" : "is not", testcases[i].expect ? "is" : "is not",
testcases[i].name2); testcases[i].name2);
ATF_CHECK_EQ(dns_name_issubdomain(n1, n2), assert_int_equal(dns_name_issubdomain(n1, n2),
testcases[i].expect); testcases[i].expect);
} }
} }
ATF_TC(countlabels); /* dns_nane_countlabels */
ATF_TC_HEAD(countlabels, tc) { static void
atf_tc_set_md_var(tc, "descr", "dns_nane_countlabels"); countlabels_test(void **state) {
}
ATF_TC_BODY(countlabels, tc) {
struct { struct {
const char *namestr; const char *namestr;
unsigned int expect; unsigned int expect;
@@ -533,7 +547,9 @@ ATF_TC_BODY(countlabels, tc) {
}; };
unsigned int i; unsigned int i;
for (i = 0; i < (sizeof(testcases)/sizeof(testcases[0])); i++) { UNUSED(state);
for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
isc_result_t result; isc_result_t result;
dns_fixedname_t fname; dns_fixedname_t fname;
dns_name_t *name; dns_name_t *name;
@@ -542,21 +558,19 @@ ATF_TC_BODY(countlabels, tc) {
result = dns_name_fromstring2(name, testcases[i].namestr, result = dns_name_fromstring2(name, testcases[i].namestr,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
printf("%s: expect %u labels\n", printf("%s: expect %u labels\n",
testcases[i].namestr, testcases[i].expect); testcases[i].namestr, testcases[i].expect);
ATF_REQUIRE_EQ(dns_name_countlabels(name), assert_int_equal(dns_name_countlabels(name),
testcases[i].expect); testcases[i].expect);
} }
} }
ATF_TC(getlabel); /* dns_nane_getlabel */
ATF_TC_HEAD(getlabel, tc) { static void
atf_tc_set_md_var(tc, "descr", "dns_nane_getlabel"); getlabel_test(void **state) {
}
ATF_TC_BODY(getlabel, tc) {
struct { struct {
const char *name1; const char *name1;
unsigned int pos1; unsigned int pos1;
@@ -569,7 +583,9 @@ ATF_TC_BODY(getlabel, tc) {
}; };
unsigned int i; unsigned int i;
for (i = 0; i < (sizeof(testcases)/sizeof(testcases[0])); i++) { UNUSED(state);
for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
isc_result_t result; isc_result_t result;
dns_fixedname_t f1, f2; dns_fixedname_t f1, f2;
dns_name_t *n1, *n2; dns_name_t *n1, *n2;
@@ -582,28 +598,26 @@ ATF_TC_BODY(getlabel, tc) {
result = dns_name_fromstring2(n1, testcases[i].name1, result = dns_name_fromstring2(n1, testcases[i].name1,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
result = dns_name_fromstring2(n2, testcases[i].name2, result = dns_name_fromstring2(n2, testcases[i].name2,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
dns_name_getlabel(n1, testcases[i].pos1, &l1); dns_name_getlabel(n1, testcases[i].pos1, &l1);
dns_name_getlabel(n2, testcases[i].pos2, &l2); dns_name_getlabel(n2, testcases[i].pos2, &l2);
ATF_CHECK_EQ(l1.length, l2.length); assert_int_equal(l1.length, l2.length);
p1 = l1.base; p1 = l1.base;
p2 = l2.base; p2 = l2.base;
for (j = 0; j < l1.length; j++) { for (j = 0; j < l1.length; j++) {
ATF_REQUIRE_EQ(*p1++, *p2++); assert_int_equal(*p1++, *p2++);
} }
} }
} }
ATF_TC(getlabelsequence); /* dns_nane_getlabelsequence */
ATF_TC_HEAD(getlabelsequence, tc) { static void
atf_tc_set_md_var(tc, "descr", "dns_nane_getlabelsequence"); getlabelsequence_test(void **state) {
}
ATF_TC_BODY(getlabelsequence, tc) {
struct { struct {
const char *name1; const char *name1;
unsigned int pos1; unsigned int pos1;
@@ -618,7 +632,9 @@ ATF_TC_BODY(getlabelsequence, tc) {
}; };
unsigned int i; unsigned int i;
for (i = 0; i < (sizeof(testcases)/sizeof(testcases[0])); i++) { UNUSED(state);
for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
isc_result_t result; isc_result_t result;
dns_name_t t1, t2; dns_name_t t1, t2;
dns_fixedname_t f1, f2; dns_fixedname_t f1, f2;
@@ -634,17 +650,17 @@ ATF_TC_BODY(getlabelsequence, tc) {
result = dns_name_fromstring2(n1, testcases[i].name1, result = dns_name_fromstring2(n1, testcases[i].name1,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
result = dns_name_fromstring2(n2, testcases[i].name2, result = dns_name_fromstring2(n2, testcases[i].name2,
NULL, 0, NULL); NULL, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
dns_name_getlabelsequence(n1, testcases[i].pos1, dns_name_getlabelsequence(n1, testcases[i].pos1,
testcases[i].range, &t1); testcases[i].range, &t1);
dns_name_getlabelsequence(n2, testcases[i].pos2, dns_name_getlabelsequence(n2, testcases[i].pos2,
testcases[i].range, &t2); testcases[i].range, &t2);
ATF_REQUIRE(dns_name_equal(&t1, &t2)); assert_true(dns_name_equal(&t1, &t2));
} }
} }
@@ -655,11 +671,7 @@ ATF_TC_BODY(getlabelsequence, tc) {
* name parser, but we don't require it as part of the unit test runs. * name parser, but we don't require it as part of the unit test runs.
*/ */
ATF_TC(benchmark); /* Benchmark dns_name_fromwire() implementation */
ATF_TC_HEAD(benchmark, tc) {
atf_tc_set_md_var(tc, "descr",
"Benchmark dns_name_fromwire() implementation");
}
static void * static void *
fromwire_thread(void *arg) { fromwire_thread(void *arg) {
@@ -700,7 +712,8 @@ fromwire_thread(void *arg) {
return (NULL); return (NULL);
} }
ATF_TC_BODY(benchmark, tc) { static void
benchmark_test(void **state) {
isc_result_t result; isc_result_t result;
unsigned int i; unsigned int i;
isc_time_t ts1, ts2; isc_time_t ts1, ts2;
@@ -708,61 +721,70 @@ ATF_TC_BODY(benchmark, tc) {
unsigned int nthreads; unsigned int nthreads;
isc_thread_t threads[32]; isc_thread_t threads[32];
UNUSED(tc); UNUSED(state);
debug_mem_record = false; debug_mem_record = false;
result = dns_test_begin(NULL, true);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = isc_time_now(&ts1); result = isc_time_now(&ts1);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
nthreads = ISC_MIN(isc_os_ncpus(), 32); nthreads = ISC_MIN(isc_os_ncpus(), 32);
nthreads = ISC_MAX(nthreads, 1); nthreads = ISC_MAX(nthreads, 1);
for (i = 0; i < nthreads; i++) { for (i = 0; i < nthreads; i++) {
result = isc_thread_create(fromwire_thread, NULL, &threads[i]); result = isc_thread_create(fromwire_thread, NULL, &threads[i]);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
} }
for (i = 0; i < nthreads; i++) { for (i = 0; i < nthreads; i++) {
result = isc_thread_join(threads[i], NULL); result = isc_thread_join(threads[i], NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
} }
result = isc_time_now(&ts2); result = isc_time_now(&ts2);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
t = isc_time_microdiff(&ts2, &ts1); t = isc_time_microdiff(&ts2, &ts1);
printf("%u dns_name_fromwire() calls, %f seconds, %f calls/second\n", printf("%u dns_name_fromwire() calls, %f seconds, %f calls/second\n",
nthreads * 32000000, t / 1000000.0, nthreads * 32000000, t / 1000000.0,
(nthreads * 32000000) / (t / 1000000.0)); (nthreads * 32000000) / (t / 1000000.0));
dns_test_end();
} }
#endif /* DNS_BENCHMARK_TESTS */ #endif /* DNS_BENCHMARK_TESTS */
/* int
* Main main(void) {
*/ const struct CMUnitTest tests[] = {
ATF_TP_ADD_TCS(tp) { cmocka_unit_test(fullcompare_test),
ATF_TP_ADD_TC(tp, fullcompare); cmocka_unit_test_setup_teardown(compression_test,
ATF_TP_ADD_TC(tp, compression); _setup, _teardown),
ATF_TP_ADD_TC(tp, istat); cmocka_unit_test(istat_test),
ATF_TP_ADD_TC(tp, init); cmocka_unit_test(init_test),
ATF_TP_ADD_TC(tp, invalidate); cmocka_unit_test(invalidate_test),
ATF_TP_ADD_TC(tp, buffer); cmocka_unit_test(buffer_test),
ATF_TP_ADD_TC(tp, isabsolute); cmocka_unit_test(isabsolute_test),
ATF_TP_ADD_TC(tp, hash); cmocka_unit_test(hash_test),
ATF_TP_ADD_TC(tp, issubdomain); cmocka_unit_test(issubdomain_test),
ATF_TP_ADD_TC(tp, countlabels); cmocka_unit_test(countlabels_test),
ATF_TP_ADD_TC(tp, getlabel); cmocka_unit_test(getlabel_test),
ATF_TP_ADD_TC(tp, getlabelsequence); cmocka_unit_test(getlabelsequence_test),
#ifdef DNS_BENCHMARK_TESTS #ifdef DNS_BENCHMARK_TESTS
ATF_TP_ADD_TC(tp, benchmark); cmocka_unit_test_setup_teardown(benchmark_test,
_setup, _teardown),
#endif /* DNS_BENCHMARK_TESTS */ #endif /* DNS_BENCHMARK_TESTS */
};
return (atf_no_error()); return (cmocka_run_group_tests(tests, NULL, NULL));
} }
#else /* HAVE_CMOCKA */
#include <stdio.h>
int
main(void) {
printf("1..0 # Skipped: cmocka not available\n");
return (0);
}
#endif