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.
17 * @file json/test_jsonapi.c
18 * @brief Tests for jsonapi conversion functions
19 * @author Martin Schanzenbach
22 #include "gnunet_util_lib.h"
23 #include "gnunet_jsonapi_lib.h"
24 #include "gnunet_json_lib.h"
26 #define TEST_JSONAPI_DOCUMENT "{\"data\":[{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}]}"
28 #define TEST_JSONAPI_DOCUMENT_ERR "{\"errors\":[{\"id\":\"1\",\"status\":\"403\",\"code\":\"23\", \"title\":\"Error\", \"detail\":\"Error details\"}]}"
31 test_document_error ()
33 struct GNUNET_JSONAPI_Document *obj;
34 struct GNUNET_JSONAPI_Error *error;
39 obj = GNUNET_JSONAPI_document_new ();
40 error = GNUNET_JSONAPI_error_new ("1",
50 GNUNET_JSONAPI_document_error_add (obj,
53 GNUNET_assert (GNUNET_OK ==
54 GNUNET_JSONAPI_document_to_json (obj,
56 data_js = json_loads (TEST_JSONAPI_DOCUMENT_ERR,
59 GNUNET_assert (NULL != data_js);
60 GNUNET_assert (0 != json_equal (data_js, doc_json));
61 GNUNET_JSONAPI_document_delete (obj);
62 json_decref (data_js);
63 json_decref (doc_json);
71 struct GNUNET_JSONAPI_Document *obj;
72 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 ret = json_equal (data_js, doc_json) ? 0 : 1;
98 GNUNET_JSONAPI_document_delete (obj);
99 json_decref (data_js);
100 json_decref (doc_json);
107 struct GNUNET_JSONAPI_Document *obj;
113 struct GNUNET_JSON_Specification jsonapispec[] = {
114 GNUNET_JSON_spec_jsonapi_document (&obj),
115 GNUNET_JSON_spec_end()
117 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
120 GNUNET_assert (NULL != data_js);
121 GNUNET_assert (GNUNET_OK ==
122 GNUNET_JSON_parse (data_js, jsonapispec,
124 GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_document_serialize (obj,
126 GNUNET_JSON_parse_free (jsonapispec);
127 tmp_data_js = json_loads (tmp_data, JSON_DECODE_ANY, &err);
128 GNUNET_assert (NULL != tmp_data_js);
129 ret = (1 == json_equal (tmp_data_js, data_js)) ? 0 : 1;
130 json_decref (data_js);
131 json_decref (tmp_data_js);
132 GNUNET_free (tmp_data);
137 * Test rsa conversions from/to JSON.
139 * @return 0 on success
144 struct GNUNET_JSONAPI_Document *obj;
145 struct GNUNET_JSONAPI_Resource *res;
146 const char* data = "{\"data\":{\"id\":\"1\", \"type\":\"test\"}}";
150 struct GNUNET_JSON_Specification jsonapispec[] = {
151 GNUNET_JSON_spec_jsonapi_document (&obj),
152 GNUNET_JSON_spec_end()
154 data_js = json_loads (data, JSON_DECODE_ANY, &err);
155 GNUNET_assert (NULL != data_js);
156 GNUNET_assert (GNUNET_OK ==
157 GNUNET_JSON_parse (data_js, jsonapispec,
159 json_decref (data_js);
160 res = GNUNET_JSONAPI_document_get_resource (obj, 0);
161 GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_id (res, "1"));
162 GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_type (res, "test"));
163 GNUNET_assert (1 == GNUNET_JSONAPI_document_resource_count (obj));
164 GNUNET_JSON_parse_free (jsonapispec);
171 const char *const argv[])
173 GNUNET_log_setup ("test-jsonapi",
176 if (0 != test_spec_jsonapi ())
178 if (0 != test_serialize ())
180 if (0 != test_document ())
182 if (0 != test_document_error ())
187 /* end of test_json.c */