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/>.
20 * @file json/test_json.c
21 * @brief Tests for JSON conversion functions
22 * @author Christian Grothoff <christian@grothoff.org>
25 #include "gnunet_util_lib.h"
26 #include "gnunet_json_lib.h"
30 * Test absolute time conversion from/to JSON.
32 * @return 0 on success
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()
44 struct GNUNET_JSON_Specification s2[] = {
45 GNUNET_JSON_spec_absolute_time (NULL, &a2),
46 GNUNET_JSON_spec_end()
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 ==
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 ==
72 * Test relative time conversion from/to JSON.
74 * @return 0 on success
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()
86 struct GNUNET_JSON_Specification s2[] = {
87 GNUNET_JSON_spec_relative_time (NULL, &r2),
88 GNUNET_JSON_spec_end()
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 ==
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 ==
113 * Test raw (binary) conversion from/to JSON.
115 * @return 0 on success
127 struct GNUNET_JSON_Specification spec[] = {
128 GNUNET_JSON_spec_fixed (NULL, blob2, i),
129 GNUNET_JSON_spec_end()
133 j = GNUNET_JSON_from_data (blob, i);
134 GNUNET_assert (NULL != j);
135 GNUNET_assert (GNUNET_OK ==
136 GNUNET_JSON_parse (j, spec,
148 * Test rsa conversions from/to JSON.
150 * @return 0 on success
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()
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()
167 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
168 struct GNUNET_HashCode msg;
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,
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,
182 GNUNET_assert (GNUNET_OK ==
183 GNUNET_JSON_parse (js, sspec,
186 GNUNET_CRYPTO_rsa_signature_cmp (sig,
189 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
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);
201 * Test rsa conversions from/to JSON.
203 * @return 0 on success
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()
217 json = json_object ();
218 json_object_set_new (json, "b1", json_true ());
219 json_object_set_new (json, "b2", json_false ());
221 GNUNET_assert (GNUNET_OK ==
222 GNUNET_JSON_parse (json, pspec,
225 GNUNET_assert (GNUNET_YES == b1);
226 GNUNET_assert (GNUNET_NO == b2);
228 json_object_set_new (json, "b1", json_integer (42));
230 GNUNET_assert (GNUNET_OK !=
231 GNUNET_JSON_parse (json, pspec,
240 const char *const argv[])
242 GNUNET_log_setup ("test-json",
245 if (0 != test_abs_time ())
247 if (0 != test_rel_time ())
249 if (0 != test_raw ())
251 if (0 != test_rsa ())
253 if (0 != test_boolean ())
255 /* FIXME: test EdDSA signature conversion... */
259 /* end of test_json.c */