| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#define UNIT_TESTING |
| 24 |
#include <cmocka.h> |
| 25 |
|
| 26 |
#include <isc/buffer.h> |
| 27 |
#include <isc/lex.h> |
| 28 |
#include <isc/mem.h> |
| 29 |
#include <isc/util.h> |
| 30 |
|
| 31 |
#include <tests/isc.h> |
| 32 |
|
| 33 |
#define AS_STR(x) (x).value.as_textregion.base |
| 34 |
|
| 35 |
/* check handling of 0xff */ |
| 36 |
ISC_RUN_TEST_IMPL(lex_0xff) { |
| 37 |
isc_result_t result; |
| 38 |
isc_lex_t *lex = NULL; |
| 39 |
isc_buffer_t death_buf; |
| 40 |
isc_token_t token; |
| 41 |
|
| 42 |
unsigned char death[] = { EOF, 'A' }; |
| 43 |
|
| 44 |
UNUSED(state); |
| 45 |
|
| 46 |
isc_lex_create(mctx, 1024, &lex); |
| 47 |
|
| 48 |
isc_buffer_init(&death_buf, &death[0], sizeof(death)); |
| 49 |
isc_buffer_add(&death_buf, sizeof(death)); |
| 50 |
|
| 51 |
result = isc_lex_openbuffer(lex, &death_buf); |
| 52 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 53 |
|
| 54 |
result = isc_lex_gettoken(lex, 0, &token); |
| 55 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 56 |
|
| 57 |
isc_lex_destroy(&lex); |
| 58 |
} |
| 59 |
|
| 60 |
/* check setting of source line */ |
| 61 |
ISC_RUN_TEST_IMPL(lex_setline) { |
| 62 |
isc_result_t result; |
| 63 |
isc_lex_t *lex = NULL; |
| 64 |
unsigned char text[] = "text\nto\nbe\nprocessed\nby\nlexer"; |
| 65 |
isc_buffer_t buf; |
| 66 |
isc_token_t token; |
| 67 |
unsigned long line; |
| 68 |
int i; |
| 69 |
|
| 70 |
UNUSED(state); |
| 71 |
|
| 72 |
isc_lex_create(mctx, 1024, &lex); |
| 73 |
|
| 74 |
isc_buffer_init(&buf, &text[0], sizeof(text)); |
| 75 |
isc_buffer_add(&buf, sizeof(text)); |
| 76 |
|
| 77 |
result = isc_lex_openbuffer(lex, &buf); |
| 78 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 79 |
|
| 80 |
result = isc_lex_setsourceline(lex, 100); |
| 81 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 82 |
|
| 83 |
for (i = 0; i < 6; i++) { |
| 84 |
result = isc_lex_gettoken(lex, 0, &token); |
| 85 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 86 |
|
| 87 |
line = isc_lex_getsourceline(lex); |
| 88 |
assert_int_equal(line, 100U + i); |
| 89 |
} |
| 90 |
|
| 91 |
result = isc_lex_gettoken(lex, 0, &token); |
| 92 |
assert_int_equal(result, ISC_R_EOF); |
| 93 |
|
| 94 |
line = isc_lex_getsourceline(lex); |
| 95 |
assert_int_equal(line, 105U); |
| 96 |
|
| 97 |
isc_lex_destroy(&lex); |
| 98 |
} |
| 99 |
|
| 100 |
static struct { |
| 101 |
const char *text; |
| 102 |
const char *string_value; |
| 103 |
isc_result_t string_result; |
| 104 |
isc_tokentype_t string_type; |
| 105 |
const char *qstring_value; |
| 106 |
isc_result_t qstring_result; |
| 107 |
isc_tokentype_t qstring_type; |
| 108 |
const char *qvpair_value; |
| 109 |
isc_result_t qvpair_result; |
| 110 |
isc_tokentype_t qvpair_type; |
| 111 |
} parse_tests[] = { |
| 112 |
{ "", "", ISC_R_SUCCESS, isc_tokentype_eof, "", ISC_R_SUCCESS, |
| 113 |
isc_tokentype_eof, "", ISC_R_SUCCESS, isc_tokentype_eof }, |
| 114 |
{ "1234", "1234", ISC_R_SUCCESS, isc_tokentype_string, "1234", |
| 115 |
ISC_R_SUCCESS, isc_tokentype_string, "1234", ISC_R_SUCCESS, |
| 116 |
isc_tokentype_string }, |
| 117 |
{ "1234=", "1234=", ISC_R_SUCCESS, isc_tokentype_string, |
| 118 |
"1234=", ISC_R_SUCCESS, isc_tokentype_string, "1234=", ISC_R_SUCCESS, |
| 119 |
isc_tokentype_vpair }, |
| 120 |
{ "1234=foo", "1234=foo", ISC_R_SUCCESS, isc_tokentype_string, |
| 121 |
"1234=foo", ISC_R_SUCCESS, isc_tokentype_string, "1234=foo", |
| 122 |
ISC_R_SUCCESS, isc_tokentype_vpair }, |
| 123 |
{ "1234=\"foo", "1234=\"foo", ISC_R_SUCCESS, isc_tokentype_string, |
| 124 |
"1234=\"foo", ISC_R_SUCCESS, isc_tokentype_string, NULL, |
| 125 |
ISC_R_UNEXPECTEDEND, 0 }, |
| 126 |
{ "1234=\"foo\"", "1234=\"foo\"", ISC_R_SUCCESS, isc_tokentype_string, |
| 127 |
"1234=\"foo\"", ISC_R_SUCCESS, isc_tokentype_string, "1234=foo", |
| 128 |
ISC_R_SUCCESS, isc_tokentype_qvpair }, |
| 129 |
{ "key", "key", ISC_R_SUCCESS, isc_tokentype_string, "key", |
| 130 |
ISC_R_SUCCESS, isc_tokentype_string, "key", ISC_R_SUCCESS, |
| 131 |
isc_tokentype_string }, |
| 132 |
{ "\"key=", "\"key=", ISC_R_SUCCESS, isc_tokentype_string, NULL, |
| 133 |
ISC_R_UNEXPECTEDEND, 0, "\"key=", ISC_R_SUCCESS, |
| 134 |
isc_tokentype_vpair }, |
| 135 |
{ "\"key=\"", "\"key=\"", ISC_R_SUCCESS, isc_tokentype_string, "key=", |
| 136 |
ISC_R_SUCCESS, isc_tokentype_qstring, NULL, ISC_R_UNEXPECTEDEND, 0 }, |
| 137 |
{ "key=\"\"", "key=\"\"", ISC_R_SUCCESS, isc_tokentype_string, |
| 138 |
"key=\"\"", ISC_R_SUCCESS, isc_tokentype_string, |
| 139 |
"key=", ISC_R_SUCCESS, isc_tokentype_qvpair }, |
| 140 |
{ "key=\"a b\"", "key=\"a", ISC_R_SUCCESS, isc_tokentype_string, |
| 141 |
"key=\"a", ISC_R_SUCCESS, isc_tokentype_string, "key=a b", |
| 142 |
ISC_R_SUCCESS, isc_tokentype_qvpair }, |
| 143 |
{ "key=\"a\tb\"", "key=\"a", ISC_R_SUCCESS, isc_tokentype_string, |
| 144 |
"key=\"a", ISC_R_SUCCESS, isc_tokentype_string, "key=a\tb", |
| 145 |
ISC_R_SUCCESS, isc_tokentype_qvpair }, |
| 146 |
/* double quote not immediately after '=' is not special. */ |
| 147 |
{ "key=c\"a b\"", "key=c\"a", ISC_R_SUCCESS, isc_tokentype_string, |
| 148 |
"key=c\"a", ISC_R_SUCCESS, isc_tokentype_string, "key=c\"a", |
| 149 |
ISC_R_SUCCESS, isc_tokentype_vpair }, |
| 150 |
/* remove special meaning for '=' by escaping */ |
| 151 |
{ "key\\=", "key\\=", ISC_R_SUCCESS, isc_tokentype_string, |
| 152 |
"key\\=", ISC_R_SUCCESS, isc_tokentype_string, |
| 153 |
"key\\=", ISC_R_SUCCESS, isc_tokentype_string }, |
| 154 |
{ "key\\=\"a\"", "key\\=\"a\"", ISC_R_SUCCESS, isc_tokentype_string, |
| 155 |
"key\\=\"a\"", ISC_R_SUCCESS, isc_tokentype_string, "key\\=\"a\"", |
| 156 |
ISC_R_SUCCESS, isc_tokentype_string }, |
| 157 |
{ "key\\=\"a \"", "key\\=\"a", ISC_R_SUCCESS, isc_tokentype_string, |
| 158 |
"key\\=\"a", ISC_R_SUCCESS, isc_tokentype_string, "key\\=\"a", |
| 159 |
ISC_R_SUCCESS, isc_tokentype_string }, |
| 160 |
/* vpair with a key of 'key\=' (would need to be deescaped) */ |
| 161 |
{ "key\\==", "key\\==", ISC_R_SUCCESS, isc_tokentype_string, |
| 162 |
"key\\==", ISC_R_SUCCESS, isc_tokentype_string, |
| 163 |
"key\\==", ISC_R_SUCCESS, isc_tokentype_vpair }, |
| 164 |
{ "key\\==\"\"", "key\\==\"\"", ISC_R_SUCCESS, isc_tokentype_string, |
| 165 |
"key\\==\"\"", ISC_R_SUCCESS, isc_tokentype_string, |
| 166 |
"key\\==", ISC_R_SUCCESS, isc_tokentype_qvpair }, |
| 167 |
{ "key=\\\\\\\\", "key=\\\\\\\\", ISC_R_SUCCESS, isc_tokentype_string, |
| 168 |
"key=\\\\\\\\", ISC_R_SUCCESS, isc_tokentype_string, "key=\\\\\\\\", |
| 169 |
ISC_R_SUCCESS, isc_tokentype_vpair }, |
| 170 |
{ "key=\\\\\\\"", "key=\\\\\\\"", ISC_R_SUCCESS, isc_tokentype_string, |
| 171 |
"key=\\\\\\\"", ISC_R_SUCCESS, isc_tokentype_string, "key=\\\\\\\"", |
| 172 |
ISC_R_SUCCESS, isc_tokentype_vpair }, |
| 173 |
/* incomplete escape sequence */ |
| 174 |
{ "key=\\\"\\", NULL, ISC_R_UNEXPECTEDEND, isc_tokentype_string, NULL, |
| 175 |
ISC_R_UNEXPECTEDEND, 0, NULL, ISC_R_UNEXPECTEDEND, 0 }, |
| 176 |
/* incomplete escape sequence */ |
| 177 |
{ "key=\\", NULL, ISC_R_UNEXPECTEDEND, isc_tokentype_string, NULL, |
| 178 |
ISC_R_UNEXPECTEDEND, 0, NULL, ISC_R_UNEXPECTEDEND, 0 }, |
| 179 |
}; |
| 180 |
|
| 181 |
/*% |
| 182 |
* string |
| 183 |
*/ |
| 184 |
ISC_RUN_TEST_IMPL(lex_string) { |
| 185 |
isc_buffer_t buf; |
| 186 |
isc_lex_t *lex = NULL; |
| 187 |
isc_result_t result; |
| 188 |
isc_token_t token; |
| 189 |
size_t i; |
| 190 |
|
| 191 |
UNUSED(state); |
| 192 |
|
| 193 |
for (i = 0; i < ARRAY_SIZE(parse_tests); i++) { |
| 194 |
isc_lex_create(mctx, 1024, &lex); |
| 195 |
|
| 196 |
isc_buffer_constinit(&buf, parse_tests[i].text, |
| 197 |
strlen(parse_tests[i].text)); |
| 198 |
isc_buffer_add(&buf, strlen(parse_tests[i].text)); |
| 199 |
|
| 200 |
result = isc_lex_openbuffer(lex, &buf); |
| 201 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 202 |
|
| 203 |
result = isc_lex_setsourceline(lex, 100); |
| 204 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 205 |
|
| 206 |
memset(&token, 0, sizeof(token)); |
| 207 |
result = isc_lex_getmastertoken(lex, &token, |
| 208 |
isc_tokentype_string, true); |
| 209 |
|
| 210 |
assert_int_equal(result, parse_tests[i].string_result); |
| 211 |
if (result == ISC_R_SUCCESS) { |
| 212 |
switch (token.type) { |
| 213 |
case isc_tokentype_string: |
| 214 |
case isc_tokentype_qstring: |
| 215 |
case isc_tokentype_vpair: |
| 216 |
case isc_tokentype_qvpair: |
| 217 |
assert_int_equal(token.type, |
| 218 |
parse_tests[i].string_type); |
| 219 |
assert_string_equal( |
| 220 |
AS_STR(token), |
| 221 |
parse_tests[i].string_value); |
| 222 |
break; |
| 223 |
default: |
| 224 |
assert_int_equal(token.type, |
| 225 |
parse_tests[i].string_type); |
| 226 |
break; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
isc_lex_destroy(&lex); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/*% |
| 235 |
* qstring |
| 236 |
*/ |
| 237 |
ISC_RUN_TEST_IMPL(lex_qstring) { |
| 238 |
isc_buffer_t buf; |
| 239 |
isc_lex_t *lex = NULL; |
| 240 |
isc_result_t result; |
| 241 |
isc_token_t token; |
| 242 |
size_t i; |
| 243 |
|
| 244 |
UNUSED(state); |
| 245 |
|
| 246 |
for (i = 0; i < ARRAY_SIZE(parse_tests); i++) { |
| 247 |
isc_lex_create(mctx, 1024, &lex); |
| 248 |
|
| 249 |
isc_buffer_constinit(&buf, parse_tests[i].text, |
| 250 |
strlen(parse_tests[i].text)); |
| 251 |
isc_buffer_add(&buf, strlen(parse_tests[i].text)); |
| 252 |
|
| 253 |
result = isc_lex_openbuffer(lex, &buf); |
| 254 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 255 |
|
| 256 |
result = isc_lex_setsourceline(lex, 100); |
| 257 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 258 |
|
| 259 |
memset(&token, 0, sizeof(token)); |
| 260 |
result = isc_lex_getmastertoken(lex, &token, |
| 261 |
isc_tokentype_qstring, true); |
| 262 |
|
| 263 |
assert_int_equal(result, parse_tests[i].qstring_result); |
| 264 |
if (result == ISC_R_SUCCESS) { |
| 265 |
switch (token.type) { |
| 266 |
case isc_tokentype_string: |
| 267 |
case isc_tokentype_qstring: |
| 268 |
case isc_tokentype_vpair: |
| 269 |
case isc_tokentype_qvpair: |
| 270 |
assert_int_equal(token.type, |
| 271 |
parse_tests[i].qstring_type); |
| 272 |
assert_string_equal( |
| 273 |
AS_STR(token), |
| 274 |
parse_tests[i].qstring_value); |
| 275 |
break; |
| 276 |
default: |
| 277 |
assert_int_equal(token.type, |
| 278 |
parse_tests[i].qstring_type); |
| 279 |
break; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
isc_lex_destroy(&lex); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/*% |
| 288 |
* keypair is <string>=<qstring>. This has implications double quotes |
| 289 |
* in key names. |
| 290 |
*/ |
| 291 |
ISC_RUN_TEST_IMPL(lex_keypair) { |
| 292 |
isc_buffer_t buf; |
| 293 |
isc_lex_t *lex = NULL; |
| 294 |
isc_result_t result; |
| 295 |
isc_token_t token; |
| 296 |
size_t i; |
| 297 |
|
| 298 |
UNUSED(state); |
| 299 |
|
| 300 |
for (i = 0; i < ARRAY_SIZE(parse_tests); i++) { |
| 301 |
isc_lex_create(mctx, 1024, &lex); |
| 302 |
|
| 303 |
isc_buffer_constinit(&buf, parse_tests[i].text, |
| 304 |
strlen(parse_tests[i].text)); |
| 305 |
isc_buffer_add(&buf, strlen(parse_tests[i].text)); |
| 306 |
|
| 307 |
result = isc_lex_openbuffer(lex, &buf); |
| 308 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 309 |
|
| 310 |
result = isc_lex_setsourceline(lex, 100); |
| 311 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 312 |
|
| 313 |
memset(&token, 0, sizeof(token)); |
| 314 |
result = isc_lex_getmastertoken(lex, &token, |
| 315 |
isc_tokentype_qvpair, true); |
| 316 |
|
| 317 |
assert_int_equal(result, parse_tests[i].qvpair_result); |
| 318 |
if (result == ISC_R_SUCCESS) { |
| 319 |
switch (token.type) { |
| 320 |
case isc_tokentype_string: |
| 321 |
case isc_tokentype_qstring: |
| 322 |
case isc_tokentype_vpair: |
| 323 |
case isc_tokentype_qvpair: |
| 324 |
assert_int_equal(token.type, |
| 325 |
parse_tests[i].qvpair_type); |
| 326 |
assert_string_equal( |
| 327 |
AS_STR(token), |
| 328 |
parse_tests[i].qvpair_value); |
| 329 |
break; |
| 330 |
default: |
| 331 |
assert_int_equal(token.type, |
| 332 |
parse_tests[i].qvpair_type); |
| 333 |
break; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
isc_lex_destroy(&lex); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
ISC_TEST_LIST_START |
| 342 |
ISC_TEST_ENTRY(lex_0xff) |
| 343 |
ISC_TEST_ENTRY(lex_keypair) |
| 344 |
ISC_TEST_ENTRY(lex_setline) |
| 345 |
ISC_TEST_ENTRY(lex_string) |
| 346 |
ISC_TEST_ENTRY(lex_qstring) |
| 347 |
ISC_TEST_LIST_END |
| 348 |
|
| 349 |
ISC_TEST_MAIN |