summaryrefslogtreecommitdiff
path: root/test/lex-auto.c
blob: e7e7c83977670e224da458880108581168f32025 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <parserutils/input/inputstream.h>

#include <libcss/libcss.h>

#include "charset/detect.h"
#include "lex/lex.h"
#include "utils/utils.h"

#include "testutils.h"

typedef struct exp_entry {
	css_token_type type;
#define EXP_ENTRY_TEXT_LEN (128)
	char text[EXP_ENTRY_TEXT_LEN];
	size_t textLen;
	bool hasText;
} exp_entry;

typedef struct line_ctx {
	size_t buflen;
	size_t bufused;
	uint8_t *buf;

	size_t explen;
	size_t expused;
	exp_entry *exp;

	bool indata;
	bool inexp;
} line_ctx;

static bool handle_line(const char *data, size_t datalen, void *pw);
static void css__parse_expected(line_ctx *ctx, const char *data, size_t len);
static const char *string_from_type(css_token_type type);
static css_token_type string_to_type(const char *data, size_t len);
static void run_test(const uint8_t *data, size_t len,
		exp_entry *exp, size_t explen);

static void test_logger(
		void *_ctx, nslog_entry_context_t *ctx,
		const char *fmt,
		va_list args)
{
	UNUSED(_ctx);
	fprintf(stderr, "%s: %s: %s:%i: ",
			nslog_level_name(ctx->level),
			ctx->category->description,
			ctx->filename, ctx->lineno);
	vfprintf(stderr, fmt, args);
	fprintf(stderr, "\n");
}

int main(int argc, char **argv)
{
	line_ctx ctx;

	if (argc != 2) {
		printf("Usage: %s <filename>\n", argv[0]);
		return 1;
	}

	if (nslog_set_render_callback(test_logger, NULL) != NSLOG_NO_ERROR) {
		printf("Unable to set log render callback\n");
		return 1;
	}

	if (nslog_uncork() != NSLOG_NO_ERROR) {
		printf("Failed to uncork log.\n");
		return 1;
	}

	ctx.buflen = css__parse_filesize(argv[1]);
	if (ctx.buflen == 0)
		return 1;

	ctx.buf = malloc(ctx.buflen);
	if (ctx.buf == NULL) {
		printf("Failed allocating %u bytes\n",
				(unsigned int) ctx.buflen);
		return 1;
	}

	ctx.buf[0] = '\0';
	ctx.bufused = 0;
	ctx.explen = 0;
	ctx.expused = 0;
	ctx.exp = NULL;
	ctx.indata = false;
	ctx.inexp = false;

	assert(css__parse_testfile(argv[1], handle_line, &ctx) == true);

	/* and run final test */
	if (ctx.bufused > 0)
		run_test(ctx.buf, ctx.bufused, ctx.exp, ctx.expused);

	free(ctx.buf);
	free(ctx.exp);

	nslog_cleanup();

	printf("PASS\n");

	return 0;
}

bool handle_line(const char *data, size_t datalen, void *pw)
{
	line_ctx *ctx = (line_ctx *) pw;

	if (data[0] == '#') {
		if (ctx->inexp) {
			/* This marks end of testcase, so run it */

			run_test(ctx->buf, ctx->bufused,
					ctx->exp, ctx->expused);

			ctx->buf[0] = '\0';
			ctx->bufused = 0;

			ctx->expused = 0;
		}

		if (ctx->indata && strncasecmp(data+1, "expected", 8) == 0) {
			ctx->indata = false;
			ctx->inexp = true;
		} else if (!ctx->indata) {
			ctx->indata = (strncasecmp(data+1, "data", 4) == 0);
			ctx->inexp  = (strncasecmp(data+1, "expected", 8) == 0);
		} else {
			memcpy(ctx->buf + ctx->bufused, data, datalen);
			ctx->bufused += datalen;
		}
	} else {
		if (ctx->indata) {
			memcpy(ctx->buf + ctx->bufused, data, datalen);
			ctx->bufused += datalen;
		}
		if (ctx->inexp) {
			if (data[datalen - 1] == '\n')
				datalen -= 1;

			css__parse_expected(ctx, data, datalen);
		}
	}

	return true;
}

void css__parse_expected(line_ctx *ctx, const char *data, size_t len)
{
	css_token_type type;
	const char *colon = css__parse_strnchr(data, len, ':');

	if (colon == NULL)
		colon = data + len;

	type = string_to_type(data, colon - data);

	/* Append to list of expected tokens */
	if (ctx->expused == ctx->explen) {
		size_t num = ctx->explen == 0 ? 4 : ctx->explen;

		exp_entry *temp = realloc(ctx->exp,
				num * 2 * sizeof(exp_entry));
		if (temp == NULL) {
			assert(0 && "No memory for expected tokens");
		}

		ctx->exp = temp;
		ctx->explen = num * 2;
	}

	ctx->exp[ctx->expused].type = type;
	ctx->exp[ctx->expused].textLen = 0;
	ctx->exp[ctx->expused].hasText = (colon != data + len);

	if (colon != data + len) {
		const char *p = colon + 1;
		bool escape = false;

		for (len = len - (colon + 1 - data); len > 0; len--, p++) {
			char c;

			if (escape == false && *p == '\\') {
				escape = true;
				continue;
			}

			if (escape) {
				switch (*p) {
				case 'n':
					c = 0xa;
					break;
				case 't':
					c = 0x9;
					break;
				default:
					c = *p;
					break;
				}
				escape = false;
			} else {
				c = *p;
			}

			ctx->exp[ctx->expused].text[
					ctx->exp[ctx->expused].textLen] = c;
			ctx->exp[ctx->expused].textLen++;

			assert(ctx->exp[ctx->expused].textLen <
					EXP_ENTRY_TEXT_LEN);
		}
	}
	ctx->expused++;
}

const char *string_from_type(css_token_type type)
{
	const char *names[] =
	{
		"IDENT", "ATKEYWORD", "HASH", "FUNCTION", "STRING", "INVALID",
		"URI", "UNICODE-RANGE", "CHAR", "NUMBER", "PERCENTAGE",
		"DIMENSION", "last_intern", "CDO", "CDC", "S", "COMMENT",
		"INCLUDES", "DASHMATCH", "PREFIXMATCH", "SUFFIXMATCH",
		"SUBSTRINGMATCH", "EOF"
	};

	return names[type];
}

css_token_type string_to_type(const char *data, size_t len)
{
	if (len == 5 && strncasecmp(data, "IDENT", len) == 0)
		return CSS_TOKEN_IDENT;
	else if (len == 9 && strncasecmp(data, "ATKEYWORD", len) == 0)
		return CSS_TOKEN_ATKEYWORD;
	else if (len == 6 && strncasecmp(data, "STRING", len) == 0)
		return CSS_TOKEN_STRING;
	else if (len == 7 && strncasecmp(data, "INVALID", len) == 0)
		return CSS_TOKEN_INVALID_STRING;
	else if (len == 4 && strncasecmp(data, "HASH", len) == 0)
		return CSS_TOKEN_HASH;
	else if (len == 6 && strncasecmp(data, "NUMBER", len) == 0)
		return CSS_TOKEN_NUMBER;
	else if (len == 10 && strncasecmp(data, "PERCENTAGE", len) == 0)
		return CSS_TOKEN_PERCENTAGE;
	else if (len == 9 && strncasecmp(data, "DIMENSION", len) == 0)
		return CSS_TOKEN_DIMENSION;
	else if (len == 3 && strncasecmp(data, "URI", len) == 0)
		return CSS_TOKEN_URI;
	else if (len == 13 && strncasecmp(data, "UNICODE-RANGE", len) == 0)
		return CSS_TOKEN_UNICODE_RANGE;
	else if (len == 3 && strncasecmp(data, "CDO", len) == 0)
		return CSS_TOKEN_CDO;
	else if (len == 3 && strncasecmp(data, "CDC", len) == 0)
		return CSS_TOKEN_CDC;
	else if (len == 1 && strncasecmp(data, "S", len) == 0)
		return CSS_TOKEN_S;
	else if (len == 7 && strncasecmp(data, "COMMENT", len) == 0)
		return CSS_TOKEN_COMMENT;
	else if (len == 8 && strncasecmp(data, "FUNCTION", len) == 0)
		return CSS_TOKEN_FUNCTION;
	else if (len == 8 && strncasecmp(data, "INCLUDES", len) == 0)
		return CSS_TOKEN_INCLUDES;
	else if (len == 9 && strncasecmp(data, "DASHMATCH", len) == 0)
		return CSS_TOKEN_DASHMATCH;
	else if (len == 11 && strncasecmp(data, "PREFIXMATCH", len) == 0)
		return CSS_TOKEN_PREFIXMATCH;
	else if (len == 11 && strncasecmp(data, "SUFFIXMATCH", len) == 0)
		return CSS_TOKEN_SUFFIXMATCH;
	else if (len == 14 && strncasecmp(data, "SUBSTRINGMATCH", len) == 0)
		return CSS_TOKEN_SUBSTRINGMATCH;
	else if (len == 4 && strncasecmp(data, "CHAR", len) == 0)
		return CSS_TOKEN_CHAR;
	else
		return CSS_TOKEN_EOF;
}

void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen)
{
	parserutils_inputstream *input;
	css_lexer *lexer;
	css_error error;
	css_token *tok;
	size_t e;
	static int testnum;

	assert(parserutils_inputstream_create("UTF-8", CSS_CHARSET_DICTATED,
			css__charset_extract, &input) == PARSERUTILS_OK);

	assert(css__lexer_create(input, &lexer) == CSS_OK);

	assert(parserutils_inputstream_append(input, data, len) ==
			PARSERUTILS_OK);

	assert(parserutils_inputstream_append(input, NULL, 0) ==
			PARSERUTILS_OK);

	e = 0;
	testnum++;

	while ((error = css__lexer_get_token(lexer, &tok)) == CSS_OK) {
		if (tok->type != exp[e].type) {
			printf("%d: Got token %s, Expected %s [%d, %d]\n",
				testnum, string_from_type(tok->type),
				string_from_type(exp[e].type),
				tok->line, tok->col);
			assert(0 && "Types differ");
		}

		if (exp[e].hasText) {
			if (tok->data.len != exp[e].textLen) {
				printf("%d: Got length %d, Expected %d\n",
					testnum, (int) tok->data.len,
					(int) exp[e].textLen);
				assert(0 && "Text lengths differ");
			}

			if (strncmp((char *) tok->data.data, exp[e].text,
					tok->data.len) != 0) {
				printf("%d: Got data '%.*s', Expected '%.*s'\n",
					testnum,
					(int) tok->data.len, tok->data.data,
					(int) exp[e].textLen, exp[e].text);
				assert(0 && "Text differs");
			}
		}

		e++;

		if (tok->type == CSS_TOKEN_EOF)
			break;
	}

	assert(e == explen);

	css__lexer_destroy(lexer);

	parserutils_inputstream_destroy(input);

	printf("Test %d: PASS\n", testnum);
}