• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-api-test-lws_struct-json
3  *
4  * Written in 2010-2019 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  * lws_struct apis are used to serialize and deserialize your C structs and
10  * linked-lists in a standardized way that's very modest on memory but
11  * convenient and easy to maintain.
12  *
13  * The API test shows how to serialize and deserialize a struct with a linked-
14  * list of child structs in JSON using lws_struct APIs.
15  */
16 
17 #include <libwebsockets.h>
18 
19 /*
20  * in this example, the JSON is for one "builder" object, which may specify
21  * a child list "targets" of zero or more "target" objects.
22  */
23 
24 static const char * const json_tests[] = {
25 	"{" /* test 1 */
26 		"\"schema\":\"com-warmcat-sai-builder\","
27 
28 		"\"hostname\":\"learn\","
29 		"\"nspawn_timeout\":1800,"
30 		"\"targets\":["
31 			"{"
32 				"\"name\":\"target1\","
33 				"\"someflag\":true"
34 			"},"
35 			"{"
36 				"\"name\":\"target2\","
37 				"\"someflag\":false"
38 			"}"
39 		"]"
40 	"}",
41 	"{" /* test 2 */
42 		"\"schema\":\"com-warmcat-sai-builder\","
43 
44 		"\"hostname\":\"learn\","
45 		"\"targets\":["
46 			"{"
47 				"\"name\":\"target1\""
48 			"},"
49 			"{"
50 				"\"name\":\"target2\""
51 			"},"
52 			"{"
53 				"\"name\":\"target3\""
54 			"}"
55 		"]"
56 	"}", "{" /* test 3 */
57 		"\"schema\":\"com-warmcat-sai-builder\","
58 
59 		"\"hostname\":\"learn\","
60 		"\"nspawn_timeout\":1800,"
61 		"\"targets\":["
62 			"{"
63 				"\"name\":\"target1\","
64 				"\"unrecognized\":\"xyz\","
65 				"\"child\": {"
66 					"\"somename\": \"abc\","
67 					"\"junk\": { \"x\": \"y\" }"
68 				"}"
69 			"},"
70 			"{"
71 				"\"name\":\"target2\""
72 			"}"
73 		"]"
74 	"}",
75 	"{" /* test 4 */
76 		"\"schema\":\"com-warmcat-sai-builder\","
77 
78 		"\"hostname\":\"learn\","
79 		"\"nspawn_timeout\":1800"
80 	"}",
81 	"{" /* test 5 */
82 		"\"schema\":\"com-warmcat-sai-builder\""
83 	"}",
84 	"{" /* test 6 ... check huge strings into smaller fixed char array */
85 		"\"schema\":\"com-warmcat-sai-builder\","
86 		"\"hostname\":\""
87 		"PYvtan6kqppjnS0KpYTCaiOLsJkc7XecAr1kcE0aCIciewYB+JcLG82mO1Vb1mJtjDwUjBxy2I6A"
88 		"zefzoWUWmqZbsv4MXR55j9bKlyz1liiSX63iO0x6JAwACMtE2MkgcLwR86TSWAD9D1QKIWqg5RJ/"
89 		"CRuVsW0DKAUMD52ql4JmPFuJpJgTq28z6PhYNzN3yI3bmQt6bzhA+A/xAsFzSBnb3MHYWzGMprr5"
90 		"3FAP1ISo5Ec9i+2ehV40sG6Q470sH3PGQZ0YRPO7Sh/SyrSQ/scONmxRc3AcXl7X/CSs417ii+CV"
91 		"8sq3ZgcxKNB7tNfN7idNx3upZ00G2BZy9jSy03cLKKLNaNUt0TQsxXbH55uDHzSEeZWvxJgT6zB1"
92 		"NoMhdC02w+oXim94M6z6COCnqT3rgkGk8PHMry9Bkh4yVpRmzIRfMmln/lEhdZgxky2+g5hhlSIG"
93 		"JYDCrdynD9kCfvfy6KGOpNIi1X+mhbbWn4lnL9ZKihL/RrfOV+oV4R26IDq+KqUiJBENeo8/GXkG"
94 		"LUH/87iPyzXKEMavr6fkrK0vTGto8yEYxmOyaVz8phG5rwf4jJgmYNoMbGo8gWvhqO7UAGy2g7MW"
95 		"v+B/t1eZZ+1euLsNrWAsFJiFbQKgdFfQT3RjB14iU8knlQ8usoy+pXssY2ddGJGVcGC21oZvstK9"
96 		"eu1eRZftda/wP+N5unT1Hw7kCoVzqxHieiYt47EGIOaaQ7XjZDK6qPN6O/grHnvJZm2vBkxuXgsY"
97 		"VkRQ7AuTWIecphqFsq7Wbc1YNbMW47SVU5zMD0WaCqbaaI0t4uIzRvPlD8cpiiTzFTrEHlIBTf8/"
98 		"uZjjEGGLhJR1jPqA9D1Ej3ChV+ye6F9JTUMlozRMsGuF8U4btDzH5xdnmvRS4Ar6LKEtAXGkj2yu"
99 		"yJln+v4RIWj2xOGPJovOqiXwi0FyM61f8U8gj0OiNA2/QlvrqQVDF7sMXgjvaE7iQt5vMETteZlx"
100 		"+z3f+jTFM/aon511W4+ZkRD+6AHwucvM9BEC\""
101 	"}",
102 	"{" /* test 7 ... check huge strings into char * */
103 		"\"schema\":\"com-warmcat-sai-builder\","
104 		"\"targets\":["
105 			"{"
106 				"\"name\":\""
107 		"PYvtan6kqppjnS0KpYTCaiOLsJkc7XecAr1kcE0aCIciewYB+JcLG82mO1Vb1mJtjDwUjBxy2I6A"
108 		"zefzoWUWmqZbsv4MXR55j9bKlyz1liiSX63iO0x6JAwACMtE2MkgcLwR86TSWAD9D1QKIWqg5RJ/"
109 		"CRuVsW0DKAUMD52ql4JmPFuJpJgTq28z6PhYNzN3yI3bmQt6bzhA+A/xAsFzSBnb3MHYWzGMprr5"
110 		"3FAP1ISo5Ec9i+2ehV40sG6Q470sH3PGQZ0YRPO7Sh/SyrSQ/scONmxRc3AcXl7X/CSs417ii+CV"
111 		"8sq3ZgcxKNB7tNfN7idNx3upZ00G2BZy9jSy03cLKKLNaNUt0TQsxXbH55uDHzSEeZWvxJgT6zB1"
112 		"NoMhdC02w+oXim94M6z6COCnqT3rgkGk8PHMry9Bkh4yVpRmzIRfMmln/lEhdZgxky2+g5hhlSIG"
113 		"JYDCrdynD9kCfvfy6KGOpNIi1X+mhbbWn4lnL9ZKihL/RrfOV+oV4R26IDq+KqUiJBENeo8/GXkG"
114 		"LUH/87iPyzXKEMavr6fkrK0vTGto8yEYxmOyaVz8phG5rwf4jJgmYNoMbGo8gWvhqO7UAGy2g7MW"
115 		"v+B/t1eZZ+1euLsNrWAsFJiFbQKgdFfQT3RjB14iU8knlQ8usoy+pXssY2ddGJGVcGC21oZvstK9"
116 		"eu1eRZftda/wP+N5unT1Hw7kCoVzqxHieiYt47EGIOaaQ7XjZDK6qPN6O/grHnvJZm2vBkxuXgsY"
117 		"VkRQ7AuTWIecphqFsq7Wbc1YNbMW47SVU5zMD0WaCqbaaI0t4uIzRvPlD8cpiiTzFTrEHlIBTf8/"
118 		"uZjjEGGLhJR1jPqA9D1Ej3ChV+ye6F9JTUMlozRMsGuF8U4btDzH5xdnmvRS4Ar6LKEtAXGkj2yu"
119 		"yJln+v4RIWj2xOGPJovOqiXwi0FyM61f8U8gj0OiNA2/QlvrqQVDF7sMXgjvaE7iQt5vMETteZlx"
120 		"+z3f+jTFM/aon511W4+ZkRD+6AHwucvM9BEC\"}]}"
121 	"}",
122 	"{" /* test 8 the "other" schema */
123 		"\"schema\":\"com-warmcat-sai-other\","
124 		"\"name\":\"somename\""
125 	"}",
126 };
127 
128 /*
129  * These are the expected outputs for each test, without pretty formatting.
130  *
131  * There are some differences to do with missing elements being rendered with
132  * default values.
133  */
134 
135 static const char * const json_expected[] = {
136 	"{\"schema\":\"com-warmcat-sai-builder\",\"hostname\":\"learn\","
137 	  "\"nspawn_timeout\":1800,\"targets\":[{\"name\":\"target1\",\"someflag\":true},"
138 	  "{\"name\":\"target2\",\"someflag\":false}]}",
139 
140 	"{\"schema\":\"com-warmcat-sai-builder\",\"hostname\":\"learn\","
141 	 "\"nspawn_timeout\":0,\"targets\":[{\"name\":\"target1\",\"someflag\":false},"
142 	  "{\"name\":\"target2\",\"someflag\":false},{\"name\":\"target3\",\"someflag\":false}]}",
143 
144 	"{\"schema\":\"com-warmcat-sai-builder\",\"hostname\":\"learn\","
145 	"\"nspawn_timeout\":1800,\"targets\":[{\"name\":\"target1\",\"someflag\":false,"
146 	  "\"child\":{\"somename\":\"abc\"}},{\"name\":\"target2\",\"someflag\":false}]}",
147 
148 	"{\"schema\":\"com-warmcat-sai-builder\","
149 	  "\"hostname\":\"learn\",\"nspawn_timeout\":1800}",
150 
151 	"{\"schema\":\"com-warmcat-sai-builder\",\"hostname\":\"\","
152 	"\"nspawn_timeout\":0}",
153 
154 	"{\"schema\":\"com-warmcat-sai-builder\",\"hostname\":"
155 		"\"PYvtan6kqppjnS0KpYTCaiOLsJkc7Xe\","
156 	"\"nspawn_timeout\":0}",
157 
158 	"{\"schema\":\"com-warmcat-sai-builder\",\"hostname\":\"\","
159 	  "\"nspawn_timeout\":0,\"targets\":[{\"name\":\"PYvtan6kqppjnS0KpYTC"
160 		"aiOLsJkc7XecAr1kcE0aCIciewYB+JcLG82mO1Vb1mJtjDwUjBxy2I6Azefz"
161 		"oWUWmqZbsv4MXR55j9bKlyz1liiSX63iO0x6JAwACMtE2MkgcLwR86TSWAD9"
162 		"D1QKIWqg5RJ/CRuVsW0DKAUMD52ql4JmPFuJpJgTq28z6PhYNzN3yI3bmQt6"
163 		"bzhA+A/xAsFzSBnb3MHYWzGMprr53FAP1ISo5Ec9i+2ehV40sG6Q470sH3PG"
164 		"QZ0YRPO7Sh/SyrSQ/scONmxRc3AcXl7X/CSs417ii+CV8sq3ZgcxKNB7tNfN"
165 		"7idNx3upZ00G2BZy9jSy03cLKKLNaNUt0TQsxXbH55uDHzSEeZWvxJgT6zB1"
166 		"NoMhdC02w+oXim94M6z6COCnqT3rgkGk8PHMry9Bkh4yVpRmzIRfMmln/lEh"
167 		"dZgxky2+g5hhlSIGJYDCrdynD9kCfvfy6KGOpNIi1X+mhbbWn4lnL9ZKihL/"
168 		"RrfOV+oV4R26IDq+KqUiJBENeo8/GXkGLUH/87iPyzXKEMavr6fkrK0vTGto"
169 		"8yEYxmOyaVz8phG5rwf4jJgmYNoMbGo8gWvhqO7UAGy2g7MWv+B/t1eZZ+1e"
170 		"uLsNrWAsFJiFbQKgdFfQT3RjB14iU8knlQ8usoy+pXssY2ddGJGVcGC21oZv"
171 		"stK9eu1eRZftda/wP+N5unT1Hw7kCoVzqxHieiYt47EGIOaaQ7XjZDK6qPN6"
172 		"O/grHnvJZm2vBkxuXgsYVkRQ7AuTWIecphqFsq7Wbc1YNbMW47SVU5zMD0Wa"
173 		"CqbaaI0t4uIzRvPlD8cpiiTzFTrEHlIBTf8/uZjjEGGLhJR1jPqA9D1Ej3Ch"
174 		"V+ye6F9JTUMlozRMsGuF8U4btDzH5xdnmvRS4Ar6LKEtAXGkj2yuyJln+v4R"
175 		"IWj2xOGPJovOqiXwi0FyM61f8U8gj0OiNA2/QlvrqQVDF7sMXgjvaE7iQt5v"
176 		"METteZlx+z3f+jTFM/aon511W4+ZkRD+6AHwucvM9BEC\""
177 			",\"someflag\":false}]}",
178 	"{\"schema\":\"com-warmcat-sai-other\",\"name\":\"somename\"}"
179 };
180 
181 /*
182  * These annotate the members in the struct that will be serialized and
183  * deserialized with type and size information, as well as the name to use
184  * in the serialization format.
185  *
186  * Struct members that aren't annotated like this won't be serialized and
187  * when the struct is created during deserialiation, the will be set to 0
188  * or NULL.
189  */
190 
191 /* child object */
192 
193 typedef struct sai_child {
194 	const char *	somename;
195 } sai_child_t;
196 
197 lws_struct_map_t lsm_child[] = { /* describes serializable members */
198 	LSM_STRING_PTR	(sai_child_t, somename,			"somename"),
199 };
200 
201 /* target object */
202 
203 typedef struct sai_target {
204 	struct lws_dll2 target_list;
205 	sai_child_t *		child;
206 
207 	const char *		name;
208 	char			someflag;
209 } sai_target_t;
210 
211 static const lws_struct_map_t lsm_target[] = {
212 	LSM_STRING_PTR	(sai_target_t, name,			"name"),
213 	LSM_BOOLEAN	(sai_target_t, someflag,		"someflag"),
214 	LSM_CHILD_PTR	(sai_target_t, child, sai_child_t,
215 			 NULL, lsm_child,			"child"),
216 };
217 
218 /* the first kind of struct / schema we can receive */
219 
220 /* builder object */
221 
222 typedef struct sai_builder {
223 	struct lws_dll2_owner	targets;
224 
225 	char 			hostname[32];
226 	unsigned int 		nspawn_timeout;
227 } sai_builder_t;
228 
229 static const lws_struct_map_t lsm_builder[] = {
230 	LSM_CARRAY	(sai_builder_t, hostname,		"hostname"),
231 	LSM_UNSIGNED	(sai_builder_t, nspawn_timeout,		"nspawn_timeout"),
232 	LSM_LIST	(sai_builder_t, targets,
233 			 sai_target_t, target_list,
234 			 NULL, lsm_target,			"targets"),
235 };
236 
237 /*
238  * the second kind of struct / schema we can receive
239  */
240 
241 typedef struct sai_other {
242 	char 			name[32];
243 } sai_other_t;
244 
245 static const lws_struct_map_t lsm_other[] = {
246 	LSM_CARRAY	(sai_other_t, name,		"name"),
247 };
248 
249 /*
250  * meta composed pointers test
251  *
252  * We serialize a struct that consists of members that point to other objects,
253  * we expect this kind of thing
254  *
255  * {
256  *   "schema": "meta",
257  *   "t": { ... },
258  *   "e": { ...}
259  * }
260  */
261 
262 typedef struct meta {
263 	sai_target_t	*t;
264 	sai_builder_t	*b;
265 } meta_t;
266 
267 static const lws_struct_map_t lsm_meta[] = {
268 	LSM_CHILD_PTR	(meta_t, t, sai_target_t, NULL, lsm_target, "t"),
269 	LSM_CHILD_PTR	(meta_t, b, sai_child_t, NULL, lsm_builder, "e"),
270 };
271 
272 static const lws_struct_map_t lsm_schema_meta[] = {
273 	LSM_SCHEMA	(meta_t, NULL, lsm_meta, "meta.schema"),
274 };
275 
276 /*
277  * Schema table
278  *
279  * Before we can understand the serialization top level format, we must read
280  * the schema, use the table below to create the right toplevel object for the
281  * schema name, and select the correct map tables to interpret the rest of the
282  * serialization.
283  *
284  * In this example there are two completely separate structs / schemas possible
285  * to receive, and we disambiguate and create the correct one using the schema
286  * JSON node.
287  *
288  * Therefore the schema table below is the starting point for the JSON
289  * deserialization.
290  */
291 
292 static const lws_struct_map_t lsm_schema_map[] = {
293 	LSM_SCHEMA	(sai_builder_t, NULL,
294 			 lsm_builder,		"com-warmcat-sai-builder"),
295 	LSM_SCHEMA	(sai_other_t, NULL,
296 			 lsm_other,		"com-warmcat-sai-other"),
297 };
298 
299 static int
show_target(struct lws_dll2 * d,void * user)300 show_target(struct lws_dll2 *d, void *user)
301 {
302 	sai_target_t *t = lws_container_of(d, sai_target_t, target_list);
303 
304 	lwsl_notice("    target.name '%s' (target %p)\n", t->name, t);
305 
306 	if (t->child)
307 		lwsl_notice("      child %p, target.child.somename '%s'\n",
308 			  t->child, t->child->somename);
309 
310 	return 0;
311 }
312 
313 
main(int argc,const char ** argv)314 int main(int argc, const char **argv)
315 {
316 	int n, m, e = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
317 #if 1
318 	lws_struct_serialize_t *ser;
319 	uint8_t buf[4096];
320 	size_t written;
321 #endif
322 	struct lejp_ctx ctx;
323 	lws_struct_args_t a;
324 	sai_builder_t *b, mb;
325 	sai_target_t mt;
326 	sai_other_t *o;
327 	const char *p;
328 	meta_t meta;
329 
330 	if ((p = lws_cmdline_option(argc, argv, "-d")))
331 		logs = atoi(p);
332 
333 	lws_set_log_level(logs, NULL);
334 	lwsl_user("LWS API selftest: lws_struct JSON\n");
335 
336 	for (m = 0; m < (int)LWS_ARRAY_SIZE(json_tests); m++) {
337 
338 		/* 1. deserialize the canned JSON into structs */
339 
340 		lwsl_notice("%s: ++++++++++++++++ test %d\n", __func__, m + 1);
341 
342 		memset(&a, 0, sizeof(a));
343 		a.map_st[0] = lsm_schema_map;
344 		a.map_entries_st[0] = LWS_ARRAY_SIZE(lsm_schema_map);
345 		a.ac_block_size = 512;
346 
347 		lws_struct_json_init_parse(&ctx, NULL, &a);
348 		n = (int)(signed char)lejp_parse(&ctx, (uint8_t *)json_tests[m],
349 						 strlen(json_tests[m]));
350 		if (n < 0) {
351 			lwsl_err("%s: notification JSON decode failed '%s'\n",
352 					__func__, lejp_error_to_string(n));
353 			e++;
354 			goto done;
355 		}
356 		lwsac_info(a.ac);
357 
358 		if (m + 1 != 8) {
359 			b = a.dest;
360 			if (!b) {
361 				lwsl_err("%s: didn't produce any output\n", __func__);
362 				e++;
363 				goto done;
364 			}
365 
366 			if (a.top_schema_index) {
367 				lwsl_err("%s: wrong top_schema_index\n", __func__);
368 				e++;
369 				goto done;
370 			}
371 
372 			lwsl_notice("builder.hostname = '%s', timeout = %d, targets (%d)\n",
373 				    b->hostname, b->nspawn_timeout,
374 				    b->targets.count);
375 
376 			lws_dll2_foreach_safe(&b->targets, NULL, show_target);
377 		} else {
378 			o = a.dest;
379 			if (!o) {
380 				lwsl_err("%s: didn't produce any output\n", __func__);
381 				e++;
382 				goto done;
383 			}
384 
385 			if (a.top_schema_index != 1) {
386 				lwsl_err("%s: wrong top_schema_index\n", __func__);
387 				e++;
388 				goto done;
389 			}
390 
391 			lwsl_notice("other.name = '%s'\n", o->name);
392 		}
393 
394 		/* 2. serialize the structs into JSON and confirm */
395 
396 		lwsl_notice("%s:    .... strarting serialization of test %d\n",
397 				__func__, m + 1);
398 
399 		if (m + 1 != 8) {
400 			ser = lws_struct_json_serialize_create(lsm_schema_map,
401 						LWS_ARRAY_SIZE(lsm_schema_map),
402 						       0//LSSERJ_FLAG_PRETTY
403 						       , b);
404 		} else {
405 			ser = lws_struct_json_serialize_create(&lsm_schema_map[1],
406 						1,
407 						       0//LSSERJ_FLAG_PRETTY
408 						       , o);
409 		}
410 		if (!ser) {
411 			lwsl_err("%s: unable to init serialization\n", __func__);
412 			goto bail;
413 		}
414 
415 		do {
416 			n = lws_struct_json_serialize(ser, buf, sizeof(buf),
417 						      &written);
418 			switch (n) {
419 			case LSJS_RESULT_FINISH:
420 				puts((const char *)buf);
421 				break;
422 			case LSJS_RESULT_CONTINUE:
423 			case LSJS_RESULT_ERROR:
424 				goto bail;
425 			}
426 		} while(n == LSJS_RESULT_CONTINUE);
427 
428 		if (strcmp(json_expected[m], (char *)buf)) {
429 			lwsl_err("%s: test %d: expected %s\n", __func__, m + 1,
430 					json_expected[m]);
431 			e++;
432 			goto done;
433 		}
434 
435 		lws_struct_json_serialize_destroy(&ser);
436 
437 done:
438 		lwsac_free(&a.ac);
439 	}
440 
441 	if (e)
442 		goto bail;
443 
444 	/* ad-hoc tests */
445 
446 	memset(&meta, 0, sizeof(meta));
447 	memset(&mb, 0, sizeof(mb));
448 	memset(&mt, 0, sizeof(mt));
449 
450 	meta.t = &mt;
451 	meta.b = &mb;
452 
453 	meta.t->name = "mytargetname";
454 	lws_strncpy(meta.b->hostname, "myhostname", sizeof(meta.b->hostname));
455 	ser = lws_struct_json_serialize_create(lsm_schema_meta, 1, 0,
456 					       &meta);
457 	if (!ser) {
458 		lwsl_err("%s: failed to create json\n", __func__);
459 
460 
461 	}
462 	do {
463 		n = lws_struct_json_serialize(ser, buf, sizeof(buf), &written);
464 		switch (n) {
465 		case LSJS_RESULT_CONTINUE:
466 		case LSJS_RESULT_FINISH:
467 			puts((const char *)buf);
468 			if (strcmp((const char *)buf,
469 				"{\"schema\":\"meta.schema\","
470 				"\"t\":{\"name\":\"mytargetname\","
471 					"\"someflag\":false},"
472 				"\"e\":{\"hostname\":\"myhostname\","
473 					"\"nspawn_timeout\":0}}")) {
474 				lwsl_err("%s: meta test fail\n", __func__);
475 				goto bail;
476 			}
477 			break;
478 		case LSJS_RESULT_ERROR:
479 			goto bail;
480 		}
481 	} while(n == LSJS_RESULT_CONTINUE);
482 
483 	lws_struct_json_serialize_destroy(&ser);
484 
485 
486 	lwsl_user("Completed: PASS\n");
487 
488 	return 0;
489 
490 bail:
491 	lwsl_user("Completed: FAIL\n");
492 
493 	return 1;
494 }
495