Skip to content
Snippets Groups Projects
api_test.c 13.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • #include <stdarg.h>
    #include <stddef.h>
    #include <setjmp.h>
    #include <cmocka.h>
    #include <json-c/json.h>
    #include <libwebsockets.h>
    
    #include "api.h"
    
    struct json_object *file_obj, *modify_obj;
    
    static void test_cfg_parse_success(void **state)
    {
    	(void) state; /* unused */
    
    	struct json_object *file = path_to_obj("/home/jakob/git/json-editor-api/test.json");
    
        struct json_object *obj = json_object_new_object();
        struct json_object *tot = json_object_new_object();
    
        json_object_object_add(obj, "api", json_object_new_string("test2"));
        json_object_object_add(tot, "nested", obj);
        json_object_object_add(tot, "test", json_object_new_string("success"));
    
        assert_int_equal(1, json_object_equal(file, tot));
    
    }
    
    static void test_cfg_parse_fail(void **state)
    {
    	(void) state; /* unused */
    
    	struct json_object *obj = path_to_obj("NON_EXISTENT_FILE");
    	assert_null(obj);
    	if (obj)
    		json_object_put(obj);
    }
    
    static void test_build_from_scratch(void **state)
    {
    	(void) state; /* unused */
    
    	struct json_object *file = path_to_obj("/home/jakob/git/json-editor-api/test.json");
        struct json_object *jobj = NULL;
    
    
        set_by_string(NULL, &jobj, "{ \"test\":\"success\", \"nested\": { \"api\":\"test2\"} }", json_type_object);
    
        printf("file_obj=%s\n", json_object_get_string(file));
        printf("modify_obj=%s\n", json_object_get_string(jobj));
    
        assert_int_equal(1, json_object_equal(file, jobj));
    
    }
    
    static void test_json_add_object(void **state)
    {
        (void) state;
    
    
    	struct json_object *obj = json_object_new_object();
        json_object_object_add(obj, "test2", json_object_new_string("success"));
        json_object_object_add(file_obj, "string", obj);
    
        //json_object_object_add(file_obj, "string", json_object_new_string("1"));
    
        set_by_string("string", &modify_obj, "{\"test2\":\"success\"}", json_type_object);
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    static void test_json_add_array(void **state)
    {
        (void) 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(file_obj, "ints", arr);
        //json_object_object_add(file_obj, "string", json_object_new_string("1"));
    
        set_by_string("ints", &modify_obj, "[ 1, 2, 3 ]", json_type_array);
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    static void test_json_add_multi_types(void **state)
    {
        (void) state;
    
    	struct json_object *arr = json_object_new_array();
        struct json_object *obj = json_object_new_object();
        struct json_object *nested = json_object_new_object();
    
        json_object_object_add(nested, "integer", json_object_new_int(1));
        json_object_object_add(obj, "nested1", nested);
        json_object_object_add(file_obj, "nested0", obj);
    
    
        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(file_obj, "ints", arr);
    
        json_object_object_add(file_obj, "string", json_object_new_string("1"));
        json_object_object_add(file_obj, "integer", json_object_new_int(1));
    
        set_by_string("nested0.nested1.integer", &modify_obj, "1", json_type_int);
        set_by_string("ints", &modify_obj, "[ 1, 2, 3 ]", json_type_array);
        set_by_string("string", &modify_obj, "1", json_type_string);
        set_by_string("integer", &modify_obj, "1", json_type_int);
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    static void test_json_add_multi_obj(void **state)
    {
        (void) state;
    
    	struct json_object *arr = json_object_new_array();
        struct json_object *obj = json_object_new_object();
        struct json_object *nested = json_object_new_object();
    
        json_object_object_add(nested, "integer", json_object_new_int(1));
        json_object_object_add(obj, "nested1", nested);
        json_object_object_add(file_obj, "nested0", obj);
    
    
        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(file_obj, "ints", arr);
    
        json_object_object_add(file_obj, "string", json_object_new_string("1"));
        json_object_object_add(file_obj, "integer", json_object_new_int(1));
    
    
        set_by_string(NULL, &modify_obj, "{ \"nested0\": {\"nested1\": {\"integer\": 1}}, \"ints\": [1, 2, 3], \"string\":\"1\", \"integer\": 1}", json_type_object);
    
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    static void test_json_add_string(void **state)
    {
        (void) state;
    
    	//struct json_object *obj = json_object_new_object();
    
        json_object_object_add(file_obj, "string", json_object_new_string("1"));
    
        set_by_string("string", &modify_obj, "1", json_type_string);
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    
    static void test_json_overwrite_string(void **state)
    {
        (void) state;
    
    	//struct json_object *obj = json_object_new_object();
    
        json_object_object_add(file_obj, "test", json_object_new_string("1"));
        set_by_string("test", &modify_obj, "1", json_type_string);
    
        printf("file_obj    = %s\n", json_object_get_string(file_obj));
        printf("modify_obj  = %s\n", json_object_get_string(modify_obj));
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    
        //json_object_object_add(file_obj, "test", json_object_new_string("1"));
        struct json_object *obj, *nested;
    
        json_object_object_get_ex(file_obj, "nested", &nested);
        json_object_object_get_ex(nested, "api", &obj);
        json_object_set_string(obj, "2");
    
        set_by_string("nested.api", &modify_obj, "2", json_type_string);
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    static void test_json_add_int(void **state)
    {
        (void) state;
    
    	//struct json_object *obj = json_object_new_object();
    
        json_object_object_add(file_obj, "integer", json_object_new_int(1));
    
        set_by_string("integer", &modify_obj, "1", json_type_int);
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    static void test_json_add_int_nested(void **state)
    {
        (void) state;
    
    	struct json_object *obj = json_object_new_object();
        struct json_object *nested = json_object_new_object();
    
        json_object_object_add(nested, "integer", json_object_new_int(1));
        json_object_object_add(obj, "nested1", nested);
        json_object_object_add(file_obj, "nested0", obj);
    
        set_by_string("nested0.nested1.integer", &modify_obj, "1", json_type_int);
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    
    static void test_json_add_array_int(void **state)
    {
        (void) state;
    
        struct json_object *arr = json_object_new_array();
    
        struct json_object *obj = json_object_new_object();
    
    
        json_object_array_add(arr, json_object_new_int(1));
        json_object_object_add(file_obj, "array", arr);
    
        json_object_object_add(obj, "array", json_object_get(arr));
        json_object_object_add(file_obj, "inner", obj);
    
    
        set_by_string("array[-1]", &modify_obj, "1", json_type_int);
    
        set_by_string("inner.array[-1]", &modify_obj, "1", json_type_int);
    
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    
    static void test_json_add_array_object(void **state)
    {
        (void) state;
    
        struct json_object *arr = json_object_new_array();
        struct json_object *obj = json_object_new_object();
    
        json_object_object_add(obj, "integer", json_object_new_int(1));
        json_object_object_add(obj, "string", json_object_new_string("test"));
    
        json_object_array_add(arr, obj);
        json_object_array_add(arr, json_object_get(obj)); // array will now hold two references to the same object (will double free otherwise)
        json_object_object_add(file_obj, "array", arr);
    
        set_by_string("array[-1].integer", &modify_obj, "1", json_type_int);
        set_by_string("array[0].string", &modify_obj, "test", json_type_string);
        set_by_string("array[-1]", &modify_obj, "{\"integer\": 1, \"string\":\"test\"}", json_type_object);
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    static void test_json_add_array_nested_object(void **state)
    {
        (void) state;
    
        struct json_object *arr = json_object_new_array();
        struct json_object *obj = json_object_new_object();
        struct json_object *nested = json_object_new_object();
        struct json_object *nested1 = json_object_new_object();
    
        json_object_object_add(nested, "integer", json_object_new_int(1));
        json_object_object_add(nested1, "nested1", nested);
        json_object_object_add(obj, "nested", nested1);
        json_object_array_add(arr, obj);
        json_object_array_add(arr, json_object_get(obj));
    
        json_object_array_add(arr, json_object_get(obj));
        json_object_array_add(arr, json_object_get(obj));
        json_object_array_add(arr, json_object_get(obj));
    
        json_object_object_add(file_obj, "array", arr);
    
    
        set_by_string(NULL, &modify_obj, "{\"array\": [{\"nested\": {\"nested1\": {\"integer\": 1}}}]}", json_type_object);
    
        set_by_string("array[5].nested.nested1.integer", &modify_obj, "1", json_type_int);
    
        set_by_string("array[-1].nested.nested1", &modify_obj, "{\"integer\": 1}", json_type_object);
    
        set_by_string("array[-1].nested", &modify_obj, "{\"nested1\": {\"integer\": 1}}", json_type_object);
        set_by_string("array[-1]", &modify_obj, "{\"nested\": {\"nested1\": {\"integer\": 1}}}", json_type_object);
    
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    
    static void test_json_add_array_nested_array(void **state)
    {
        (void) state;
    
        struct json_object *arr = json_object_new_array();
        struct json_object *nested_arr = json_object_new_array();
        struct json_object *nested1_arr = json_object_new_array();
    
        json_object_array_add(nested_arr, json_object_new_int(1));
        json_object_array_add(nested_arr, json_object_new_int(2));
        json_object_array_add(nested_arr, json_object_new_int(3));
        json_object_array_add(nested1_arr, json_object_new_int(5));
        json_object_array_add(nested1_arr, json_object_new_int(6));
        json_object_array_add(nested1_arr, json_object_new_int(7));
    
        json_object_array_add(arr, nested_arr);
        json_object_array_add(arr, nested1_arr);
    
        json_object_object_add(file_obj, "array", arr);
    
    
        //set_by_string("array[0][0].test", &modify_obj, "1", json_type_int);
        set_by_string("array[0][0]", &modify_obj, "1", json_type_int);
        set_by_string("array[0][1]", &modify_obj, "2", json_type_int);
        set_by_string("array[0][2]", &modify_obj, "3", json_type_int);
    
        set_by_string("array[1][-1]", &modify_obj, "5", json_type_int);
        set_by_string("array[1][-1]", &modify_obj, "6", json_type_int);
        set_by_string("array[1][-1]", &modify_obj, "7", json_type_int);
    
    
        printf("file_obj=%s\n", json_object_get_string(file_obj));
        printf("modify_obj=%s\n", json_object_get_string(modify_obj));
    
        assert_int_equal(1, json_object_equal(modify_obj, file_obj));
    }
    
    
    static int setup (void** state) {
        file_obj = path_to_obj("/home/jakob/git/json-editor-api/test.json");
        modify_obj = path_to_obj("/home/jakob/git/json-editor-api/test.json");
    	return 0;
    }
    
    static int teardown (void** state) {
    	json_object_put(file_obj);
        json_object_put(modify_obj);
    	return 0;
    }
    
    int main(void) {
    	const struct CMUnitTest tests[] = {
    		cmocka_unit_test(test_cfg_parse_success),
    		cmocka_unit_test(test_cfg_parse_fail),
            cmocka_unit_test(test_build_from_scratch),
            cmocka_unit_test_setup_teardown(test_json_add_int, setup, teardown),
            cmocka_unit_test_setup_teardown(test_json_add_int_nested, setup, teardown),
            cmocka_unit_test_setup_teardown(test_json_add_string, setup, teardown),
            cmocka_unit_test_setup_teardown(test_json_add_object, setup, teardown),
            cmocka_unit_test_setup_teardown(test_json_add_array, setup, teardown),
            cmocka_unit_test_setup_teardown(test_json_add_multi_types, setup, teardown),
            cmocka_unit_test_setup_teardown(test_json_add_multi_obj, setup, teardown),
            cmocka_unit_test_setup_teardown(test_json_overwrite_string, setup, teardown),
    
            cmocka_unit_test_setup_teardown(test_json_add_array_int, setup, teardown),
            cmocka_unit_test_setup_teardown(test_json_add_array_object, setup, teardown),
            cmocka_unit_test_setup_teardown(test_json_add_array_nested_object, setup, teardown),
    
            cmocka_unit_test_setup_teardown(test_json_add_array_nested_array, setup, teardown),
    
    	};
    
    	return cmocka_run_group_tests(tests, NULL, NULL);
    }