Skip to content

Commit 8a60ce4

Browse files
committed
Verify correct operation with blank input
1 parent e633ccc commit 8a60ce4

File tree

1 file changed

+68
-19
lines changed

1 file changed

+68
-19
lines changed

tests/syntax.c

+68-19
Original file line numberDiff line numberDiff line change
@@ -488,18 +488,22 @@ static int32_t dummy_callback(
488488
const uint8_t *rdata,
489489
void *user_data)
490490
{
491+
size_t *count = (size_t *)user_data;
492+
491493
(void)parser;
492494
(void)owner;
493495
(void)type;
494496
(void)class;
495497
(void)ttl;
496498
(void)rdlength;
497499
(void)rdata;
498-
(void)user_data;
500+
501+
(*count)++;
502+
499503
return 0;
500504
}
501505

502-
static int32_t parse_text(const char *text)
506+
static int32_t parse(const char *text, size_t *count)
503507
{
504508
zone_parser_t parser;
505509
zone_name_buffer_t name;
@@ -515,7 +519,7 @@ static int32_t parse_text(const char *text)
515519
options.default_class = 1;
516520

517521
fprintf(stderr, "INPUT: '%s'\n", text);
518-
return zone_parse_string(&parser, &options, &buffers, text, strlen(text), NULL);
522+
return zone_parse_string(&parser, &options, &buffers, text, strlen(text), count);
519523
}
520524

521525
static char *generate_include(const char *text)
@@ -536,33 +540,78 @@ static char *generate_include(const char *text)
536540

537541
diagnostic_push()
538542
msvc_diagnostic_ignored(4996)
539-
540-
/*!cmocka */
541-
void quote_no_unquote(void **state)
543+
static void remove_include(const char *path)
542544
{
543-
(void)state;
545+
unlink(path);
546+
}
547+
diagnostic_pop()
544548

549+
static int32_t parse_as_include(const char *text, size_t *count)
550+
{
545551
int32_t code;
546-
static const char *no_unquote = PAD("foo. TXT \"unterminated string");
547-
548-
// verify unterminated strings are caught
549-
code = parse_text(no_unquote);
550-
assert_int_equal(code, ZONE_SYNTAX_ERROR);
551-
552-
// verify unterminated strings are caught in included file
553-
char *path = generate_include(no_unquote);
552+
char *path = generate_include(text);
554553
assert_non_null(path);
555554
char dummy[16];
556555
int length = snprintf(dummy, sizeof(dummy), "$INCLUDE \"%s\"\n", path);
557556
assert_true(length > 0 && length < INT_MAX - ZONE_PADDING_SIZE);
558557
char *include = malloc((size_t)length + 1 + ZONE_PADDING_SIZE);
559558
assert_non_null(include);
560559
(void)snprintf(include, (size_t)length + 1, "$INCLUDE \"%s\"\n", path);
561-
code = parse_text(include);
562-
assert_int_equal(code, ZONE_SYNTAX_ERROR);
560+
code = parse(include, count);
563561
free(include);
564-
unlink(path);
562+
remove_include(path);
565563
free(path);
564+
return code;
566565
}
567566

568-
diagnostic_pop()
567+
/*!cmocka */
568+
void quote_no_unquote(void **state)
569+
{
570+
(void)state;
571+
572+
int32_t code;
573+
size_t count = 0;
574+
static const char *no_unquote = PAD("foo. TXT \"unterminated string");
575+
576+
code = parse(no_unquote, &count);
577+
assert_int_equal(code, ZONE_SYNTAX_ERROR);
578+
579+
code = parse_as_include(no_unquote, &count);
580+
assert_int_equal(code, ZONE_SYNTAX_ERROR);
581+
}
582+
583+
/*!cmocka */
584+
void not_so_famous_last_words(void **state)
585+
{
586+
(void)state;
587+
588+
int32_t code;
589+
size_t count = 0;
590+
static const char *last_words = PAD("; not so famous last words");
591+
592+
code = parse(last_words, &count);
593+
assert_int_equal(code, ZONE_SUCCESS);
594+
assert_true(count == 0);
595+
596+
code = parse_as_include(last_words, &count);
597+
assert_int_equal(code, ZONE_SUCCESS);
598+
assert_true(count == 0);
599+
}
600+
601+
/*!cmocka */
602+
void no_famous_last_words(void **state)
603+
{
604+
(void)state;
605+
606+
int32_t code;
607+
size_t count = 0;
608+
static const char *empty = PAD(" ");
609+
610+
code = parse(empty, &count);
611+
assert_int_equal(code, ZONE_SUCCESS);
612+
assert_true(count == 0);
613+
614+
code = parse_as_include(empty, &count);
615+
assert_int_equal(code, ZONE_SUCCESS);
616+
assert_true(count == 0);
617+
}

0 commit comments

Comments
 (0)