From f38670f0eff1aec5c232c1061fde6afc1597eabd Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakob.olsson@iopsys.eu>
Date: Thu, 3 Oct 2019 12:43:11 +0200
Subject: [PATCH] remove global variable usage from tests

---
 test/api_test.c | 388 +++++++++++++++++++++++++++---------------------
 1 file changed, 221 insertions(+), 167 deletions(-)

diff --git a/test/api_test.c b/test/api_test.c
index a0bae12..920ee9f 100644
--- a/test/api_test.c
+++ b/test/api_test.c
@@ -7,19 +7,19 @@
 
 #include "api.h"
 
-struct json_object *file_obj, *modify_obj;
+struct test_env {
+    struct json_object *file_obj;
+    struct json_object *modify_obj;
+};
 
 static void test_cfg_parse_success(void **state)
 {
-	(void) state; /* unused */
-
 	struct json_object *file = json_object_file_to_obj("test.json");
-
-    assert_non_null(file);
-
     struct json_object *obj = json_object_new_object();
     struct json_object *tot = json_object_new_object();
 
+    assert_non_null(file);
+
     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"));
@@ -30,9 +30,8 @@ static void test_cfg_parse_success(void **state)
 
 static void test_cfg_parse_fail(void **state)
 {
-	(void) state; /* unused */
-
 	struct json_object *obj = json_object_file_to_obj("NON_EXISTENT_FILE");
+
 	assert_null(obj);
 	if (obj)
 		json_object_put(obj);
@@ -40,8 +39,6 @@ static void test_cfg_parse_fail(void **state)
 
 static void test_build_from_scratch(void **state)
 {
-	(void) state; /* unused */
-
 	struct json_object *file = json_object_file_to_obj("test.json");
     struct json_object *jobj = NULL;
 
@@ -71,8 +68,8 @@ static void test_build_from_scratch(void **state)
             }\
         }", json_type_object);
 
-    printf("file_obj=%s\n", json_object_get_string(file));
-    printf("modify_obj=%s\n", json_object_get_string(jobj));
+    printf("e->file_obj=%s\n", json_object_get_string(file));
+    printf("e->modify_obj=%s\n", json_object_get_string(jobj));
 
     assert_int_equal(1, json_object_equal(file, jobj));
     json_object_put(jobj);
@@ -81,44 +78,47 @@ static void test_build_from_scratch(void **state)
 
 static void test_json_set_object(void **state)
 {
-    (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(file_obj, "string", obj);
-    //json_object_object_add(file_obj, "string", json_object_new_string("1"));
+    json_object_object_add(e->file_obj, "string", obj);
 
-    json_object_set_by_string(&modify_obj, "string", "{\"test2\":\"success\"}", json_type_object);
+    json_object_set_by_string(&e->modify_obj, "string", "{\"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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_set_array(void **state)
 {
-    (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(file_obj, "ints", arr);
-    //json_object_object_add(file_obj, "string", json_object_new_string("1"));
+    json_object_object_add(e->file_obj, "ints", arr);
 
-    json_object_set_by_string(&modify_obj, "ints", "[ 1, 2, 3 ]", json_type_array);
+    json_object_set_by_string(&e->modify_obj, "ints", "[ 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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_set_multi_types(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
 
 	struct json_object *arr = json_object_new_array();
     struct json_object *obj = json_object_new_object();
@@ -126,31 +126,33 @@ static void test_json_set_multi_types(void **state)
 
     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_object_add(e->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(e->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));
+    json_object_object_add(e->file_obj, "string", json_object_new_string("1"));
+    json_object_object_add(e->file_obj, "integer", json_object_new_int(1));
 
-    json_object_set_by_string(&modify_obj, "nested0.nested1.integer", "1", json_type_int);
-    json_object_set_by_string(&modify_obj, "ints", "[ 1, 2, 3 ]", json_type_array);
-    json_object_set_by_string(&modify_obj, "string", "1", json_type_string);
-    json_object_set_by_string(&modify_obj, "integer", "1", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "nested0.nested1.integer", "1", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "ints", "[ 1, 2, 3 ]", json_type_array);
+    json_object_set_by_string(&e->modify_obj, "string", "1", json_type_string);
+    json_object_set_by_string(&e->modify_obj, "integer", "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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_set_multi_obj(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
 
 	struct json_object *arr = json_object_new_array();
     struct json_object *obj = json_object_new_object();
@@ -158,130 +160,134 @@ static void test_json_set_multi_obj(void **state)
 
     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_object_add(e->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(e->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));
+    json_object_object_add(e->file_obj, "string", json_object_new_string("1"));
+    json_object_object_add(e->file_obj, "integer", json_object_new_int(1));
 
-    json_object_set_by_string(&modify_obj, NULL, "{ \"nested0\": {\"nested1\": {\"integer\": 1}}, \"ints\": [1, 2, 3], \"string\":\"1\", \"integer\": 1}", json_type_object);
+    json_object_set_by_string(&e->modify_obj, NULL, "{ \"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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_set_string(void **state)
 {
-    (void) state;
-
-	//struct json_object *obj = json_object_new_object();
+    struct test_env *e = (struct test_env *) *state;
 
-    json_object_object_add(file_obj, "string", json_object_new_string("1"));
+    json_object_object_add(e->file_obj, "string", json_object_new_string("1"));
 
-    json_object_set_by_string(&modify_obj, "string", "1", json_type_string);
+    json_object_set_by_string(&e->modify_obj, "string", "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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
-
 static void test_json_set_overwrite_string(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
 
-	//struct json_object *obj = json_object_new_object();
-
-    json_object_object_add(file_obj, "test", json_object_new_string("1"));
-    json_object_set_by_string(&modify_obj, "test", "1", json_type_string);
+    struct json_object *obj, *nested;
 
-    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(e->file_obj, "test", json_object_new_string("1"));
+    json_object_set_by_string(&e->modify_obj, "test", "1", json_type_string);
 
-    //json_object_object_add(file_obj, "test", json_object_new_string("1"));
-    struct json_object *obj, *nested;
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 
-    json_object_object_get_ex(file_obj, "nested", &nested);
+    json_object_object_get_ex(e->file_obj, "nested", &nested);
     json_object_object_get_ex(nested, "api", &obj);
     json_object_set_string(obj, "2");
 
-    json_object_set_by_string(&modify_obj, "nested.api", "2", json_type_string);
+    json_object_set_by_string(&e->modify_obj, "nested.api", "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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_set_int(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
 
-	//struct json_object *obj = json_object_new_object();
+    json_object_object_add(e->file_obj, "integer", json_object_new_int(1));
 
-    json_object_object_add(file_obj, "integer", json_object_new_int(1));
+    json_object_set_by_string(&e->modify_obj, "integer", "1", json_type_int);
 
-    json_object_set_by_string(&modify_obj, "integer", "1", 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));
 
-    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));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_set_int_nested(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *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);
+    json_object_object_add(e->file_obj, "nested0", obj);
 
-    json_object_set_by_string(&modify_obj, "nested0.nested1.integer", "1", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "nested0.nested1.integer", "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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_set_array_int(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *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, "array1", arr);
+    json_object_object_add(e->file_obj, "array1", arr);
     json_object_object_add(obj, "array", json_object_get(arr));
-    json_object_object_add(file_obj, "inner", obj);
+    json_object_object_add(e->file_obj, "inner", obj);
 
-    json_object_set_by_string(&modify_obj, "array1[-1]", "1", json_type_int);
-    json_object_set_by_string(&modify_obj, "inner.array[-1]", "1", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "array1[-1]", "1", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "inner.array[-1]", "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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 
 static void test_json_set_array_object(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
 
     struct json_object *arr = json_object_new_array();
     struct json_object *obj = json_object_new_object();
@@ -290,22 +296,24 @@ static void test_json_set_array_object(void **state)
     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, "array123", arr);
+    json_object_array_add(arr, json_object_get(obj));
+    json_object_object_add(e->file_obj, "array123", arr);
 
-    json_object_set_by_string(&modify_obj, "array123[-1].integer", "1", json_type_int);
-    json_object_set_by_string(&modify_obj, "array123[0].string", "test", json_type_string);
-    json_object_set_by_string(&modify_obj, "array123[-1]", "{\"integer\": 1, \"string\":\"test\"}", json_type_object);
+    json_object_set_by_string(&e->modify_obj, "array123[-1].integer", "1", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "array123[0].string", "test", json_type_string);
+    json_object_set_by_string(&e->modify_obj, "array123[-1]", "{\"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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_set_array_nested_object(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
 
     struct json_object *arr = json_object_new_array();
     struct json_object *obj = json_object_new_object();
@@ -320,23 +328,25 @@ static void test_json_set_array_nested_object(void **state)
     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, "array123", arr);
+    json_object_object_add(e->file_obj, "array123", arr);
 
-    json_object_set_by_string(&modify_obj, NULL, "{\"array123\": [{\"nested\": {\"nested1\": {\"integer\": 1}}}]}", json_type_object);
-    json_object_set_by_string(&modify_obj, "array123[5].nested.nested1.integer", "1", json_type_int);
-    json_object_set_by_string(&modify_obj, "array123[-1].nested.nested1", "{\"integer\": 1}", json_type_object);
-    json_object_set_by_string(&modify_obj, "array123[-1].nested", "{\"nested1\": {\"integer\": 1}}", json_type_object);
-    json_object_set_by_string(&modify_obj, "array123[-1]", "{\"nested\": {\"nested1\": {\"integer\": 1}}}", json_type_object);
+    json_object_set_by_string(&e->modify_obj, NULL, "{\"array123\": [{\"nested\": {\"nested1\": {\"integer\": 1}}}]}", json_type_object);
+    json_object_set_by_string(&e->modify_obj, "array123[5].nested.nested1.integer", "1", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "array123[-1].nested.nested1", "{\"integer\": 1}", json_type_object);
+    json_object_set_by_string(&e->modify_obj, "array123[-1].nested", "{\"nested1\": {\"integer\": 1}}", json_type_object);
+    json_object_set_by_string(&e->modify_obj, "array123[-1]", "{\"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));
+    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_int_equal(1, json_object_equal(modify_obj, file_obj));
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_set_array_nested_array(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
 
     struct json_object *arr = json_object_new_array();
     struct json_object *nested_arr = json_object_new_array();
@@ -345,6 +355,7 @@ static void test_json_set_array_nested_array(void **state)
     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));
@@ -352,174 +363,217 @@ static void test_json_set_array_nested_array(void **state)
     json_object_array_add(arr, nested_arr);
     json_object_array_add(arr, nested1_arr);
 
-    json_object_object_add(file_obj, "array123", arr);
+    json_object_object_add(e->file_obj, "array123", arr);
 
-    //json_object_set_by_string("array[0][0].test", &modify_obj, "1", json_type_int);
-    json_object_set_by_string(&modify_obj, "array123[0][0]", "1", json_type_int);
-    json_object_set_by_string(&modify_obj, "array123[0][1]", "2", json_type_int);
-    json_object_set_by_string(&modify_obj, "array123[0][2]", "3", json_type_int);
+    //json_object_set_by_string("array[0][0].test", &e->modify_obj, "1", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "array123[0][0]", "1", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "array123[0][1]", "2", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "array123[0][2]", "3", json_type_int);
 
-    json_object_set_by_string(&modify_obj, "array123[1][-1]", "5", json_type_int);
-    json_object_set_by_string(&modify_obj, "array123[1][-1]", "6", json_type_int);
-    json_object_set_by_string(&modify_obj, "array123[1][-1]", "7", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "array123[1][-1]", "5", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "array123[1][-1]", "6", json_type_int);
+    json_object_set_by_string(&e->modify_obj, "array123[1][-1]", "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 = json_object_file_to_obj("test.json");
-    modify_obj = json_object_file_to_obj("test.json");
-	return 0;
-}
+    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));
 
-static int teardown (void** state) {
-	json_object_put(file_obj);
-    json_object_put(modify_obj);
-	return 0;
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
+    assert_int_equal(1, json_object_equal(e->modify_obj, e->file_obj));
 }
 
 static void test_json_get_int(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
     struct json_object *tmp, *obj;
 
-    json_object_object_get_ex(file_obj, "integer", &tmp);
+    json_object_object_get_ex(e->file_obj, "integer", &tmp);
 
-    obj = json_object_get_by_string(file_obj, "integer");
+    obj = json_object_get_by_string(e->file_obj, "integer");
 
     printf("obj=%s\n", json_object_get_string(obj));
     printf("tmp=%s\n", json_object_get_string(tmp));
 
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
     assert_int_equal(1, json_object_equal(tmp, obj));
 }
 
 static void test_json_get_string(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
     struct json_object *tmp, *obj;
 
-    json_object_object_get_ex(file_obj, "test", &tmp);
+    json_object_object_get_ex(e->file_obj, "test", &tmp);
 
-    obj = json_object_get_by_string(file_obj, "test");
+    obj = json_object_get_by_string(e->file_obj, "test");
 
     printf("obj=%s\n", json_object_get_string(obj));
     printf("tmp=%s\n", json_object_get_string(tmp));
 
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
     assert_int_equal(1, json_object_equal(tmp, obj));
 }
 
 static void test_json_get_array(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
     struct json_object *tmp, *obj;
 
-    json_object_object_get_ex(file_obj, "simple_array", &tmp);
+    json_object_object_get_ex(e->file_obj, "simple_array", &tmp);
 
-    obj = json_object_get_by_string(file_obj, "simple_array");
+    obj = json_object_get_by_string(e->file_obj, "simple_array");
 
     printf("obj=%s\n", json_object_get_string(obj));
     printf("tmp=%s\n", json_object_get_string(tmp));
 
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
     assert_int_equal(1, json_object_equal(tmp, obj));
 }
 
 static void test_json_get_object(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
     struct json_object *tmp, *obj;
 
-    json_object_object_get_ex(file_obj, "nested", &tmp);
+    json_object_object_get_ex(e->file_obj, "nested", &tmp);
 
-    obj = json_object_get_by_string(file_obj, "nested");
+    obj = json_object_get_by_string(e->file_obj, "nested");
 
     printf("obj=%s\n", json_object_get_string(obj));
     printf("tmp=%s\n", json_object_get_string(tmp));
 
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
     assert_int_equal(1, json_object_equal(tmp, obj));
 }
 
 static void test_json_get_nested_object(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
     struct json_object *tmp, *obj;
 
-    json_object_object_get_ex(file_obj, "nested", &tmp);
+    json_object_object_get_ex(e->file_obj, "nested", &tmp);
     json_object_object_get_ex(tmp, "api", &tmp);
 
-    obj = json_object_get_by_string(file_obj, "nested.api");
+    obj = json_object_get_by_string(e->file_obj, "nested.api");
 
     printf("obj=%s\n", json_object_get_string(obj));
     printf("tmp=%s\n", json_object_get_string(tmp));
 
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
     assert_int_equal(1, json_object_equal(tmp, obj));
 }
 
 static void test_json_get_array_int(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
     struct json_object *tmp, *obj;
 
-    json_object_object_get_ex(file_obj, "simple_array", &tmp);
+    json_object_object_get_ex(e->file_obj, "simple_array", &tmp);
     tmp = json_object_array_get_idx(tmp, 1);
-    obj = json_object_get_by_string(file_obj, "simple_array[1]");
+    obj = json_object_get_by_string(e->file_obj, "simple_array[1]");
 
     printf("obj=%s\n", json_object_get_string(obj));
     printf("tmp=%s\n", json_object_get_string(tmp));
 
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
     assert_int_equal(1, json_object_equal(tmp, obj));
 }
 
 static void test_json_get_array_nested_int(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
     struct json_object *tmp, *obj;
 
-    json_object_object_get_ex(file_obj, "array", &tmp);
+    json_object_object_get_ex(e->file_obj, "array", &tmp);
     tmp = json_object_array_get_idx(tmp, 1);
     tmp = json_object_array_get_idx(tmp, 2);
-    obj = json_object_get_by_string(file_obj, "array[1][-1]");
+    obj = json_object_get_by_string(e->file_obj, "array[1][-1]");
 
     printf("obj=%s\n", json_object_get_string(obj));
     printf("tmp=%s\n", json_object_get_string(tmp));
 
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
     assert_int_equal(1, json_object_equal(tmp, obj));
 }
 
 static void test_json_get_object_array_nested_int(void **state)
 {
-    (void) state;
+    struct test_env *e = (struct test_env *) *state;
     struct json_object *tmp, *obj;
 
-    json_object_object_get_ex(file_obj, "complex_nested", &tmp);
+    json_object_object_get_ex(e->file_obj, "complex_nested", &tmp);
     json_object_object_get_ex(tmp, "array", &tmp);
     tmp = json_object_array_get_idx(tmp, 1);
     json_object_object_get_ex(tmp, "test2", &tmp);
-    obj = json_object_get_by_string(file_obj, "complex_nested.array[1].test2");
+    obj = json_object_get_by_string(e->file_obj, "complex_nested.array[1].test2");
 
     printf("obj=%s\n", json_object_get_string(obj));
     printf("tmp=%s\n", json_object_get_string(tmp));
 
+    assert_non_null(e->file_obj);
+    assert_non_null(e->modify_obj);
     assert_int_equal(1, json_object_equal(tmp, obj));
 }
 
 static void test_json_write_to_file(void **state)
 {
-    (void) state;
-    struct json_object *tmp, *obj;
+    struct test_env *e = (struct test_env *) *state;
+    struct json_object *obj;
 
-    json_object_obj_to_file(file_obj, "cpy.json");
+    json_object_obj_to_file(e->file_obj, "cpy.json");
 
     obj = json_object_file_to_obj("cpy.json");
 
-    assert_int_equal(1, json_object_equal(file_obj, obj));
+    printf("obj=%s\n", json_object_get_string(obj));
+    printf("e->file_obj=%s\n", json_object_get_string(e->file_obj));
+
+    assert_non_null(e->file_obj);
+    assert_non_null(obj);
+    assert_int_equal(1, json_object_equal(e->file_obj, obj));
     json_object_put(obj);
 }
 
+static int setup (void** state) {
+    struct test_env *e = (struct test_env *) *state;
+
+    e->file_obj = json_object_file_to_obj("test.json");
+    e->modify_obj = json_object_file_to_obj("test.json");
+	return 0;
+}
+
+static int teardown (void** state) {
+    struct test_env *e = (struct test_env *) *state;
+
+	json_object_put(e->file_obj);
+    json_object_put(e->modify_obj);
+	return 0;
+}
+
+static int group_setup(void** state) {
+        struct test_env *e;
+
+        e = calloc(1, sizeof(struct test_env));
+        if (!e)
+                return 1;
+
+        *state = e;
+        return 0;
+}
+
+static int group_teardown(void** state) {
+    struct test_env *e = (struct test_env *) *state;
+
+	free(e);
+    return 0;
+}
+
 int main(void) {
 	const struct CMUnitTest tests[] = {
 		//cmocka_unit_test(test_cfg_parse_success),
@@ -548,5 +602,5 @@ int main(void) {
         cmocka_unit_test_setup_teardown(test_json_write_to_file, setup, teardown),
 	};
 
-	return cmocka_run_group_tests(tests, NULL, NULL);
+	return cmocka_run_group_tests(tests, group_setup, group_teardown);
 }
-- 
GitLab