|
| 1 | +/* |
| 2 | + * bounds.c -- test for correct indexer operation on boundaries |
| 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 <stdlib.h> |
| 13 | +#include <cmocka.h> |
| 14 | + |
| 15 | +#include "zone.h" |
| 16 | + |
| 17 | +// indexer(s) scans in 64-byte chunks, use white space for positioning |
| 18 | + |
| 19 | +// terminate contiguous on last byte of block |
| 20 | +const char contiguous_end_last[] = |
| 21 | + "foo. TXT bar\nfoo. TXT baz"; |
| 22 | + |
| 23 | +// terminate contiguous on first byte of next block |
| 24 | +const char contiguous_end_first[] = |
| 25 | + "foo. TXT bar\nfoo. TXT baz"; |
| 26 | + |
| 27 | +// terminate quoted on last byte of the block |
| 28 | +const char quoted_end_last[] = |
| 29 | + "foo. TXT \"bar\"\nfoo. TXT baz"; |
| 30 | + |
| 31 | +// terminate quoted on first byte of next block |
| 32 | +const char quoted_end_first[] = |
| 33 | + "foo. TXT \"bar\"\nfoo. TXT baz"; |
| 34 | + |
| 35 | +// terminate comment on last byte of block |
| 36 | +const char comment_end_last[] = |
| 37 | + "foo. TXT bar ; comment\nfoo. TXT baz"; |
| 38 | + |
| 39 | +// terminate comment on first byte of next block |
| 40 | +const char comment_end_first[] = |
| 41 | + "foo. TXT bar ; comment\nfoo. TXT baz"; |
| 42 | + |
| 43 | +// start contiguous on last byte of block |
| 44 | +const char contiguous_start_last[] = |
| 45 | + "foo. TXT bar" |
| 46 | + "\nfoo. TXT baz"; |
| 47 | + |
| 48 | +// start quoted on last byte of block |
| 49 | +const char quoted_start_last[] = |
| 50 | + "foo. TXT \"" |
| 51 | + "bar\"\nfoo. TXT baz"; |
| 52 | + |
| 53 | +// start quoted on last byte of block, end on first byte of next block |
| 54 | +const char quoted_start_last_end_first[] = |
| 55 | + "foo. TXT \"" |
| 56 | + "\"\nfoo. TXT baz"; |
| 57 | + |
| 58 | +// start quoted on last byte of block, end of first byte of next next block |
| 59 | +const char quoted_start_last_end_next_first[] = |
| 60 | + "foo. TXT \"" |
| 61 | + "bar " |
| 62 | + "\"\nfoo. TXT baz"; |
| 63 | + |
| 64 | +// start comment on last byte of block |
| 65 | +const char comment_start_last[] = |
| 66 | + "foo. TXT bar;" |
| 67 | + " foobar\nfoo. TXT baz"; |
| 68 | + |
| 69 | +// start comment on last byte of block, end on first byte of next block |
| 70 | +const char comment_start_last_end_first[] = |
| 71 | + "foo. TXT bar;" |
| 72 | + "\nfoo. TXT baz"; |
| 73 | + |
| 74 | +// start comment on last byte of block, end on first byte of next next block |
| 75 | +const char comment_start_last_end_next_first[] = |
| 76 | + "foo. TXT bar;" |
| 77 | + " " |
| 78 | + "\nfoo. TXT baz"; |
| 79 | + |
| 80 | +// FIXME: the above can be testen on buffer boundaries too |
| 81 | +// FIXME: add a maximum buffer size test |
| 82 | +// FIXME: test buffer is not resized when processing a comment |
| 83 | + |
| 84 | +static int32_t accept_bar_baz( |
| 85 | + zone_parser_t *parser, |
| 86 | + const zone_name_t *owner, |
| 87 | + uint16_t type, |
| 88 | + uint16_t class, |
| 89 | + uint32_t ttl, |
| 90 | + uint16_t rdlength, |
| 91 | + const uint8_t *rdata, |
| 92 | + void *user_data) |
| 93 | +{ |
| 94 | + (void)parser; |
| 95 | + (void)class; |
| 96 | + (void)ttl; |
| 97 | + |
| 98 | + static const uint8_t foo[5] = { 3, 'f', 'o', 'o', 0 }; |
| 99 | + |
| 100 | + if (owner->length != 5 || memcmp(owner->octets, foo, 5) != 0) |
| 101 | + return ZONE_SYNTAX_ERROR; |
| 102 | + if (type != ZONE_TYPE_TXT) |
| 103 | + return ZONE_SYNTAX_ERROR; |
| 104 | + |
| 105 | + if (rdlength == 1 && rdata[0] == 0) { |
| 106 | + *((size_t *)user_data) += 1; |
| 107 | + return 0; |
| 108 | + } else if (rdlength > 3 && rdata[0] >= 3) { |
| 109 | + switch (*((size_t *)user_data)) { |
| 110 | + case 0: // expect bar |
| 111 | + if (memcmp(rdata+1, "bar", 3) != 0) |
| 112 | + return ZONE_SYNTAX_ERROR; |
| 113 | + break; |
| 114 | + case 1: // expect baz |
| 115 | + if (memcmp(rdata+1, "baz", 3) != 0) |
| 116 | + return ZONE_SYNTAX_ERROR; |
| 117 | + break; |
| 118 | + default: |
| 119 | + return ZONE_SYNTAX_ERROR; |
| 120 | + } |
| 121 | + |
| 122 | + *((size_t *)user_data) += 1; |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + return ZONE_SYNTAX_ERROR; |
| 127 | +} |
| 128 | + |
| 129 | +/*!cmocka */ |
| 130 | +void block_boundary(void **state) |
| 131 | +{ |
| 132 | + (void)state; |
| 133 | + |
| 134 | + static const uint8_t root[1] = { 0 }; |
| 135 | + static const struct { |
| 136 | + const char *input; size_t length; |
| 137 | + } tests[] = { |
| 138 | + { contiguous_end_last, sizeof(contiguous_end_last) }, |
| 139 | + { contiguous_end_first, sizeof(contiguous_end_first) }, |
| 140 | + { quoted_end_last, sizeof(quoted_end_last) }, |
| 141 | + { quoted_end_first, sizeof(quoted_end_first) }, |
| 142 | + { comment_end_last, sizeof(comment_end_last) }, |
| 143 | + { comment_end_first, sizeof(comment_end_first) }, |
| 144 | + { contiguous_start_last, sizeof(contiguous_start_last) }, |
| 145 | + { quoted_start_last, sizeof(quoted_start_last) }, |
| 146 | + { quoted_start_last_end_first, sizeof(quoted_start_last_end_first) }, |
| 147 | + { quoted_start_last_end_next_first, sizeof(quoted_start_last_end_next_first) }, |
| 148 | + { comment_start_last, sizeof(comment_start_last) }, |
| 149 | + { comment_start_last_end_first, sizeof(comment_start_last_end_first) }, |
| 150 | + { comment_start_last_end_next_first, sizeof(comment_start_last_end_next_first) } |
| 151 | + }; |
| 152 | + |
| 153 | + zone_parser_t parser; |
| 154 | + zone_options_t options; |
| 155 | + zone_name_buffer_t owner; |
| 156 | + zone_rdata_buffer_t rdata; |
| 157 | + zone_buffers_t buffers = { 1, &owner, &rdata }; |
| 158 | + |
| 159 | + for (int i=0, n=sizeof(tests)/sizeof(tests[0]); i < n; i++) { |
| 160 | + // allocate memory instead of using a static buffer for asan |
| 161 | + char *input = malloc(tests[i].length + 64); |
| 162 | + assert_non_null(input); |
| 163 | + memcpy(input, tests[i].input, tests[i].length); |
| 164 | + memset(&parser, 0, sizeof(parser)); |
| 165 | + memset(&options, 0, sizeof(options)); |
| 166 | + options.origin.octets = root; |
| 167 | + options.origin.length = 1; |
| 168 | + options.accept.callback = &accept_bar_baz; |
| 169 | + options.default_ttl = 3600; |
| 170 | + options.default_class = 1; |
| 171 | + |
| 172 | + fprintf(stderr, "INPUT:\n%s\n", input); |
| 173 | + |
| 174 | + size_t count = 0; |
| 175 | + int32_t code = zone_parse_string( |
| 176 | + &parser, &options, &buffers, input, tests[i].length - 1, &count); |
| 177 | + assert_int_equal(code, ZONE_SUCCESS); |
| 178 | + assert_int_equal(count, 2); |
| 179 | + free(input); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +static int32_t count_openpgp( |
| 184 | + zone_parser_t *parser, |
| 185 | + const zone_name_t *owner, |
| 186 | + uint16_t type, |
| 187 | + uint16_t class, |
| 188 | + uint32_t ttl, |
| 189 | + uint16_t rdlength, |
| 190 | + const uint8_t *rdata, |
| 191 | + void *user_data) |
| 192 | +{ |
| 193 | + (void)parser; |
| 194 | + (void)owner; |
| 195 | + (void)class; |
| 196 | + (void)ttl; |
| 197 | + (void)rdlength; |
| 198 | + (void)rdata; |
| 199 | + if (type == ZONE_TYPE_OPENPGPKEY) |
| 200 | + *((size_t *)user_data) += 1; |
| 201 | + return 0; |
| 202 | +} |
| 203 | + |
| 204 | +extern unsigned char xbounds_zone[]; |
| 205 | +extern unsigned int xbounds_zone_len; |
| 206 | + |
| 207 | +/*!cmocka */ |
| 208 | +void contiguous_on_buffer_boundary(void **state) |
| 209 | +{ |
| 210 | + // test if buffer is properly resized if token crosses boundary |
| 211 | + |
| 212 | + (void)state; |
| 213 | + |
| 214 | + static const uint8_t root[1] = { 0 }; |
| 215 | + |
| 216 | + zone_parser_t parser; |
| 217 | + memset(&parser, 0, sizeof(parser)); |
| 218 | + zone_options_t options; |
| 219 | + memset(&options, 0, sizeof(options)); |
| 220 | + options.origin.octets = root; |
| 221 | + options.origin.length = 1; |
| 222 | + options.accept.callback = &count_openpgp; |
| 223 | + options.default_ttl = 3600; |
| 224 | + options.default_class = 1; |
| 225 | + |
| 226 | + zone_name_buffer_t owner; |
| 227 | + zone_rdata_buffer_t rdata; |
| 228 | + zone_buffers_t buffers = { 1, &owner, &rdata }; |
| 229 | + |
| 230 | + // generate zone file to parse |
| 231 | + char *path = tempnam(NULL, "xbounds"); |
| 232 | + assert_non_null(path); |
| 233 | + FILE *handle = fopen(path, "wb"); |
| 234 | + assert_non_null(handle); |
| 235 | + size_t written = fwrite(xbounds_zone, 1, xbounds_zone_len, handle); |
| 236 | + assert_int_equal((int)written, xbounds_zone_len); |
| 237 | + (void)fclose(handle); |
| 238 | + size_t count = 0; |
| 239 | + int32_t code = zone_parse(&parser, &options, &buffers, path, &count); |
| 240 | + assert_int_equal(code, ZONE_SUCCESS); |
| 241 | + assert_int_equal(count, 3); |
| 242 | + free(path); |
| 243 | +} |
0 commit comments