From 115cc1150d51550e1b469d41569716aa3f7abc36 Mon Sep 17 00:00:00 2001 From: Jan Viktorin Date: Mon, 13 Jun 2016 17:07:39 +0200 Subject: [PATCH] app/test: create files from resources A resource can be written into the target filesystem by calling resource_fwrite or resource_fwrite_file. Such file can be created before a test is started and removed after the test finishes. Signed-off-by: Jan Viktorin --- app/test/resource.c | 35 +++++++++++++++++++++++++++++++++++ app/test/resource.h | 14 ++++++++++++++ app/test/test_resource.c | 10 ++++++++++ 3 files changed, 59 insertions(+) diff --git a/app/test/resource.c b/app/test/resource.c index 30513db840..acb63c137b 100644 --- a/app/test/resource.c +++ b/app/test/resource.c @@ -31,6 +31,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -60,6 +61,40 @@ const struct resource *resource_find(const char *name) return NULL; } +int resource_fwrite(const struct resource *r, FILE *f) +{ + const size_t goal = resource_size(r); + size_t total = 0; + + while (total < goal) { + size_t wlen = fwrite(r->begin + total, 1, goal - total, f); + if (wlen == 0) { + perror(__func__); + return -1; + } + + total += wlen; + } + + return 0; +} + +int resource_fwrite_file(const struct resource *r, const char *fname) +{ + FILE *f; + int ret; + + f = fopen(fname, "w"); + if (f == NULL) { + perror(__func__); + return -1; + } + + ret = resource_fwrite(r, f); + fclose(f); + return ret; +} + void resource_register(struct resource *r) { TAILQ_INSERT_TAIL(&resource_list, r, next); diff --git a/app/test/resource.h b/app/test/resource.h index 966fc2428a..ac9cae6f8a 100644 --- a/app/test/resource.h +++ b/app/test/resource.h @@ -45,6 +45,7 @@ */ #include +#include #include #include @@ -74,6 +75,19 @@ size_t resource_size(const struct resource *r); */ const struct resource *resource_find(const char *name); +/** + * Write the raw data of the resource to the given file. + * @return 0 on success + */ +int resource_fwrite(const struct resource *r, FILE *f); + +/** + * Write the raw data of the resource to the given file given by name. + * The name is relative to the current working directory. + * @return 0 on success + */ +int resource_fwrite_file(const struct resource *r, const char *fname); + /** * Register a resource in the global list of resources. * Not intended for direct use, please check the REGISTER_RESOURCE diff --git a/app/test/test_resource.c b/app/test/test_resource.c index b397fa8f20..3d1bf00bc8 100644 --- a/app/test/test_resource.c +++ b/app/test/test_resource.c @@ -65,6 +65,7 @@ REGISTER_LINKED_RESOURCE(test_resource_c); static int test_resource_c(void) { const struct resource *r; + FILE *f; r = resource_find("test_resource_c"); TEST_ASSERT_NOT_NULL(r, "No test_resource_c found"); @@ -72,6 +73,15 @@ static int test_resource_c(void) "Found resource %s, expected test_resource_c", r->name); + TEST_ASSERT_SUCCESS(resource_fwrite_file(r, "test_resource.c"), + "Failed to to write file %s", r->name); + + f = fopen("test_resource.c", "r"); + TEST_ASSERT_NOT_NULL(f, + "Missing extracted file resource.c"); + fclose(f); + remove("test_resource.c"); + return 0; } -- 2.20.1