-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / json / test_json.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_json.c
21  * @brief Tests for JSON conversion functions
22  * @author Christian Grothoff <christian@grothoff.org>
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_json_lib.h"
27
28
29 /**
30  * Test absolute time conversion from/to JSON.
31  *
32  * @return 0 on success
33  */
34 static int
35 test_abs_time ()
36 {
37   json_t *j;
38   struct GNUNET_TIME_Absolute a1;
39   struct GNUNET_TIME_Absolute a2;
40   struct GNUNET_JSON_Specification s1[] = {
41     GNUNET_JSON_spec_absolute_time (NULL, &a2),
42     GNUNET_JSON_spec_end()
43   };
44   struct GNUNET_JSON_Specification s2[] = {
45     GNUNET_JSON_spec_absolute_time (NULL, &a2),
46     GNUNET_JSON_spec_end()
47   };
48
49   a1 = GNUNET_TIME_absolute_get ();
50   GNUNET_TIME_round_abs (&a1);
51   j = GNUNET_JSON_from_time_abs (a1);
52   GNUNET_assert (NULL != j);
53   GNUNET_assert (GNUNET_OK ==
54                  GNUNET_JSON_parse (j, s1, NULL, NULL));
55   GNUNET_assert (a1.abs_value_us ==
56                  a2.abs_value_us);
57   json_decref (j);
58
59   a1 = GNUNET_TIME_UNIT_FOREVER_ABS;
60   j = GNUNET_JSON_from_time_abs (a1);
61   GNUNET_assert (NULL != j);
62   GNUNET_assert (GNUNET_OK ==
63                  GNUNET_JSON_parse (j, s2, NULL, NULL));
64   GNUNET_assert (a1.abs_value_us ==
65                  a2.abs_value_us);
66   json_decref (j);
67   return 0;
68 }
69
70
71 /**
72  * Test relative time conversion from/to JSON.
73  *
74  * @return 0 on success
75  */
76 static int
77 test_rel_time ()
78 {
79   json_t *j;
80   struct GNUNET_TIME_Relative r1;
81   struct GNUNET_TIME_Relative r2;
82   struct GNUNET_JSON_Specification s1[] = {
83     GNUNET_JSON_spec_relative_time (NULL, &r2),
84     GNUNET_JSON_spec_end()
85   };
86   struct GNUNET_JSON_Specification s2[] = {
87     GNUNET_JSON_spec_relative_time (NULL, &r2),
88     GNUNET_JSON_spec_end()
89   };
90
91   r1 = GNUNET_TIME_UNIT_SECONDS;
92   j = GNUNET_JSON_from_time_rel (r1);
93   GNUNET_assert (NULL != j);
94   GNUNET_assert (GNUNET_OK ==
95                  GNUNET_JSON_parse (j, s1, NULL, NULL));
96   GNUNET_assert (r1.rel_value_us ==
97                  r2.rel_value_us);
98   json_decref (j);
99
100   r1 = GNUNET_TIME_UNIT_FOREVER_REL;
101   j = GNUNET_JSON_from_time_rel (r1);
102   GNUNET_assert (NULL != j);
103   GNUNET_assert (GNUNET_OK ==
104                  GNUNET_JSON_parse (j, s2, NULL, NULL));
105   GNUNET_assert (r1.rel_value_us ==
106                  r2.rel_value_us);
107   json_decref (j);
108   return 0;
109 }
110
111
112 /**
113  * Test raw (binary) conversion from/to JSON.
114  *
115  * @return 0 on success
116  */
117 static int
118 test_raw ()
119 {
120   char blob[256];
121   unsigned int i;
122   json_t *j;
123
124   for (i=0;i<=256;i++)
125   {
126     char blob2[256];
127     struct GNUNET_JSON_Specification spec[] = {
128       GNUNET_JSON_spec_fixed (NULL, blob2, i),
129       GNUNET_JSON_spec_end()
130     };
131
132     memset (blob, i, i);
133     j = GNUNET_JSON_from_data (blob, i);
134     GNUNET_assert (NULL != j);
135     GNUNET_assert (GNUNET_OK ==
136                    GNUNET_JSON_parse (j, spec,
137                                       NULL, NULL));
138     GNUNET_assert (0 ==
139                    memcmp (blob,
140                            blob2,
141                            i));
142   }
143   return 0;
144 }
145
146
147 /**
148  * Test rsa conversions from/to JSON.
149  *
150  * @return 0 on success
151  */
152 static int
153 test_rsa ()
154 {
155   struct GNUNET_CRYPTO_RsaPublicKey *pub;
156   struct GNUNET_CRYPTO_RsaPublicKey *pub2;
157   struct GNUNET_JSON_Specification pspec[] = {
158     GNUNET_JSON_spec_rsa_public_key (NULL, &pub2),
159     GNUNET_JSON_spec_end()
160   };
161   struct GNUNET_CRYPTO_RsaSignature *sig;
162   struct GNUNET_CRYPTO_RsaSignature *sig2;
163   struct GNUNET_JSON_Specification sspec[] = {
164     GNUNET_JSON_spec_rsa_signature (NULL, &sig2),
165     GNUNET_JSON_spec_end()
166   };
167   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
168   struct GNUNET_HashCode msg;
169   json_t *jp;
170   json_t *js;
171
172   priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
173   pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
174   memset (&msg, 42, sizeof (msg));
175   sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
176                                     &msg);
177   GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub)));
178   GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig)));
179   GNUNET_assert (GNUNET_OK ==
180                  GNUNET_JSON_parse (jp, pspec,
181                                     NULL, NULL));
182   GNUNET_assert (GNUNET_OK ==
183                  GNUNET_JSON_parse (js, sspec,
184                                     NULL, NULL));
185   GNUNET_break (0 ==
186                 GNUNET_CRYPTO_rsa_signature_cmp (sig,
187                                                  sig2));
188   GNUNET_break (0 ==
189                 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
190                                                   pub2));
191   GNUNET_CRYPTO_rsa_signature_free (sig);
192   GNUNET_CRYPTO_rsa_signature_free (sig2);
193   GNUNET_CRYPTO_rsa_private_key_free (priv);
194   GNUNET_CRYPTO_rsa_public_key_free (pub);
195   GNUNET_CRYPTO_rsa_public_key_free (pub2);
196   return 0;
197 }
198
199
200 /**
201  * Test rsa conversions from/to JSON.
202  *
203  * @return 0 on success
204  */
205 static int
206 test_boolean ()
207 {
208   int b1;
209   int b2;
210   json_t *json;
211   struct GNUNET_JSON_Specification pspec[] = {
212     GNUNET_JSON_spec_boolean ("b1", &b1),
213     GNUNET_JSON_spec_boolean ("b2", &b2),
214     GNUNET_JSON_spec_end()
215   };
216
217   json = json_object ();
218   json_object_set_new (json, "b1", json_true ());
219   json_object_set_new (json, "b2", json_false ());
220
221   GNUNET_assert (GNUNET_OK ==
222                  GNUNET_JSON_parse (json, pspec,
223                                     NULL, NULL));
224
225   GNUNET_assert (GNUNET_YES == b1);
226   GNUNET_assert (GNUNET_NO == b2);
227
228   json_object_set_new (json, "b1", json_integer (42));
229
230   GNUNET_assert (GNUNET_OK !=
231                  GNUNET_JSON_parse (json, pspec,
232                                     NULL, NULL));
233
234   return 0;
235 }
236
237
238 int
239 main(int argc,
240      const char *const argv[])
241 {
242   GNUNET_log_setup ("test-json",
243                     "WARNING",
244                     NULL);
245   if (0 != test_abs_time ())
246     return 1;
247   if (0 != test_rel_time ())
248     return 1;
249   if (0 != test_raw ())
250     return 1;
251   if (0 != test_rsa ())
252     return 1;
253   if (0 != test_boolean ())
254     return 1;
255   /* FIXME: test EdDSA signature conversion... */
256   return 0;
257 }
258
259 /* end of test_json.c */