a5b470620223f1a6e554448215032a10f45e761e
[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 FILE *fd = NULL;
67 size_t len = 0;
68 char buf[BUF_LEN+1] = { 0 };
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 len = fread(&buf, 1, BUF_LEN, fd);
79 fclose(fd);
80
81 r = blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb, buf, len);
82 if (r)
83 return;
84
85 if (!tb[INSTANCE_ATTR_COMMAND] || !tb[INSTANCE_ATTR_NICE] || !tb[INSTANCE_ATTR_STDERR])
86 return;
87
88 if (!blobmsg_check_attr_list(tb[INSTANCE_ATTR_COMMAND], BLOBMSG_TYPE_STRING))
89 return;
90
91 if (blobmsg_get_u32(tb[INSTANCE_ATTR_NICE]) != 19)
92 return;
93
94 if (!blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
95 return;
96
97 fprintf(stderr, "%s: OK\n", fname);
98 }
99
100 int main(int argc, char *argv[])
101 {
102 if (argc != 2) {
103 fprintf(stderr, "Usage: %s <blobmsg.bin>\n", argv[0]);
104 return 3;
105 }
106
107 test_blobmsg_procd_instance(argv[1]);
108
109 return 0;
110 }