Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test-blobmsg-parse.c « tests - git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b2844f397f0dcdebef071134f4b7aa2428e809cd (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
#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;
	size_t len = 0;
	FILE *fd = NULL;
	char *buf = NULL;
	struct blob_attr *tb[__FOO_MAX];

	fd = fopen(filename, "r");
	if (!fd) {
		fprintf(stderr, "unable to open %s\n", filename);
		return;
	}

	buf = malloc(BUF_LEN+1);
	if (!buf)
		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);

	free(buf);
}

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;
}