summaryrefslogtreecommitdiff
path: root/test/parse-auto.c
blob: 4c42359864f1ceff5e5646a1668fa2a3125c5732 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libcss/libcss.h>

#include "stylesheet.h"
#include "utils/utils.h"

#include "testutils.h"

/** \todo at some point, we need to extend this to handle nested blocks */
typedef struct exp_entry {
	int type;
#define MAX_RULE_NAME_LEN (128)
	char name[MAX_RULE_NAME_LEN];
	size_t bclen;
	size_t bcused;
	uint8_t *bytecode;

	size_t stlen;
	size_t stused;
	struct stentry {
		size_t off;
		char *string;
	} *stringtab;
} 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 inerrors;
	bool inexp;

	bool inrule;
} line_ctx;

static bool handle_line(const char *data, size_t datalen, void *pw);
static void parse_expected(line_ctx *ctx, 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 validate_rule_selector(css_rule_selector *s, exp_entry *e, 
		int testnum);
static void validate_rule_charset(css_rule_charset *s, exp_entry *e, 
		int testnum);
static void validate_rule_import(css_rule_import *s, exp_entry *e, 
		int testnum);

static void dump_selector_list(css_selector *list, char **ptr);
static void dump_selector(css_selector *selector, char **ptr);
static void dump_selector_detail(css_selector_detail *detail, char **ptr);
static void dump_string(const parserutils_hash_entry *string, char **ptr);

static void *myrealloc(void *data, size_t len, void *pw)
{
	UNUSED(pw);

	return realloc(data, len);
}

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

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

	assert(css_initialise(argv[1], myrealloc, NULL) == CSS_OK);

	ctx.buflen = parse_filesize(argv[2]);
	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.inerrors = false;
	ctx.inexp = false;

	assert(parse_testfile(argv[2], 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);

	assert(css_finalise(myrealloc, NULL) == CSS_OK);

	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, "errors", 6) == 0) {
			ctx->indata = false;
			ctx->inerrors = true;
			ctx->inexp = false;
		} else if (ctx->inerrors && 
				strncasecmp(data+1, "expected", 8) == 0) {
			ctx->indata = false;
			ctx->inerrors = false;
			ctx->inexp = true;
			ctx->inrule = false;
		} else if (ctx->inexp && strncasecmp(data+1, "data", 4) == 0) {
			ctx->indata = true;
			ctx->inerrors = false;
			ctx->inexp = false;
		} else if (ctx->indata) {
			memcpy(ctx->buf + ctx->bufused, data, datalen);
			ctx->bufused += datalen;
		} else {
			ctx->indata = (strncasecmp(data+1, "data", 4) == 0);
			ctx->inerrors = (strncasecmp(data+1, "errors", 6) == 0);
			ctx->inexp = (strncasecmp(data+1, "expected", 8) == 0);
		}
	} else {
		if (ctx->indata) {
			memcpy(ctx->buf + ctx->bufused, data, datalen);
			ctx->bufused += datalen;
		}
		if (ctx->inexp) {
			if (data[datalen - 1] == '\n')
				datalen -= 1;

			parse_expected(ctx, data, datalen);
		}
	}

	return true;
}

void parse_expected(line_ctx *ctx, const char *data, size_t len)
{
	/* Ignore blanks or lines that don't start with | */
	if (len == 0 || data[0] != '|')
		return;

	if (ctx->inrule == false) {
		char *name;
		int type;
		
start_rule:
		type = strtol(data + 1, &name, 10);

		while (isspace(*name))
			name++;

		/* Append to list of expected rules */
		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 rules");
			}

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

		ctx->exp[ctx->expused].type = type;
		memcpy(ctx->exp[ctx->expused].name, name, 
				min(len - (name - data), MAX_RULE_NAME_LEN));
		ctx->exp[ctx->expused].name[min(len - (name - data), 
				MAX_RULE_NAME_LEN - 1)] = '\0';
		ctx->exp[ctx->expused].bclen = 0;
		ctx->exp[ctx->expused].bcused = 0;
		ctx->exp[ctx->expused].bytecode = NULL;
		ctx->exp[ctx->expused].stlen = 0;
		ctx->exp[ctx->expused].stused = 0;
		ctx->exp[ctx->expused].stringtab =  NULL;

		ctx->expused++;
		
		ctx->inrule = true;
	} else {
		char *next = (char *) data + 1;
		exp_entry *rule = &ctx->exp[ctx->expused - 1];

		if (data[2] != ' ') {
			ctx->inrule = false;
			goto start_rule;
		}

		/** \todo how to deal with pointers? */
		while (next < data + len) {
			/* Skip whitespace */
			while (next < data + len && isspace(*next))
				next++;

			if (next == data + len)
				break;

			if (rule->bcused >= rule->bclen) {
				size_t num = rule->bcused == 0 ? 4 : 
						rule->bcused;

				uint8_t *temp = realloc(rule->bytecode,
						num * 2);
				if (temp == NULL) {
					assert(0 && "No memory for bytecode");
				}

				rule->bytecode = temp;
				rule->bclen = num * 2;
			}

			if (*next == 'P') {
				/* Pointer */
				const char *str;

				while (next < data + len && *next != '(')
					next++;
				str = next + 1;
				while (next < data + len && *next != ')')
					next++;
				next++;

				if (rule->stused >= rule->stlen) {
					size_t num = rule->stused == 0 ? 4 :
							rule->stused;

					struct stentry *temp = realloc(
						rule->stringtab, 
						num * 2 * sizeof(struct stentry));
					if (temp == NULL) {
						assert(0 && 
						"No memory for string table");
					}

					rule->stringtab = temp;
					rule->stlen = num * 2;
				}

				rule->stringtab[rule->stused].off = 
						rule->bcused;
				rule->stringtab[rule->stused].string =
						malloc(next - str);
				assert(rule->stringtab[rule->stused].string != 
						NULL);
				memcpy(rule->stringtab[rule->stused].string,
						str, next - str - 1);
				rule->stringtab[rule->stused].string[
						next - str - 1]  = '\0';

				rule->bcused += sizeof(void *);
				rule->stused++;
			} else {
				/* Assume hexnum */
				uint32_t val = strtoul(next, &next, 16);

				/* Append to bytecode */
				memcpy(rule->bytecode + rule->bcused, 
						&val, sizeof(val));
				rule->bcused += sizeof(val);
			}
		}
	}
}

void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen)
{
	css_stylesheet *sheet;
	css_rule *rule;
	css_error error;
	size_t e;
	static int testnum;

	assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", "foo", NULL,
			CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL,
			myrealloc, NULL, &sheet) == CSS_OK);

	error = css_stylesheet_append_data(sheet, data, len);
	if (error != CSS_OK && error != CSS_NEEDDATA) {
		printf("Failed appending data: %d\n", error);
		assert(0);
	}

	error = css_stylesheet_data_done(sheet);
	assert(error == CSS_OK || error == CSS_IMPORTS_PENDING);

	while (error == CSS_IMPORTS_PENDING) {
		css_string url;
		uint64_t media;

		error = css_stylesheet_next_pending_import(sheet,
				&url, &media);
		assert(error == CSS_OK || error == CSS_INVALID);

		if (error == CSS_OK) {
			css_stylesheet *import;
			char buf[url.len + 1];

			memcpy(buf, url.data, url.len);
			buf[url.len] = '\0';

			assert(css_stylesheet_create(CSS_LEVEL_21,
				"UTF-8", buf, NULL, CSS_ORIGIN_AUTHOR,
				media, myrealloc, NULL, &import) == 
				CSS_OK);

			assert(css_stylesheet_register_import(sheet,
				import) == CSS_OK);

			error = CSS_IMPORTS_PENDING;
		}
	}

	e = 0;
	testnum++;

	if (sheet->rule_count != explen) {
		printf("%d: Got %d rules. Expected %zu\n",
				testnum, sheet->rule_count, explen);
		assert(0 && "Unexpected number of rules");
	}

	for (rule = sheet->rule_list; rule != NULL; rule = rule->next, e++) {
		if (rule->type != exp[e].type) {
			printf("%d: Got type %d. Expected %d\n", 
				testnum, rule->type, exp[e].type);
			assert(0 && "Types differ");
		}

		switch (rule->type) {
		case CSS_RULE_SELECTOR:
			validate_rule_selector((css_rule_selector *) rule,
					&exp[e], testnum);
			break;
		case CSS_RULE_CHARSET:
			validate_rule_charset((css_rule_charset *) rule,
					&exp[e], testnum);
			break;
		case CSS_RULE_IMPORT:
			validate_rule_import((css_rule_import *) rule,
					&exp[e], testnum);
			break;
		default:
			printf("%d: Unhandled rule type %d\n",
				testnum, rule->type);
			break;
		}
	}

	assert(e == explen);

	css_stylesheet_destroy(sheet);

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

void validate_rule_selector(css_rule_selector *s, exp_entry *e, int testnum)
{
	char name[MAX_RULE_NAME_LEN];
	char *ptr = name;

	/* Build selector string */
	for (uint32_t i = 0; i < s->base.items; i++) {
		dump_selector_list(s->selectors[i], &ptr);
		if (i != (uint32_t) (s->base.items - 1)) {
			memcpy(ptr, ", ", 2);
			ptr += 2;
		}
	}
	*ptr = '\0';

	/* Compare with expected selector */
	if (strcmp(e->name, name) != 0) {
		printf("%d: Got name '%s'. Expected '%s'\n",
			testnum, name, e->name);
		assert(0 && "Mismatched names");
	}

	/* Now compare bytecode */
	if (e->bytecode != NULL && s->style == NULL) {
		printf("%d: Expected bytecode but none created\n",
			testnum);
		assert(0 && "No bytecode");
	} else if (e->bytecode == NULL && s->style != NULL) {
		printf("%d: No bytecode expected but some created\n",
			testnum);
		assert(0 && "Unexpected bytecode");
	} else if (e->bytecode != NULL && s->style != NULL) {
		if (s->style->length != e->bcused) {
			printf("%d: Got length %d, Expected %zu\n",
				testnum, s->style->length, 
				e->bcused);
			assert(0 && "Bytecode lengths differ");
		}

		for (size_t i = 0; i < e->bcused; i++) {
			size_t j;

			for (j = 0; j < e->stused; j++) {
				if (e->stringtab[j].off == i)
					break;
			}

			if (j != e->stused) {
				const parserutils_hash_entry **p =
						(void *) ((uint8_t *) 
						s->style->bytecode + i);

				if ((*p)->len != 
					strlen(e->stringtab[j].string) ||
					memcmp((*p)->data, 
						e->stringtab[j].string,
						(*p)->len) != 0) {
					printf("%d: Got string '%.*s'. Expected '%s'\n",
						testnum, (int) (*p)->len, 
						(char *) (*p)->data, 
						e->stringtab[j].string);
					assert(0 && "Strings differ");
				}

				i += sizeof (void *) - 1;
			} else if (((uint8_t *) s->style->bytecode)[i] != 
					e->bytecode[i]) {
				printf("%d: Bytecode differs at %zu\n",
					testnum, i);
				while (i < e->bcused) {
					printf("%.2x ", 
						((uint8_t *) s->style->bytecode)[i]);
					i++;
				}
				printf("\n");
				assert(0 && "Bytecode differs");
			}
		}
	}
}

void validate_rule_charset(css_rule_charset *s, exp_entry *e, int testnum)
{
	char name[MAX_RULE_NAME_LEN];
	char *ptr = name;

	dump_string(s->encoding, &ptr);
	*ptr = '\0';

	if (strcmp(name, e->name) != 0) {
		printf("%d: Got charset '%s'. Expected '%s'\n",
			testnum, name, e->name);
		assert(0 && "Mismatched charsets");
	}
}

void validate_rule_import(css_rule_import *s, exp_entry *e, int testnum)
{
	if (strncmp((const char *) s->url->data, e->name,
			(int) s->url->len) != 0) {
		printf("%d: Got URL '%.*s'. Expected '%s'\n",
			testnum, (int) s->url->len, (const char *) s->url->data,
		e->name);
		assert(0 && "Mismatched URLs");
	}
}

void dump_selector_list(css_selector *list, char **ptr)
{
	if (list->combinator != NULL) {
		dump_selector_list(list->combinator, ptr);
	}

	switch (list->data.comb) {
	case CSS_COMBINATOR_NONE:
		break;
	case CSS_COMBINATOR_ANCESTOR:
		(*ptr)[0] = ' ';
		*ptr += 1;
		break;
	case CSS_COMBINATOR_PARENT:
		memcpy(*ptr, " > ", 3);
		*ptr += 3;
		break;
	case CSS_COMBINATOR_SIBLING:
		memcpy(*ptr, " + ", 3);
		*ptr += 3;
		break;
	}

	dump_selector(list, ptr);
}


void dump_selector(css_selector *selector, char **ptr)
{
	css_selector_detail *d = &selector->data;

	while (true) {
		dump_selector_detail(d, ptr);

		if (d->next == 0)
			break;

		d++;
	}
}

void dump_selector_detail(css_selector_detail *detail, char **ptr)
{
	switch (detail->type) {
	case CSS_SELECTOR_ELEMENT:
		if (detail->name->len == 1 && detail->name->data[0] == '*' &&
				detail->next == 0) {
			dump_string(detail->name, ptr);
		} else if (detail->name->len != 1 ||
				detail->name->data[0] != '*') {
			dump_string(detail->name, ptr);
		}
		break;
	case CSS_SELECTOR_CLASS:
		**ptr = '.';
		*ptr += 1;
		dump_string(detail->name, ptr);
		break;
	case CSS_SELECTOR_ID:
		**ptr = '#';
		*ptr += 1;
		dump_string(detail->name, ptr);
		break;
	case CSS_SELECTOR_PSEUDO_CLASS:
	case CSS_SELECTOR_PSEUDO_ELEMENT:
		**ptr = ':';
		*ptr += 1;
		dump_string(detail->name, ptr);
		if (detail->value != NULL) {
			**ptr = '(';
			*ptr += 1;
			dump_string(detail->value, ptr);
			**ptr = ')';
			*ptr += 1;
		}
		break;
	case CSS_SELECTOR_ATTRIBUTE:
		**ptr = '[';
		*ptr += 1;
		dump_string(detail->name, ptr);
		**ptr = ']';
		*ptr += 1;
		break;
	case CSS_SELECTOR_ATTRIBUTE_EQUAL:
		**ptr = '[';
		*ptr += 1;
		dump_string(detail->name, ptr);
		(*ptr)[0] = '=';
		(*ptr)[1] = '"';
		*ptr += 2;
		dump_string(detail->value, ptr);
		(*ptr)[0] = '"';
		(*ptr)[1] = ']';
		*ptr += 2;
		break;
	case CSS_SELECTOR_ATTRIBUTE_DASHMATCH:
		**ptr = '[';
		*ptr += 1;
		dump_string(detail->name, ptr);
		(*ptr)[0] = '|';
		(*ptr)[1] = '=';
		(*ptr)[2] = '"';
		*ptr += 3;
		dump_string(detail->value, ptr);
		(*ptr)[0] = '"';
		(*ptr)[1] = ']';
		*ptr += 2;
		break;
	case CSS_SELECTOR_ATTRIBUTE_INCLUDES:
		**ptr = '[';
		*ptr += 1;
		dump_string(detail->name, ptr);
		(*ptr)[0] = '~';
		(*ptr)[1] = '=';
		(*ptr)[2] = '"';
		*ptr += 3;
		dump_string(detail->value, ptr);
		(*ptr)[0] = '"';
		(*ptr)[1] = ']';
		*ptr += 2;
		break;
	}
}

void dump_string(const parserutils_hash_entry *string, char **ptr)
{
	*ptr += sprintf(*ptr, "%.*s", (int) string->len, string->data);
}