test_yparser.c 6.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <tap/basic.h>

22
#include "libknot/yparser/yparser.h"
23
#include "libknot/libknot.h"
24

25
const char *syntax_ok =
26 27
	"#comment\n"
	" # comment\n"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	"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"
	"\n"
	"c: [a,b]\n"
	"c: [a, b]\n"
	"c: [a ,b]\n"
	"c: [a , b]\n"
	"c: [ a , b ]\n"
	"\n"
52 53
	"- d: d\n"
	"- d : d # comment\n"
54
	"\n"
55 56 57
	"e: \"a#b' c[d,]\"\n"
	"\n"
	"zone:\n"
58 59 60
	"#comment\n"
	" # comment\n"
	"  -   domain: example. # comment\n"
61 62
	"      master: bind\n"
	"  - domain: example.\n"
63 64 65
	"    master: bind\n"
	"zone2:\n"
	"    - a: b # different indentation";
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

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";
81 82 83 84 85 86 87 88

int main(int argc, char *argv[])
{
	plan_lazy();

	int ret;
	size_t line;

89 90 91
	yp_parser_t yparser;
	yp_parser_t *yp = &yparser;
	yp_init(yp);
92

93 94
	// OK input.
	ret = yp_set_input_string(yp, syntax_ok, strlen(syntax_ok));
95 96
	ok(ret == KNOT_EOK, "set input string");

97
	line = 3;
98 99 100 101 102 103 104 105
	for (int i = 0; i < 3; i++) {
		ret = yp_parse(yp);
		ok(ret == KNOT_EOK, "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);
	}

106
	line = 7;
107
	for (int i = 0; i < 6; i++) {
108 109 110
		ret = yp_parse(yp);
		ok(ret == KNOT_EOK, "parse %i. key0 with value", i);
		ok(yp->key_len == 1 && yp->key[0] == 'b' &&
111
		   yp->data_len == 1 && yp->data[0] == 'b' &&
112 113 114 115
		   yp->event == YP_EKEY0 && yp->line_count == line + i,
		   "compare %i. key0 with value", i);
	}

116
	line = 14;
117
	for (int i = 0; i < 6; i++) {
118 119 120
		ret = yp_parse(yp);
		ok(ret == KNOT_EOK, "parse %i. key1 with value", i);
		ok(yp->key_len == 1 && yp->key[0] == 'f' &&
121
		   yp->data_len == 1 && yp->data[0] == 'f' &&
122 123 124 125
		   yp->event == YP_EKEY1 && yp->line_count == line + i,
		   "compare %i. key1 with value", i);
	}

126
	line = 21;
127
	for (int i = 0; i < 5; i++) {
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		ret = yp_parse(yp);
		ok(ret == KNOT_EOK, "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);
		ok(ret == KNOT_EOK, "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);
	}

143
	line = 27;
144
	for (int i = 0; i < 2; i++) {
145 146 147
		ret = yp_parse(yp);
		ok(ret == KNOT_EOK, "parse %i. id", i);
		ok(yp->key_len == 1 && yp->key[0] == 'd' &&
148
		   yp->data_len == 1 && yp->data[0] == 'd' &&
149 150 151 152
		   yp->event == YP_EID && yp->line_count == line + i,
		   "compare %i. id", i);
	}

153
	line = 30;
154 155 156 157 158 159 160
	ret = yp_parse(yp);
	ok(ret == KNOT_EOK, "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");

161
	line = 32;
162 163 164 165 166 167 168
	ret = yp_parse(yp);
	ok(ret == KNOT_EOK, "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");

169
	line = 35;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	for (int i = 0; i < 2; i++) {
		ret = yp_parse(yp);
		ok(ret == KNOT_EOK, "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);
		ok(ret == KNOT_EOK, "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");
	}

185 186 187 188 189 190 191 192 193 194 195 196 197 198
	line = 39;
	ret = yp_parse(yp);
	ok(ret == KNOT_EOK, "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);
	ok(ret == KNOT_EOK, "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");

199 200 201
	ret = yp_parse(yp);
	ok(ret == KNOT_EOF, "parse EOF");

202 203 204 205 206 207 208 209
	// Error input 1.
	ret = yp_set_input_string(yp, syntax_error1, strlen(syntax_error1));
	ok(ret == KNOT_EOK, "set error input string 1");
	ret = yp_parse(yp);
	ok(ret == KNOT_EOK, "parse key0");
	ret = yp_parse(yp);
	ok(ret == KNOT_EOK, "parse key1");
	ret = yp_parse(yp);
210
	ok(ret == KNOT_YP_EINVAL_INDENT, "parse key1 - invalid indentation");
211 212 213 214 215 216 217 218 219

	// Error input 2.
	ret = yp_set_input_string(yp, syntax_error2, strlen(syntax_error2));
	ok(ret == KNOT_EOK, "set error input string 2");
	ret = yp_parse(yp);
	ok(ret == KNOT_EOK, "parse key0");
	ret = yp_parse(yp);
	ok(ret == KNOT_EOK, "parse key1");
	ret = yp_parse(yp);
220
	ok(ret == KNOT_YP_EINVAL_INDENT, "parse key1 - invalid indentation");
221 222 223 224 225 226 227 228 229

	// Error input 3.
	ret = yp_set_input_string(yp, syntax_error3, strlen(syntax_error3));
	ok(ret == KNOT_EOK, "set error input string 3");
	ret = yp_parse(yp);
	ok(ret == KNOT_EOK, "parse key0");
	ret = yp_parse(yp);
	ok(ret == KNOT_EOK, "parse key1");
	ret = yp_parse(yp);
230
	ok(ret == KNOT_YP_EINVAL_INDENT, "parse key1 - invalid indentation");
231

232
	yp_deinit(yp);
233 234 235

	return 0;
}