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