Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
test_dns_naptr.c File Reference
#include "asterisk.h"
#include <arpa/nameser.h>
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/dns_core.h"
#include "asterisk/dns_resolver.h"
#include "asterisk/dns_naptr.h"
#include "asterisk/dns_test.h"
Include dependency graph for test_dns_naptr.c:

Go to the source code of this file.

Data Structures

struct  naptr_record
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (naptr_resolve_nominal)
 
 AST_TEST_DEFINE (naptr_resolve_off_nominal_length)
 
 AST_TEST_DEFINE (naptr_resolve_off_nominal_flags)
 
 AST_TEST_DEFINE (naptr_resolve_off_nominal_services)
 
 AST_TEST_DEFINE (naptr_resolve_off_nominal_regexp)
 
 AST_TEST_DEFINE (naptr_resolve_off_nominal_interactions)
 
static int generate_naptr_record (void *dns_record, char *buf)
 Given a NAPTR record, generate a binary form, as would appear in DNS RDATA. More...
 
static int load_module (void)
 
static int naptr_cancel (struct ast_dns_query *query)
 A STUB. More...
 
static int naptr_resolve (struct ast_dns_query *query)
 Mock NAPTR resolution method. More...
 
static void * naptr_thread (void *dns_query)
 Asynchronous NAPTR resolution thread. More...
 
static enum ast_test_result_state off_nominal_test (struct ast_test *test, struct naptr_record *records, int num_records)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "DNS API Tests" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static char ans_buffer [1024]
 A buffer to place raw DNS records into. More...
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_dns_resolver naptr_resolver
 Mock NAPTR resolver. More...
 
static int num_test_records
 The number of records in the test_records array. More...
 
static struct naptr_recordtest_records
 A pointer to an array of records for a test. More...
 

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 502 of file test_dns_naptr.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 502 of file test_dns_naptr.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 502 of file test_dns_naptr.c.

◆ AST_TEST_DEFINE() [1/6]

AST_TEST_DEFINE ( naptr_resolve_nominal  )

Definition at line 165 of file test_dns_naptr.c.

References ans_buffer, ARRAY_LEN, ast_dns_naptr_get_flags(), ast_dns_naptr_get_order(), ast_dns_naptr_get_preference(), ast_dns_naptr_get_regexp(), ast_dns_naptr_get_replacement(), ast_dns_naptr_get_service(), ast_dns_record_get_next(), ast_dns_resolve(), ast_dns_resolver_register(), ast_dns_resolver_unregister(), ast_dns_result_free(), ast_dns_result_get_records(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, cleanup(), naptr_record::flags, sip_to_pjsip::info(), NULL, num_test_records, naptr_record::order, naptr_record::preference, RAII_VAR, records, naptr_record::regexp, naptr_record::replacement, result, naptr_record::services, TEST_EXECUTE, TEST_INIT, and ast_dns_test_string::val.

166 {
168  const struct ast_dns_record *record;
169  struct naptr_record records[] = {
170  /* Incredibly plain record */
171  { 200, 100, {1, "A"}, {4, "BLAH"}, {0, ""}, "goose.down" },
172  /* Records with valid but unusual flags */
173  { 300, 8, {0, ""}, {4, "BLAH"}, {0, ""}, "goose.down" },
174  { 300, 6, {1, "3"}, {4, "BLAH"}, {0, ""}, "goose.down" },
175  { 100, 2, {2, "32"}, {4, "BLAH"}, {0, ""}, "goose.down" },
176  { 400, 100, {3, "A32"}, {4, "BLAH"}, {0, ""}, "goose.down" },
177  /* Records with valid but unusual services */
178  { 100, 700, {0, ""}, {0, ""}, {0, ""}, "goose.down" },
179  { 500, 102, {1, "A"}, {42, "A+B12+C+D+EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"}, {0, ""}, "goose.down" },
180  { 500, 100, {1, "A"}, {14, "A+B12+C+D+EEEE"}, {0, ""}, "goose.down" },
181  /* Records with valid regexes (regexes are always unusual) */
182  { 500, 101, {1, "A"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, "" },
183  { 500, 99, {1, "A"}, {4, "BLAH"}, {15, "0.*0horse.mane0"}, "" },
184  { 10, 100, {1, "A"}, {4, "BLAH"}, {11, "!.*!\\!\\!\\!!"}, "" },
185  { 700, 999, {1, "A"}, {4, "BLAH"}, {30, "!(.)(.)(.)(.)!\\1.m.\\2.n\\3.o\\4!"}, "" },
186  };
187 
188  int naptr_record_order[] = { 10, 3, 5, 0, 2, 1, 4, 9, 7, 8, 6, 11};
190  int i;
191 
192  switch (cmd) {
193  case TEST_INIT:
194  info->name = "naptr_resolve";
195  info->category = "/main/dns/naptr/";
196  info->summary = "Test nominal resolution of NAPTR records";
197  info->description = "This test defines four valid NAPTR records and\n"
198  "performs a resolution of the domain to which they belong. The test\n"
199  "ensures that all fields of the NAPTR records are parsed correctly\n"
200  "and that the records are returned in sorted order";
201  return AST_TEST_NOT_RUN;
202  case TEST_EXECUTE:
203  break;
204  }
205 
207  num_test_records = ARRAY_LEN(records);
208  memset(ans_buffer, 0, sizeof(ans_buffer));
209 
211 
212  if (ast_dns_resolve("goose.feathers", T_NAPTR, C_IN, &result)) {
213  ast_test_status_update(test, "DNS resolution failed\n");
214  res = AST_TEST_FAIL;
215  goto cleanup;
216  }
217 
218  if (!result) {
219  ast_test_status_update(test, "DNS resolution returned no result\n");
220  res = AST_TEST_FAIL;
221  goto cleanup;
222  }
223 
224  i = 0;
225  for (record = ast_dns_result_get_records(result); record; record = ast_dns_record_get_next(record)) {
226  if (ast_dns_naptr_get_order(record) != records[naptr_record_order[i]].order) {
227  ast_test_status_update(test, "Expected order %hu, got order %hu from NAPTR record\n",
228  records[naptr_record_order[i]].order, ast_dns_naptr_get_order(record));
229  res = AST_TEST_FAIL;
230  }
231  if (ast_dns_naptr_get_preference(record) != records[naptr_record_order[i]].preference) {
232  ast_test_status_update(test, "Expected preference %hu, got preference %hu from NAPTR record\n",
233  records[naptr_record_order[i]].preference, ast_dns_naptr_get_preference(record));
234  res = AST_TEST_FAIL;
235  }
236  if (strcmp(ast_dns_naptr_get_flags(record), records[naptr_record_order[i]].flags.val)) {
237  ast_test_status_update(test, "Expected flags %s, got flags %s from NAPTR record\n",
238  records[naptr_record_order[i]].flags.val, ast_dns_naptr_get_flags(record));
239  res = AST_TEST_FAIL;
240  }
241  if (strcmp(ast_dns_naptr_get_service(record), records[naptr_record_order[i]].services.val)) {
242  ast_test_status_update(test, "Expected services %s, got services %s from NAPTR record\n",
243  records[naptr_record_order[i]].services.val, ast_dns_naptr_get_service(record));
244  res = AST_TEST_FAIL;
245  }
246  if (strcmp(ast_dns_naptr_get_regexp(record), records[naptr_record_order[i]].regexp.val)) {
247  ast_test_status_update(test, "Expected regexp %s, got regexp %s from NAPTR record\n",
248  records[naptr_record_order[i]].regexp.val, ast_dns_naptr_get_regexp(record));
249  res = AST_TEST_FAIL;
250  }
251  if (strcmp(ast_dns_naptr_get_replacement(record), records[naptr_record_order[i]].replacement)) {
252  ast_test_status_update(test, "Expected replacement %s, got replacement %s from NAPTR record\n",
253  records[naptr_record_order[i]].replacement, ast_dns_naptr_get_replacement(record));
254  res = AST_TEST_FAIL;
255  }
256  ++i;
257  }
258 
259  if (i != ARRAY_LEN(records)) {
260  ast_test_status_update(test, "Unexpected number of records returned in NAPTR lookup\n");
261  res = AST_TEST_FAIL;
262  }
263 
264 cleanup:
265 
267 
268  test_records = NULL;
269  num_test_records = 0;
270  memset(ans_buffer, 0, sizeof(ans_buffer));
271 
272  return res;
273 }
static int records
Definition: cdr_mysql.c:84
const struct ast_dns_record * ast_dns_record_get_next(const struct ast_dns_record *record)
Get the next DNS record.
Definition: dns_core.c:170
int ast_dns_resolve(const char *name, int rr_type, int rr_class, struct ast_dns_result **result)
Synchronously resolve a DNS query.
Definition: dns_core.c:314
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
void ast_dns_result_free(struct ast_dns_result *result)
Free the DNS result information.
Definition: dns_core.c:130
struct ast_dns_test_string flags
void ast_dns_resolver_unregister(struct ast_dns_resolver *resolver)
Unregister a DNS resolver.
Definition: dns_core.c:680
uint16_t preference
static struct naptr_record * test_records
A pointer to an array of records for a test.
For AST_LIST.
Definition: dns_internal.h:39
static char ans_buffer[1024]
A buffer to place raw DNS records into.
const char * ast_dns_naptr_get_regexp(const struct ast_dns_record *record)
Get the regular expression from a NAPTR record.
Definition: dns_naptr.c:616
unsigned short ast_dns_naptr_get_preference(const struct ast_dns_record *record)
Get the preference from a NAPTR record.
Definition: dns_naptr.c:640
#define NULL
Definition: resample.c:96
const char * ast_dns_naptr_get_service(const struct ast_dns_record *record)
Get the service from a NAPTR record.
Definition: dns_naptr.c:608
static int num_test_records
The number of records in the test_records array.
static struct ast_dns_resolver naptr_resolver
Mock NAPTR resolver.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
integer order
Definition: analys.c:66
int ast_dns_resolver_register(struct ast_dns_resolver *resolver)
Register a DNS resolver.
Definition: dns_core.c:630
struct ast_dns_test_string services
The result of a DNS query.
Definition: dns_internal.h:117
const char * ast_dns_naptr_get_flags(const struct ast_dns_record *record)
Get the flags from a NAPTR record.
Definition: dns_naptr.c:600
def info(msg)
const char * replacement
const char * val
Definition: dns_test.h:35
const struct ast_dns_record * ast_dns_result_get_records(const struct ast_dns_result *result)
Get the first record of a DNS Result.
Definition: dns_core.c:102
static void * cleanup(void *unused)
Definition: pbx_realtime.c:124
unsigned short ast_dns_naptr_get_order(const struct ast_dns_record *record)
Get the order from a NAPTR record.
Definition: dns_naptr.c:632
static PGresult * result
Definition: cel_pgsql.c:88
const char * ast_dns_naptr_get_replacement(const struct ast_dns_record *record)
Get the replacement value from a NAPTR record.
Definition: dns_naptr.c:624
uint16_t order
struct ast_dns_test_string regexp
ast_test_result_state
Definition: test.h:200

◆ AST_TEST_DEFINE() [2/6]

AST_TEST_DEFINE ( naptr_resolve_off_nominal_length  )

Definition at line 316 of file test_dns_naptr.c.

References ARRAY_LEN, AST_TEST_NOT_RUN, sip_to_pjsip::info(), off_nominal_test(), TEST_EXECUTE, and TEST_INIT.

317 {
318  struct naptr_record records[] = {
319  { 100, 100, {255, "A"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, "" },
320  { 100, 100, {0, "A"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, "" },
321  { 100, 100, {1, "A"}, {255, "BLAH"}, {15, "!.*!horse.mane!"}, "" },
322  { 100, 100, {1, "A"}, {2, "BLAH"}, {15, "!.*!horse.mane!"}, "" },
323  { 100, 100, {1, "A"}, {4, "BLAH"}, {255, "!.*!horse.mane!"}, "" },
324  { 100, 100, {1, "A"}, {4, "BLAH"}, {3, "!.*!horse.mane!"}, "" },
325  { 100, 100, {255, "A"}, {255, "BLAH"}, {255, "!.*!horse.mane!"}, "" },
326  { 100, 100, {0, "A"}, {2, "BLAH"}, {3, "!.*!horse.mane!"}, "" },
327  };
328 
329  switch (cmd) {
330  case TEST_INIT:
331  info->name = "naptr_resolve_off_nominal_length";
332  info->category = "/main/dns/naptr/";
333  info->summary = "Test resolution of NAPTR records with off-nominal lengths";
334  info->description = "This test defines a set of records where the strings provided\n"
335  "within the record are valid, but the lengths of the strings in the record are\n"
336  "invalid, either too large or too small. The goal of this test is to ensure that\n"
337  "these invalid lengths result in resolution failures";
338  return AST_TEST_NOT_RUN;
339  case TEST_EXECUTE:
340  break;
341  }
342 
343  return off_nominal_test(test, records, ARRAY_LEN(records));
344 }
static int records
Definition: cdr_mysql.c:84
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static enum ast_test_result_state off_nominal_test(struct ast_test *test, struct naptr_record *records, int num_records)
def info(msg)

◆ AST_TEST_DEFINE() [3/6]

AST_TEST_DEFINE ( naptr_resolve_off_nominal_flags  )

Definition at line 346 of file test_dns_naptr.c.

References ARRAY_LEN, AST_TEST_NOT_RUN, sip_to_pjsip::info(), off_nominal_test(), TEST_EXECUTE, and TEST_INIT.

347 {
348  struct naptr_record records[] = {
349  /* Non-alphanumeric flag */
350  { 100, 100, {1, "!"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
351  /* Mix of valid and non-alphanumeric */
352  { 100, 100, {2, "A!"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
353  { 100, 100, {2, "!A"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
354  /* Invalid combinations of flags */
355  { 100, 100, {2, "sa"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
356  { 100, 100, {2, "su"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
357  { 100, 100, {2, "sp"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
358  { 100, 100, {2, "as"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
359  { 100, 100, {2, "au"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
360  { 100, 100, {2, "ap"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
361  { 100, 100, {2, "ua"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
362  { 100, 100, {2, "us"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
363  { 100, 100, {2, "up"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
364  { 100, 100, {2, "pa"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
365  { 100, 100, {2, "ps"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
366  { 100, 100, {2, "pu"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, ""},
367  };
368 
369  switch (cmd) {
370  case TEST_INIT:
371  info->name = "naptr_resolve_off_nominal_flags";
372  info->category = "/main/dns/naptr/";
373  info->summary = "Ensure that NAPTR records with invalid flags are not presented in results";
374  info->description = "This test defines a set of records where the flags provided are\n"
375  "invalid in some way. This may be due to providing non-alphanumeric characters or\n"
376  "by providing clashing flags. The result should be that none of the defined records\n"
377  "are returned by the resolver";
378  return AST_TEST_NOT_RUN;
379  case TEST_EXECUTE:
380  break;
381  }
382 
383  return off_nominal_test(test, records, ARRAY_LEN(records));
384 }
static int records
Definition: cdr_mysql.c:84
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static enum ast_test_result_state off_nominal_test(struct ast_test *test, struct naptr_record *records, int num_records)
def info(msg)

◆ AST_TEST_DEFINE() [4/6]

AST_TEST_DEFINE ( naptr_resolve_off_nominal_services  )

Definition at line 386 of file test_dns_naptr.c.

References ARRAY_LEN, AST_TEST_NOT_RUN, sip_to_pjsip::info(), off_nominal_test(), TEST_EXECUTE, and TEST_INIT.

387 {
388  struct naptr_record records[] = {
389  { 100, 100, {1, "A"}, {5, "BLAH!"}, {15, "!.*!horse.mane!"}, ""},
390  { 100, 100, {1, "A"}, {5, "BL!AH"}, {15, "!.*!horse.mane!"}, ""},
391  { 100, 100, {1, "A"}, {8, "1SIP+D2U"}, {15, "!.*!horse.mane!"}, ""},
392  { 100, 100, {1, "A"}, {8, "SIP+1D2U"}, {15, "!.*!horse.mane!"}, ""},
393  { 100, 100, {1, "A"}, {4, "+D2U"}, {15, "!.*!horse.mane!"}, ""},
394  { 100, 100, {1, "A"}, {4, "SIP+"}, {15, "!.*!horse.mane!"}, ""},
395  { 100, 100, {1, "A"}, {8, "SIP++D2U"}, {15, "!.*!horse.mane!"}, ""},
396  { 100, 100, {1, "A"}, {37, "SIPSIPSIPSIPSIPSIPSIPSIPSIPSIPSIP+D2U"}, {15, "!.*!horse.mane!"}, ""},
397  { 100, 100, {1, "A"}, {37, "SIP+D2UD2UD2UD2UD2UD2UD2UD2UD2UD2UD2U"}, {15, "!.*!horse.mane!"}, ""},
398  };
399 
400  switch (cmd) {
401  case TEST_INIT:
402  info->name = "naptr_resolve_off_nominal_services";
403  info->category = "/main/dns/naptr/";
404  info->summary = "Ensure that NAPTR records with invalid services are not presented in results";
405  info->description = "This test defines a set of records where the services provided are\n"
406  "invalid in some way. This may be due to providing non-alphanumeric characters, providing\n"
407  "protocols or resolution services that start with a non-alphabetic character, or\n"
408  "providing fields that are too long.";
409  return AST_TEST_NOT_RUN;
410  case TEST_EXECUTE:
411  break;
412  }
413 
414  return off_nominal_test(test, records, ARRAY_LEN(records));
415 }
static int records
Definition: cdr_mysql.c:84
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static enum ast_test_result_state off_nominal_test(struct ast_test *test, struct naptr_record *records, int num_records)
def info(msg)

◆ AST_TEST_DEFINE() [5/6]

AST_TEST_DEFINE ( naptr_resolve_off_nominal_regexp  )

Definition at line 417 of file test_dns_naptr.c.

References ARRAY_LEN, AST_TEST_NOT_RUN, sip_to_pjsip::info(), off_nominal_test(), TEST_EXECUTE, and TEST_INIT.

418 {
419  struct naptr_record records[] = {
420  /* Invalid delim-char */
421  { 100, 100, {1, "A"}, {4, "BLAH"}, {15, "1.*1horse.mane1"}, ""},
422  /* Not enough delim-chars */
423  { 100, 100, {1, "A"}, {4, "BLAH"}, {14, "!.*!horse.mane"}, ""},
424  /* Not enough delim-chars, part 2 */
425  { 100, 100, {1, "A"}, {4, "BLAH"}, {16, "!.*!horse.mane\\!"}, ""},
426  /* Too many delim-chars */
427  { 100, 100, {1, "A"}, {4, "BLAH"}, {15, "!.*!horse!mane!"}, ""},
428  /* Invalid regex flag */
429  { 100, 100, {1, "A"}, {4, "BLAH"}, {16, "!.*!horse.mane!o"}, ""},
430  /* Invalid backreference */
431  { 100, 100, {1, "A"}, {4, "BLAH"}, {13, "!.*!horse.\\0!"}, ""},
432  /* Invalid regex */
433  { 100, 100, {1, "A"}, {4, "BLAH"}, {16, "!(.*!horse.mane!"}, ""},
434  };
435 
436  switch (cmd) {
437  case TEST_INIT:
438  info->name = "naptr_resolve_off_nominal_regexp";
439  info->category = "/main/dns/naptr/";
440  info->summary = "Ensure that NAPTR records with invalid regexps are not presented in results";
441  info->description = "This test defines a set of records where the regexps provided are\n"
442  "invalid in some way. The test ensures that none of the invalid records are returned\n"
443  "when performing a NAPTR lookup";
444  return AST_TEST_NOT_RUN;
445  case TEST_EXECUTE:
446  break;
447  }
448 
449  return off_nominal_test(test, records, ARRAY_LEN(records));
450 }
static int records
Definition: cdr_mysql.c:84
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static enum ast_test_result_state off_nominal_test(struct ast_test *test, struct naptr_record *records, int num_records)
def info(msg)

◆ AST_TEST_DEFINE() [6/6]

AST_TEST_DEFINE ( naptr_resolve_off_nominal_interactions  )

Definition at line 452 of file test_dns_naptr.c.

References ARRAY_LEN, AST_TEST_NOT_RUN, sip_to_pjsip::info(), off_nominal_test(), TEST_EXECUTE, and TEST_INIT.

453 {
454  struct naptr_record records[] = {
455  /* Both regexp and replacement are specified */
456  { 100, 100, {1, "A"}, {4, "BLAH"}, {15, "!.*!horse.mane!"}, "goose.down"},
457  /* XXX RFC 2915 says that a service MUST be present if terminal flags are
458  * specified. However, RFCs 3401-3404 do not specify this behavior, so
459  * I am not putting in a test for it
460  */
461  };
462 
463  switch (cmd) {
464  case TEST_INIT:
465  info->name = "naptr_resolve_off_nominal_interactions";
466  info->category = "/main/dns/naptr/";
467  info->summary = "Ensure that NAPTR records with invalid interactions are not presented in results";
468  info->description = "This test defines a set of records where all parts are individually valid,\n"
469  "but when combined do not make sense and are thus invalid.";
470  return AST_TEST_NOT_RUN;
471  case TEST_EXECUTE:
472  break;
473  }
474 
475  return off_nominal_test(test, records, ARRAY_LEN(records));
476 }
static int records
Definition: cdr_mysql.c:84
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static enum ast_test_result_state off_nominal_test(struct ast_test *test, struct naptr_record *records, int num_records)
def info(msg)

◆ generate_naptr_record()

static int generate_naptr_record ( void *  dns_record,
char *  buf 
)
static

Given a NAPTR record, generate a binary form, as would appear in DNS RDATA.

This is part of a DNS answer, specific to NAPTR. It consists of all parts of the NAPTR record, encoded as it should be in a DNS record.

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.

Parameters
stringThe NAPTR record to encode
bufThe buffer to write the record into
Returns
The number of bytes written to the buffer

Definition at line 58 of file test_dns_naptr.c.

References ast_dns_test_write_domain(), ast_dns_test_write_string(), buf, naptr_record::flags, naptr_record::order, naptr_record::preference, naptr_record::regexp, naptr_record::replacement, and naptr_record::services.

Referenced by naptr_thread().

59 {
60  struct naptr_record *record = dns_record;
61  uint16_t net_order = htons(record->order);
62  uint16_t net_preference = htons(record->preference);
63  char *ptr = buf;
64 
65  memcpy(ptr, &net_order, sizeof(net_order));
66  ptr += sizeof(net_order);
67 
68  memcpy(ptr, &net_preference, sizeof(net_preference));
69  ptr += sizeof(net_preference);
70 
71  ptr += ast_dns_test_write_string(&record->flags, ptr);
72  ptr += ast_dns_test_write_string(&record->services, ptr);
73  ptr += ast_dns_test_write_string(&record->regexp, ptr);
74  ptr += ast_dns_test_write_domain(record->replacement, ptr);
75 
76  return ptr - buf;
77 }
struct ast_dns_test_string flags
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
uint16_t preference
A DNS record to be used during a test.
struct ast_dns_test_string services
int ast_dns_test_write_domain(const char *string, char *buf)
Write a DNS domain to a buffer.
Definition: dns_test.c:196
const char * replacement
uint16_t order
int ast_dns_test_write_string(const struct ast_dns_test_string *string, char *buf)
Write a DNS string to a buffer.
Definition: dns_test.c:162
struct ast_dns_test_string regexp

◆ load_module()

static int load_module ( void  )
static

Definition at line 490 of file test_dns_naptr.c.

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

491 {
492  AST_TEST_REGISTER(naptr_resolve_nominal);
493  AST_TEST_REGISTER(naptr_resolve_off_nominal_length);
494  AST_TEST_REGISTER(naptr_resolve_off_nominal_flags);
495  AST_TEST_REGISTER(naptr_resolve_off_nominal_services);
496  AST_TEST_REGISTER(naptr_resolve_off_nominal_regexp);
497  AST_TEST_REGISTER(naptr_resolve_off_nominal_interactions);
498 
500 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

◆ naptr_cancel()

static int naptr_cancel ( struct ast_dns_query query)
static

A STUB.

Definition at line 150 of file test_dns_naptr.c.

151 {
152  return 0;
153 }

◆ naptr_resolve()

static int naptr_resolve ( struct ast_dns_query query)
static

Mock NAPTR resolution method.

This spawns a thread to handle generation of the necessary NAPTR records

Definition at line 140 of file test_dns_naptr.c.

References ao2_bump, ast_pthread_create_detached, naptr_thread(), NULL, and thread.

141 {
142  pthread_t thread;
143 
144  return ast_pthread_create_detached(&thread, NULL, naptr_thread, ao2_bump(query));
145 }
pthread_t thread
Definition: app_meetme.c:1089
#define ast_pthread_create_detached(a, b, c, d)
Definition: utils.h:563
#define NULL
Definition: resample.c:96
#define ao2_bump(obj)
Definition: astobj2.h:491
static void * naptr_thread(void *dns_query)
Asynchronous NAPTR resolution thread.

◆ naptr_thread()

static void* naptr_thread ( void *  dns_query)
static

Asynchronous NAPTR resolution thread.

This builds an appropriate DNS response based on the NAPTR records for a given test. Once the records have been created, the records are added to the DNS result

Definition at line 110 of file test_dns_naptr.c.

References ans_buffer, ao2_ref, ast_dns_resolver_add_record(), ast_dns_resolver_completed(), ast_dns_resolver_set_result(), ast_dns_test_generate_result(), generate_naptr_record(), NULL, and num_test_records.

Referenced by naptr_resolve().

111 {
112  struct ast_dns_query *query = dns_query;
113  int i;
114  int ans_size;
115 
118 
119  ast_dns_resolver_set_result(query, 0, 0, NOERROR, "goose.feathers", ans_buffer, ans_size);
120 
121  for (i = 0; i < num_test_records; ++i) {
122  char record[128];
123  int naptr_size;
124 
125  naptr_size = generate_naptr_record(&test_records[i], record);
126  ast_dns_resolver_add_record(query, T_NAPTR, C_IN, 12345, record, naptr_size);
127  }
128 
130 
131  ao2_ref(query, -1);
132  return NULL;
133 }
int ast_dns_resolver_add_record(struct ast_dns_query *query, int rr_type, int rr_class, int ttl, const char *data, const size_t size)
Add a DNS record to the result of a DNS query.
Definition: dns_core.c:535
int ast_dns_test_generate_result(struct ast_dns_query *query, void *records, size_t num_records, size_t record_size, record_fn generate, char *buffer)
Generate a full DNS response for the given DNS records.
Definition: dns_test.c:222
static struct naptr_record * test_records
A pointer to an array of records for a test.
static char ans_buffer[1024]
A buffer to place raw DNS records into.
#define NULL
Definition: resample.c:96
static int num_test_records
The number of records in the test_records array.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void ast_dns_resolver_completed(struct ast_dns_query *query)
Mark a DNS query as having been completed.
Definition: dns_core.c:599
A DNS query.
Definition: dns_internal.h:137
static int generate_naptr_record(void *dns_record, char *buf)
Given a NAPTR record, generate a binary form, as would appear in DNS RDATA.
int ast_dns_resolver_set_result(struct ast_dns_query *query, unsigned int secure, unsigned int bogus, unsigned int rcode, const char *canonical, const char *answer, size_t answer_size)
Set result information for a DNS query.
Definition: dns_core.c:456

◆ off_nominal_test()

static enum ast_test_result_state off_nominal_test ( struct ast_test test,
struct naptr_record records,
int  num_records 
)
static

Definition at line 275 of file test_dns_naptr.c.

References ans_buffer, ast_dns_resolve(), ast_dns_resolver_register(), ast_dns_resolver_unregister(), ast_dns_result_free(), ast_dns_result_get_records(), AST_TEST_FAIL, AST_TEST_PASS, ast_test_status_update, cleanup(), NULL, num_test_records, RAII_VAR, records, and result.

Referenced by AST_TEST_DEFINE().

276 {
279  const struct ast_dns_record *record;
280 
282  num_test_records = num_records;
283  memset(ans_buffer, 0, sizeof(ans_buffer));
284 
286 
287  if (ast_dns_resolve("goose.feathers", T_NAPTR, C_IN, &result)) {
288  ast_test_status_update(test, "Failed to perform DNS resolution, despite using valid inputs\n");
289  res = AST_TEST_FAIL;
290  goto cleanup;
291  }
292 
293  if (!result) {
294  ast_test_status_update(test, "Synchronous DNS resolution failed to set a result\n");
295  res = AST_TEST_FAIL;
296  goto cleanup;
297  }
298 
300  if (record) {
301  ast_test_status_update(test, "DNS resolution returned records when it was not expected to\n");
302  res = AST_TEST_FAIL;
303  goto cleanup;
304  }
305 
306 cleanup:
308 
309  test_records = NULL;
310  num_test_records = 0;
311  memset(ans_buffer, 0, sizeof(ans_buffer));
312 
313  return res;
314 }
static int records
Definition: cdr_mysql.c:84
int ast_dns_resolve(const char *name, int rr_type, int rr_class, struct ast_dns_result **result)
Synchronously resolve a DNS query.
Definition: dns_core.c:314
void ast_dns_result_free(struct ast_dns_result *result)
Free the DNS result information.
Definition: dns_core.c:130
void ast_dns_resolver_unregister(struct ast_dns_resolver *resolver)
Unregister a DNS resolver.
Definition: dns_core.c:680
static struct naptr_record * test_records
A pointer to an array of records for a test.
For AST_LIST.
Definition: dns_internal.h:39
static char ans_buffer[1024]
A buffer to place raw DNS records into.
#define NULL
Definition: resample.c:96
static int num_test_records
The number of records in the test_records array.
static struct ast_dns_resolver naptr_resolver
Mock NAPTR resolver.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
int ast_dns_resolver_register(struct ast_dns_resolver *resolver)
Register a DNS resolver.
Definition: dns_core.c:630
The result of a DNS query.
Definition: dns_internal.h:117
const struct ast_dns_record * ast_dns_result_get_records(const struct ast_dns_result *result)
Get the first record of a DNS Result.
Definition: dns_core.c:102
static void * cleanup(void *unused)
Definition: pbx_realtime.c:124
static PGresult * result
Definition: cel_pgsql.c:88
ast_test_result_state
Definition: test.h:200

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 478 of file test_dns_naptr.c.

References AST_TEST_UNREGISTER.

479 {
480  AST_TEST_UNREGISTER(naptr_resolve_nominal);
481  AST_TEST_UNREGISTER(naptr_resolve_off_nominal_length);
482  AST_TEST_UNREGISTER(naptr_resolve_off_nominal_flags);
483  AST_TEST_UNREGISTER(naptr_resolve_off_nominal_services);
484  AST_TEST_UNREGISTER(naptr_resolve_off_nominal_regexp);
485  AST_TEST_UNREGISTER(naptr_resolve_off_nominal_interactions);
486 
487  return 0;
488 }
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "DNS API Tests" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 502 of file test_dns_naptr.c.

◆ ans_buffer

char ans_buffer[1024]
static

A buffer to place raw DNS records into.

This buffer is way larger than any DNS records we actually wish to create during any of the tests, but that's fine.

Definition at line 101 of file test_dns_naptr.c.

Referenced by AST_TEST_DEFINE(), naptr_thread(), and off_nominal_test().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 502 of file test_dns_naptr.c.

◆ naptr_resolver

struct ast_dns_resolver naptr_resolver
static

Mock NAPTR resolver.

Definition at line 158 of file test_dns_naptr.c.

◆ num_test_records

int num_test_records
static

The number of records in the test_records array.

Each test must set this to the appropriate value at the beginning of the test and must set this back to zero at the end of the test.

Definition at line 94 of file test_dns_naptr.c.

Referenced by AST_TEST_DEFINE(), naptr_thread(), and off_nominal_test().

◆ test_records

struct naptr_record* test_records
static

A pointer to an array of records for a test.

Each test is expected to set this pointer to its local array of records and then re-set tis pointer to NULL at the end of the test

Definition at line 86 of file test_dns_naptr.c.