diff --git a/test/api_test.c b/test/api_test.c index 920ee9f1455c5318e5d5a373f202cd52a67edb2e..be3d36445f19dddef0c2ada1aea1111cc115850e 100644 --- a/test/api_test.c +++ b/test/api_test.c @@ -540,6 +540,77 @@ static void test_json_write_to_file(void **state) json_object_put(obj); } +static void test_json_set_object_invalid_format(void **state) +{ + struct test_env *e = (struct test_env *) *state; + + struct json_object *obj = json_object_new_object(); + + json_object_object_add(obj, "test2", json_object_new_string("success")); + json_object_object_add(e->file_obj, "string", obj); + + json_object_set_by_string(&e->modify_obj, "string", "{\"test2\":\"success\"", json_type_object); + + printf("e->file_obj=%s\n", json_object_get_string(e->file_obj)); + printf("e->modify_obj=%s\n", json_object_get_string(e->modify_obj)); + + assert_non_null(e->file_obj); + assert_non_null(e->modify_obj); + assert_int_not_equal(1, json_object_equal(e->modify_obj, e->file_obj)); +} + +static void test_json_set_array_invalid_format(void **state) +{ + struct test_env *e = (struct test_env *) *state; + + struct json_object *arr = json_object_new_array(); + + json_object_array_add(arr, json_object_new_int(1)); + json_object_array_add(arr, json_object_new_int(2)); + json_object_array_add(arr, json_object_new_int(3)); + json_object_object_add(e->file_obj, "ints", arr); + + json_object_set_by_string(&e->modify_obj, "ints", "[ 1, 2 3 ]", json_type_array); + + printf("e->file_obj=%s\n", json_object_get_string(e->file_obj)); + printf("e->modify_obj=%s\n", json_object_get_string(e->modify_obj)); + + assert_non_null(e->file_obj); + assert_non_null(e->modify_obj); + assert_int_not_equal(1, json_object_equal(e->modify_obj, e->file_obj)); +} + +static void test_json_set_wrong_type_string_to_int(void **state) +{ + struct test_env *e = (struct test_env *) *state; + + json_object_object_add(e->file_obj, "string", json_object_new_string("abc")); + + json_object_set_by_string(&e->modify_obj, "string", "abc", json_type_int); + + printf("e->file_obj=%s\n", json_object_get_string(e->file_obj)); + printf("e->modify_obj=%s\n", json_object_get_string(e->modify_obj)); + + assert_non_null(e->file_obj); + assert_non_null(e->modify_obj); + assert_int_not_equal(1, json_object_equal(e->modify_obj, e->file_obj)); +} + +static void test_json_set_wrong_type_int_to_string(void **state) +{ + struct test_env *e = (struct test_env *) *state; + + json_object_object_add(e->file_obj, "string", json_object_new_int(1)); + + json_object_set_by_string(&e->modify_obj, "string", "1", json_type_string); + + printf("e->file_obj=%s\n", json_object_get_string(e->file_obj)); + printf("e->modify_obj=%s\n", json_object_get_string(e->modify_obj)); + + assert_non_null(e->modify_obj); + assert_int_not_equal(1, json_object_equal(e->modify_obj, e->file_obj)); +} + static int setup (void** state) { struct test_env *e = (struct test_env *) *state; @@ -600,6 +671,10 @@ int main(void) { cmocka_unit_test_setup_teardown(test_json_get_array_nested_int, setup, teardown), cmocka_unit_test_setup_teardown(test_json_get_object_array_nested_int, setup, teardown), cmocka_unit_test_setup_teardown(test_json_write_to_file, setup, teardown), + cmocka_unit_test_setup_teardown(test_json_set_object_invalid_format, setup, teardown), + cmocka_unit_test_setup_teardown(test_json_set_array_invalid_format, setup, teardown), + cmocka_unit_test_setup_teardown(test_json_set_wrong_type_string_to_int, setup, teardown), + cmocka_unit_test_setup_teardown(test_json_set_wrong_type_int_to_string, setup, teardown), }; return cmocka_run_group_tests(tests, group_setup, group_teardown);