yparser.c 5.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*  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>

#include "libknot/internal/yparser/yparser.h"
#include "libknot/errcode.h"

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

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";
75 76 77 78 79 80 81 82

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

	int ret;
	size_t line;

83 84 85
	yp_parser_t yparser;
	yp_parser_t *yp = &yparser;
	yp_init(yp);
86

87 88
	// OK input.
	ret = yp_set_input_string(yp, syntax_ok, strlen(syntax_ok));
89 90 91 92 93 94 95 96 97 98 99 100
	ok(ret == KNOT_EOK, "set input string");

	line = 1;
	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);
	}

	line = 5;
101
	for (int i = 0; i < 6; i++) {
102 103 104 105 106 107 108 109
		ret = yp_parse(yp);
		ok(ret == KNOT_EOK, "parse %i. key0 with value", i);
		ok(yp->key_len == 1 && yp->key[0] == 'b' &&
		   yp->data_len == 1 && yp->key[0] == 'b' &&
		   yp->event == YP_EKEY0 && yp->line_count == line + i,
		   "compare %i. key0 with value", i);
	}

110 111
	line = 12;
	for (int i = 0; i < 6; i++) {
112 113 114 115 116 117 118 119
		ret = yp_parse(yp);
		ok(ret == KNOT_EOK, "parse %i. key1 with value", i);
		ok(yp->key_len == 1 && yp->key[0] == 'f' &&
		   yp->data_len == 1 && yp->key[0] == 'f' &&
		   yp->event == YP_EKEY1 && yp->line_count == line + i,
		   "compare %i. key1 with value", i);
	}

120 121
	line = 19;
	for (int i = 0; i < 5; i++) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		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);
	}

137 138
	line = 25;
	for (int i = 0; i < 2; i++) {
139 140 141 142 143 144 145 146
		ret = yp_parse(yp);
		ok(ret == KNOT_EOK, "parse %i. id", i);
		ok(yp->key_len == 1 && yp->key[0] == 'd' &&
		   yp->data_len == 1 && yp->key[0] == 'd' &&
		   yp->event == YP_EID && yp->line_count == line + i,
		   "compare %i. id", i);
	}

147
	line = 28;
148 149 150 151 152 153 154
	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");

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	line = 30;
	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");

	line = 31;
	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");
	}

179 180 181
	ret = yp_parse(yp);
	ok(ret == KNOT_EOF, "parse EOF");

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
	// 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);
	ok(ret == KNOT_EPARSEFAIL, "parse invalid key1");

	// 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);
	ok(ret == KNOT_EPARSEFAIL, "parse invalid key1");

	// 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);
	ok(ret == KNOT_EPARSEFAIL, "parse invalid key1");

212
	yp_deinit(yp);
213 214 215

	return 0;
}