1 /*
2 * lws-api-test-lejp
3 *
4 * Written in 2010-2020 by Andy Green <andy@warmcat.com>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 *
9 * sanity tests for lejp
10 */
11
12 #include <libwebsockets.h>
13
14 /*
15 * in this example, the JSON is for one "builder" object, which may specify
16 * a child list "targets" of zero or more "target" objects.
17 */
18
19 static const char * const json_tests[] = {
20 "{" /* test 1 */
21 "\"schema\":\"com-warmcat-sai-builder\","
22
23 "\"hostname\":\"learn\","
24 "\"nspawn_timeout\":1800,"
25 "\"targets\":["
26 "{"
27 "\"name\":\"target1\","
28 "\"someflag\":true"
29 "},"
30 "{"
31 "\"name\":\"target2\","
32 "\"someflag\":false"
33 "}"
34 "]"
35 "}",
36 "{" /* test 2 */
37 "\"schema\":\"com-warmcat-sai-builder\","
38
39 "\"hostname\":\"learn\","
40 "\"targets\":["
41 "{"
42 "\"name\":\"target1\""
43 "},"
44 "{"
45 "\"name\":\"target2\""
46 "},"
47 "{"
48 "\"name\":\"target3\""
49 "}"
50 "]"
51 "}", "{" /* test 3 */
52 "\"schema\":\"com-warmcat-sai-builder\","
53
54 "\"hostname\":\"learn\","
55 "\"nspawn_timeout\":1800,"
56 "\"targets\":["
57 "{"
58 "\"name\":\"target1\","
59 "\"unrecognized\":\"xyz\","
60 "\"child\": {"
61 "\"somename\": \"abc\","
62 "\"junk\": { \"x\": \"y\" }"
63 "}"
64 "},"
65 "{"
66 "\"name\":\"target2\""
67 "}"
68 "]"
69 "}",
70 "{" /* test 4 */
71 "\"schema\":\"com-warmcat-sai-builder\","
72
73 "\"hostname\":\"learn\","
74 "\"nspawn_timeout\":1800"
75 "}",
76 "{" /* test 5 */
77 "\"schema\":\"com-warmcat-sai-builder\""
78 "}",
79 "{" /* test 6 ... check huge strings into smaller fixed char array */
80 "\"schema\":\"com-warmcat-sai-builder\","
81 "\"hostname\":\""
82 "PYvtan6kqppjnS0KpYTCaiOLsJkc7XecAr1kcE0aCIciew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strings into char * */
98 "\"schema\":\"com-warmcat-sai-builder\","
99 "\"targets\":["
100 "{"
101 "\"name\":\""
102 "PYvtan6kqppjnS0KpYTCaiOLsJkc7XecAr1kcE0aCIciewYB+JcLG82mO1Vb1mJtjDwUjBxy2I6A"
103 "zefzoWUWmqZbsv4MXR55j9bKlyz1liiSX63iO0x6JAwACMtE2MkgcLwR86TSWAD9D1QKIWqg5RJ/"
104 "CRuVsW0DKAUMD52ql4JmPFuJpJgTq28z6PhYNzN3yI3bmQt6bzhA+A/xAsFzSBnb3MHYWzGMprr5"
105 "3FAP1ISo5Ec9i+2ehV40sG6Q470sH3PGQZ0YRPO7Sh/SyrSQ/scONmxRc3AcXl7X/CSs417ii+CV"
106 "8sq3ZgcxKNB7tNfN7idNx3upZ00G2BZy9jSy03cLKKLNaNUt0TQsxXbH55uDHzSEeZWvxJgT6zB1"
107 "NoMhdC02w+oXim94M6z6COCnqT3rgkGk8PHMry9Bkh4yVpRmzIRfMmln/lEhdZgxky2+g5hhlSIG"
108 "JYDCrdynD9kCfvfy6KGOpNIi1X+mhbbWn4lnL9ZKihL/RrfOV+oV4R26IDq+KqUiJBENeo8/GXkG"
109 "LUH/87iPyzXKEMavr6fkrK0vTGto8yEYxmOyaVz8phG5rwf4jJgmYNoMbGo8gWvhqO7UAGy2g7MW"
110 "v+B/t1eZZ+1euLsNrWAsFJiFbQKgdFfQT3RjB14iU8knlQ8usoy+pXssY2ddGJGVcGC21oZvstK9"
111 "eu1eRZftda/wP+N5unT1Hw7kCoVzqxHieiYt47EGIOaaQ7XjZDK6qPN6O/grHnvJZm2vBkxuXgsY"
112 "VkRQ7AuTWIecphqFsq7Wbc1YNbMW47SVU5zMD0WaCqbaaI0t4uIzRvPlD8cpiiTzFTrEHlIBTf8/"
113 "uZjjEGGLhJR1jPqA9D1Ej3ChV+ye6F9JTUMlozRMsGuF8U4btDzH5xdnmvRS4Ar6LKEtAXGkj2yu"
114 "yJln+v4RIWj2xOGPJovOqiXwi0FyM61f8U8gj0OiNA2/QlvrqQVDF7sMXgjvaE7iQt5vMETteZlx"
115 "+z3f+jTFM/aon511W4+ZkRD+6AHwucvM9BEC\"}]}"
116 "}",
117 "{" /* test 8 the "other" schema */
118 "\"schema\":\"com-warmcat-sai-logs\","
119 "\"task_uuid\":\"97fc90052506af8b3eb43b87aaa6fb76feab32bc128ede479a8a6b961e801f06\","
120 "\"timestamp\": 170366786103,\"channel\":3, \"len\":20, "
121 "\"log\": \"PnNhaWI+IE5TU1RBVEVfSU5JVAo=\"}\x0a"
122 "ntu-xenial-amd64\"},{\"name\":\"linux-ubuntu-bionic-amd64\"},{\"name\":\"linux-fedora-32-x86_64\"}]}\",",
123
124 "{" /* test 9, empty object */
125 "\"a\":123,\"b\":{}"
126 "}",
127
128 "{" /* SHOULD_FAIL: test 10, missing open */
129 "\"a\":123,\"b\":}"
130 "}"
131 };
132
133 static const char * const tok[] = {
134 "something",
135 };
136
137 static signed char
test_cb(struct lejp_ctx * ctx,char reason)138 test_cb(struct lejp_ctx *ctx, char reason)
139 {
140 lwsl_info("%s: ctx->path %s, buf %s\n", __func__, ctx->path, ctx->buf);
141 return 0;
142 }
143
144 /* authz JSON parsing */
145
146
main(int argc,const char ** argv)147 int main(int argc, const char **argv)
148 {
149 int n, m, e = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
150 struct lejp_ctx ctx;
151 const char *p;
152
153 if ((p = lws_cmdline_option(argc, argv, "-d")))
154 logs = atoi(p);
155
156 lws_set_log_level(logs, NULL);
157 lwsl_user("LWS API selftest: lws_struct JSON\n");
158
159 for (m = 0; m < (int)LWS_ARRAY_SIZE(json_tests); m++) {
160
161 lwsl_info("%s: ++++++++++++++++ test %d\n", __func__, m + 1);
162
163 lejp_construct(&ctx, test_cb, NULL, tok, LWS_ARRAY_SIZE(tok));
164
165 lwsl_hexdump_info(json_tests[m], strlen(json_tests[m]));
166
167 if (m == 7)
168 n = lejp_parse(&ctx, (uint8_t *)json_tests[m],
169 0xc8);
170 else
171 n = lejp_parse(&ctx, (uint8_t *)json_tests[m],
172 (int)strlen(json_tests[m]));
173
174 lwsl_info("n = %d\n", n);
175 if (n < 0 && m != 9) {
176 lwsl_err("%s: test %d: JSON decode failed '%s'\n",
177 __func__, m + 1, lejp_error_to_string(n));
178 e++;
179 }
180 if (n >= 0 && m == 9) {
181 lwsl_err("%s: test %d: JSON decode should have failed '%s'\n",
182 __func__, m + 1, lejp_error_to_string(n));
183 e++;
184 }
185 }
186
187 {
188 const char *cs;
189 size_t cslen;
190 cs = lws_json_simple_find("{\"blah\":123,\"ext\":{\"authorized\":1}}", 35,
191 "\"ext\":", &cslen);
192 if (!cs) {
193 lwsl_err("%s: simple_find failed\n", __func__);
194 e++;
195 } else {
196 if (lws_json_simple_strcmp(cs, cslen,
197 "\"authorized\":", "1"))
198 e++;
199 }
200 cs = lws_json_simple_find("{\"blah\":123,\"auth_user\":\"andy@warmcat.com\",\"thing\":\"yeah\"}", 57,
201 "\"auth_user\":", &cslen);
202 if (cslen != 16) {
203 lwsl_err("%s: wrong string len %d isolated\n", __func__, (int)cslen);
204 e++;
205 }
206 }
207
208 if (e)
209 goto bail;
210
211 lwsl_user("Completed: PASS\n");
212
213 return 0;
214
215 bail:
216 lwsl_user("Completed: FAIL\n");
217
218 return 1;
219 }
220