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 under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
18 * @file json/test_json.c
19 * @brief Tests for JSON conversion functions
20 * @author Christian Grothoff <christian@grothoff.org>
23 #include "gnunet_util_lib.h"
24 #include "gnunet_json_lib.h"
28 * Test absolute time conversion from/to JSON.
30 * @return 0 on success
36 struct GNUNET_TIME_Absolute a1;
37 struct GNUNET_TIME_Absolute a2;
38 struct GNUNET_JSON_Specification s1[] = {
39 GNUNET_JSON_spec_absolute_time (NULL, &a2),
40 GNUNET_JSON_spec_end()
42 struct GNUNET_JSON_Specification s2[] = {
43 GNUNET_JSON_spec_absolute_time (NULL, &a2),
44 GNUNET_JSON_spec_end()
47 a1 = GNUNET_TIME_absolute_get ();
48 GNUNET_TIME_round_abs (&a1);
49 j = GNUNET_JSON_from_time_abs (a1);
50 GNUNET_assert (NULL != j);
51 GNUNET_assert (GNUNET_OK ==
52 GNUNET_JSON_parse (j, s1, NULL, NULL));
53 GNUNET_assert (a1.abs_value_us ==
57 a1 = GNUNET_TIME_UNIT_FOREVER_ABS;
58 j = GNUNET_JSON_from_time_abs (a1);
59 GNUNET_assert (NULL != j);
60 GNUNET_assert (GNUNET_OK ==
61 GNUNET_JSON_parse (j, s2, NULL, NULL));
62 GNUNET_assert (a1.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[] = {
81 GNUNET_JSON_spec_relative_time (NULL, &r2),
82 GNUNET_JSON_spec_end()
84 struct GNUNET_JSON_Specification s2[] = {
85 GNUNET_JSON_spec_relative_time (NULL, &r2),
86 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 ==
93 GNUNET_JSON_parse (j, s1, NULL, NULL));
94 GNUNET_assert (r1.rel_value_us ==
98 r1 = GNUNET_TIME_UNIT_FOREVER_REL;
99 j = GNUNET_JSON_from_time_rel (r1);
100 GNUNET_assert (NULL != j);
101 GNUNET_assert (GNUNET_OK ==
102 GNUNET_JSON_parse (j, s2, NULL, NULL));
103 GNUNET_assert (r1.rel_value_us ==
111 * Test raw (binary) conversion from/to JSON.
113 * @return 0 on success
125 struct GNUNET_JSON_Specification spec[] = {
126 GNUNET_JSON_spec_fixed (NULL, blob2, i),
127 GNUNET_JSON_spec_end()
131 j = GNUNET_JSON_from_data (blob, i);
132 GNUNET_assert (NULL != j);
133 GNUNET_assert (GNUNET_OK ==
134 GNUNET_JSON_parse (j, spec,
146 * Test rsa conversions from/to JSON.
148 * @return 0 on success
153 struct GNUNET_CRYPTO_RsaPublicKey *pub;
154 struct GNUNET_CRYPTO_RsaPublicKey *pub2;
155 struct GNUNET_JSON_Specification pspec[] = {
156 GNUNET_JSON_spec_rsa_public_key (NULL, &pub2),
157 GNUNET_JSON_spec_end()
159 struct GNUNET_CRYPTO_RsaSignature *sig;
160 struct GNUNET_CRYPTO_RsaSignature *sig2;
161 struct GNUNET_JSON_Specification sspec[] = {
162 GNUNET_JSON_spec_rsa_signature (NULL, &sig2),
163 GNUNET_JSON_spec_end()
165 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
166 struct GNUNET_HashCode msg;
170 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
171 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
172 memset (&msg, 42, sizeof (msg));
173 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
175 GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub)));
176 GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig)));
177 GNUNET_assert (GNUNET_OK ==
178 GNUNET_JSON_parse (jp, pspec,
180 GNUNET_assert (GNUNET_OK ==
181 GNUNET_JSON_parse (js, sspec,
184 GNUNET_CRYPTO_rsa_signature_cmp (sig,
187 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
189 GNUNET_CRYPTO_rsa_signature_free (sig);
190 GNUNET_CRYPTO_rsa_signature_free (sig2);
191 GNUNET_CRYPTO_rsa_private_key_free (priv);
192 GNUNET_CRYPTO_rsa_public_key_free (pub);
193 GNUNET_CRYPTO_rsa_public_key_free (pub2);
200 const char *const argv[])
202 GNUNET_log_setup ("test-json",
205 if (0 != test_abs_time ())
207 if (0 != test_rel_time ())
209 if (0 != test_raw ())
211 if (0 != test_rsa ())
213 /* FIXME: test EdDSA signature conversion... */
217 /* end of test_json.c */