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