Author: mmichelson
Date: Wed Mar  4 17:01:59 2015
New Revision: 432476

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=432476
Log:
Refine DNS record-adding tests.

Now we have separate nominal and off-nominal tests for adding
records to results. In the nominal tests, we now also check the
data stored on the DNS query result to make sure it is what we
expect.


Modified:
    team/group/dns/tests/test_dns.c

Modified: team/group/dns/tests/test_dns.c
URL: 
http://svnview.digium.com/svn/asterisk/team/group/dns/tests/test_dns.c?view=diff&rev=432476&r1=432475&r2=432476
==============================================================================
--- team/group/dns/tests/test_dns.c (original)
+++ team/group/dns/tests/test_dns.c Wed Mar  4 17:01:59 2015
@@ -273,91 +273,9 @@
        return AST_TEST_PASS;
 }
 
-AST_TEST_DEFINE(resolver_add_record)
-{
-       struct ast_dns_query some_query;
-       static const char *LOCAL_ADDR = "127.0.0.1";
-       static const size_t bufsize = sizeof(struct in_addr);
-       char buf[bufsize];
-
-       switch (cmd) {
-       case TEST_INIT:
-               info->name = "resolver_add_record";
-               info->category = "/main/dns/";
-               info->summary = "Test adding DNS records to a query";
-               info->description =
-                       "This test performs the following:\n"
-                       "\t* Ensure a nominal A record can be added to a 
query\n"
-                       "\t* Ensure that an A record with invalid RR types 
cannot be added to a query\n"
-                       "\t* Ensure that an A record with invalid RR classes 
cannot be added to a query\n"
-                       "\t* Ensure that an A record with invalid TTL cannot be 
added to a query\n"
-                       "\t* Ensure that an A record with NULL data cannot be 
added to a query\n"
-                       "\t* Ensure that an A record with invalid length cannot 
be added to a query\n";
-               return AST_TEST_NOT_RUN;
-       case TEST_EXECUTE:
-               break;
-       }
-
-       memset(&some_query, 0, sizeof(some_query));
-
-       inet_ntop(AF_INET, LOCAL_ADDR, buf, bufsize);
-
-       /* Nominal Record */
-       if (ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, 12345, 
buf, bufsize)) {
-               ast_test_status_update(test, "Unable to add nominal record to 
query\n");
-               return AST_TEST_FAIL;
-       }
-
-       /* Invalid RR types */
-       if (!ast_dns_resolver_add_record(&some_query, -1, ns_c_in, 12345, buf, 
bufsize)) {
-               ast_test_status_update(test, "Successfully added DNS record 
with negative RR type\n");
-               return AST_TEST_FAIL;
-       }
-       
-       if (!ast_dns_resolver_add_record(&some_query, ns_t_max + 1, ns_c_in, 
12345, buf, bufsize)) {
-               ast_test_status_update(test, "Successfully added DNS record 
with too large RR type\n");
-               return AST_TEST_FAIL;
-       }
-
-       /* Invalid RR classes */
-       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, -1, 12345, buf, 
bufsize)) {
-               ast_test_status_update(test, "Successfully added DNS record 
with negative RR class\n");
-               return AST_TEST_FAIL;
-       }
-
-       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_max + 1, 
12345, buf, bufsize)) {
-               ast_test_status_update(test, "Successfully added DNS record 
with too large RR class\n");
-               return AST_TEST_FAIL;
-       }
-
-       /* Invalid TTL */
-       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, -1, buf, 
bufsize)) {
-               ast_test_status_update(test, "Successfully added DNS record 
with negative TTL\n");
-               return AST_TEST_FAIL;
-       }
-
-       /* No data */
-       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, 12345, 
NULL, 0)) {
-               ast_test_status_update(test, "Successfully added a DNS record 
with no data\n");
-               return AST_TEST_FAIL;
-       }
-
-       /* Lie about the length */
-       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, 12345, 
buf, 0)) {
-               ast_test_status_update(test, "Successfully added a DNS record 
with length zero\n");
-               return AST_TEST_FAIL;
-       }
-       
-       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, 12345, 
buf, bufsize * 3)) {
-               ast_test_status_update(test, "Successfully added a DNS record 
with overly-large length\n");
-               return AST_TEST_FAIL;
-       }
-
-       return AST_TEST_PASS;
-}
-
 static int test_results(struct ast_test *test, const struct ast_dns_query 
*query,
-               int expected_nxdomain, int expected_secure, int expected_bogus)
+               int expected_nxdomain, int expected_secure, int expected_bogus,
+               const char *expected_canonical)
 {
        struct ast_dns_result *result;
 
@@ -369,7 +287,8 @@
 
        if (ast_dns_result_get_nxdomain(result) != expected_nxdomain ||
                        ast_dns_result_get_secure(result) != expected_secure ||
-                       ast_dns_result_get_bogus(result) != expected_bogus) {
+                       ast_dns_result_get_bogus(result) != expected_bogus ||
+                       strcmp(ast_dns_result_get_canonical(result), 
expected_canonical)) {
                ast_test_status_update(test, "Unexpected values in result from 
query\n");
                return -1;
        }
@@ -406,7 +325,7 @@
                return AST_TEST_FAIL;
        }
 
-       if (test_results(test, &some_query, 0, 0, 0)) {
+       if (test_results(test, &some_query, 0, 0, 0, "asterisk.org")) {
                return AST_TEST_FAIL;
        }
 
@@ -415,7 +334,7 @@
                return AST_TEST_FAIL;
        }
 
-       if (test_results(test, &some_query, 0, 0, 1)) {
+       if (test_results(test, &some_query, 0, 0, 1, "asterisk.org")) {
                return AST_TEST_FAIL;
        }
 
@@ -424,7 +343,7 @@
                return AST_TEST_FAIL;
        }
 
-       if (test_results(test, &some_query, 0, 1, 0)) {
+       if (test_results(test, &some_query, 0, 1, 0, "asterisk.org")) {
                return AST_TEST_FAIL;
        }
 
@@ -433,7 +352,7 @@
                return AST_TEST_FAIL;
        }
 
-       if (test_results(test, &some_query, 1, 0, 0)) {
+       if (test_results(test, &some_query, 1, 0, 0, "asterisk.org")) {
                return AST_TEST_FAIL;
        }
 
@@ -472,15 +391,225 @@
        return AST_TEST_PASS;
 }
 
+static int test_record(struct ast_test *test, const struct ast_dns_record 
*record,
+               int rr_type, int rr_class, int ttl, const char *data, const 
size_t size)
+{
+       if (ast_dns_record_get_rr_type(record) != rr_type) {
+               ast_test_status_update(test, "Unexpected rr_type from DNS 
record\n");
+               return -1;
+       }
+
+       if (ast_dns_record_get_rr_class(record) != rr_class) {
+               ast_test_status_update(test, "Unexpected rr_class from DNS 
record\n");
+               return -1;
+       }
+
+       if (ast_dns_record_get_ttl(record) != ttl) {
+               ast_test_status_update(test, "Unexpected ttl from DNS 
record\n");
+               return -1;
+       }
+
+       if (memcmp(ast_dns_record_get_data(record), data, size)) {
+               ast_test_status_update(test, "Unexpected data in DNS record\n");
+               return -1;
+       }
+
+       return 0;
+}
+
+AST_TEST_DEFINE(resolver_add_record)
+{
+       struct ast_dns_query some_query;
+       struct ast_dns_result *result;
+       const struct ast_dns_record *record;
+
+       static const char *V4 = "127.0.0.1";
+       static const size_t V4_BUFSIZE = sizeof(struct in_addr);
+       char v4_buf[V4_BUFSIZE];
+
+       static const char *V6 = "::1";
+       static const size_t V6_BUFSIZE = sizeof(struct in6_addr);
+       char v6_buf[V6_BUFSIZE];
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "resolver_add_record";
+               info->category = "/main/dns/";
+               info->summary = "Test adding DNS records to a query";
+               info->description =
+                       "This test performs the following:\n"
+                       "\t* Ensure a nominal A record can be added to a query 
result\n"
+                       "\t* Ensures that the record can be retrieved\n"
+                       "\t* Ensure that a second record can be added to the 
query result\n"
+                       "\t* Ensures that both records can be retrieved\n";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       memset(&some_query, 0, sizeof(some_query));
+
+       if (ast_dns_resolver_set_result(&some_query, 0, 0, 0, "asterisk.org")) {
+               ast_test_status_update(test, "Unable to set result for DNS 
query\n");
+               return AST_TEST_FAIL;
+       }
+
+       inet_pton(AF_INET, V4, v4_buf);
+
+       /* Nominal Record */
+       if (ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, 12345, 
v4_buf, V4_BUFSIZE)) {
+               ast_test_status_update(test, "Unable to add nominal record to 
query result\n");
+               return AST_TEST_FAIL;
+       }
+
+       /* I should only be able to retrieve one record */
+       result = ast_dns_query_get_result(&some_query);
+       if (!result) {
+               ast_test_status_update(test, "Unable to retrieve result from 
query\n");
+               return AST_TEST_FAIL;
+       }
+
+       record = ast_dns_result_get_records(result);
+       if (!record) {
+               ast_test_status_update(test, "Unable to retrieve record from 
result\n");
+               return AST_TEST_FAIL;
+       }
+
+       if (test_record(test, record, ns_t_a, ns_c_in, 12345, v4_buf, 
V4_BUFSIZE)) {
+               return AST_TEST_FAIL;
+       }
+
+       inet_pton(AF_INET6, V6, v6_buf);
+
+       if (ast_dns_resolver_add_record(&some_query, ns_t_aaaa, ns_c_in, 12345, 
v6_buf, V6_BUFSIZE)) {
+               ast_test_status_update(test, "Unable to add second record to 
query result\n");
+               return AST_TEST_FAIL;
+       }
+
+       for (record = ast_dns_result_get_records(result); record; record = 
ast_dns_record_get_next(record)) {
+               /* The order of returned records is not specified. We use the 
record type as the discriminator
+                * to determine which record we expect
+                *
+                * XXX There currently is no guarantee that both records will 
be visited by this loop, and there
+                * is no check that only two records are visited.
+                */
+               int res;
+
+               if (ast_dns_record_get_rr_type(record) == ns_t_a) {
+                       res = test_record(test, record, ns_t_a, ns_c_in, 12345, 
v4_buf, V4_BUFSIZE);
+               } else if (ast_dns_record_get_rr_type(record) == ns_t_aaaa) {
+                       res = test_record(test, record, ns_t_aaaa, ns_c_in, 
12345, v6_buf, V6_BUFSIZE);
+               } else {
+                       ast_test_status_update(test, "Unknown record type found 
in DNS results\n");
+                       return AST_TEST_FAIL;
+               }
+
+               if (res) {
+                       return AST_TEST_FAIL;
+               }
+       }
+
+       return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(resolver_add_record_off_nominal)
+{
+       struct ast_dns_query some_query;
+       static const char *V4 = "127.0.0.1";
+       static const size_t V4_BUFSIZE = sizeof(struct in_addr);
+       char v4_buf[V4_BUFSIZE];
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "resolver_add_record_off_nominal";
+               info->category = "/main/dns/";
+               info->summary = "Test adding off-nominal DNS records to a 
query";
+               info->description =
+                       "This test performs the following:\n"
+                       "\t* Ensure a nominal A record cannot be added if no 
result has been set.\n"
+                       "\t* Ensure that an A record with invalid RR types 
cannot be added to a query\n"
+                       "\t* Ensure that an A record with invalid RR classes 
cannot be added to a query\n"
+                       "\t* Ensure that an A record with invalid TTL cannot be 
added to a query\n"
+                       "\t* Ensure that an A record with NULL data cannot be 
added to a query\n"
+                       "\t* Ensure that an A record with invalid length cannot 
be added to a query\n";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       memset(&some_query, 0, sizeof(some_query));
+
+       inet_ntop(AF_INET, V4, v4_buf, V4_BUFSIZE);
+
+       /* Add record before setting result */
+       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, 12345, 
v4_buf, V4_BUFSIZE)) {
+               ast_test_status_update(test, "Successfully added DNS record to 
query before setting a result\n");
+               return AST_TEST_FAIL;
+       }
+
+       if (ast_dns_resolver_set_result(&some_query, 0, 0, 0, "asterisk.org")) {
+               ast_test_status_update(test, "Unable to set result for DNS 
query\n");
+               return AST_TEST_FAIL;
+       }
+
+       /* Invalid RR types */
+       if (!ast_dns_resolver_add_record(&some_query, -1, ns_c_in, 12345, 
v4_buf, V4_BUFSIZE)) {
+               ast_test_status_update(test, "Successfully added DNS record 
with negative RR type\n");
+               return AST_TEST_FAIL;
+       }
+       
+       if (!ast_dns_resolver_add_record(&some_query, ns_t_max + 1, ns_c_in, 
12345, v4_buf, V4_BUFSIZE)) {
+               ast_test_status_update(test, "Successfully added DNS record 
with too large RR type\n");
+               return AST_TEST_FAIL;
+       }
+
+       /* Invalid RR classes */
+       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, -1, 12345, 
v4_buf, V4_BUFSIZE)) {
+               ast_test_status_update(test, "Successfully added DNS record 
with negative RR class\n");
+               return AST_TEST_FAIL;
+       }
+
+       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_max + 1, 
12345, v4_buf, V4_BUFSIZE)) {
+               ast_test_status_update(test, "Successfully added DNS record 
with too large RR class\n");
+               return AST_TEST_FAIL;
+       }
+
+       /* Invalid TTL */
+       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, -1, 
v4_buf, V4_BUFSIZE)) {
+               ast_test_status_update(test, "Successfully added DNS record 
with negative TTL\n");
+               return AST_TEST_FAIL;
+       }
+
+       /* No data */
+       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, 12345, 
NULL, 0)) {
+               ast_test_status_update(test, "Successfully added a DNS record 
with no data\n");
+               return AST_TEST_FAIL;
+       }
+
+       /* Lie about the length */
+       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, 12345, 
v4_buf, 0)) {
+               ast_test_status_update(test, "Successfully added a DNS record 
with length zero\n");
+               return AST_TEST_FAIL;
+       }
+       
+       if (!ast_dns_resolver_add_record(&some_query, ns_t_a, ns_c_in, 12345, 
v4_buf, V4_BUFSIZE * 3)) {
+               ast_test_status_update(test, "Successfully added a DNS record 
with overly-large length\n");
+               return AST_TEST_FAIL;
+       }
+
+       return AST_TEST_PASS;
+}
+
 static int unload_module(void)
 {
        AST_TEST_UNREGISTER(resolver_register_unregister);
        AST_TEST_UNREGISTER(resolver_register_off_nominal);
        AST_TEST_UNREGISTER(resolver_unregister_off_nominal);
        AST_TEST_UNREGISTER(resolver_data);
-       AST_TEST_UNREGISTER(resolver_add_record);
        AST_TEST_UNREGISTER(resolver_set_result);
        AST_TEST_UNREGISTER(resolver_set_result_off_nominal);
+       AST_TEST_UNREGISTER(resolver_add_record);
+       AST_TEST_REGISTER(resolver_add_record_off_nominal);
 
        return 0;
 }
@@ -491,9 +620,10 @@
        AST_TEST_REGISTER(resolver_register_off_nominal);
        AST_TEST_REGISTER(resolver_unregister_off_nominal);
        AST_TEST_REGISTER(resolver_data);
-       AST_TEST_REGISTER(resolver_add_record);
        AST_TEST_REGISTER(resolver_set_result);
        AST_TEST_REGISTER(resolver_set_result_off_nominal);
+       AST_TEST_REGISTER(resolver_add_record);
+       AST_TEST_REGISTER(resolver_add_record_off_nominal);
 
        return AST_MODULE_LOAD_SUCCESS;
 }


-- 
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --

svn-commits mailing list
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/svn-commits

Reply via email to