From b86a7a0533c4d723ea940ac2071f845f165f832c Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Fri, 27 Sep 2013 14:19:20 +0800 Subject: discover: Add parser_request_file Add a function to allow parsers to access files on a local device. Signed-off-by: Jeremy Kerr --- test/parser/parser-test.h | 7 +++++++ test/parser/utils.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) (limited to 'test') diff --git a/test/parser/parser-test.h b/test/parser/parser-test.h index 7e43a68..eb43c25 100644 --- a/test/parser/parser-test.h +++ b/test/parser/parser-test.h @@ -9,6 +9,7 @@ struct parser_test { struct device_handler *handler; struct discover_context *ctx; + struct list files; struct config *config; struct { void *buf; @@ -35,6 +36,12 @@ int test_run_parser(struct parser_test *test, const char *parser_name); void test_hotplug_device(struct parser_test *test, struct discover_device *dev); +void test_add_file_data(struct parser_test *test, struct discover_device *dev, + const char *filename, void *data, int size); + +#define test_add_file_string(test, dev, filename, str) \ + test_add_file_data(test, dev, filename, str, sizeof(str)) + struct discover_boot_option *get_boot_option(struct discover_context *ctx, int idx); diff --git a/test/parser/utils.c b/test/parser/utils.c index 40737c4..d1ced73 100644 --- a/test/parser/utils.c +++ b/test/parser/utils.c @@ -22,6 +22,14 @@ struct p_item { struct parser *parser; }; +struct test_file { + struct discover_device *dev; + const char *name; + void *data; + int size; + struct list_item list; +}; + STATIC_LIST(parsers); void __register_parser(struct parser *parser) @@ -91,6 +99,7 @@ struct parser_test *test_init(void) test->config = test_config_init(test); test->handler = device_handler_init(NULL, NULL, 0); test->ctx = test_create_context(test); + list_init(&test->files); return test; } @@ -142,6 +151,41 @@ void test_set_conf_source(struct parser_test *test, const char *url) assert(test->ctx->conf_url); } +void test_add_file_data(struct parser_test *test, struct discover_device *dev, + const char *filename, void *data, int size) +{ + struct test_file *file; + + file = talloc_zero(test, struct test_file); + file->dev = dev; + file->name = filename; + file->data = data; + file->size = size; + list_add(&test->files, &file->list); +} + + +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; + + list_for_each_entry(&test->files, file, list) { + if (file->dev != dev) + continue; + if (strcmp(file->name, filename)) + continue; + + *buf = talloc_memdup(test, file->data, file->size); + *len = file->size; + return 0; + } + + return -1; +} + int test_run_parser(struct parser_test *test, const char *parser_name) { struct p_item* i; -- cgit v1.2.1