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;
78 obj = GNUNET_JSONAPI_document_new ();
79 res = GNUNET_JSONAPI_resource_new ("bar",
82 GNUNET_assert (GNUNET_OK ==
83 GNUNET_JSONAPI_resource_add_attr (res,
85 json_string ("bar")));
87 GNUNET_JSONAPI_document_resource_add (obj,
90 GNUNET_assert (GNUNET_OK ==
91 GNUNET_JSONAPI_document_to_json (obj,
93 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
96 GNUNET_assert (NULL != data_js);
97 GNUNET_assert (0 != json_equal (data_js, doc_json));
98 GNUNET_JSONAPI_document_delete (obj);
99 json_decref (data_js);
100 json_decref (doc_json);
107 struct GNUNET_JSONAPI_Document *obj;
112 struct GNUNET_JSON_Specification jsonapispec[] = {
113 GNUNET_JSON_spec_jsonapi_document (&obj),
114 GNUNET_JSON_spec_end()
116 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
119 GNUNET_assert (NULL != data_js);
120 GNUNET_assert (GNUNET_OK ==
121 GNUNET_JSON_parse (data_js, jsonapispec,
123 GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_document_serialize (obj,
125 GNUNET_JSON_parse_free (jsonapispec);
126 tmp_data_js = json_loads (tmp_data, JSON_DECODE_ANY, &err);
127 GNUNET_assert (NULL != tmp_data_js);
128 GNUNET_assert (0 != json_equal (tmp_data_js, data_js));
129 json_decref (data_js);
130 json_decref (tmp_data_js);
131 GNUNET_free (tmp_data);
136 * Test rsa conversions from/to JSON.
138 * @return 0 on success
143 struct GNUNET_JSONAPI_Document *obj;
144 struct GNUNET_JSONAPI_Resource *res;
145 const char* data = "{\"data\":{\"id\":\"1\", \"type\":\"test\"}}";
149 struct GNUNET_JSON_Specification jsonapispec[] = {
150 GNUNET_JSON_spec_jsonapi_document (&obj),
151 GNUNET_JSON_spec_end()
153 data_js = json_loads (data, JSON_DECODE_ANY, &err);
154 GNUNET_assert (NULL != data_js);
155 GNUNET_assert (GNUNET_OK ==
156 GNUNET_JSON_parse (data_js, jsonapispec,
158 json_decref (data_js);
159 res = GNUNET_JSONAPI_document_get_resource (obj, 0);
160 GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_id (res, "1"));
161 GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_type (res, "test"));
162 GNUNET_assert (1 == GNUNET_JSONAPI_document_resource_count (obj));
163 GNUNET_JSON_parse_free (jsonapispec);
170 const char *const argv[])
172 GNUNET_log_setup ("test-jsonapi",
175 if (0 != test_spec_jsonapi ())
177 if (0 != test_serialize ())
179 if (0 != test_document ())
181 if (0 != test_document_error ())
186 /* end of test_json.c */