|
| 1 | +/* |
| 2 | + * ttl.c -- Test $TTL works as advertised |
| 3 | + * |
| 4 | + * Copyright (c) 2024, NLnet Labs. All rights reserved. |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: BSD-3-Clause |
| 7 | + * |
| 8 | + */ |
| 9 | +#include <stdarg.h> |
| 10 | +#include <setjmp.h> |
| 11 | +#include <string.h> |
| 12 | +#include <cmocka.h> |
| 13 | +#include <stdint.h> |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +#include "zone.h" |
| 17 | +#include "tools.h" |
| 18 | + |
| 19 | +struct rr_ttl { |
| 20 | + size_t rr; |
| 21 | + size_t ttl_count; |
| 22 | + uint32_t *ttls; |
| 23 | +}; |
| 24 | + |
| 25 | +static int32_t accept_rr( |
| 26 | + zone_parser_t *parser, |
| 27 | + const zone_name_t *owner, |
| 28 | + uint16_t type, |
| 29 | + uint16_t class, |
| 30 | + uint32_t ttl, |
| 31 | + uint16_t rdlength, |
| 32 | + const uint8_t *rdata, |
| 33 | + void *user_data) |
| 34 | +{ |
| 35 | + (void)parser; |
| 36 | + (void)owner; |
| 37 | + (void)type; |
| 38 | + (void)class; |
| 39 | + (void)rdlength; |
| 40 | + (void)rdata; |
| 41 | + |
| 42 | + struct rr_ttl *rr_ttl = user_data; |
| 43 | + |
| 44 | + if (rr_ttl->rr >= rr_ttl->ttl_count) |
| 45 | + return ZONE_SYNTAX_ERROR; |
| 46 | + if (rr_ttl->ttls[rr_ttl->rr++] != ttl) |
| 47 | + return ZONE_SYNTAX_ERROR; |
| 48 | + return ZONE_SUCCESS; |
| 49 | +} |
| 50 | + |
| 51 | +/*!cmocka */ |
| 52 | +void correct_ttl_is_used(void **state) |
| 53 | +{ |
| 54 | + |
| 55 | + (void)state; |
| 56 | + |
| 57 | + struct { |
| 58 | + const char *str; |
| 59 | + struct rr_ttl ttls; |
| 60 | + } tests[] = { |
| 61 | + { |
| 62 | + "$ORIGIN com.\n" |
| 63 | + "example 300 IN SOA ns hostmaster 2024081901 3600 600 86400 3600\n" |
| 64 | + "example IN NS ns\n", |
| 65 | + { 0, 2, (uint32_t[]){ 300, 300 } } |
| 66 | + }, |
| 67 | + { |
| 68 | + "$ORIGIN com.\n" |
| 69 | + "$TTL 350\n" |
| 70 | + "example 300 IN SOA ns hostmaster 2024081901 3600 600 86400 3600\n" |
| 71 | + "example IN NS ns\n", |
| 72 | + { 0, 2, (uint32_t[]){ 300, 350 } } |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + for (int i=0, n=sizeof(tests)/sizeof(tests[0]); i < n; i++) { |
| 77 | + size_t len = strlen(tests[i].str); |
| 78 | + char *str = malloc(len + ZONE_BLOCK_SIZE + 1); |
| 79 | + assert_non_null(str); |
| 80 | + memcpy(str, tests[i].str, len + 1); |
| 81 | + |
| 82 | + zone_parser_t parser; |
| 83 | + zone_name_buffer_t name; |
| 84 | + zone_rdata_buffer_t rdata; |
| 85 | + zone_buffers_t buffers = { 1, &name, &rdata }; |
| 86 | + zone_options_t options; |
| 87 | + const uint8_t origin[1] = { 0 }; |
| 88 | + int32_t code; |
| 89 | + |
| 90 | + memset(&options, 0, sizeof(options)); |
| 91 | + options.accept.callback = accept_rr; |
| 92 | + options.origin.octets = origin; |
| 93 | + options.origin.length = sizeof(origin); |
| 94 | + options.default_ttl = 3600; |
| 95 | + options.default_class = 1; |
| 96 | + |
| 97 | + code = zone_parse_string(&parser, &options, &buffers, str, len, &tests[i].ttls); |
| 98 | + free(str); |
| 99 | + assert_int_equal(code, ZONE_SUCCESS); |
| 100 | + assert_int_equal(tests[i].ttls.rr, tests[i].ttls.ttl_count); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/*!cmocka */ |
| 105 | +void correct_ttl_is_used_in_include(void **state) |
| 106 | +{ |
| 107 | + (void)state; |
| 108 | + |
| 109 | + struct { |
| 110 | + const char *fmt; |
| 111 | + const char *str; |
| 112 | + struct rr_ttl ttls; |
| 113 | + } tests[] = { |
| 114 | + { "$ORIGIN com.\n" |
| 115 | + "example 300 IN SOA ns hostmaster 2024081901 3600 600 86400 3600\n" |
| 116 | + "$INCLUDE \"%s\"\n" |
| 117 | + "example IN A 192.0.2.1\n", |
| 118 | + "example 600 IN A 192.0.2.2\n" |
| 119 | + "example IN A 192.0.2.3\n", |
| 120 | + { 0, 4, (uint32_t[]){ 300, 600, 600, 300 } } |
| 121 | + }, |
| 122 | + { "$ORIGIN com.\n" |
| 123 | + "$TTL 350\n" |
| 124 | + "example 300 IN SOA ns hostmaster 2024081901 3600 600 86400 3600\n" |
| 125 | + "$INCLUDE \"%s\"\n" |
| 126 | + "example IN A 192.0.2.1\n", |
| 127 | + "$TTL 650\n" |
| 128 | + "example 600 IN A 192.0.2.2\n" |
| 129 | + "example IN A 192.0.2.3\n", |
| 130 | + { 0, 4, (uint32_t[]){ 300, 600, 650, 350 } } |
| 131 | + } |
| 132 | + }; |
| 133 | + |
| 134 | + for (int i=0, n=sizeof(tests)/sizeof(tests[0]); i < n; i++) { |
| 135 | + char *inc = get_tempnam(NULL, "zone"); |
| 136 | + assert_non_null(inc); |
| 137 | + |
| 138 | + char buf[32]; |
| 139 | + int len = snprintf(buf, sizeof(buf), tests[i].fmt, inc); |
| 140 | + assert_false(len < 0); |
| 141 | + char *str = malloc((size_t)len + ZONE_BLOCK_SIZE + 1); |
| 142 | + assert_non_null(str); |
| 143 | + (void)snprintf(str, (size_t)len + 1, tests[i].fmt, inc); |
| 144 | + |
| 145 | + FILE *handle = fopen(inc, "wb"); |
| 146 | + assert_non_null(handle); |
| 147 | + int count = fputs(tests[i].str, handle); |
| 148 | + assert_int_not_equal(count, EOF); |
| 149 | + (void)fflush(handle); |
| 150 | + (void)fclose(handle); |
| 151 | + |
| 152 | + zone_parser_t parser; |
| 153 | + zone_name_buffer_t name; |
| 154 | + zone_rdata_buffer_t rdata; |
| 155 | + zone_buffers_t buffers = { 1, &name, &rdata }; |
| 156 | + zone_options_t options; |
| 157 | + const uint8_t origin[1] = { 0 }; |
| 158 | + int32_t code; |
| 159 | + |
| 160 | + memset(&options, 0, sizeof(options)); |
| 161 | + options.accept.callback = accept_rr; |
| 162 | + options.origin.octets = origin; |
| 163 | + options.origin.length = sizeof(origin); |
| 164 | + options.default_ttl = 3600; |
| 165 | + options.default_class = 1; |
| 166 | + |
| 167 | + code = zone_parse_string(&parser, &options, &buffers, str, len, &tests[i].ttls); |
| 168 | + remove(inc); |
| 169 | + free(str); |
| 170 | + assert_int_equal(code, ZONE_SUCCESS); |
| 171 | + assert_int_equal(tests[i].ttls.rr, tests[i].ttls.ttl_count); |
| 172 | + } |
| 173 | +} |
0 commit comments