diff options
author | Petr Štetiar <ynezz@true.cz> | 2019-12-10 13:51:43 +0300 |
---|---|---|
committer | Petr Štetiar <ynezz@true.cz> | 2019-12-25 12:31:58 +0300 |
commit | 2df6d35e3299ecce7975502a007c1ca456f4b8bb (patch) | |
tree | 878374dbe9d3640210e7d4804401b299b33c4135 /tests/test-blobmsg-parse.c | |
parent | 8a34788b46c4800a8ed66bfe028da5c621f267f3 (diff) |
tests: add test cases for blobmsg parsing
Increasing test coverage.
Signed-off-by: Petr Štetiar <ynezz@true.cz>
Diffstat (limited to 'tests/test-blobmsg-parse.c')
-rw-r--r-- | tests/test-blobmsg-parse.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/test-blobmsg-parse.c b/tests/test-blobmsg-parse.c new file mode 100644 index 0000000..ca710fd --- /dev/null +++ b/tests/test-blobmsg-parse.c @@ -0,0 +1,74 @@ +#include <stdio.h> +#include <stdint.h> +#include <stddef.h> +#include <libgen.h> + +#include "blobmsg.h" + +enum { + FOO_MESSAGE, + FOO_LIST, + FOO_TESTDATA, + __FOO_MAX +}; + +static const struct blobmsg_policy foo_policy[] = { + [FOO_MESSAGE] = { + .name = "message", + .type = BLOBMSG_TYPE_STRING, + }, + [FOO_LIST] = { + .name = "list", + .type = BLOBMSG_TYPE_ARRAY, + }, + [FOO_TESTDATA] = { + .name = "testdata", + .type = BLOBMSG_TYPE_TABLE, + }, +}; + +static void dump_result(const char *fn, int r, const char *filename, struct blob_attr **tb) +{ + fprintf(stdout, "%s: %s: %c%c%c (%d)\n", basename((char *) filename), fn, + tb[FOO_MESSAGE] ? 'M' : '.', + tb[FOO_LIST] ? 'L' : '.', + tb[FOO_TESTDATA] ? 'T' : '.', + r); +} + +static void test_blobmsg(const char *filename) +{ +#define BUF_LEN 256 + int r = 0; + FILE *fd = NULL; + size_t len = 0; + char buf[BUF_LEN+1] = { 0 }; + struct blob_attr *tb[__FOO_MAX]; + + fd = fopen(filename, "r"); + if (!fd) { + fprintf(stderr, "unable to open %s", filename); + return; + } + + len = fread(&buf, 1, BUF_LEN, fd); + fclose(fd); + + r = blobmsg_parse(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len); + dump_result("blobmsg_parse", r, filename, tb); + + r = blobmsg_parse_array(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len); + dump_result("blobmsg_parse_array", r, filename, tb); +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + fprintf(stderr, "Usage: %s <blobmsg.bin>\n", argv[0]); + return 3; + } + + test_blobmsg(argv[1]); + + return 0; +} |