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[] = { GNUNET_JSON_spec_absolute_time (
45 GNUNET_JSON_spec_end () };
46 struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_absolute_time (
49 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 == GNUNET_JSON_parse (j, s1, NULL, NULL));
56 GNUNET_assert (a1.abs_value_us == a2.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 == GNUNET_JSON_parse (j, s2, NULL, NULL));
63 GNUNET_assert (a1.abs_value_us == a2.abs_value_us);
70 * Test relative time conversion from/to JSON.
72 * @return 0 on success
78 struct GNUNET_TIME_Relative r1;
79 struct GNUNET_TIME_Relative r2;
80 struct GNUNET_JSON_Specification s1[] = { GNUNET_JSON_spec_relative_time (
83 GNUNET_JSON_spec_end () };
84 struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_relative_time (
87 GNUNET_JSON_spec_end () };
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);
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);
107 * Test raw (binary) conversion from/to JSON.
109 * @return 0 on success
118 for (i = 0; i <= 256; i++)
121 struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_fixed (NULL,
124 GNUNET_JSON_spec_end () };
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));
137 * Test rsa conversions from/to JSON.
139 * @return 0 on success
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;
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);
177 * Test rsa conversions from/to JSON.
179 * @return 0 on success
187 struct GNUNET_JSON_Specification pspec[] = { GNUNET_JSON_spec_boolean ("b1",
189 GNUNET_JSON_spec_boolean ("b2",
191 GNUNET_JSON_spec_end () };
193 json = json_object ();
194 json_object_set_new (json, "b1", json_true ());
195 json_object_set_new (json, "b2", json_false ());
197 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (json, pspec, NULL, NULL));
199 GNUNET_assert (GNUNET_YES == b1);
200 GNUNET_assert (GNUNET_NO == b2);
202 json_object_set_new (json, "b1", json_integer (42));
204 GNUNET_assert (GNUNET_OK != GNUNET_JSON_parse (json, pspec, NULL, NULL));
211 main (int argc, const char *const argv[])
213 GNUNET_log_setup ("test-json", "WARNING", NULL);
214 if (0 != test_abs_time ())
216 if (0 != test_rel_time ())
218 if (0 != test_raw ())
220 if (0 != test_rsa ())
222 if (0 != test_boolean ())
224 /* FIXME: test EdDSA signature conversion... */
229 /* end of test_json.c */