diff options
Diffstat (limited to 'src/lib/json/test_json.c')
-rw-r--r-- | src/lib/json/test_json.c | 247 |
1 files changed, 247 insertions, 0 deletions
diff --git a/src/lib/json/test_json.c b/src/lib/json/test_json.c new file mode 100644 index 000000000..1d27518b2 --- /dev/null +++ b/src/lib/json/test_json.c | |||
@@ -0,0 +1,247 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2015, 2016, 2021 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_timestamp (void) | ||
38 | { | ||
39 | json_t *j; | ||
40 | struct GNUNET_TIME_Absolute a1; | ||
41 | struct GNUNET_TIME_Timestamp t1; | ||
42 | struct GNUNET_TIME_Timestamp t2; | ||
43 | struct GNUNET_JSON_Specification s1[] = { | ||
44 | GNUNET_JSON_spec_timestamp (NULL, | ||
45 | &t2), | ||
46 | GNUNET_JSON_spec_end () | ||
47 | }; | ||
48 | struct GNUNET_JSON_Specification s2[] = { | ||
49 | GNUNET_JSON_spec_timestamp (NULL, | ||
50 | &t2), | ||
51 | GNUNET_JSON_spec_end () | ||
52 | }; | ||
53 | |||
54 | a1 = GNUNET_TIME_absolute_get (); | ||
55 | t1 = GNUNET_TIME_absolute_to_timestamp (a1); | ||
56 | j = GNUNET_JSON_from_timestamp (t1); | ||
57 | GNUNET_assert (NULL != j); | ||
58 | GNUNET_assert (GNUNET_OK == | ||
59 | GNUNET_JSON_parse (j, | ||
60 | s1, | ||
61 | NULL, | ||
62 | NULL)); | ||
63 | GNUNET_assert (GNUNET_TIME_timestamp_cmp (t1, ==, t2)); | ||
64 | json_decref (j); | ||
65 | |||
66 | a1 = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
67 | j = GNUNET_JSON_from_timestamp (t1); | ||
68 | GNUNET_assert (NULL != j); | ||
69 | GNUNET_assert (GNUNET_OK == | ||
70 | GNUNET_JSON_parse (j, | ||
71 | s2, | ||
72 | NULL, | ||
73 | NULL)); | ||
74 | GNUNET_assert (GNUNET_TIME_timestamp_cmp (t1, ==, t2)); | ||
75 | json_decref (j); | ||
76 | return 0; | ||
77 | } | ||
78 | |||
79 | |||
80 | /** | ||
81 | * Test relative time conversion from/to JSON. | ||
82 | * | ||
83 | * @return 0 on success | ||
84 | */ | ||
85 | static int | ||
86 | test_rel_time (void) | ||
87 | { | ||
88 | json_t *j; | ||
89 | struct GNUNET_TIME_Relative r1; | ||
90 | struct GNUNET_TIME_Relative r2; | ||
91 | struct GNUNET_JSON_Specification s1[] = { | ||
92 | GNUNET_JSON_spec_relative_time (NULL, | ||
93 | &r2), | ||
94 | GNUNET_JSON_spec_end () | ||
95 | }; | ||
96 | struct GNUNET_JSON_Specification s2[] = { | ||
97 | GNUNET_JSON_spec_relative_time (NULL, | ||
98 | &r2), | ||
99 | GNUNET_JSON_spec_end () | ||
100 | }; | ||
101 | |||
102 | r1 = GNUNET_TIME_UNIT_SECONDS; | ||
103 | j = GNUNET_JSON_from_time_rel (r1); | ||
104 | GNUNET_assert (NULL != j); | ||
105 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); | ||
106 | GNUNET_assert (r1.rel_value_us == r2.rel_value_us); | ||
107 | json_decref (j); | ||
108 | |||
109 | r1 = GNUNET_TIME_UNIT_FOREVER_REL; | ||
110 | j = GNUNET_JSON_from_time_rel (r1); | ||
111 | GNUNET_assert (NULL != j); | ||
112 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); | ||
113 | GNUNET_assert (r1.rel_value_us == r2.rel_value_us); | ||
114 | json_decref (j); | ||
115 | return 0; | ||
116 | } | ||
117 | |||
118 | |||
119 | /** | ||
120 | * Test raw (binary) conversion from/to JSON. | ||
121 | * | ||
122 | * @return 0 on success | ||
123 | */ | ||
124 | static int | ||
125 | test_raw () | ||
126 | { | ||
127 | char blob[256]; | ||
128 | unsigned int i; | ||
129 | json_t *j; | ||
130 | |||
131 | for (i = 0; i <= 256; i++) | ||
132 | { | ||
133 | char blob2[256]; | ||
134 | struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_fixed (NULL, | ||
135 | blob2, | ||
136 | i), | ||
137 | GNUNET_JSON_spec_end () }; | ||
138 | |||
139 | memset (blob, i, i); | ||
140 | j = GNUNET_JSON_from_data (blob, i); | ||
141 | GNUNET_assert (NULL != j); | ||
142 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, spec, NULL, NULL)); | ||
143 | GNUNET_assert (0 == memcmp (blob, blob2, i)); | ||
144 | json_decref (j); | ||
145 | } | ||
146 | return 0; | ||
147 | } | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Test rsa conversions from/to JSON. | ||
152 | * | ||
153 | * @return 0 on success | ||
154 | */ | ||
155 | static int | ||
156 | test_rsa () | ||
157 | { | ||
158 | struct GNUNET_CRYPTO_RsaPublicKey *pub; | ||
159 | struct GNUNET_CRYPTO_RsaPublicKey *pub2; | ||
160 | struct GNUNET_JSON_Specification pspec[] = | ||
161 | { GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), GNUNET_JSON_spec_end () }; | ||
162 | struct GNUNET_CRYPTO_RsaSignature *sig; | ||
163 | struct GNUNET_CRYPTO_RsaSignature *sig2; | ||
164 | struct GNUNET_JSON_Specification sspec[] = | ||
165 | { GNUNET_JSON_spec_rsa_signature (NULL, &sig2), GNUNET_JSON_spec_end () }; | ||
166 | struct GNUNET_CRYPTO_RsaPrivateKey *priv; | ||
167 | struct GNUNET_HashCode msg; | ||
168 | json_t *jp; | ||
169 | json_t *js; | ||
170 | |||
171 | priv = GNUNET_CRYPTO_rsa_private_key_create (1024); | ||
172 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); | ||
173 | memset (&msg, 42, sizeof(msg)); | ||
174 | sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, &msg, sizeof (msg)); | ||
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 == GNUNET_JSON_parse (jp, pspec, NULL, NULL)); | ||
178 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (js, sspec, NULL, NULL)); | ||
179 | GNUNET_break (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig2)); | ||
180 | GNUNET_break (0 == GNUNET_CRYPTO_rsa_public_key_cmp (pub, pub2)); | ||
181 | json_decref (jp); | ||
182 | json_decref (js); | ||
183 | GNUNET_CRYPTO_rsa_signature_free (sig); | ||
184 | GNUNET_CRYPTO_rsa_signature_free (sig2); | ||
185 | GNUNET_CRYPTO_rsa_private_key_free (priv); | ||
186 | GNUNET_CRYPTO_rsa_public_key_free (pub); | ||
187 | GNUNET_CRYPTO_rsa_public_key_free (pub2); | ||
188 | return 0; | ||
189 | } | ||
190 | |||
191 | |||
192 | /** | ||
193 | * Test rsa conversions from/to JSON. | ||
194 | * | ||
195 | * @return 0 on success | ||
196 | */ | ||
197 | static int | ||
198 | test_boolean () | ||
199 | { | ||
200 | int b1; | ||
201 | int b2; | ||
202 | json_t *json; | ||
203 | struct GNUNET_JSON_Specification pspec[] = { GNUNET_JSON_spec_boolean ("b1", | ||
204 | &b1), | ||
205 | GNUNET_JSON_spec_boolean ("b2", | ||
206 | &b2), | ||
207 | GNUNET_JSON_spec_end () }; | ||
208 | |||
209 | json = json_object (); | ||
210 | json_object_set_new (json, "b1", json_true ()); | ||
211 | json_object_set_new (json, "b2", json_false ()); | ||
212 | |||
213 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (json, pspec, NULL, NULL)); | ||
214 | |||
215 | GNUNET_assert (GNUNET_YES == b1); | ||
216 | GNUNET_assert (GNUNET_NO == b2); | ||
217 | |||
218 | json_object_set_new (json, "b1", json_integer (42)); | ||
219 | |||
220 | GNUNET_assert (GNUNET_OK != GNUNET_JSON_parse (json, pspec, NULL, NULL)); | ||
221 | |||
222 | json_decref (json); | ||
223 | |||
224 | return 0; | ||
225 | } | ||
226 | |||
227 | |||
228 | int | ||
229 | main (int argc, const char *const argv[]) | ||
230 | { | ||
231 | GNUNET_log_setup ("test-json", "WARNING", NULL); | ||
232 | if (0 != test_timestamp ()) | ||
233 | return 1; | ||
234 | if (0 != test_rel_time ()) | ||
235 | return 1; | ||
236 | if (0 != test_raw ()) | ||
237 | return 1; | ||
238 | if (0 != test_rsa ()) | ||
239 | return 1; | ||
240 | if (0 != test_boolean ()) | ||
241 | return 1; | ||
242 | /* FIXME: test EdDSA signature conversion... */ | ||
243 | return 0; | ||
244 | } | ||
245 | |||
246 | |||
247 | /* end of test_json.c */ | ||