From ec24e7596080222816eacd214879f4dde83356be Mon Sep 17 00:00:00 2001 From: Jeroen Koekkoek Date: Mon, 19 Aug 2024 15:36:34 +0200 Subject: [PATCH] Add test to verify correct TTL propagation --- tests/CMakeLists.txt | 2 +- tests/tools.c | 2 +- tests/ttl.c | 174 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 tests/ttl.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 66eacd6..95815bd 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,7 +9,7 @@ if(HAVE_HASWELL) set_source_files_properties(haswell/bits.c PROPERTIES COMPILE_FLAGS "-march=haswell") endif() -cmocka_add_tests(zone-tests types.c include.c ip4.c time.c base32.c svcb.c syntax.c semantics.c eui.c bounds.c bits.c) +cmocka_add_tests(zone-tests types.c include.c ip4.c time.c base32.c svcb.c syntax.c semantics.c eui.c bounds.c bits.c ttl.c) set(xbounds ${CMAKE_CURRENT_SOURCE_DIR}/zones/xbounds.zone) set(xbounds_c "${CMAKE_CURRENT_BINARY_DIR}/xbounds.c") diff --git a/tests/tools.c b/tests/tools.c index 3876cf0..9381557 100644 --- a/tests/tools.c +++ b/tests/tools.c @@ -45,7 +45,7 @@ diagnostic_pop() #else tmpdir = getenv("TMPDIR"); #endif - if (is_dir(tmpdir)) + if (tmpdir && is_dir(tmpdir)) return tmpdir; if (dir && is_dir(tmpdir)) return dir; diff --git a/tests/ttl.c b/tests/ttl.c new file mode 100644 index 0000000..a318b26 --- /dev/null +++ b/tests/ttl.c @@ -0,0 +1,174 @@ +/* + * ttl.c -- Test $TTL works as advertised + * + * Copyright (c) 2024, NLnet Labs. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + * + */ +#include +#include +#include +#include +#include +#include + +#include "zone.h" +#include "tools.h" + +struct rr_ttl { + size_t rr; + size_t ttl_count; + uint32_t *ttls; +}; + +static int32_t accept_rr( + zone_parser_t *parser, + const zone_name_t *owner, + uint16_t type, + uint16_t class, + uint32_t ttl, + uint16_t rdlength, + const uint8_t *rdata, + void *user_data) +{ + (void)parser; + (void)owner; + (void)type; + (void)class; + (void)rdlength; + (void)rdata; + + struct rr_ttl *rr_ttl = user_data; + + if (rr_ttl->rr >= rr_ttl->ttl_count) + return ZONE_SYNTAX_ERROR; + if (rr_ttl->ttls[rr_ttl->rr++] != ttl) + return ZONE_SYNTAX_ERROR; + return ZONE_SUCCESS; +} + +/*!cmocka */ +void correct_ttl_is_used(void **state) +{ + + (void)state; + + struct { + const char *str; + struct rr_ttl ttls; + } tests[] = { + { + "$ORIGIN com.\n" + "example 300 IN SOA ns hostmaster 2024081901 3600 600 86400 3600\n" + "example IN NS ns\n", + { 0, 2, (uint32_t[]){ 300, 300 } } + }, + { + "$ORIGIN com.\n" + "$TTL 350\n" + "example 300 IN SOA ns hostmaster 2024081901 3600 600 86400 3600\n" + "example IN NS ns\n", + { 0, 2, (uint32_t[]){ 300, 350 } } + } + }; + + for (int i=0, n=sizeof(tests)/sizeof(tests[0]); i < n; i++) { + size_t len = strlen(tests[i].str); + char *str = malloc(len + ZONE_BLOCK_SIZE + 1); + assert_non_null(str); + memcpy(str, tests[i].str, len + 1); + + zone_parser_t parser; + zone_name_buffer_t name; + zone_rdata_buffer_t rdata; + zone_buffers_t buffers = { 1, &name, &rdata }; + zone_options_t options; + const uint8_t origin[1] = { 0 }; + int32_t code; + + memset(&options, 0, sizeof(options)); + options.accept.callback = accept_rr; + options.origin.octets = origin; + options.origin.length = sizeof(origin); + options.default_ttl = 3600; + options.default_class = 1; + + code = zone_parse_string(&parser, &options, &buffers, str, (size_t)len, &tests[i].ttls); + free(str); + assert_int_equal(code, ZONE_SUCCESS); + assert_int_equal(tests[i].ttls.rr, tests[i].ttls.ttl_count); + } +} + +/*!cmocka */ +void correct_ttl_is_used_in_include(void **state) +{ + (void)state; + + struct { + const char *fmt; + const char *str; + struct rr_ttl ttls; + } tests[] = { + { "$ORIGIN com.\n" + "example 300 IN SOA ns hostmaster 2024081901 3600 600 86400 3600\n" + "$INCLUDE \"%s\"\n" + "example IN A 192.0.2.1\n", + "example 600 IN A 192.0.2.2\n" + "example IN A 192.0.2.3\n", + { 0, 4, (uint32_t[]){ 300, 600, 600, 300 } } + }, + { "$ORIGIN com.\n" + "$TTL 350\n" + "example 300 IN SOA ns hostmaster 2024081901 3600 600 86400 3600\n" + "$INCLUDE \"%s\"\n" + "example IN A 192.0.2.1\n", + "$TTL 650\n" + "example 600 IN A 192.0.2.2\n" + "example IN A 192.0.2.3\n", + { 0, 4, (uint32_t[]){ 300, 600, 650, 350 } } + } + }; + + for (int i=0, n=sizeof(tests)/sizeof(tests[0]); i < n; i++) { + char *inc = get_tempnam(NULL, "zone"); + assert_non_null(inc); + + char buf[32]; + int len = snprintf(buf, sizeof(buf), tests[i].fmt, inc); + assert_false(len < 0); + char *str = malloc((size_t)len + ZONE_BLOCK_SIZE + 1); + assert_non_null(str); + (void)snprintf(str, (size_t)len + 1, tests[i].fmt, inc); + + FILE *handle = fopen(inc, "wb"); + assert_non_null(handle); + int count = fputs(tests[i].str, handle); + assert_int_not_equal(count, EOF); + (void)fflush(handle); + (void)fclose(handle); + + zone_parser_t parser; + zone_name_buffer_t name; + zone_rdata_buffer_t rdata; + zone_buffers_t buffers = { 1, &name, &rdata }; + zone_options_t options; + const uint8_t origin[1] = { 0 }; + int32_t code; + + memset(&options, 0, sizeof(options)); + options.accept.callback = accept_rr; + options.origin.octets = origin; + options.origin.length = sizeof(origin); + options.default_ttl = 3600; + options.default_class = 1; + + code = zone_parse_string(&parser, &options, &buffers, str, (size_t)len, &tests[i].ttls); + remove(inc); + free(inc); + free(str); + assert_int_equal(code, ZONE_SUCCESS); + assert_int_equal(tests[i].ttls.rr, tests[i].ttls.ttl_count); + } +}