Fix: Cast closure to proper type
[oweals/gnunet.git] / src / jsonapi / test_jsonapi.c
1 /*
2    This file is part of GNUnet
3    (C) 2015, 2016 GNUnet e.V.
4
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.
9
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.
14   
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/>.
17    */
18
19 /**
20  * @file json/test_jsonapi.c
21  * @brief Tests for jsonapi conversion functions
22  * @author Martin Schanzenbach
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_jsonapi_lib.h"
27 #include "gnunet_json_lib.h"
28
29 #define TEST_JSONAPI_DOCUMENT "{\"data\":[{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}]}"
30
31 #define TEST_JSONAPI_DOCUMENT_ERR "{\"errors\":[{\"id\":\"1\",\"status\":\"403\",\"code\":\"23\", \"title\":\"Error\", \"detail\":\"Error details\"}]}"
32
33 static int
34 test_document_error ()
35 {
36   struct GNUNET_JSONAPI_Document *obj;
37   struct GNUNET_JSONAPI_Error *error;
38   json_t *doc_json;
39   json_t *data_js;
40   json_error_t err;
41
42   obj = GNUNET_JSONAPI_document_new ();
43   error = GNUNET_JSONAPI_error_new ("1",
44                                     "403",
45                                     "23",
46                                     "Error",
47                                     "Error details",
48                                     NULL,
49                                     NULL,
50                                     NULL);
51
52
53   GNUNET_JSONAPI_document_error_add (obj,
54                                      error);
55
56   GNUNET_assert (GNUNET_OK == 
57                  GNUNET_JSONAPI_document_to_json (obj,
58                                                   &doc_json));
59   data_js = json_loads (TEST_JSONAPI_DOCUMENT_ERR,
60                         JSON_DECODE_ANY,
61                         &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);
67   return 0;
68 }
69
70
71 static int
72 test_document ()
73 {
74   struct GNUNET_JSONAPI_Document *obj;
75   struct GNUNET_JSONAPI_Resource *res;
76   json_t *doc_json;
77   json_t *data_js;
78   json_error_t err;
79   int ret;
80    
81   obj = GNUNET_JSONAPI_document_new ();
82   res = GNUNET_JSONAPI_resource_new ("bar",
83                                      "1");
84
85   GNUNET_assert (GNUNET_OK == 
86                  GNUNET_JSONAPI_resource_add_attr (res,
87                                                    "foo",
88                                                    json_string ("bar")));
89
90   GNUNET_JSONAPI_document_resource_add (obj,
91                                         res);
92
93   GNUNET_assert (GNUNET_OK == 
94                  GNUNET_JSONAPI_document_to_json (obj,
95                                                   &doc_json));
96   data_js = json_loads (TEST_JSONAPI_DOCUMENT,
97                         JSON_DECODE_ANY,
98                         &err);
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);
104   return ret;
105 }
106
107 static int
108 test_serialize ()
109 {
110   struct GNUNET_JSONAPI_Document *obj;
111   char* tmp_data;
112   int ret;
113   json_t* data_js;
114   json_t* tmp_data_js;
115   json_error_t err;
116   struct GNUNET_JSON_Specification jsonapispec[] = {
117     GNUNET_JSON_spec_jsonapi_document (&obj),
118     GNUNET_JSON_spec_end()
119   };
120   data_js = json_loads (TEST_JSONAPI_DOCUMENT,
121                         JSON_DECODE_ANY,
122                         &err);
123   GNUNET_assert (NULL != data_js);
124   GNUNET_assert (GNUNET_OK ==
125                  GNUNET_JSON_parse (data_js, jsonapispec,
126                                     NULL, NULL));
127   GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_document_serialize (obj,
128                                                                  &tmp_data));
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);
136   return ret;
137 }
138
139 /**
140  * Test rsa conversions from/to JSON.
141  *
142  * @return 0 on success
143  */
144 static int
145 test_spec_jsonapi ()
146 {
147   struct GNUNET_JSONAPI_Document *obj;
148   struct GNUNET_JSONAPI_Resource *res;
149   const char* data = "{\"data\":{\"id\":\"1\", \"type\":\"test\"}}";
150   json_t* data_js;
151   json_error_t err;
152
153   struct GNUNET_JSON_Specification jsonapispec[] = {
154     GNUNET_JSON_spec_jsonapi_document (&obj),
155     GNUNET_JSON_spec_end()
156   };
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,
161                                     NULL, NULL));
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);
168   return 0;
169 }
170
171
172 int
173 main(int argc,
174      const char *const argv[])
175 {
176   GNUNET_log_setup ("test-jsonapi",
177                     "WARNING",
178                     NULL);
179   if (0 != test_spec_jsonapi ())
180     return 1;
181   if (0 != test_serialize ())
182     return 1;
183   if (0 != test_document ())
184     return 1;
185   if (0 != test_document_error ())
186     return 1;
187   return 0;
188 }
189
190 /* end of test_json.c */