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