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 under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
18 * @file json/test_jsonapi.c
19 * @brief Tests for jsonapi conversion functions
20 * @author Martin Schanzenbach
23 #include "gnunet_util_lib.h"
24 #include "gnunet_jsonapi_lib.h"
25 #include "gnunet_json_lib.h"
27 #define TEST_JSONAPI_DOCUMENT "{\"data\":[{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}]}"
29 #define TEST_JSONAPI_DOCUMENT_ERR "{\"errors\":[{\"id\":\"1\",\"status\":\"403\",\"code\":\"23\", \"title\":\"Error\", \"detail\":\"Error details\"}]}"
32 test_document_error ()
34 struct GNUNET_JSONAPI_Document *obj;
35 struct GNUNET_JSONAPI_Error *error;
40 obj = GNUNET_JSONAPI_document_new ();
41 error = GNUNET_JSONAPI_error_new ("1",
51 GNUNET_JSONAPI_document_error_add (obj,
54 GNUNET_assert (GNUNET_OK ==
55 GNUNET_JSONAPI_document_to_json (obj,
57 data_js = json_loads (TEST_JSONAPI_DOCUMENT_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);
72 struct GNUNET_JSONAPI_Document *obj;
73 struct GNUNET_JSONAPI_Resource *res;
79 obj = GNUNET_JSONAPI_document_new ();
80 res = GNUNET_JSONAPI_resource_new ("bar",
83 GNUNET_assert (GNUNET_OK ==
84 GNUNET_JSONAPI_resource_add_attr (res,
86 json_string ("bar")));
88 GNUNET_JSONAPI_document_resource_add (obj,
91 GNUNET_assert (GNUNET_OK ==
92 GNUNET_JSONAPI_document_to_json (obj,
94 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
97 GNUNET_assert (NULL != data_js);
98 ret = json_equal (data_js, doc_json) ? 0 : 1;
99 GNUNET_JSONAPI_document_delete (obj);
100 json_decref (data_js);
101 json_decref (doc_json);
108 struct GNUNET_JSONAPI_Document *obj;
114 struct GNUNET_JSON_Specification jsonapispec[] = {
115 GNUNET_JSON_spec_jsonapi_document (&obj),
116 GNUNET_JSON_spec_end()
118 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
121 GNUNET_assert (NULL != data_js);
122 GNUNET_assert (GNUNET_OK ==
123 GNUNET_JSON_parse (data_js, jsonapispec,
125 GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_document_serialize (obj,
127 GNUNET_JSON_parse_free (jsonapispec);
128 tmp_data_js = json_loads (tmp_data, JSON_DECODE_ANY, &err);
129 GNUNET_assert (NULL != tmp_data_js);
130 ret = (1 == json_equal (tmp_data_js, data_js)) ? 0 : 1;
131 json_decref (data_js);
132 json_decref (tmp_data_js);
133 GNUNET_free (tmp_data);
138 * Test rsa conversions from/to JSON.
140 * @return 0 on success
145 struct GNUNET_JSONAPI_Document *obj;
146 struct GNUNET_JSONAPI_Resource *res;
147 const char* data = "{\"data\":{\"id\":\"1\", \"type\":\"test\"}}";
151 struct GNUNET_JSON_Specification jsonapispec[] = {
152 GNUNET_JSON_spec_jsonapi_document (&obj),
153 GNUNET_JSON_spec_end()
155 data_js = json_loads (data, JSON_DECODE_ANY, &err);
156 GNUNET_assert (NULL != data_js);
157 GNUNET_assert (GNUNET_OK ==
158 GNUNET_JSON_parse (data_js, jsonapispec,
160 json_decref (data_js);
161 res = GNUNET_JSONAPI_document_get_resource (obj, 0);
162 GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_id (res, "1"));
163 GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_type (res, "test"));
164 GNUNET_assert (1 == GNUNET_JSONAPI_document_resource_count (obj));
165 GNUNET_JSON_parse_free (jsonapispec);
172 const char *const argv[])
174 GNUNET_log_setup ("test-jsonapi",
177 if (0 != test_spec_jsonapi ())
179 if (0 != test_serialize ())
181 if (0 != test_document ())
183 if (0 != test_document_error ())
188 /* end of test_json.c */