2 This file is part of GNUnet
3 (C) 2015, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file json/test_jsonapi.c
21 * @brief Tests for jsonapi conversion functions
22 * @author Martin Schanzenbach
25 #include "gnunet_util_lib.h"
26 #include "gnunet_jsonapi_lib.h"
27 #include "gnunet_json_lib.h"
29 #define TEST_JSONAPI_DOCUMENT "{\"data\":[{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}]}"
31 #define TEST_JSONAPI_DOCUMENT_ERR "{\"errors\":[{\"id\":\"1\",\"status\":\"403\",\"code\":\"23\", \"title\":\"Error\", \"detail\":\"Error details\"}]}"
34 test_document_error ()
36 struct GNUNET_JSONAPI_Document *obj;
37 struct GNUNET_JSONAPI_Error *error;
42 obj = GNUNET_JSONAPI_document_new ();
43 error = GNUNET_JSONAPI_error_new ("1",
53 GNUNET_JSONAPI_document_error_add (obj,
56 GNUNET_assert (GNUNET_OK ==
57 GNUNET_JSONAPI_document_to_json (obj,
59 data_js = json_loads (TEST_JSONAPI_DOCUMENT_ERR,
62 GNUNET_assert (NULL != data_js);
63 GNUNET_assert (0 != json_equal (data_js, doc_json));
64 GNUNET_JSONAPI_document_delete (obj);
65 json_decref (data_js);
66 json_decref (doc_json);
74 struct GNUNET_JSONAPI_Document *obj;
75 struct GNUNET_JSONAPI_Resource *res;
81 obj = GNUNET_JSONAPI_document_new ();
82 res = GNUNET_JSONAPI_resource_new ("bar",
85 GNUNET_assert (GNUNET_OK ==
86 GNUNET_JSONAPI_resource_add_attr (res,
88 json_string ("bar")));
90 GNUNET_JSONAPI_document_resource_add (obj,
93 GNUNET_assert (GNUNET_OK ==
94 GNUNET_JSONAPI_document_to_json (obj,
96 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
99 GNUNET_assert (NULL != data_js);
100 ret = json_equal (data_js, doc_json) ? 0 : 1;
101 GNUNET_JSONAPI_document_delete (obj);
102 json_decref (data_js);
103 json_decref (doc_json);
110 struct GNUNET_JSONAPI_Document *obj;
116 struct GNUNET_JSON_Specification jsonapispec[] = {
117 GNUNET_JSON_spec_jsonapi_document (&obj),
118 GNUNET_JSON_spec_end()
120 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
123 GNUNET_assert (NULL != data_js);
124 GNUNET_assert (GNUNET_OK ==
125 GNUNET_JSON_parse (data_js, jsonapispec,
127 GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_document_serialize (obj,
129 GNUNET_JSON_parse_free (jsonapispec);
130 tmp_data_js = json_loads (tmp_data, JSON_DECODE_ANY, &err);
131 GNUNET_assert (NULL != tmp_data_js);
132 ret = (1 == json_equal (tmp_data_js, data_js)) ? 0 : 1;
133 json_decref (data_js);
134 json_decref (tmp_data_js);
135 GNUNET_free (tmp_data);
140 * Test rsa conversions from/to JSON.
142 * @return 0 on success
147 struct GNUNET_JSONAPI_Document *obj;
148 struct GNUNET_JSONAPI_Resource *res;
149 const char* data = "{\"data\":{\"id\":\"1\", \"type\":\"test\"}}";
153 struct GNUNET_JSON_Specification jsonapispec[] = {
154 GNUNET_JSON_spec_jsonapi_document (&obj),
155 GNUNET_JSON_spec_end()
157 data_js = json_loads (data, JSON_DECODE_ANY, &err);
158 GNUNET_assert (NULL != data_js);
159 GNUNET_assert (GNUNET_OK ==
160 GNUNET_JSON_parse (data_js, jsonapispec,
162 json_decref (data_js);
163 res = GNUNET_JSONAPI_document_get_resource (obj, 0);
164 GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_id (res, "1"));
165 GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_type (res, "test"));
166 GNUNET_assert (1 == GNUNET_JSONAPI_document_resource_count (obj));
167 GNUNET_JSON_parse_free (jsonapispec);
174 const char *const argv[])
176 GNUNET_log_setup ("test-jsonapi",
179 if (0 != test_spec_jsonapi ())
181 if (0 != test_serialize ())
183 if (0 != test_document ())
185 if (0 != test_document_error ())
190 /* end of test_json.c */