diff options
Diffstat (limited to 'src/json/test_json.c')
-rw-r--r-- | src/json/test_json.c | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/src/json/test_json.c b/src/json/test_json.c new file mode 100644 index 000000000..a334bf599 --- /dev/null +++ b/src/json/test_json.c | |||
@@ -0,0 +1,217 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2015, 2016 GNUnet e.V. | ||
4 | |||
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. | ||
8 | |||
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. | ||
12 | |||
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/> | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file json/test_json.c | ||
19 | * @brief Tests for JSON conversion functions | ||
20 | * @author Christian Grothoff <christian@grothoff.org> | ||
21 | */ | ||
22 | #include "platform.h" | ||
23 | #include "gnunet_util_lib.h" | ||
24 | #include "gnunet_json_lib.h" | ||
25 | |||
26 | |||
27 | /** | ||
28 | * Test absolute time conversion from/to JSON. | ||
29 | * | ||
30 | * @return 0 on success | ||
31 | */ | ||
32 | static int | ||
33 | test_abs_time () | ||
34 | { | ||
35 | json_t *j; | ||
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() | ||
41 | }; | ||
42 | struct GNUNET_JSON_Specification s2[] = { | ||
43 | GNUNET_JSON_spec_absolute_time (NULL, &a2), | ||
44 | GNUNET_JSON_spec_end() | ||
45 | }; | ||
46 | |||
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 == | ||
54 | a2.abs_value_us); | ||
55 | json_decref (j); | ||
56 | |||
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 == | ||
63 | a2.abs_value_us); | ||
64 | json_decref (j); | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | |||
69 | /** | ||
70 | * Test relative time conversion from/to JSON. | ||
71 | * | ||
72 | * @return 0 on success | ||
73 | */ | ||
74 | static int | ||
75 | test_rel_time () | ||
76 | { | ||
77 | json_t *j; | ||
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() | ||
83 | }; | ||
84 | struct GNUNET_JSON_Specification s2[] = { | ||
85 | GNUNET_JSON_spec_relative_time (NULL, &r2), | ||
86 | GNUNET_JSON_spec_end() | ||
87 | }; | ||
88 | |||
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 == | ||
95 | r2.rel_value_us); | ||
96 | json_decref (j); | ||
97 | |||
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 == | ||
104 | r2.rel_value_us); | ||
105 | json_decref (j); | ||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | |||
110 | /** | ||
111 | * Test raw (binary) conversion from/to JSON. | ||
112 | * | ||
113 | * @return 0 on success | ||
114 | */ | ||
115 | static int | ||
116 | test_raw () | ||
117 | { | ||
118 | char blob[256]; | ||
119 | unsigned int i; | ||
120 | json_t *j; | ||
121 | |||
122 | for (i=0;i<=256;i++) | ||
123 | { | ||
124 | char blob2[256]; | ||
125 | struct GNUNET_JSON_Specification spec[] = { | ||
126 | GNUNET_JSON_spec_fixed (NULL, blob2, i), | ||
127 | GNUNET_JSON_spec_end() | ||
128 | }; | ||
129 | |||
130 | memset (blob, i, i); | ||
131 | j = GNUNET_JSON_from_data (blob, i); | ||
132 | GNUNET_assert (NULL != j); | ||
133 | GNUNET_assert (GNUNET_OK == | ||
134 | GNUNET_JSON_parse (j, spec, | ||
135 | NULL, NULL)); | ||
136 | GNUNET_assert (0 == | ||
137 | memcmp (blob, | ||
138 | blob2, | ||
139 | i)); | ||
140 | } | ||
141 | return 0; | ||
142 | } | ||
143 | |||
144 | |||
145 | /** | ||
146 | * Test rsa conversions from/to JSON. | ||
147 | * | ||
148 | * @return 0 on success | ||
149 | */ | ||
150 | static int | ||
151 | test_rsa () | ||
152 | { | ||
153 | struct GNUNET_CRYPTO_rsa_PublicKey *pub; | ||
154 | struct GNUNET_CRYPTO_rsa_PublicKey *pub2; | ||
155 | struct GNUNET_JSON_Specification pspec[] = { | ||
156 | GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), | ||
157 | GNUNET_JSON_spec_end() | ||
158 | }; | ||
159 | struct GNUNET_CRYPTO_rsa_Signature *sig; | ||
160 | struct GNUNET_CRYPTO_rsa_Signature *sig2; | ||
161 | struct GNUNET_JSON_Specification sspec[] = { | ||
162 | GNUNET_JSON_spec_rsa_signature (NULL, &sig2), | ||
163 | GNUNET_JSON_spec_end() | ||
164 | }; | ||
165 | struct GNUNET_CRYPTO_rsa_PrivateKey *priv; | ||
166 | char msg[] = "Hello"; | ||
167 | json_t *jp; | ||
168 | json_t *js; | ||
169 | |||
170 | priv = GNUNET_CRYPTO_rsa_private_key_create (1024); | ||
171 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); | ||
172 | sig = GNUNET_CRYPTO_rsa_sign (priv, | ||
173 | msg, | ||
174 | 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 == | ||
178 | GNUNET_JSON_parse (jp, pspec, | ||
179 | NULL, NULL)); | ||
180 | GNUNET_assert (GNUNET_OK == | ||
181 | GNUNET_JSON_parse (js, sspec, | ||
182 | NULL, NULL)); | ||
183 | GNUNET_break (0 == | ||
184 | GNUNET_CRYPTO_rsa_signature_cmp (sig, | ||
185 | sig2)); | ||
186 | GNUNET_break (0 == | ||
187 | GNUNET_CRYPTO_rsa_public_key_cmp (pub, | ||
188 | pub2)); | ||
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); | ||
194 | return 0; | ||
195 | } | ||
196 | |||
197 | |||
198 | int | ||
199 | main(int argc, | ||
200 | const char *const argv[]) | ||
201 | { | ||
202 | GNUNET_log_setup ("test-json", | ||
203 | "WARNING", | ||
204 | NULL); | ||
205 | if (0 != test_abs_time ()) | ||
206 | return 1; | ||
207 | if (0 != test_rel_time ()) | ||
208 | return 1; | ||
209 | if (0 != test_raw ()) | ||
210 | return 1; | ||
211 | if (0 != test_rsa ()) | ||
212 | return 1; | ||
213 | /* FIXME: test EdDSA signature conversion... */ | ||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | /* end of test_json.c */ | ||