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