tests: prefer dynamically allocated buffers
[project/libubox.git] / tests / test-blobmsg-procd-instance.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stddef.h>
4 #include <libgen.h>
5
6 #include "blobmsg.h"
7 #include "utils.h"
8
9 enum {
10 INSTANCE_ATTR_COMMAND,
11 INSTANCE_ATTR_ENV,
12 INSTANCE_ATTR_DATA,
13 INSTANCE_ATTR_NETDEV,
14 INSTANCE_ATTR_FILE,
15 INSTANCE_ATTR_TRIGGER,
16 INSTANCE_ATTR_RESPAWN,
17 INSTANCE_ATTR_NICE,
18 INSTANCE_ATTR_LIMITS,
19 INSTANCE_ATTR_WATCH,
20 INSTANCE_ATTR_ERROR,
21 INSTANCE_ATTR_USER,
22 INSTANCE_ATTR_GROUP,
23 INSTANCE_ATTR_STDOUT,
24 INSTANCE_ATTR_STDERR,
25 INSTANCE_ATTR_NO_NEW_PRIVS,
26 INSTANCE_ATTR_JAIL,
27 INSTANCE_ATTR_TRACE,
28 INSTANCE_ATTR_SECCOMP,
29 INSTANCE_ATTR_PIDFILE,
30 INSTANCE_ATTR_RELOADSIG,
31 INSTANCE_ATTR_TERMTIMEOUT,
32 INSTANCE_ATTR_FACILITY,
33 __INSTANCE_ATTR_MAX
34 };
35
36 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
37 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
38 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
39 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
40 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
41 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
42 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
43 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
44 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
45 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
46 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
47 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
48 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
49 [INSTANCE_ATTR_GROUP] = { "group", BLOBMSG_TYPE_STRING },
50 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
51 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
52 [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
53 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
54 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
55 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
56 [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
57 [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
58 [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
59 [INSTANCE_ATTR_FACILITY] = { "facility", BLOBMSG_TYPE_STRING },
60 };
61
62 static void test_blobmsg_procd_instance(const char *filename)
63 {
64 #define BUF_LEN 2048
65 int r = 0;
66 size_t len = 0;
67 FILE *fd = NULL;
68 char *buf = NULL;
69 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
70 const char *fname = basename((char *) filename);
71
72 fd = fopen(filename, "r");
73 if (!fd) {
74 fprintf(stderr, "unable to open %s\n", fname);
75 return;
76 }
77
78 buf = malloc(BUF_LEN+1);
79 if (!buf)
80 return;
81
82 len = fread(buf, 1, BUF_LEN, fd);
83 fclose(fd);
84
85 r = blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb, buf, len);
86 if (r)
87 goto out;
88
89 if (!tb[INSTANCE_ATTR_COMMAND] || !tb[INSTANCE_ATTR_NICE] || !tb[INSTANCE_ATTR_STDERR])
90 goto out;
91
92 if (!blobmsg_check_attr_list(tb[INSTANCE_ATTR_COMMAND], BLOBMSG_TYPE_STRING))
93 goto out;
94
95 if (blobmsg_get_u32(tb[INSTANCE_ATTR_NICE]) != 19)
96 goto out;
97
98 if (!blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
99 goto out;
100
101 fprintf(stderr, "%s: OK\n", fname);
102 out:
103 free(buf);
104 }
105
106 int main(int argc, char *argv[])
107 {
108 if (argc != 2) {
109 fprintf(stderr, "Usage: %s <blobmsg.bin>\n", argv[0]);
110 return 3;
111 }
112
113 test_blobmsg_procd_instance(argv[1]);
114
115 return 0;
116 }