Commit 07efbe6d authored by Sanjay Vallimanalan's avatar Sanjay Vallimanalan Committed by Chris Friedt
Browse files

tests: lib: json: add test for mixed array types



Add tests for encoding, parsing and calculating length mixed array
types.

Signed-off-by: default avatarSanjay Vallimanalan <sanjay@linumiz.com>
parent ce4eba52
Loading
Loading
Loading
Loading
+128 −0
Original line number Diff line number Diff line
@@ -348,6 +348,28 @@ static const struct json_obj_descr enums_descr[] = {
	JSON_OBJ_DESCR_PRIM(struct test_enums, u32, JSON_TOK_UINT),
};

struct test_mixed_arr {
	const char *msg_type;
	uint64_t dev_id;
	struct test_nested nested;
	int arr[3];
	size_t arr_len;
	char status_buf[10];
	size_t count;
};

static const struct json_obj_descr test_mixed_arr_descr_arr[] = {
	JSON_OBJ_DESCR_ARRAY(struct test_mixed_arr, arr, 3, arr_len, JSON_TOK_NUMBER),
};

static const struct json_mixed_arr_descr test_mixed_arr_descr[] = {
	JSON_MIXED_ARR_DESCR_PRIM(struct test_mixed_arr, msg_type, JSON_TOK_STRING, count),
	JSON_MIXED_ARR_DESCR_PRIM(struct test_mixed_arr, dev_id, JSON_TOK_UINT64, count),
	JSON_MIXED_ARR_DESCR_OBJECT(struct test_mixed_arr, nested, nested_descr, count),
	JSON_MIXED_ARR_DESCR_ARRAY(struct test_mixed_arr, arr, 3, test_mixed_arr_descr_arr, count),
	JSON_MIXED_ARR_DESCR_PRIM(struct test_mixed_arr, status_buf, JSON_TOK_STRING_BUF, count),
};


ZTEST(lib_json_test, test_json_encoding)
{
@@ -2198,4 +2220,110 @@ ZTEST(lib_json_test, test_json_string_nullptr)
	zassert_str_equal(ts.some_string, "", "String not decoded correctly");
}

ZTEST(lib_json_test, test_json_mixed_arr_parse)
{
	struct test_mixed_arr arr = {0};
	char json[] = "[\"msg\", 123456, {\"nested_int\":42,\"nested_bool\":true,"
		      "\"nested_string\":\"abc\","
		      "\"nested_string_buf\":\"buf\",\"nested_int8\":1,\"nested_uint8\":2,"
		      "\"nested_int64\":3,\"nested_uint64\":4}, [10,20,30], \"ok\"]";
	int ret = json_mixed_arr_parse(json, strlen(json),
				       test_mixed_arr_descr,
				       ARRAY_SIZE(test_mixed_arr_descr), &arr);

	zassert_equal(ret, 5, "Should parse 5 elements");
	zassert_str_equal(arr.msg_type, "msg", NULL);
	zassert_equal(arr.dev_id, 123456, NULL);
	zassert_equal(arr.nested.nested_int, 42, NULL);
	zassert_equal(arr.arr_len, 3, NULL);
	zassert_equal(arr.arr[0], 10, NULL);
	zassert_equal(arr.arr[1], 20, NULL);
	zassert_equal(arr.arr[2], 30, NULL);
	zassert_str_equal(arr.status_buf, "ok", NULL);
}

ZTEST(lib_json_test, test_json_mixed_arr_encode)
{
	char buf[256];
	struct test_mixed_arr pkt = {0};
	struct test_mixed_arr arr = {
		.msg_type = "msg",
		.dev_id = 123456,
		.nested = {
			.nested_int = 42,
			.nested_bool = true,
			.nested_string = "abc",
			.nested_string_buf = "buf",
			.nested_int8 = 1,
			.nested_uint8 = 2,
			.nested_int64 = 3,
			.nested_uint64 = 4,
		},
		.arr = {10, 20, 30},
		.arr_len = 3,
		.status_buf = "ok",
		.count = 5
	};
	int ret = json_mixed_arr_encode_buf(test_mixed_arr_descr,
					    ARRAY_SIZE(test_mixed_arr_descr),
					    &arr, buf, sizeof(buf));

	zassert_equal(ret, 0, NULL);

	ret = json_mixed_arr_parse(buf, strlen(buf),
				   test_mixed_arr_descr,
				   ARRAY_SIZE(test_mixed_arr_descr), &pkt);

	zassert_equal(ret, 5, NULL);
	zassert_str_equal(pkt.msg_type, "msg", NULL);
	zassert_equal(pkt.dev_id, 123456, NULL);
	zassert_equal(pkt.arr[0], 10, NULL);
	zassert_str_equal(pkt.status_buf, "ok", NULL);
}

ZTEST(lib_json_test, test_json_mixed_arr_empty)
{
	struct test_mixed_arr arr = {0};
	char json[] = "[]";
	int ret = json_mixed_arr_parse(json, strlen(json),
		test_mixed_arr_descr, 0, &arr);

	zassert_equal(ret, 0, NULL);
}

ZTEST(lib_json_test, test_json_mixed_arr_calc_len)
{
	char buf[256];
	ssize_t calc_len;
	int ret;
	struct test_mixed_arr arr = {
		.msg_type = "msg",
		.dev_id = 123456,
		.nested = {
			.nested_int = 42,
			.nested_bool = true,
			.nested_string = "abc",
			.nested_string_buf = "buf",
			.nested_int8 = 1,
			.nested_uint8 = 2,
			.nested_int64 = 3,
			.nested_uint64 = 4,
		},
		.arr = {10, 20, 30},
		.arr_len = 3,
		.status_buf = "ok",
		.count = 5
	};

	calc_len = json_calc_mixed_arr_len(test_mixed_arr_descr,
					   ARRAY_SIZE(test_mixed_arr_descr),
					   &arr);
	ret = json_mixed_arr_encode_buf(test_mixed_arr_descr,
					ARRAY_SIZE(test_mixed_arr_descr),
					&arr, buf, sizeof(buf));
	zassert_equal(ret, 0, NULL);

	zassert_equal(calc_len, (ssize_t)strlen(buf), "Length mismatch");
}

ZTEST_SUITE(lib_json_test, NULL, NULL, NULL, NULL, NULL);