summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMark Michelson <mmichelson@digium.com>2015-04-09 14:58:02 +0000
committerMark Michelson <mmichelson@digium.com>2015-04-09 14:58:02 +0000
commitc08ebc6eeb4d68609d2b7ba3e7eb97e66a24da00 (patch)
treec878c52e2b28f776466183cfba4470966c94ff2f /tests
parentea0098724efbd64a06b3103d19bb5f711f6f3cd7 (diff)
Reduce duplication of common DNS code.
The NAPTR and SRV branches were worked on independently and resulted in some code being duplicated in each. Since both have been merged into trunk now, this patch reduces the duplication by factoring out common code into its own source files. git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@434490 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'tests')
-rw-r--r--tests/test_dns_naptr.c249
-rw-r--r--tests/test_dns_srv.c130
2 files changed, 29 insertions, 350 deletions
diff --git a/tests/test_dns_naptr.c b/tests/test_dns_naptr.c
index 1aaac674b..c7c7eb364 100644
--- a/tests/test_dns_naptr.c
+++ b/tests/test_dns_naptr.c
@@ -30,217 +30,15 @@
#include "asterisk/dns_core.h"
#include "asterisk/dns_resolver.h"
#include "asterisk/dns_naptr.h"
-
-#define DNS_HEADER_SIZE 96
-
-const char DNS_HEADER[] = {
- /* ID == 0 */
- 0x00, 0x00,
- /* QR == 1, Opcode == 0, AA == 1, TC == 0, RD == 1 */
- 0x85,
- /* RA == 1, Z == 0, RCODE == 0 */
- 0x80,
- /* QDCOUNT == 1 */
- 0x00, 0x01,
- /* ANCOUNT == 1 */
- 0x00, 0x00,
- /* NSCOUNT == 0 */
- 0x00, 0x00,
- /* ARCOUNT == 0 */
- 0x00, 0x00,
-};
-
-/*!
- * \brief Generate a DNS header and write it to a buffer
- *
- * The DNS header is the first part of a DNS request or response. In our
- * case, the only part of the header that a test can affect is the number
- * of answers. The rest of the DNS header is based on hard-coded values.
- *
- * There is no buffer size passed to this function since we provide
- * the data ourselves and have sized the buffer to be way larger
- * than necessary for the tests.
- *
- * \param num_records The number of DNS records in this DNS response
- * \param buf The buffer to write the header into
- * \retval The number of bytes written to the buffer
- */
-static int generate_dns_header(unsigned short num_records, char *buf)
-{
- unsigned short net_num_records = htons(num_records);
-
- memcpy(buf, DNS_HEADER, ARRAY_LEN(DNS_HEADER));
- /* Overwrite the ANCOUNT with the actual number of answers */
- memcpy(&buf[6], &net_num_records, sizeof(num_records));
-
- return ARRAY_LEN(DNS_HEADER);
-}
-
-const char DNS_QUESTION [] = {
- /* goose */
- 0x05, 0x67, 0x6f, 0x6f, 0x73, 0x65,
- /* feathers */
- 0x08, 0x66, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x73,
- /* end label */
- 0x00,
- /* NAPTR type */
- 0x00, 0x23,
- /* IN class */
- 0x00, 0x01,
-};
-
-/*!
- * \brief Generate a DNS question and write it to a buffer
- *
- * The DNS question is the second part of a DNS request or response.
- * All DNS questions in this file are for the same domain and thus
- * the DNS question is a hard-coded value.
- *
- * There is no buffer size passed to this function since we provide
- * the data ourselves and have sized the buffer to be way larger
- * than necessary for the tests.
- *
- * \param buf The buffer to write the question into
- * \retval The number of bytes written to the buffer
- */
-static int generate_dns_question(char *buf)
-{
- memcpy(buf, DNS_QUESTION, ARRAY_LEN(DNS_QUESTION));
- return ARRAY_LEN(DNS_QUESTION);
-}
-
-const char NAPTR_ANSWER [] = {
- /* Domain points to name from question */
- 0xc0, 0x0c,
- /* NAPTR type */
- 0x00, 0x23,
- /* IN Class */
- 0x00, 0x01,
- /* TTL (12345 by default) */
- 0x00, 0x00, 0x30, 0x39,
-};
-
-/*!
- * \brief Generate a DNS answer and write it to a buffer
- *
- * The DNS answer is the third (and in our case final) part of a
- * DNS response. The DNS answer generated here is only partial.
- * The record-specific data is generated by a separate function.
- * DNS answers in our tests may have variable TTLs, but the rest
- * is hard-coded.
- *
- * There is no buffer size passed to this function since we provide
- * the data ourselves and have sized the buffer to be way larger
- * than necessary for the tests.
- *
- * \param buf The buffer to write the answer into
- * \retval The number of bytes written to the buffer
- */
-static int generate_dns_answer(int ttl, char *buf)
-{
- int net_ttl = htonl(ttl);
-
- memcpy(buf, NAPTR_ANSWER, ARRAY_LEN(NAPTR_ANSWER));
- /* Overwrite TTL if one is provided */
- if (ttl) {
- memcpy(&buf[6], &net_ttl, sizeof(int));
- }
-
- return ARRAY_LEN(NAPTR_ANSWER);
-}
-
-/*!
- * \brief Representation of a string in DNS
- *
- * In DNS, a string has a byte to indicate the length,
- * followed by a series of bytes representing the string.
- * DNS does not NULL-terminate its strings.
- */
-struct dns_string {
- uint8_t len;
- const char *val;
-};
-
-/*!
- * \brief Write a DNS string to a buffer
- *
- * This writes the DNS string to the buffer and returns the total
- * number of bytes written to the buffer.
- *
- * There is no buffer size passed to this function since we provide
- * the data ourselves and have sized the buffer to be way larger
- * than necessary for the tests.
- *
- * \param string The string to write
- * \param buf The buffer to write the string into
- * \return The number of bytes written to the buffer
- */
-static int write_dns_string(const struct dns_string *string, char *buf)
-{
- uint8_t len = string->len;
- buf[0] = len;
- /*
- * We use the actual length of the string instead of
- * the stated value since sometimes we're going to lie about
- * the length of the string
- */
- if (strlen(string->val)) {
- memcpy(&buf[1], string->val, strlen(string->val));
- }
-
- return strlen(string->val) + 1;
-}
-
-/*!
- * \brief Write a DNS domain to a buffer
- *
- * A DNS domain consists of a series of labels separated
- * by dots. Each of these labels gets written as a DNS
- * string. A DNS domain ends with a NULL label, which is
- * essentially a zero-length DNS string.
- *
- *
- * There is no buffer size passed to this function since we provide
- * the data ourselves and have sized the buffer to be way larger
- * than necessary for the tests.
- *
- * \param string The DNS domain to write
- * \param buf The buffer to write the domain into
- * \return The number of bytes written to the buffer
- */
-static int write_dns_domain(const char *string, char *buf)
-{
- char *copy = ast_strdupa(string);
- char *part;
- char *ptr = buf;
- static const struct dns_string null_label = {
- .len = 0,
- .val = "",
- };
-
- while (1) {
- struct dns_string dns_str;
- part = strsep(&copy, ".");
- if (ast_strlen_zero(part)) {
- break;
- }
- dns_str.len = strlen(part);
- dns_str.val = part;
-
- ptr += write_dns_string(&dns_str, ptr);
- }
- ptr += write_dns_string(&null_label, ptr);
-
- return ptr - buf;
-}
+#include "asterisk/dns_test.h"
struct naptr_record {
uint16_t order;
uint16_t preference;
- struct dns_string flags;
- struct dns_string services;
- struct dns_string regexp;
- const char * replacement;
+ struct ast_dns_test_string flags;
+ struct ast_dns_test_string services;
+ struct ast_dns_test_string regexp;
+ const char *replacement;
};
/*!
@@ -257,8 +55,9 @@ struct naptr_record {
* \param buf The buffer to write the record into
* \return The number of bytes written to the buffer
*/
-static int generate_naptr_record(struct naptr_record *record, char *buf)
+static int generate_naptr_record(void *dns_record, char *buf)
{
+ struct naptr_record *record = dns_record;
uint16_t net_order = htons(record->order);
uint16_t net_preference = htons(record->preference);
char *ptr = buf;
@@ -269,10 +68,10 @@ static int generate_naptr_record(struct naptr_record *record, char *buf)
memcpy(ptr, &net_preference, sizeof(net_preference));
ptr += sizeof(net_preference);
- ptr += write_dns_string(&record->flags, ptr);
- ptr += write_dns_string(&record->services, ptr);
- ptr += write_dns_string(&record->regexp, ptr);
- ptr += write_dns_domain(record->replacement, ptr);
+ ptr += ast_dns_test_write_string(&record->flags, ptr);
+ ptr += ast_dns_test_write_string(&record->services, ptr);
+ ptr += ast_dns_test_write_string(&record->regexp, ptr);
+ ptr += ast_dns_test_write_domain(record->replacement, ptr);
return ptr - buf;
}
@@ -312,31 +111,19 @@ static void *naptr_thread(void *dns_query)
{
struct ast_dns_query *query = dns_query;
int i;
- char *ptr = ans_buffer;
-
- ptr += generate_dns_header(num_test_records, ptr);
- ptr += generate_dns_question(ptr);
+ int ans_size;
- for (i = 0; i < num_test_records; ++i) {
- unsigned short rdlength;
- unsigned short net_rdlength;
-
- ptr += generate_dns_answer(0, ptr);
- rdlength = generate_naptr_record(&test_records[i], ptr + 2);
- net_rdlength = htons(rdlength);
- memcpy(ptr, &net_rdlength, 2);
- ptr += 2;
- ptr += rdlength;
- }
+ ans_size = ast_dns_test_generate_result(query, test_records, num_test_records,
+ sizeof(struct naptr_record), generate_naptr_record, ans_buffer);
- ast_dns_resolver_set_result(query, 0, 0, ns_r_noerror, "goose.feathers", ans_buffer, ptr - ans_buffer);
+ ast_dns_resolver_set_result(query, 0, 0, ns_r_noerror, "goose.feathers", ans_buffer, ans_size);
for (i = 0; i < num_test_records; ++i) {
char record[128];
- ptr = record;
+ int naptr_size;
- ptr += generate_naptr_record(&test_records[i], ptr);
- ast_dns_resolver_add_record(query, ns_t_naptr, ns_c_in, 12345, record, ptr - record);
+ naptr_size = generate_naptr_record(&test_records[i], record);
+ ast_dns_resolver_add_record(query, ns_t_naptr, ns_c_in, 12345, record, naptr_size);
}
ast_dns_resolver_completed(query);
diff --git a/tests/test_dns_srv.c b/tests/test_dns_srv.c
index de79be1d9..c5145bb2e 100644
--- a/tests/test_dns_srv.c
+++ b/tests/test_dns_srv.c
@@ -31,104 +31,7 @@
#include "asterisk/dns_core.h"
#include "asterisk/dns_resolver.h"
#include "asterisk/dns_srv.h"
-
-#define DNS_HEADER_SIZE 96
-
-const char DNS_HEADER[] = {
- /* ID == 0 */
- 0x00, 0x00,
- /* QR == 1, Opcode == 0, AA == 1, TC == 0, RD == 1 */
- 0x85,
- /* RA == 1, Z == 0, RCODE == 0 */
- 0x80,
- /* QDCOUNT == 1 */
- 0x00, 0x01,
- /* ANCOUNT == 1 */
- 0x00, 0x00,
- /* NSCOUNT == 0 */
- 0x00, 0x00,
- /* ARCOUNT == 0 */
- 0x00, 0x00,
-};
-
-static int generate_dns_header(unsigned short num_records, char *buf)
-{
- unsigned short net_num_records = htons(num_records);
-
- memcpy(buf, DNS_HEADER, ARRAY_LEN(DNS_HEADER));
- /* Overwrite the ANCOUNT with the actual number of answers */
- memcpy(&buf[6], &net_num_records, sizeof(num_records));
-
- return ARRAY_LEN(DNS_HEADER);
-}
-
-const char DNS_QUESTION [] = {
- /* goose */
- 0x05, 0x67, 0x6f, 0x6f, 0x73, 0x65,
- /* feathers */
- 0x08, 0x66, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x73,
- /* end label */
- 0x00,
- /* SRV type */
- 0x00, 0x23,
- /* IN class */
- 0x00, 0x01,
-};
-
-static int generate_dns_question(char *buf)
-{
- memcpy(buf, DNS_QUESTION, ARRAY_LEN(DNS_QUESTION));
- return ARRAY_LEN(DNS_QUESTION);
-}
-
-const char SRV_ANSWER [] = {
- /* Domain points to name from question */
- 0xc0, 0x0c,
- /* NAPTR type */
- 0x00, 0x23,
- /* IN Class */
- 0x00, 0x01,
- /* TTL (12345 by default) */
- 0x00, 0x00, 0x30, 0x39,
-};
-
-static int generate_dns_answer(int ttl, char *buf)
-{
- int net_ttl = htonl(ttl);
-
- memcpy(buf, SRV_ANSWER, ARRAY_LEN(SRV_ANSWER));
- /* Overwrite TTL if one is provided */
- if (ttl) {
- memcpy(&buf[6], &net_ttl, sizeof(int));
- }
-
- return ARRAY_LEN(SRV_ANSWER);
-}
-
-static int write_dns_string(const char *string, char *buf)
-{
- uint8_t len = strlen(string);
- buf[0] = len;
- if (len) {
- memcpy(&buf[1], string, len);
- }
-
- return len + 1;
-}
-
-static int write_dns_domain(const char *string, char *buf)
-{
- char *copy = ast_strdupa(string);
- char *part;
- char *ptr = buf;
-
- while ((part = strsep(&copy, "."))) {
- ptr += write_dns_string(part, ptr);
- }
- ptr += write_dns_string("", ptr);
-
- return ptr - buf;
-}
+#include "asterisk/dns_test.h"
struct srv_record {
uint16_t priority;
@@ -141,8 +44,9 @@ struct srv_record {
unsigned int ignore_host;
};
-static int generate_srv_record(struct srv_record *record, char *buf)
+static int generate_srv_record(void *dns_record, char *buf)
{
+ struct srv_record *record = dns_record;
uint16_t priority = htons(record->priority);
uint16_t weight = htons(record->weight);
uint16_t port = htons(record->port);
@@ -164,7 +68,7 @@ static int generate_srv_record(struct srv_record *record, char *buf)
}
if (!record->ignore_host) {
- ptr += write_dns_domain(record->host, ptr);
+ ptr += ast_dns_test_write_domain(record->host, ptr);
}
return ptr - buf;
@@ -178,31 +82,19 @@ static void *srv_thread(void *dns_query)
{
struct ast_dns_query *query = dns_query;
int i;
- char *ptr = ans_buffer;
-
- ptr += generate_dns_header(num_test_records, ptr);
- ptr += generate_dns_question(ptr);
+ int ans_size;
- for (i = 0; i < num_test_records; ++i) {
- unsigned short rdlength;
- unsigned short net_rdlength;
-
- ptr += generate_dns_answer(0, ptr);
- rdlength = generate_srv_record(&test_records[i], ptr + 2);
- net_rdlength = htons(rdlength);
- memcpy(ptr, &net_rdlength, 2);
- ptr += 2;
- ptr += rdlength;
- }
+ ans_size = ast_dns_test_generate_result(query, test_records, num_test_records,
+ sizeof(struct srv_record), generate_srv_record, ans_buffer);
- ast_dns_resolver_set_result(query, 0, 0, ns_r_noerror, "goose.feathers", ans_buffer, ptr - ans_buffer);
+ ast_dns_resolver_set_result(query, 0, 0, ns_r_noerror, "goose.feathers", ans_buffer, ans_size);
for (i = 0; i < num_test_records; ++i) {
char record[128];
- ptr = record;
+ int srv_size;
- ptr += generate_srv_record(&test_records[i], ptr);
- ast_dns_resolver_add_record(query, ns_t_srv, ns_c_in, 12345, record, ptr - record);
+ srv_size = generate_srv_record(&test_records[i], record);
+ ast_dns_resolver_add_record(query, ns_t_srv, ns_c_in, 12345, record, srv_size);
}
ast_dns_resolver_completed(query);