aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2016-05-05 10:35:48 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2016-05-05 10:35:48 +0000
commit2b987c3684cbf1d15b9ed44bbce911653bd6f330 (patch)
tree0dfd45382884462cca00094896eb40aef602a22a /src
parent8aad5eb78dc229a519dca35099ba9e9ce86bc956 (diff)
downloadgnunet-2b987c3684cbf1d15b9ed44bbce911653bd6f330.tar.gz
gnunet-2b987c3684cbf1d15b9ed44bbce911653bd6f330.zip
- add test, bugfix
Diffstat (limited to 'src')
-rw-r--r--src/jsonapi/jsonapi_document.c24
-rw-r--r--src/jsonapi/jsonapi_error.c32
-rw-r--r--src/jsonapi/test_jsonapi.c44
3 files changed, 79 insertions, 21 deletions
diff --git a/src/jsonapi/jsonapi_document.c b/src/jsonapi/jsonapi_document.c
index b99a7a4fe..faf0bd831 100644
--- a/src/jsonapi/jsonapi_document.c
+++ b/src/jsonapi/jsonapi_document.c
@@ -53,7 +53,20 @@ GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *doc)
53{ 53{
54 struct GNUNET_JSONAPI_Resource *res; 54 struct GNUNET_JSONAPI_Resource *res;
55 struct GNUNET_JSONAPI_Resource *res_next; 55 struct GNUNET_JSONAPI_Resource *res_next;
56 56 struct GNUNET_JSONAPI_Error *err;
57 struct GNUNET_JSONAPI_Error *err_next;
58
59
60 for (err = doc->err_list_head;
61 err != NULL;)
62 {
63 err_next = err->next;
64 GNUNET_CONTAINER_DLL_remove (doc->err_list_head,
65 doc->err_list_tail,
66 err);
67 GNUNET_JSONAPI_error_delete (err);
68 err = err_next;
69 }
57 70
58 for (res = doc->res_list_head; 71 for (res = doc->res_list_head;
59 res != NULL;) 72 res != NULL;)
@@ -65,6 +78,9 @@ GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *doc)
65 GNUNET_JSONAPI_resource_delete (res); 78 GNUNET_JSONAPI_resource_delete (res);
66 res = res_next; 79 res = res_next;
67 } 80 }
81
82 if (NULL != doc->meta)
83 json_decref (doc->meta);
68 GNUNET_free (doc); 84 GNUNET_free (doc);
69 doc = NULL; 85 doc = NULL;
70} 86}
@@ -95,7 +111,7 @@ GNUNET_JSONAPI_document_new ()
95 */ 111 */
96void 112void
97GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc, 113GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
98 struct GNUNET_JSONAPI_Error *err) 114 struct GNUNET_JSONAPI_Error *err)
99{ 115{
100 GNUNET_CONTAINER_DLL_insert (doc->err_list_head, 116 GNUNET_CONTAINER_DLL_insert (doc->err_list_head,
101 doc->err_list_tail, 117 doc->err_list_tail,
@@ -113,7 +129,7 @@ GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
113 */ 129 */
114void 130void
115GNUNET_JSONAPI_document_resource_add (struct GNUNET_JSONAPI_Document *doc, 131GNUNET_JSONAPI_document_resource_add (struct GNUNET_JSONAPI_Document *doc,
116 struct GNUNET_JSONAPI_Resource *res) 132 struct GNUNET_JSONAPI_Resource *res)
117{ 133{
118 GNUNET_CONTAINER_DLL_insert (doc->res_list_head, 134 GNUNET_CONTAINER_DLL_insert (doc->res_list_head,
119 doc->res_list_tail, 135 doc->res_list_tail,
@@ -310,7 +326,7 @@ GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc,
310 GNUNET_assert (GNUNET_OK == 326 GNUNET_assert (GNUNET_OK ==
311 GNUNET_JSONAPI_error_to_json (error, 327 GNUNET_JSONAPI_error_to_json (error,
312 &res_json_tmp)); 328 &res_json_tmp));
313 json_array_append (res_json, res_json_tmp); 329 json_array_append_new (res_json, res_json_tmp);
314 } 330 }
315 json_object_set_new (*root_json, 331 json_object_set_new (*root_json,
316 GNUNET_JSONAPI_KEY_ERRORS, 332 GNUNET_JSONAPI_KEY_ERRORS,
diff --git a/src/jsonapi/jsonapi_error.c b/src/jsonapi/jsonapi_error.c
index b7fc08d72..8ce71d26f 100644
--- a/src/jsonapi/jsonapi_error.c
+++ b/src/jsonapi/jsonapi_error.c
@@ -149,22 +149,22 @@ GNUNET_JSONAPI_error_new (const char *id,
149 struct GNUNET_JSONAPI_Error *error; 149 struct GNUNET_JSONAPI_Error *error;
150 error = GNUNET_new (struct GNUNET_JSONAPI_Error); 150 error = GNUNET_new (struct GNUNET_JSONAPI_Error);
151 151
152 GNUNET_assert (NULL != id); 152 if (NULL != id)
153 error->id = GNUNET_strdup (id); 153 error->id = GNUNET_strdup (id);
154 GNUNET_assert (NULL != status); 154 if (NULL != status)
155 error->status = GNUNET_strdup (status); 155 error->status = GNUNET_strdup (status);
156 GNUNET_assert (NULL != code); 156 if (NULL != code)
157 error->code = GNUNET_strdup (code); 157 error->code = GNUNET_strdup (code);
158 GNUNET_assert (NULL != title); 158 if (NULL != title)
159 error->title = GNUNET_strdup (title); 159 error->title = GNUNET_strdup (title);
160 GNUNET_assert (NULL != detail); 160 if (NULL != detail)
161 error->detail = GNUNET_strdup (detail); 161 error->detail = GNUNET_strdup (detail);
162 GNUNET_assert (NULL != links); 162 if (NULL != links)
163 error->links = json_deep_copy (links); 163 error->links = json_deep_copy (links);
164 GNUNET_assert (NULL != source); 164 if (NULL != source)
165 error->source = json_deep_copy (source); 165 error->source = json_deep_copy (source);
166 GNUNET_assert (NULL != meta); 166 if (NULL != meta)
167 error->meta = json_deep_copy (meta); 167 error->meta = json_deep_copy (meta);
168 return error; 168 return error;
169} 169}
170/** 170/**
diff --git a/src/jsonapi/test_jsonapi.c b/src/jsonapi/test_jsonapi.c
index b5f4d4cba..379dab9af 100644
--- a/src/jsonapi/test_jsonapi.c
+++ b/src/jsonapi/test_jsonapi.c
@@ -26,6 +26,46 @@
26 26
27#define TEST_JSONAPI_DOCUMENT "{\"data\":{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}}" 27#define TEST_JSONAPI_DOCUMENT "{\"data\":{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}}"
28 28
29#define TEST_JSONAPI_DOCUMENT_ERR "{\"errors\":[{\"id\":\"1\",\"status\":\"403\",\"code\":\"23\", \"title\":\"Error\", \"detail\":\"Error details\"}]}"
30
31static int
32test_document_error ()
33{
34 struct GNUNET_JSONAPI_Document *obj;
35 struct GNUNET_JSONAPI_Error *error;
36 json_t *doc_json;
37 json_t *data_js;
38 json_error_t err;
39
40 obj = GNUNET_JSONAPI_document_new ();
41 error = GNUNET_JSONAPI_error_new ("1",
42 "403",
43 "23",
44 "Error",
45 "Error details",
46 NULL,
47 NULL,
48 NULL);
49
50
51 GNUNET_JSONAPI_document_error_add (obj,
52 error);
53
54 GNUNET_assert (GNUNET_OK ==
55 GNUNET_JSONAPI_document_to_json (obj,
56 &doc_json));
57 data_js = json_loads (TEST_JSONAPI_DOCUMENT_ERR,
58 JSON_DECODE_ANY,
59 &err);
60 GNUNET_assert (NULL != data_js);
61 GNUNET_assert (0 != json_equal (data_js, doc_json));
62 GNUNET_JSONAPI_document_delete (obj);
63 json_decref (data_js);
64 json_decref (doc_json);
65 return 0;
66}
67
68
29static int 69static int
30test_document () 70test_document ()
31{ 71{
@@ -38,7 +78,7 @@ test_document ()
38 obj = GNUNET_JSONAPI_document_new (); 78 obj = GNUNET_JSONAPI_document_new ();
39 res = GNUNET_JSONAPI_resource_new ("bar", 79 res = GNUNET_JSONAPI_resource_new ("bar",
40 "1"); 80 "1");
41 81
42 GNUNET_assert (GNUNET_OK == 82 GNUNET_assert (GNUNET_OK ==
43 GNUNET_JSONAPI_resource_add_attr (res, 83 GNUNET_JSONAPI_resource_add_attr (res,
44 "foo", 84 "foo",
@@ -138,6 +178,8 @@ main(int argc,
138 return 1; 178 return 1;
139 if (0 != test_document ()) 179 if (0 != test_document ())
140 return 1; 180 return 1;
181 if (0 != test_document_error ())
182 return 1;
141 return 0; 183 return 0;
142} 184}
143 185