Commit 973075d4 authored by Konrad Sztyber's avatar Konrad Sztyber Committed by Tomasz Zawadzki
Browse files

ut/json: add tests for UUID decode/write functions



These functions were added recently, but were missing unit tests, so a
couple of small test cases were added to verify them.

Signed-off-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Change-Id: I2dd5b617970e9738b7b5e2059900258b38e9dbc4
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/20533


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Community-CI: Mellanox Build Bot
parent c7fec8a9
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
@@ -769,6 +769,48 @@ test_decode_string(void)
	free(value);
}

static void
test_decode_uuid(void)
{
	struct spdk_json_val v;
	struct spdk_uuid expected, uuid = {};
	const char *uuidstr = "e524acae-8c26-43e4-882a-461b8690583b";
	const char *invalid = "e524acae-8c26";
	int rc;

	rc = spdk_uuid_parse(&expected, uuidstr);
	CU_ASSERT_EQUAL(rc, 0);

	/* Check a valid UUID */
	v.type = SPDK_JSON_VAL_STRING;
	v.start = (void *)uuidstr;
	v.len = strlen(uuidstr);
	rc = spdk_json_decode_uuid(&v, &uuid);
	CU_ASSERT_EQUAL(rc, 0);
	CU_ASSERT_EQUAL(spdk_uuid_compare(&uuid, &expected), 0);

	/* Check empty string as UUID */
	v.type = SPDK_JSON_VAL_STRING;
	v.start = "";
	v.len = 0;
	rc = spdk_json_decode_uuid(&v, &uuid);
	CU_ASSERT_EQUAL(rc, -1);

	/* Check non-empty string that's not a UUID */
	v.type = SPDK_JSON_VAL_STRING;
	v.start = (void *)invalid;
	v.len = strlen(invalid);
	rc = spdk_json_decode_uuid(&v, &uuid);
	CU_ASSERT_EQUAL(rc, -1);

	/* Check decoding UUID on a non-string value */
	v.type = SPDK_JSON_VAL_TRUE;
	v.start = NULL;
	v.len = 0;
	rc = spdk_json_decode_uuid(&v, &uuid);
	CU_ASSERT_EQUAL(rc, -1);
}

char ut_json_text[] =
	"{"
	"	\"string\": \"Some string data\","
@@ -971,6 +1013,7 @@ main(int argc, char **argv)
	CU_ADD_TEST(suite, test_decode_uint32);
	CU_ADD_TEST(suite, test_decode_uint64);
	CU_ADD_TEST(suite, test_decode_string);
	CU_ADD_TEST(suite, test_decode_uuid);
	CU_ADD_TEST(suite, test_find);
	CU_ADD_TEST(suite, test_find_array);
	CU_ADD_TEST(suite, test_iterating);
+19 −0
Original line number Diff line number Diff line
@@ -100,6 +100,8 @@ write_cb(void *cb_ctx, const void *data, size_t size)

#define VAL_DOUBLE(d) CU_ASSERT(spdk_json_write_double(w, d) == 0);

#define VAL_UUID(u) CU_ASSERT(spdk_json_write_uuid(w, u) == 0)

#define VAL_ARRAY_BEGIN() CU_ASSERT(spdk_json_write_array_begin(w) == 0)
#define VAL_ARRAY_END() CU_ASSERT(spdk_json_write_array_end(w) == 0)

@@ -550,6 +552,22 @@ test_write_number_double(void)
	END_SIZE("-1.23456780000000003383e+03", 27);
}

static void
test_write_uuid(void)
{
#define UT_UUID "e524acae-8c26-43e4-882a-461b8690583b"
	struct spdk_json_write_ctx *w;
	struct spdk_uuid uuid;
	int rc;

	rc = spdk_uuid_parse(&uuid, UT_UUID);
	CU_ASSERT_EQUAL(rc, 0);

	BEGIN();
	VAL_UUID(&uuid);
	END("\"" UT_UUID "\"");
}

static void
test_write_array(void)
{
@@ -866,6 +884,7 @@ main(int argc, char **argv)
	CU_ADD_TEST(suite, test_write_number_int64);
	CU_ADD_TEST(suite, test_write_number_uint64);
	CU_ADD_TEST(suite, test_write_number_double);
	CU_ADD_TEST(suite, test_write_uuid);
	CU_ADD_TEST(suite, test_write_array);
	CU_ADD_TEST(suite, test_write_object);
	CU_ADD_TEST(suite, test_write_nesting);