1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/* Copyright (C) CZ.NIC, z.s.p.o. and contributors
* SPDX-License-Identifier: GPL-2.0-or-later
* For more information, see <https://www.knot-dns.cz/>
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <tap/basic.h>
#include "libknot/yparser/yparser.h"
#include "libknot/libknot.h"
const char *syntax_ok =
"#comment\n"
" # comment\n"
"a:\n"
"a :\n"
"a: \"\"\n"
"a: []\n"
"a: [ ]\n"
"a : #comment\n"
"\n"
"b: \"b\"\n"
"b: b #comment\n"
"b : b\n"
"b: [ b] # comment\n"
"b: [b ]\n"
"b: [ b ]\n"
"\n"
" f: \"f\"\n"
" f: f #comment\n"
" f : f\n"
" f: [ f] # comment\n"
" f: [f ]\n"
" f: [ f ]\n"
" f: [ \"f\" ]\n"
"\n"
"c: [a,b]\n"
"c: [a, b]\n"
"c: [a ,b]\n"
"c: [a , b]\n"
"c: [ a , b ]\n"
"c: [ \"a\" , \"b\" ]\n"
"\n"
"- d: d\n"
"- d : d # comment\n"
"\n"
"e: \"a#b' c[d,]\"\n"
"\n"
"zone:\n"
"#comment\n"
" # comment\n"
" - domain: example. # comment\n"
" master: bind\n"
" - domain: example.\n"
" master: bind\n"
"zone2:\n"
" - a: b # different indentation";
const char *syntax_error1 =
"f:\n"
" - a: b\n"
" - b: c\n";
const char *syntax_error2 =
"f:\n"
" - a: b\n"
" c: d\n";
const char *syntax_error3 =
"f:\n"
" a: b\n"
" c: d\n";
const char *tab_error1 =
"a:\n"
"b:\t\n";
const char *tab_error2 =
"a:\n"
"b: c\t\n";
const char *tab_error3 =
"a:\n"
"\t\n";
const char *dname_ok =
".:\n"
"dom-ain:\n"
"\\070-\\071.\\072.:\n"
"*.wildchar.com:\n"
"_ldap._tcp.example.com:\n";
const char *quotes_ok =
"g: \"\"\n"
"g: a\\ b\n"
"g: \"\\# 1 00\"\n"
"g: \"\\\"\\\"\"\n"
"g: \" a \\\" b \\\" \\\"c\\\" \"\n"
"g: \"\\@ \\[ \\# \\, \\]\"\n";
const char *utf8_ok =
"key: příšera\n";
static void test_syntax_ok(yp_parser_t *yp)
{
// OK input.
int ret = yp_set_input_string(yp, syntax_ok, strlen(syntax_ok));
is_int(KNOT_EOK, ret, "set input string");
size_t line = 3;
for (int i = 0; i < 6; i++) {
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse %i. key0", i);
ok(yp->key_len == 1 && yp->key[0] == 'a' &&
yp->data_len == 0 && yp->event == YP_EKEY0 &&
yp->line_count == line + i, "compare %i. key0", i);
}
line += 7;
for (int i = 0; i < 6; i++) {
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse %i. key0 with value", i);
ok(yp->key_len == 1 && yp->key[0] == 'b' &&
yp->data_len == 1 && yp->data[0] == 'b' &&
yp->event == YP_EKEY0 && yp->line_count == line + i,
"compare %i. key0 with value", i);
}
line += 7;
for (int i = 0; i < 7; i++) {
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse %i. key1 with value", i);
ok(yp->key_len == 1 && yp->key[0] == 'f' &&
yp->data_len == 1 && yp->data[0] == 'f' &&
yp->event == YP_EKEY1 && yp->line_count == line + i,
"compare %i. key1 with value", i);
}
line += 8;
for (int i = 0; i < 6; i++) {
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse %i. key0 with first value", i);
ok(yp->key_len == 1 && yp->key[0] == 'c' &&
yp->data_len == 1 && yp->data[0] == 'a' &&
yp->event == YP_EKEY0 && yp->line_count == line + i,
"compare %i. key0 with first value", i);
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse %i. key0 with second value", i);
ok(yp->key_len == 1 && yp->key[0] == 'c' &&
yp->data_len == 1 && yp->data[0] == 'b' &&
yp->event == YP_EKEY0 && yp->line_count == line + i,
"compare %i. key0 with second value", i);
}
line += 7;
for (int i = 0; i < 2; i++) {
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse %i. id", i);
ok(yp->key_len == 1 && yp->key[0] == 'd' &&
yp->data_len == 1 && yp->data[0] == 'd' &&
yp->event == YP_EID && yp->line_count == line + i,
"compare %i. id", i);
}
line += 3;
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse key0 with quoted value");
ok(yp->key_len == 1 && yp->key[0] == 'e' && yp->data_len == 10 &&
memcmp(yp->data, "a#b' c[d,]", yp->data_len) == 0 &&
yp->event == YP_EKEY0 && yp->line_count == line,
"compare key0 with quoted value");
line += 2;
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse key0");
ok(yp->key_len == 4 && strcmp(yp->key, "zone") == 0 &&
yp->data_len == 0 &&
yp->event == YP_EKEY0 && yp->line_count == line,
"compare key0 value");
line += 3;
for (int i = 0; i < 2; i++) {
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse %i. id", i);
ok(yp->key_len == 6 && strcmp(yp->key, "domain") == 0 &&
yp->data_len == 8 && strcmp(yp->data, "example.") == 0 &&
yp->event == YP_EID && yp->line_count == line + 2 * i,
"compare id");
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse %i. key1", i);
ok(yp->key_len == 6 && strcmp(yp->key, "master") == 0 &&
yp->data_len == 4 && strcmp(yp->data, "bind") == 0 &&
yp->event == YP_EKEY1 && yp->line_count == line + 2 * i + 1,
"compare key1");
}
line += 4;
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse key0");
ok(yp->key_len == 5 && strcmp(yp->key, "zone2") == 0 &&
yp->data_len == 0 &&
yp->event == YP_EKEY0 && yp->line_count == line,
"compare key0 value");
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse key1");
ok(yp->key_len == 1 && strcmp(yp->key, "a") == 0 &&
yp->data_len == 1 && strcmp(yp->data, "b") == 0 &&
yp->event == YP_EID && yp->line_count == line + 1,
"compare key1 value");
ret = yp_parse(yp);
is_int(KNOT_EOF, ret, "parse EOF");
}
static void test_syntax_error(yp_parser_t *yp, const char *input)
{
static int count = 1;
int ret = yp_set_input_string(yp, input, strlen(input));
is_int(KNOT_EOK, ret, "set syntax error input string %i", count++);
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse key0");
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse key1");
ret = yp_parse(yp);
is_int(KNOT_YP_EINVAL_INDENT, ret, "parse key1 - invalid indentation");
is_int(yp->line_count, 3, "invalid indentation line");
}
static void test_tab_error(yp_parser_t *yp, const char *input)
{
static int count = 1;
int ret = yp_set_input_string(yp, input, strlen(input));
is_int(KNOT_EOK, ret, "set tab error input string %i", count++);
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse key0");
ret = yp_parse(yp);
is_int(KNOT_YP_ECHAR_TAB, ret, "invalid tabulator");
is_int(yp->line_count, 2, "invalid tabulator line");
}
static void test_dname(yp_parser_t *yp)
{
#define CHECK_DNAME(str) \
ret = yp_parse(yp); \
is_int(KNOT_EOK, ret, "parse dname " str); \
ok(yp->key_len == strlen(str) && strcmp(yp->key, str) == 0 && yp->data_len == 0 && \
yp->event == YP_EKEY0 && yp->line_count == line++, "compare " str);
// Dname key value.
int ret = yp_set_input_string(yp, dname_ok, strlen(dname_ok));
is_int(KNOT_EOK, ret, "set input string");
size_t line = 1;
CHECK_DNAME(".");
CHECK_DNAME("dom-ain");
CHECK_DNAME("\\070-\\071.\\072.");
CHECK_DNAME("*.wildchar.com");
CHECK_DNAME("_ldap._tcp.example.com");
}
static void test_quotes(yp_parser_t *yp)
{
#define CHECK_QUOTE(str) \
ret = yp_parse(yp); \
is_int(KNOT_EOK, ret, "parse quoted " str); \
ok(yp->key_len == 1 && yp->key[0] == 'g' && \
yp->data_len == strlen(str) && strcmp(yp->data, str) == 0 && \
yp->event == YP_EKEY0 && yp->line_count == line++, "compare " str);
int ret = yp_set_input_string(yp, quotes_ok, strlen(quotes_ok));
is_int(KNOT_EOK, ret, "set input string");
size_t line = 1;
CHECK_QUOTE("");
CHECK_QUOTE("a\\ b");
CHECK_QUOTE("\\# 1 00");
CHECK_QUOTE("\"\"");
CHECK_QUOTE(" a \" b \" \"c\" ");
CHECK_QUOTE("\\@ \\[ \\# \\, \\]");
}
// Check that wrong wildcard dname is NOT parsed as valid dname.
static void test_wildcard(yp_parser_t *yp)
{
#define CHECK_NOT_WILDCARD(str) \
ret = yp_set_input_string(yp, str, strlen(str)); \
is_int(KNOT_EOK, ret, "set input string");\
ret = yp_parse(yp); \
is_int(KNOT_EPARSEFAIL, ret, str " is not wildcard"); \
ok(yp->key_len != strlen(str) || strcmp(yp->key, str) != 0 || \
yp->event != YP_EKEY0, "compare " str);
int ret;
CHECK_NOT_WILDCARD("a.*.example.com.");
CHECK_NOT_WILDCARD("**.example.com.");
CHECK_NOT_WILDCARD("*example.com.");
}
static void test_utf8(yp_parser_t *yp)
{
int ret = yp_set_input_string(yp, utf8_ok, strlen(utf8_ok));
is_int(KNOT_EOK, ret, "set input string");
ret = yp_parse(yp);
is_int(KNOT_EOK, ret, "parse key with a value in UTF-8");
ok(yp->key_len == 3 && strcmp(yp->key, "key") == 0 &&
yp->data_len == 10 && strcmp(yp->data, "p""\xC5\x99\xC3\xAD\xC5\xA1""era") == 0,
"compare UTF-8 value");
}
int main(int argc, char *argv[])
{
plan_lazy();
yp_parser_t yp;
yp_init(&yp);
test_syntax_ok(&yp);
test_syntax_error(&yp, syntax_error1);
test_syntax_error(&yp, syntax_error2);
test_syntax_error(&yp, syntax_error3);
test_tab_error(&yp, tab_error1);
test_tab_error(&yp, tab_error2);
test_tab_error(&yp, tab_error3);
test_dname(&yp);
test_quotes(&yp);
test_wildcard(&yp);
test_utf8(&yp);
yp_deinit(&yp);
return 0;
}
|