2 This file is part of GNUnet
3 (C) 2015, 2016 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file json/test_json.c
23 * @brief Tests for JSON conversion functions
24 * @author Christian Grothoff <christian@grothoff.org>
27 #include "gnunet_util_lib.h"
28 #include "gnunet_json_lib.h"
32 * Test absolute time conversion from/to JSON.
34 * @return 0 on success
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()
46 struct GNUNET_JSON_Specification s2[] = {
47 GNUNET_JSON_spec_absolute_time (NULL, &a2),
48 GNUNET_JSON_spec_end()
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 ==
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 ==
74 * Test relative time conversion from/to JSON.
76 * @return 0 on success
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()
88 struct GNUNET_JSON_Specification s2[] = {
89 GNUNET_JSON_spec_relative_time (NULL, &r2),
90 GNUNET_JSON_spec_end()
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 ==
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 ==
115 * Test raw (binary) conversion from/to JSON.
117 * @return 0 on success
129 struct GNUNET_JSON_Specification spec[] = {
130 GNUNET_JSON_spec_fixed (NULL, blob2, i),
131 GNUNET_JSON_spec_end()
135 j = GNUNET_JSON_from_data (blob, i);
136 GNUNET_assert (NULL != j);
137 GNUNET_assert (GNUNET_OK ==
138 GNUNET_JSON_parse (j, spec,
150 * Test rsa conversions from/to JSON.
152 * @return 0 on success
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()
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()
169 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
170 struct GNUNET_HashCode msg;
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,
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,
184 GNUNET_assert (GNUNET_OK ==
185 GNUNET_JSON_parse (js, sspec,
188 GNUNET_CRYPTO_rsa_signature_cmp (sig,
191 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
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);
203 * Test rsa conversions from/to JSON.
205 * @return 0 on success
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()
219 json = json_object ();
220 json_object_set_new (json, "b1", json_true ());
221 json_object_set_new (json, "b2", json_false ());
223 GNUNET_assert (GNUNET_OK ==
224 GNUNET_JSON_parse (json, pspec,
227 GNUNET_assert (GNUNET_YES == b1);
228 GNUNET_assert (GNUNET_NO == b2);
230 json_object_set_new (json, "b1", json_integer (42));
232 GNUNET_assert (GNUNET_OK !=
233 GNUNET_JSON_parse (json, pspec,
242 const char *const argv[])
244 GNUNET_log_setup ("test-json",
247 if (0 != test_abs_time ())
249 if (0 != test_rel_time ())
251 if (0 != test_raw ())
253 if (0 != test_rsa ())
255 if (0 != test_boolean ())
257 /* FIXME: test EdDSA signature conversion... */
261 /* end of test_json.c */