summaryrefslogtreecommitdiffstats
path: root/test/parser/utils.c
blob: f0796fd30f9beea75005df2bd93c00430e56d50e (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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669

#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <talloc/talloc.h>
#include <types/types.h>
#include <url/url.h>

#include "device-handler.h"
#include "parser.h"
#include "resource.h"
#include "event.h"
#include "platform.h"
#include "paths.h"
#include "parser-conf.h"

#include "parser-test.h"

struct p_item {
	struct list_item list;
	struct parser *parser;
};

struct test_file {
	struct discover_device	*dev;
	enum {
		TEST_FILE,
		TEST_DIR,
	}			type;
	const char		*name;
	void			*data;
	int			size;
	struct list_item	list;
};

STATIC_LIST(parsers);

void __register_parser(struct parser *parser)
{
	struct p_item* i = talloc(NULL, struct p_item);

	i->parser = parser;
	list_add(&parsers, &i->list);
}

static void __attribute__((destructor)) __cleanup_parsers(void)
{
	struct p_item *item, *tmp;

	list_for_each_entry_safe(&parsers, item, tmp, list)
		talloc_free(item);
}

static struct discover_device *test_create_device_simple(
		struct parser_test *test)
{
	static int dev_idx;
	char name[10];

	sprintf(name, "__test%d", dev_idx++);

	return test_create_device(test, name);
}

struct discover_device *test_create_device(struct parser_test *test,
		const char *name)
{
	struct discover_device *dev;

	dev = discover_device_create(test->handler, NULL, name);

	dev->device->id = talloc_strdup(dev, name);
	dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
	dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
	dev->mounted = true;

	return dev;
}

static struct discover_context *test_create_context(struct parser_test *test)
{
	struct discover_context *ctx;

	ctx = talloc_zero(test, struct discover_context);
	assert(ctx);

	list_init(&ctx->boot_options);
	ctx->device = test_create_device_simple(test);
	ctx->test_data = test;
	device_handler_add_device(test->handler, ctx->device);

	return ctx;
}

/* define our own test platform */
static bool test_platform_probe(struct platform *p __attribute__((unused)),
		void *ctx __attribute__((unused)))
{
	return true;
}

struct platform test_platform = {
	.name = "test",
	.probe = test_platform_probe,
};

register_platform(test_platform);

struct parser_test *test_init(void)
{
	struct parser_test *test;

	test = talloc_zero(NULL, struct parser_test);
	platform_init(NULL);
	test->handler = device_handler_init(NULL, NULL, 0);
	test->ctx = test_create_context(test);
	list_init(&test->files);

	return test;
}

void test_fini(struct parser_test *test)
{
	device_handler_destroy(test->handler);
	talloc_free(test);
	platform_fini();
}

void __test_read_conf_data(struct parser_test *test,
		struct discover_device *dev, const char *conf_file,
		const char *buf, size_t len)
{
	test_add_file_data(test, dev, conf_file, buf, len);
}

void test_read_conf_file(struct parser_test *test, const char *filename,
		const char *conf_file)
{
	struct stat stat;
	size_t size;
	char *path;
	int fd, rc;
	char *buf;

	path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);

	fd = open(path, O_RDONLY);
	if (fd < 0)
		err(EXIT_FAILURE, "Can't open test conf file %s\n", path);

	rc = fstat(fd, &stat);
	assert(!rc);
	(void)rc;

	size = stat.st_size;
	buf = talloc_array(test, char, size + 1);

	rc = read(fd, buf, size);
	assert(rc == (ssize_t)size);

	*(buf + size) = '\0';

	close(fd);
	talloc_free(path);

	test_add_file_data(test, test->ctx->device, conf_file, buf, size);
}

void test_add_file_data(struct parser_test *test, struct discover_device *dev,
		const char *filename, const void *data, int size)
{
	struct test_file *file;

	file = talloc_zero(test, struct test_file);
	file->type = TEST_FILE;
	file->dev = dev;
	file->name = filename;
	file->data = talloc_memdup(test, data, size);
	file->size = size;
	list_add(&test->files, &file->list);
}

void test_add_dir(struct parser_test *test, struct discover_device *dev,
		const char *dirname)
{
	struct test_file *file;

	file = talloc_zero(test, struct test_file);
	file->type = TEST_DIR;
	file->dev = dev;
	file->name = dirname;
	/* Pick a non-zero size for directories so that "[ -s <dir
	 * path> ]" sees that the file has non-zero size. */
	file->size = 1;
	list_add(&test->files, &file->list);
}

void test_set_event_source(struct parser_test *test)
{
        test->ctx->event = talloc_zero(test->ctx, struct event);
}

void test_set_event_param(struct event *event, const char *name,
                const char *value)
{
        event_set_param(event, name, value);
}

void test_set_event_device(struct event *event, const char *dev)
{
	event->device = talloc_strdup(event, dev);
}

int parser_request_file(struct discover_context *ctx,
		struct discover_device *dev, const char *filename,
		char **buf, int *len)
{
	struct parser_test *test = ctx->test_data;
	struct test_file *file;
	char *tmp;

	list_for_each_entry(&test->files, file, list) {
		if (file->dev != dev)
			continue;
		if (strcmp(file->name, filename))
			continue;
		if (file->type != TEST_FILE)
			continue;

		/* the read_file() interface always adds a trailing null
		 * for string-safety; do the same here */
		tmp = talloc_array(test, char, file->size + 1);
		memcpy(tmp, file->data, file->size);
		tmp[file->size] = '\0';
		*buf = tmp;
		*len = file->size;
		return 0;
	}

	return -1;
}

int parser_stat_path(struct discover_context *ctx,
		struct discover_device *dev, const char *path,
		struct stat *statbuf)
{
	struct parser_test *test = ctx->test_data;
	struct test_file *file;

	list_for_each_entry(&test->files, file, list) {
		if (file->dev != dev)
			continue;
		if (strcmp(file->name, path))
			continue;

		statbuf->st_size = (off_t)file->size;
		switch (file->type) {
		case TEST_FILE:
			statbuf->st_mode = S_IFREG;
			break;
		case TEST_DIR:
			statbuf->st_mode = S_IFDIR;
			break;
		default:
			fprintf(stderr, "%s: bad test file mode %d!", __func__,
				file->type);
			exit(EXIT_FAILURE);
		}

		return 0;
	}

	return -1;
}

int parser_replace_file(struct discover_context *ctx,
		struct discover_device *dev, const char *filename,
		char *buf, int len)
{
	struct parser_test *test = ctx->test_data;
	struct test_file *f, *file;

	list_for_each_entry(&test->files, f, list) {
		if (f->dev != dev)
			continue;
		if (strcmp(f->name, filename))
			continue;

		file = f;
		break;
	}

	if (!file) {
		file = talloc_zero(test, struct test_file);
		file->dev = dev;
		file->name = filename;
		list_add(&test->files, &file->list);
	}

	file->data = talloc_memdup(test, buf, len);
	file->size = len;
	return 0;
}

struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
		load_url_complete async_cb, void *async_data)
{
	struct conf_context *conf = async_data;
	struct parser_test *test = conf->dc->test_data;
	struct load_url_result *result;
	char tmp[] = "/tmp/pb-XXXXXX";
	ssize_t rc = -1, sz = 0;
	struct test_file *file;
	int fd;

	fd = mkstemp(tmp);

	if (fd < 0)
		return NULL;

	/* Some parsers will expect to need to read a file, so write the
	 * specified file to a temporary file */
	list_for_each_entry(&test->files, file, list) {
		if (file->dev)
			continue;

		if (strcmp(file->name, url->full))
			continue;

		while (sz < file->size) {
			rc = write(fd, file->data, file->size);
			if (rc < 0) {
				fprintf(stderr,
					"Failed to write to tmpfile, %m\n");
				break;
			}
			sz += rc;
		}
		break;
	}

	close(fd);

	result = talloc_zero(ctx, struct load_url_result);
	if (!result)
		return NULL;

	result->local = talloc_strdup(result, tmp);
	if (rc < 0)
		result->status = LOAD_ERROR;
	else
		result->status = result->local ? LOAD_OK : LOAD_ERROR;
	result->cleanup_local = true;

	async_cb(result, conf);

	return result;
}

int parser_request_url(struct discover_context *ctx, struct pb_url *url,
		char **buf, int *len)
{
	struct parser_test *test = ctx->test_data;
	struct test_file *file;
	char *tmp;

	list_for_each_entry(&test->files, file, list) {
		if (file->dev)
			continue;

		if (strcmp(file->name, url->full))
			continue;

		/* the read_file() interface always adds a trailing null
		 * for string-safety; do the same here */
		tmp = talloc_array(test, char, file->size + 1);
		memcpy(tmp, file->data, file->size);
		tmp[file->size] = '\0';
		*buf = tmp;
		*len = file->size;
		return 0;
	}

	return -1;
}

int test_run_parser(struct parser_test *test, const char *parser_name)
{
	struct p_item* i;

	list_for_each_entry(&parsers, i, list) {
		if (strcmp(i->parser->name, parser_name))
			continue;
		test->ctx->parser = i->parser;
		return i->parser->parse(test->ctx);
	}

	errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
}

bool resource_resolve(struct device_handler *handler, struct parser *parser,
		struct resource *resource)
{
	if (!resource)
		return true;
	if (resource->resolved)
		return true;

	assert(parser);
	assert(parser->resolve_resource);

	return parser->resolve_resource(handler, resource);
}

void boot_option_resolve(struct device_handler *handler,
		struct discover_boot_option *opt)
{
	resource_resolve(handler, opt->source, opt->boot_image);
	resource_resolve(handler, opt->source, opt->initrd);
	resource_resolve(handler, opt->source, opt->icon);
}

void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
{
	struct discover_boot_option *opt;

	device_handler_add_device(test->handler, dev);

	list_for_each_entry(&test->ctx->boot_options, opt, list)
		boot_option_resolve(test->handler, opt);
}

void test_remove_device(struct parser_test *test, struct discover_device *dev)
{
	struct discover_boot_option *opt, *tmp;

	if (dev == test->ctx->device) {
		list_for_each_entry_safe(&test->ctx->boot_options,
				opt, tmp, list) {
			list_remove(&opt->list);
			talloc_free(opt);
		}
	}

	device_handler_remove(test->handler, dev);
}

struct discover_boot_option *get_boot_option(struct discover_context *ctx,
		int idx)
{
	struct discover_boot_option *opt;
	int i = 0;

	list_for_each_entry(&ctx->boot_options, opt, list) {
		if (i++ == idx)
			return opt;
	}

	assert(0);

	return NULL;
}

void __check_boot_option_count(struct discover_context *ctx, int count,
		const char *file, int line)
{
	struct discover_boot_option *opt;
	int defaults = 0, i = 0;

	list_for_each_entry(&ctx->boot_options, opt, list) {
		i++;
		if (opt->option->is_default)
			defaults++;
	}

	if (defaults > 1) {
		fprintf(stderr, "%s:%d: parser returned multiple default "
				"options\n", file, line);
		exit(EXIT_FAILURE);
	}

	if (i == count)
		return;

	fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
	fprintf(stderr, "expected %d options, got %d:\n", count, i);

	i = 1;
	list_for_each_entry(&ctx->boot_options, opt, list)
		fprintf(stderr, "  %2d: %s [%s]\n", i++, opt->option->name,
				opt->option->id);

	exit(EXIT_FAILURE);
}

void __check_args(struct discover_boot_option *opt, const char *args,
		const char *file, int line)
{
	int rc;

	if (!opt->option->boot_args && !args)
		return;

	if (!opt->option->boot_args) {
		fprintf(stderr, "%s:%d: arg check failed\n", file, line);
		fprintf(stderr, "  no arguments parsed\n");
		fprintf(stderr, "  expected '%s'\n", args);
		exit(EXIT_FAILURE);
	}

	rc = strcmp(opt->option->boot_args, args);
	if (rc) {
		fprintf(stderr, "%s:%d: arg check failed\n", file, line);
		fprintf(stderr, "  got      '%s'\n", opt->option->boot_args);
		fprintf(stderr, "  expected '%s'\n", args);
		exit(EXIT_FAILURE);
	}
}

void __check_name(struct discover_boot_option *opt, const char *name,
		const char *file, int line)
{
	int rc;

	rc = strcmp(opt->option->name, name);
	if (rc) {
		fprintf(stderr, "%s:%d: name check failed\n", file, line);
		fprintf(stderr, "  got      '%s'\n", opt->option->name);
		fprintf(stderr, "  expected '%s'\n", name);
		exit(EXIT_FAILURE);
	}
}

void __check_is_default(struct discover_boot_option *opt,
		const char *file, int line)
{
	if (opt->option->is_default)
		return;

	fprintf(stderr, "%s:%d: default check failed\n", file, line);
	exit(EXIT_FAILURE);
}

void __check_resolved_local_resource(struct resource *res,
		struct discover_device *dev, const char *local_path,
		const char *file, int line)
{
	const char *exp_url, *got_url;

	if (!res)
		errx(EXIT_FAILURE, "%s:%d: No resource", file, line);

	if (!res->resolved)
		errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
				file, line);

	exp_url = talloc_asprintf(res, "file://%s%s",
			dev->mount_path, local_path);
	got_url = pb_url_to_string(res->url);

	if (strcmp(got_url, exp_url)) {
		fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
		fprintf(stderr, "  got      '%s'\n", got_url);
		fprintf(stderr, "  expected '%s'\n", exp_url);
		exit(EXIT_FAILURE);
	}
}

void __check_resolved_url_resource(struct resource *res,
		const char *url, const char *file, int line)
{
	char *res_url;

	if (!res)
		errx(EXIT_FAILURE, "%s:%d: No resource", file, line);

	if (!res->resolved)
		errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
				file, line);

	res_url = pb_url_to_string(res->url);
	if (strcmp(url, res_url)) {
		fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
		fprintf(stderr, "  got      '%s'\n", res_url);
		fprintf(stderr, "  expected '%s'\n", url);
		exit(EXIT_FAILURE);
	}
}
void __check_unresolved_resource(struct resource *res,
		const char *file, int line)
{
	if (!res)
		errx(EXIT_FAILURE, "%s:%d: No resource", file, line);

	if (res->resolved)
		errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
}

void __check_not_present_resource(struct resource *res,
		const char *file, int line)
{
	if (res)
		errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
}

static void dump_file_data(const void *buf, int len)
{
	int i, j, hex_len = strlen("00 ");
	const int row_len = 16;

	for (i = 0; i < len; i += row_len) {
		char hbuf[row_len * hex_len + 1];
		char cbuf[row_len + strlen("|") + 1];

		for (j = 0; (j < row_len) && ((i+j) < len); j++) {
			char c = ((const char *)buf)[i + j];

			snprintf(hbuf + j * hex_len, hex_len + 1, "%02x ", c);

			if (!isprint(c))
				c = '.';

			snprintf(cbuf + j, hex_len + 1, "%c", c);
		}

		strcat(cbuf, "|");

		fprintf(stderr, "%08x  %*s |%s\n", i,
				0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
	}
}

void __check_file_contents(struct parser_test *test,
		struct discover_device *dev, const char *filename,
		const char *buf, int len,
		const char *srcfile, int srcline)
{
	struct test_file *f, *file = NULL;

	list_for_each_entry(&test->files, f, list) {
		if (f->dev != dev)
			continue;
		if (strcmp(f->name, filename))
			continue;

		file = f;
		break;
	}

	if (!file)
		errx(EXIT_FAILURE, "%s:%d: File '%s' not found",
				srcfile, srcline, filename);

	if (file->size != len || memcmp(file->data, buf, len)) {
		fprintf(stderr, "%s:%d: File '%s' data/size mismatch\n",
				srcfile, srcline, filename);
		fprintf(stderr, "Expected:\n");
		dump_file_data(buf, len);
		fprintf(stderr, "Got:\n");
		dump_file_data(file->data, file->size);
		exit(EXIT_FAILURE);
	}
}
OpenPOWER on IntegriCloud