eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

test-json.c (14143B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2021  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <stdio.h>
     28 #include <assert.h>
     29 
     30 #include <ell/ell.h>
     31 
     32 #include "src/json.h"
     33 #include "ell/useful.h"
     34 
     35 struct json_contents;
     36 
     37 
     38 /*
     39  * Object:
     40  * { "test": "<smile emoji>" }
     41  */
     42 static void test_json_unicode(const void *data)
     43 {
     44 	struct json_iter iter;
     45 	_auto_(l_free)char *v;
     46 	uint8_t expected[] = { 0xF0, 0x9F, 0x98, 0x80, 0 };
     47 
     48 	uint8_t s[] = { '{', '"', 't', 'e', 's', 't', '"', ':',
     49 				'"', 0xf0, 0x9f, 0x98, 0x80, '"', '}', 0 };
     50 	struct json_contents *c = json_contents_new((const char *)s, sizeof(s));
     51 
     52 	json_iter_init(&iter, c);
     53 	assert(json_iter_parse(&iter, JSON_MANDATORY("test", JSON_STRING, &v),
     54 				JSON_UNDEFINED));
     55 
     56 	assert(!strcmp(v, (char *)expected));
     57 
     58 	json_contents_free(c);
     59 }
     60 
     61 static void test_json_escaped_unicode(const void *data)
     62 {
     63 	struct json_iter iter;
     64 	_auto_(l_free)char *v;
     65 	char *s = "{\"test\":\"\\uD8F0\"}";
     66 	char expected[] = "\\uD8F0";
     67 	struct json_contents *c = json_contents_new(s, strlen(s));
     68 
     69 	json_iter_init(&iter, c);
     70 	assert(json_iter_parse(&iter, JSON_MANDATORY("test", JSON_STRING, &v),
     71 				JSON_UNDEFINED));
     72 
     73 	assert(!strcmp(v, expected));
     74 
     75 	json_contents_free(c);
     76 }
     77 
     78 /*
     79  * Basic test string values and nested objects
     80  */
     81 static void test_json(const void *data)
     82 {
     83 	char json[] = "{\"wi-fi_tech\":\"infra\","
     84 			"\"discovery\":"
     85 				"{\"ssid\":\"somessid\"},"
     86 			"\"cred\":"
     87 				"{\"akm\":\"psk\","
     88 				"\"pass\":\"somepassphrase\"}}";
     89 	_auto_(l_free)char *tech;
     90 	_auto_(l_free)char *ssid;
     91 	_auto_(l_free)char *akm;
     92 	_auto_(l_free)char *pass;
     93 	_auto_(l_free)char *opt_not_found;
     94 	struct json_iter objnotfound;
     95 	struct json_iter discovery;
     96 	struct json_iter cred;
     97 	struct json_iter iter;
     98 	struct json_contents *c = json_contents_new(json, strlen(json));
     99 
    100 	json_iter_init(&iter, c);
    101 	assert(json_iter_parse(&iter,
    102 			JSON_MANDATORY("wi-fi_tech", JSON_STRING, &tech),
    103 			JSON_MANDATORY("discovery", JSON_OBJECT, &discovery),
    104 			JSON_MANDATORY("cred", JSON_OBJECT, &cred),
    105 			JSON_OPTIONAL("notfound", JSON_STRING, &opt_not_found),
    106 			JSON_OPTIONAL("objnotfound", JSON_OBJECT, &objnotfound),
    107 			JSON_UNDEFINED));
    108 
    109 	assert(!opt_not_found);
    110 	assert(!json_iter_is_valid(&objnotfound));
    111 
    112 	assert(!strcmp(tech, "infra"));
    113 
    114 	assert(json_iter_parse(&discovery,
    115 			JSON_MANDATORY("ssid", JSON_STRING, &ssid),
    116 			JSON_UNDEFINED));
    117 
    118 	assert(!strcmp(ssid, "somessid"));
    119 
    120 	assert(json_iter_parse(&cred,
    121 			JSON_MANDATORY("akm", JSON_STRING, &akm),
    122 			JSON_MANDATORY("pass", JSON_STRING, &pass),
    123 			JSON_UNDEFINED));
    124 	assert(!strcmp(akm, "psk"));
    125 	assert(!strcmp(pass, "somepassphrase"));
    126 
    127 	json_contents_free(c);
    128 }
    129 
    130 /*
    131  * Tests empty objects parse successfully
    132  */
    133 static void test_json_empty_objects(const void *data)
    134 {
    135 	char empty[] = "{}";
    136 	char nested_empty[] = "{\"empty\":{}}";
    137 	struct json_iter nested_iter;
    138 	struct json_iter iter;
    139 	struct json_contents *c = json_contents_new(empty, strlen(empty));
    140 
    141 	json_iter_init(&iter, c);
    142 	assert(json_iter_parse(&iter, JSON_UNDEFINED));
    143 
    144 	assert(json_iter_parse(&iter,
    145 				JSON_OPTIONAL("optional", JSON_OBJECT, NULL),
    146 				JSON_UNDEFINED));
    147 
    148 	assert(!json_iter_parse(&iter,
    149 				JSON_MANDATORY("optional", JSON_OBJECT, NULL),
    150 				JSON_UNDEFINED));
    151 	json_contents_free(c);
    152 
    153 	c = json_contents_new(nested_empty, strlen(nested_empty));
    154 	json_iter_init(&iter, c);
    155 	assert(json_iter_parse(&iter,
    156 			JSON_MANDATORY("empty", JSON_OBJECT, &nested_iter),
    157 			JSON_UNDEFINED));
    158 	assert(json_iter_parse(&nested_iter, JSON_UNDEFINED));
    159 	assert(!json_iter_parse(&nested_iter,
    160 				JSON_MANDATORY("mandatory", JSON_OBJECT, NULL),
    161 				JSON_UNDEFINED));
    162 	json_contents_free(c);
    163 }
    164 
    165 /*
    166  * Tests that expected key/values can be provided in an order different than
    167  * they appear in the JSON string.
    168  */
    169 static void test_json_out_of_order(const void *data)
    170 {
    171 	char object[] = "{\"key1\":{},\"key2\":\"val2\",\"key3\":\"val3\"}";
    172 	struct json_iter iter;
    173 	struct json_contents *c = json_contents_new(object, strlen(object));
    174 
    175 	json_iter_init(&iter, c);
    176 	assert(json_iter_parse(&iter,
    177 				JSON_OPTIONAL("nonexist1", JSON_STRING, NULL),
    178 				JSON_MANDATORY("key3", JSON_STRING, NULL),
    179 				JSON_MANDATORY("key2", JSON_STRING, NULL),
    180 				JSON_OPTIONAL("nonexist2", JSON_OBJECT, NULL),
    181 				JSON_MANDATORY("key1", JSON_OBJECT, NULL),
    182 				JSON_UNDEFINED));
    183 	json_contents_free(c);
    184 }
    185 
    186 /*
    187  * Tests that the token bounds checking works.
    188  */
    189 static void test_json_larger_object(const void *data)
    190 {
    191 	char json[] = "{\"test1\":\"tester1\","
    192 			"\"test2\":\"tester2\","
    193 			"\"test3\":\"tester3\","
    194 			"\"test4\":\"tester4\","
    195 			"\"test5\":\"tester5\","
    196 			"\"test6\":\"tester6\","
    197 			"\"test7\":\"tester7\","
    198 			"\"test8\":\"tester8\","
    199 			"\"test9\":\"tester9\","
    200 			"\"test10\":\"tester10\","
    201 			"\"test11\":\"tester11\","
    202 			"\"test12\":\"tester12\","
    203 			"\"test13\":\"tester13\"}";
    204 
    205 	struct json_iter iter;
    206 	struct json_contents *c = json_contents_new(json, strlen(json));
    207 
    208 	json_iter_init(&iter, c);
    209 	assert(json_iter_parse(&iter,
    210 				JSON_MANDATORY("test13", JSON_STRING, NULL),
    211 				JSON_UNDEFINED));
    212 	json_contents_free(c);
    213 }
    214 
    215 static void check_primitives(struct json_iter *i, struct json_iter *ui,
    216 				struct json_iter *t, struct json_iter *f,
    217 				struct json_iter *null, struct json_iter *obj)
    218 {
    219 
    220 	int i_val;
    221 	unsigned int ui_val;
    222 	bool b_val;
    223 
    224 	assert(json_iter_is_valid(i));
    225 	assert(!json_iter_get_uint(i, NULL));
    226 	assert(!json_iter_get_boolean(i, NULL));
    227 	assert(!json_iter_get_null(i));
    228 	assert(!json_iter_next(i));
    229 	assert(json_iter_get_int(i, &i_val));
    230 	assert(i_val == -10);
    231 
    232 	assert(json_iter_is_valid(ui));
    233 	assert(!json_iter_get_boolean(ui, NULL));
    234 	assert(!json_iter_get_null(ui));
    235 	assert(!json_iter_next(ui));
    236 	assert(json_iter_get_int(ui, &i_val));
    237 	assert(json_iter_get_uint(ui, &ui_val));
    238 	assert(i_val == 10 && ui_val == 10);
    239 
    240 	assert(json_iter_is_valid(t));
    241 	assert(!json_iter_get_null(t));
    242 	assert(!json_iter_get_int(t, NULL));
    243 	assert(!json_iter_get_uint(t, NULL));
    244 	assert(!json_iter_next(t));
    245 	assert(json_iter_get_boolean(t, &b_val));
    246 	assert(b_val == true);
    247 
    248 	assert(json_iter_is_valid(f));
    249 	assert(!json_iter_get_null(f));
    250 	assert(!json_iter_get_int(f, NULL));
    251 	assert(!json_iter_get_uint(f, NULL));
    252 	assert(!json_iter_next(f));
    253 	assert(json_iter_get_boolean(f, &b_val));
    254 	assert(b_val == false);
    255 
    256 	assert(json_iter_is_valid(null));
    257 	assert(!json_iter_get_int(null, NULL));
    258 	assert(!json_iter_get_uint(null, NULL));
    259 	assert(!json_iter_get_boolean(null, NULL));
    260 	assert(!json_iter_next(null));
    261 	assert(json_iter_get_null(null));
    262 
    263 	if (obj) {
    264 		assert(json_iter_is_valid(obj));
    265 		assert(!json_iter_next(obj));
    266 		assert(json_iter_parse(obj,
    267 			JSON_MANDATORY("null_val", JSON_PRIMITIVE, null),
    268 			JSON_MANDATORY("false_val", JSON_PRIMITIVE, f),
    269 			JSON_MANDATORY("true_val", JSON_PRIMITIVE, t),
    270 			JSON_MANDATORY("int_val", JSON_PRIMITIVE, i),
    271 			JSON_MANDATORY("uint_val", JSON_PRIMITIVE, ui),
    272 			JSON_UNDEFINED));
    273 
    274 		check_primitives(i, ui, t, f, null, NULL);
    275 	}
    276 }
    277 
    278 static void test_json_primitives(const void *data)
    279 {
    280 	char json[] = "{\"int_val\": -10,"
    281 			"\"uint_val\": 10,"
    282 			"\"true_val\": true,"
    283 			"\"false_val\": false,"
    284 			"\"null_val\": null,"
    285 			"\"obj_val\":{"
    286 				"\"int_val\": -10,"
    287 				"\"uint_val\": 10,"
    288 				"\"true_val\": true,"
    289 				"\"false_val\": false,"
    290 				"\"null_val\": null}}";
    291 	struct json_contents *c = json_contents_new(json, strlen(json));
    292 	struct json_iter outer, inner, null, f, t, i, ui;
    293 	struct json_iter not_found;
    294 
    295 	json_iter_init(&outer, c);
    296 	assert(json_iter_parse(&outer,
    297 			JSON_MANDATORY("obj_val", JSON_OBJECT, &inner),
    298 			JSON_MANDATORY("null_val", JSON_PRIMITIVE, &null),
    299 			JSON_MANDATORY("false_val", JSON_PRIMITIVE, &f),
    300 			JSON_MANDATORY("true_val", JSON_PRIMITIVE, &t),
    301 			JSON_MANDATORY("int_val", JSON_PRIMITIVE, &i),
    302 			JSON_MANDATORY("uint_val", JSON_PRIMITIVE, &ui),
    303 			JSON_OPTIONAL("not_found", JSON_PRIMITIVE, &not_found),
    304 			JSON_UNDEFINED));
    305 
    306 	assert(!json_iter_is_valid(&not_found));
    307 
    308 	check_primitives(&i, &ui, &t, &f, &null, &inner);
    309 
    310 	json_contents_free(c);
    311 }
    312 
    313 static void test_json_arrays(const void *data)
    314 {
    315 	unsigned int ui;
    316 	int i;
    317 	bool b;
    318 	int count;
    319 	char json[] = "{\"uint_array\":[1, 2, 3, 4, 5, 6],"
    320 			"\"int_array\":[-1, -2, -3, -4, -5, -6],"
    321 			"\"bool_array\":[true, false, true, false],"
    322 			"\"null_array\":[null, null, null, null],"
    323 			"\"obj_array\":[{}, {\"key\":\"value\", \"key2\":\"value2\"}],"
    324 			"\"mixed_array\":[1, -1, true, false, null, \"string\"]}";
    325 
    326 	struct json_iter iter;
    327 	struct json_iter i_array, ui_array, b_array, n_array,
    328 				m_array, obj_array;
    329 	struct json_contents *c = json_contents_new(json, strlen(json));
    330 
    331 	json_iter_init(&iter, c);
    332 	assert(json_iter_parse(&iter,
    333 			JSON_MANDATORY("mixed_array", JSON_ARRAY, &m_array),
    334 			JSON_MANDATORY("null_array", JSON_ARRAY, &n_array),
    335 			JSON_MANDATORY("bool_array", JSON_ARRAY, &b_array),
    336 			JSON_MANDATORY("int_array", JSON_ARRAY, &i_array),
    337 			JSON_MANDATORY("uint_array", JSON_ARRAY, &ui_array),
    338 			JSON_MANDATORY("obj_array", JSON_ARRAY, &obj_array),
    339 			JSON_UNDEFINED));
    340 
    341 	count = 1;
    342 
    343 	while (json_iter_next(&ui_array)) {
    344 		assert(json_iter_get_type(&ui_array) == JSON_PRIMITIVE);
    345 		assert(!json_iter_parse(&ui_array, JSON_UNDEFINED));
    346 		assert(json_iter_get_uint(&ui_array, &ui));
    347 		assert(ui == (unsigned int) count);
    348 		count++;
    349 	}
    350 
    351 	count = -1;
    352 
    353 	while (json_iter_next(&i_array)) {
    354 		assert(json_iter_get_type(&i_array) == JSON_PRIMITIVE);
    355 		assert(!json_iter_parse(&i_array, JSON_UNDEFINED));
    356 		assert(json_iter_get_int(&i_array, &i));
    357 		assert(i == count);
    358 		count--;
    359 	}
    360 
    361 	count = 0;
    362 
    363 	while (json_iter_next(&b_array)) {
    364 		assert(json_iter_get_type(&b_array) == JSON_PRIMITIVE);
    365 		assert(!json_iter_parse(&b_array, JSON_UNDEFINED));
    366 		assert(json_iter_get_boolean(&b_array, &b));
    367 		assert(b == count % 2 ? false : true);
    368 		count++;
    369 	}
    370 
    371 	count = 0;
    372 
    373 	while (json_iter_next(&n_array)) {
    374 		assert(json_iter_get_type(&n_array) == JSON_PRIMITIVE);
    375 		assert(!json_iter_parse(&n_array, JSON_UNDEFINED));
    376 		assert(json_iter_get_null(&n_array));
    377 		count++;
    378 	}
    379 
    380 	assert(count == 4);
    381 
    382 	count = 0;
    383 
    384 	while (json_iter_next(&m_array)) {
    385 		_auto_(l_free) char *str = NULL;
    386 
    387 		switch (count) {
    388 		case 0:
    389 			assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE);
    390 			assert(json_iter_get_uint(&m_array, &ui));
    391 			assert(ui == 1);
    392 			break;
    393 		case 1:
    394 			assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE);
    395 			assert(json_iter_get_int(&m_array, &i));
    396 			assert(i == -1);
    397 			break;
    398 		case 2:
    399 		case 3:
    400 			assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE);
    401 			assert(json_iter_get_boolean(&m_array, &b));
    402 			assert(b == count % 2 ? false : true);
    403 			break;
    404 		case 4:
    405 			assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE);
    406 			assert(json_iter_get_null(&m_array));
    407 			break;
    408 		case 5:
    409 			assert(json_iter_get_type(&m_array) == JSON_STRING);
    410 			assert(json_iter_get_string(&m_array, &str));
    411 			assert(!strcmp(str, "string"));
    412 			break;
    413 		}
    414 
    415 		count++;
    416 	}
    417 
    418 	count = 0;
    419 
    420 	assert(!json_iter_parse(&obj_array, JSON_UNDEFINED));
    421 
    422 	while (json_iter_next(&obj_array)) {
    423 		struct json_iter object;
    424 
    425 		assert(json_iter_get_type(&obj_array) == JSON_OBJECT);
    426 		assert(json_iter_get_container(&obj_array, &object));
    427 
    428 		switch (count) {
    429 		case 0:
    430 			assert(json_iter_parse(&object, JSON_UNDEFINED));
    431 			break;
    432 		case 1:
    433 			assert(json_iter_parse(&object,
    434 				JSON_MANDATORY("key", JSON_STRING, NULL),
    435 				JSON_UNDEFINED));
    436 			break;
    437 		}
    438 
    439 		count++;
    440 	}
    441 
    442 	assert(count == 2);
    443 
    444 	json_contents_free(c);
    445 }
    446 
    447 static void test_json_nested_arrays(const void *data)
    448 {
    449 	char json[] = "{\"array\":[[], {}, [1, 2], {\"key\":\"value\"}, [\"one\",\"two\"]]}";
    450 	int count = 0;
    451 	struct json_iter iter;
    452 	struct json_iter array;
    453 	struct json_iter inner;
    454 	struct json_contents *c = json_contents_new(json, strlen(json));
    455 
    456 	json_iter_init(&iter, c);
    457 	assert(json_iter_parse(&iter,
    458 			JSON_MANDATORY("array", JSON_ARRAY, &array),
    459 			JSON_UNDEFINED));
    460 
    461 	while (json_iter_next(&array)) {
    462 		int count2 = 0;
    463 
    464 		assert(json_iter_get_container(&array, &inner));
    465 
    466 		while (json_iter_next(&inner)) {
    467 			_auto_(l_free) char *str = NULL;
    468 
    469 			switch (count) {
    470 			case 0:
    471 			case 1:
    472 				assert(false);
    473 				break;
    474 			case 4:
    475 				assert(json_iter_get_type(&inner) ==
    476 							JSON_STRING);
    477 				assert(json_iter_get_string(&inner, &str));
    478 
    479 				if (count2 == 0)
    480 					assert(!strcmp("one", str));
    481 				else
    482 					assert(!strcmp("two", str));
    483 
    484 				break;
    485 			}
    486 
    487 			count2++;
    488 		}
    489 
    490 		switch (count) {
    491 		case 0:
    492 			assert(count2 == 0);
    493 			break;
    494 		case 1:
    495 			assert(count2 == 0);
    496 			break;
    497 		case 2:
    498 			assert(count2 == 2);
    499 			break;
    500 		case 3:
    501 			assert(count2 == 0);
    502 			break;
    503 		case 4:
    504 			assert(count2 == 2);
    505 			break;
    506 		default:
    507 			break;
    508 		}
    509 
    510 		count++;
    511 	}
    512 
    513 	assert(count == 5);
    514 
    515 	json_contents_free(c);
    516 }
    517 
    518 int main(int argc, char *argv[])
    519 {
    520 	l_test_init(&argc, &argv);
    521 
    522 	l_test_add("json unicode", test_json_unicode, NULL);
    523 	l_test_add("json escaped unicode", test_json_escaped_unicode, NULL);
    524 	l_test_add("json nested objects", test_json, NULL);
    525 	l_test_add("json empty objects", test_json_empty_objects, NULL);
    526 	l_test_add("json parse out of order", test_json_out_of_order, NULL);
    527 	l_test_add("json larger object", test_json_larger_object, NULL);
    528 	l_test_add("json test primitives", test_json_primitives, NULL);
    529 	l_test_add("json test arrays", test_json_arrays, NULL);
    530 	l_test_add("json test nested arrays", test_json_nested_arrays, NULL);
    531 
    532 	return l_test_run();
    533 }