remove 'illegal' (non-reentrant) log logic from signal handler
[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[] = { GNUNET_JSON_spec_absolute_time (
43                                               NULL,
44                                               &a2),
45                                             GNUNET_JSON_spec_end () };
46   struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_absolute_time (
47                                               NULL,
48                                               &a2),
49                                             GNUNET_JSON_spec_end () };
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 == GNUNET_JSON_parse (j, s1, NULL, NULL));
56   GNUNET_assert (a1.abs_value_us == 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 == GNUNET_JSON_parse (j, s2, NULL, NULL));
63   GNUNET_assert (a1.abs_value_us == 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[] = { GNUNET_JSON_spec_relative_time (
81                                               NULL,
82                                               &r2),
83                                             GNUNET_JSON_spec_end () };
84   struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_relative_time (
85                                               NULL,
86                                               &r2),
87                                             GNUNET_JSON_spec_end () };
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 == GNUNET_JSON_parse (j, s1, NULL, NULL));
93   GNUNET_assert (r1.rel_value_us == r2.rel_value_us);
94   json_decref (j);
95
96   r1 = GNUNET_TIME_UNIT_FOREVER_REL;
97   j = GNUNET_JSON_from_time_rel (r1);
98   GNUNET_assert (NULL != j);
99   GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL));
100   GNUNET_assert (r1.rel_value_us == r2.rel_value_us);
101   json_decref (j);
102   return 0;
103 }
104
105
106 /**
107  * Test raw (binary) conversion from/to JSON.
108  *
109  * @return 0 on success
110  */
111 static int
112 test_raw ()
113 {
114   char blob[256];
115   unsigned int i;
116   json_t *j;
117
118   for (i = 0; i <= 256; i++)
119   {
120     char blob2[256];
121     struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_fixed (NULL,
122                                                                         blob2,
123                                                                         i),
124                                                 GNUNET_JSON_spec_end () };
125
126     memset (blob, i, i);
127     j = GNUNET_JSON_from_data (blob, i);
128     GNUNET_assert (NULL != j);
129     GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, spec, NULL, NULL));
130     GNUNET_assert (0 == memcmp (blob, blob2, i));
131   }
132   return 0;
133 }
134
135
136 /**
137  * Test rsa conversions from/to JSON.
138  *
139  * @return 0 on success
140  */
141 static int
142 test_rsa ()
143 {
144   struct GNUNET_CRYPTO_RsaPublicKey *pub;
145   struct GNUNET_CRYPTO_RsaPublicKey *pub2;
146   struct GNUNET_JSON_Specification pspec[] =
147   { GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), GNUNET_JSON_spec_end () };
148   struct GNUNET_CRYPTO_RsaSignature *sig;
149   struct GNUNET_CRYPTO_RsaSignature *sig2;
150   struct GNUNET_JSON_Specification sspec[] =
151   { GNUNET_JSON_spec_rsa_signature (NULL, &sig2), GNUNET_JSON_spec_end () };
152   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
153   struct GNUNET_HashCode msg;
154   json_t *jp;
155   json_t *js;
156
157   priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
158   pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
159   memset (&msg, 42, sizeof(msg));
160   sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, &msg);
161   GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub)));
162   GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig)));
163   GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (jp, pspec, NULL, NULL));
164   GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (js, sspec, NULL, NULL));
165   GNUNET_break (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig2));
166   GNUNET_break (0 == GNUNET_CRYPTO_rsa_public_key_cmp (pub, pub2));
167   GNUNET_CRYPTO_rsa_signature_free (sig);
168   GNUNET_CRYPTO_rsa_signature_free (sig2);
169   GNUNET_CRYPTO_rsa_private_key_free (priv);
170   GNUNET_CRYPTO_rsa_public_key_free (pub);
171   GNUNET_CRYPTO_rsa_public_key_free (pub2);
172   return 0;
173 }
174
175
176 /**
177  * Test rsa conversions from/to JSON.
178  *
179  * @return 0 on success
180  */
181 static int
182 test_boolean ()
183 {
184   int b1;
185   int b2;
186   json_t *json;
187   struct GNUNET_JSON_Specification pspec[] = { GNUNET_JSON_spec_boolean ("b1",
188                                                                          &b1),
189                                                GNUNET_JSON_spec_boolean ("b2",
190                                                                          &b2),
191                                                GNUNET_JSON_spec_end () };
192
193   json = json_object ();
194   json_object_set_new (json, "b1", json_true ());
195   json_object_set_new (json, "b2", json_false ());
196
197   GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (json, pspec, NULL, NULL));
198
199   GNUNET_assert (GNUNET_YES == b1);
200   GNUNET_assert (GNUNET_NO == b2);
201
202   json_object_set_new (json, "b1", json_integer (42));
203
204   GNUNET_assert (GNUNET_OK != GNUNET_JSON_parse (json, pspec, NULL, NULL));
205
206   return 0;
207 }
208
209
210 int
211 main (int argc, const char *const argv[])
212 {
213   GNUNET_log_setup ("test-json", "WARNING", NULL);
214   if (0 != test_abs_time ())
215     return 1;
216   if (0 != test_rel_time ())
217     return 1;
218   if (0 != test_raw ())
219     return 1;
220   if (0 != test_rsa ())
221     return 1;
222   if (0 != test_boolean ())
223     return 1;
224   /* FIXME: test EdDSA signature conversion... */
225   return 0;
226 }
227
228
229 /* end of test_json.c */