diff options
Diffstat (limited to 'src/json/test_json.c')
-rw-r--r-- | src/json/test_json.c | 129 |
1 files changed, 46 insertions, 83 deletions
diff --git a/src/json/test_json.c b/src/json/test_json.c index dd957eb8e..40fb7f090 100644 --- a/src/json/test_json.c +++ b/src/json/test_json.c | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
@@ -39,32 +39,26 @@ test_abs_time () | |||
39 | json_t *j; | 39 | json_t *j; |
40 | struct GNUNET_TIME_Absolute a1; | 40 | struct GNUNET_TIME_Absolute a1; |
41 | struct GNUNET_TIME_Absolute a2; | 41 | struct GNUNET_TIME_Absolute a2; |
42 | struct GNUNET_JSON_Specification s1[] = { | 42 | struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_absolute_time (NULL, |
43 | GNUNET_JSON_spec_absolute_time (NULL, &a2), | 43 | &a2), |
44 | GNUNET_JSON_spec_end() | 44 | GNUNET_JSON_spec_end ()}; |
45 | }; | 45 | struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_absolute_time (NULL, |
46 | struct GNUNET_JSON_Specification s2[] = { | 46 | &a2), |
47 | GNUNET_JSON_spec_absolute_time (NULL, &a2), | 47 | GNUNET_JSON_spec_end ()}; |
48 | GNUNET_JSON_spec_end() | ||
49 | }; | ||
50 | 48 | ||
51 | a1 = GNUNET_TIME_absolute_get (); | 49 | a1 = GNUNET_TIME_absolute_get (); |
52 | GNUNET_TIME_round_abs (&a1); | 50 | GNUNET_TIME_round_abs (&a1); |
53 | j = GNUNET_JSON_from_time_abs (a1); | 51 | j = GNUNET_JSON_from_time_abs (a1); |
54 | GNUNET_assert (NULL != j); | 52 | GNUNET_assert (NULL != j); |
55 | GNUNET_assert (GNUNET_OK == | 53 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); |
56 | GNUNET_JSON_parse (j, s1, NULL, NULL)); | 54 | GNUNET_assert (a1.abs_value_us == a2.abs_value_us); |
57 | GNUNET_assert (a1.abs_value_us == | ||
58 | a2.abs_value_us); | ||
59 | json_decref (j); | 55 | json_decref (j); |
60 | 56 | ||
61 | a1 = GNUNET_TIME_UNIT_FOREVER_ABS; | 57 | a1 = GNUNET_TIME_UNIT_FOREVER_ABS; |
62 | j = GNUNET_JSON_from_time_abs (a1); | 58 | j = GNUNET_JSON_from_time_abs (a1); |
63 | GNUNET_assert (NULL != j); | 59 | GNUNET_assert (NULL != j); |
64 | GNUNET_assert (GNUNET_OK == | 60 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); |
65 | GNUNET_JSON_parse (j, s2, NULL, NULL)); | 61 | GNUNET_assert (a1.abs_value_us == a2.abs_value_us); |
66 | GNUNET_assert (a1.abs_value_us == | ||
67 | a2.abs_value_us); | ||
68 | json_decref (j); | 62 | json_decref (j); |
69 | return 0; | 63 | return 0; |
70 | } | 64 | } |
@@ -81,31 +75,25 @@ test_rel_time () | |||
81 | json_t *j; | 75 | json_t *j; |
82 | struct GNUNET_TIME_Relative r1; | 76 | struct GNUNET_TIME_Relative r1; |
83 | struct GNUNET_TIME_Relative r2; | 77 | struct GNUNET_TIME_Relative r2; |
84 | struct GNUNET_JSON_Specification s1[] = { | 78 | struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_relative_time (NULL, |
85 | GNUNET_JSON_spec_relative_time (NULL, &r2), | 79 | &r2), |
86 | GNUNET_JSON_spec_end() | 80 | GNUNET_JSON_spec_end ()}; |
87 | }; | 81 | struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_relative_time (NULL, |
88 | struct GNUNET_JSON_Specification s2[] = { | 82 | &r2), |
89 | GNUNET_JSON_spec_relative_time (NULL, &r2), | 83 | GNUNET_JSON_spec_end ()}; |
90 | GNUNET_JSON_spec_end() | ||
91 | }; | ||
92 | 84 | ||
93 | r1 = GNUNET_TIME_UNIT_SECONDS; | 85 | r1 = GNUNET_TIME_UNIT_SECONDS; |
94 | j = GNUNET_JSON_from_time_rel (r1); | 86 | j = GNUNET_JSON_from_time_rel (r1); |
95 | GNUNET_assert (NULL != j); | 87 | GNUNET_assert (NULL != j); |
96 | GNUNET_assert (GNUNET_OK == | 88 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); |
97 | GNUNET_JSON_parse (j, s1, NULL, NULL)); | 89 | GNUNET_assert (r1.rel_value_us == r2.rel_value_us); |
98 | GNUNET_assert (r1.rel_value_us == | ||
99 | r2.rel_value_us); | ||
100 | json_decref (j); | 90 | json_decref (j); |
101 | 91 | ||
102 | r1 = GNUNET_TIME_UNIT_FOREVER_REL; | 92 | r1 = GNUNET_TIME_UNIT_FOREVER_REL; |
103 | j = GNUNET_JSON_from_time_rel (r1); | 93 | j = GNUNET_JSON_from_time_rel (r1); |
104 | GNUNET_assert (NULL != j); | 94 | GNUNET_assert (NULL != j); |
105 | GNUNET_assert (GNUNET_OK == | 95 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); |
106 | GNUNET_JSON_parse (j, s2, NULL, NULL)); | 96 | GNUNET_assert (r1.rel_value_us == r2.rel_value_us); |
107 | GNUNET_assert (r1.rel_value_us == | ||
108 | r2.rel_value_us); | ||
109 | json_decref (j); | 97 | json_decref (j); |
110 | return 0; | 98 | return 0; |
111 | } | 99 | } |
@@ -123,24 +111,19 @@ test_raw () | |||
123 | unsigned int i; | 111 | unsigned int i; |
124 | json_t *j; | 112 | json_t *j; |
125 | 113 | ||
126 | for (i=0;i<=256;i++) | 114 | for (i = 0; i <= 256; i++) |
127 | { | 115 | { |
128 | char blob2[256]; | 116 | char blob2[256]; |
129 | struct GNUNET_JSON_Specification spec[] = { | 117 | struct GNUNET_JSON_Specification spec[] = {GNUNET_JSON_spec_fixed (NULL, |
130 | GNUNET_JSON_spec_fixed (NULL, blob2, i), | 118 | blob2, |
131 | GNUNET_JSON_spec_end() | 119 | i), |
132 | }; | 120 | GNUNET_JSON_spec_end ()}; |
133 | 121 | ||
134 | memset (blob, i, i); | 122 | memset (blob, i, i); |
135 | j = GNUNET_JSON_from_data (blob, i); | 123 | j = GNUNET_JSON_from_data (blob, i); |
136 | GNUNET_assert (NULL != j); | 124 | GNUNET_assert (NULL != j); |
137 | GNUNET_assert (GNUNET_OK == | 125 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, spec, NULL, NULL)); |
138 | GNUNET_JSON_parse (j, spec, | 126 | GNUNET_assert (0 == memcmp (blob, blob2, i)); |
139 | NULL, NULL)); | ||
140 | GNUNET_assert (0 == | ||
141 | memcmp (blob, | ||
142 | blob2, | ||
143 | i)); | ||
144 | } | 127 | } |
145 | return 0; | 128 | return 0; |
146 | } | 129 | } |
@@ -156,16 +139,12 @@ test_rsa () | |||
156 | { | 139 | { |
157 | struct GNUNET_CRYPTO_RsaPublicKey *pub; | 140 | struct GNUNET_CRYPTO_RsaPublicKey *pub; |
158 | struct GNUNET_CRYPTO_RsaPublicKey *pub2; | 141 | struct GNUNET_CRYPTO_RsaPublicKey *pub2; |
159 | struct GNUNET_JSON_Specification pspec[] = { | 142 | struct GNUNET_JSON_Specification pspec[] = |
160 | GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), | 143 | {GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), GNUNET_JSON_spec_end ()}; |
161 | GNUNET_JSON_spec_end() | ||
162 | }; | ||
163 | struct GNUNET_CRYPTO_RsaSignature *sig; | 144 | struct GNUNET_CRYPTO_RsaSignature *sig; |
164 | struct GNUNET_CRYPTO_RsaSignature *sig2; | 145 | struct GNUNET_CRYPTO_RsaSignature *sig2; |
165 | struct GNUNET_JSON_Specification sspec[] = { | 146 | struct GNUNET_JSON_Specification sspec[] = |
166 | GNUNET_JSON_spec_rsa_signature (NULL, &sig2), | 147 | {GNUNET_JSON_spec_rsa_signature (NULL, &sig2), GNUNET_JSON_spec_end ()}; |
167 | GNUNET_JSON_spec_end() | ||
168 | }; | ||
169 | struct GNUNET_CRYPTO_RsaPrivateKey *priv; | 148 | struct GNUNET_CRYPTO_RsaPrivateKey *priv; |
170 | struct GNUNET_HashCode msg; | 149 | struct GNUNET_HashCode msg; |
171 | json_t *jp; | 150 | json_t *jp; |
@@ -174,22 +153,13 @@ test_rsa () | |||
174 | priv = GNUNET_CRYPTO_rsa_private_key_create (1024); | 153 | priv = GNUNET_CRYPTO_rsa_private_key_create (1024); |
175 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); | 154 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); |
176 | memset (&msg, 42, sizeof (msg)); | 155 | memset (&msg, 42, sizeof (msg)); |
177 | sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, | 156 | sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, &msg); |
178 | &msg); | ||
179 | GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub))); | 157 | GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub))); |
180 | GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig))); | 158 | GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig))); |
181 | GNUNET_assert (GNUNET_OK == | 159 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (jp, pspec, NULL, NULL)); |
182 | GNUNET_JSON_parse (jp, pspec, | 160 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (js, sspec, NULL, NULL)); |
183 | NULL, NULL)); | 161 | GNUNET_break (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig2)); |
184 | GNUNET_assert (GNUNET_OK == | 162 | GNUNET_break (0 == GNUNET_CRYPTO_rsa_public_key_cmp (pub, pub2)); |
185 | GNUNET_JSON_parse (js, sspec, | ||
186 | NULL, NULL)); | ||
187 | GNUNET_break (0 == | ||
188 | GNUNET_CRYPTO_rsa_signature_cmp (sig, | ||
189 | sig2)); | ||
190 | GNUNET_break (0 == | ||
191 | GNUNET_CRYPTO_rsa_public_key_cmp (pub, | ||
192 | pub2)); | ||
193 | GNUNET_CRYPTO_rsa_signature_free (sig); | 163 | GNUNET_CRYPTO_rsa_signature_free (sig); |
194 | GNUNET_CRYPTO_rsa_signature_free (sig2); | 164 | GNUNET_CRYPTO_rsa_signature_free (sig2); |
195 | GNUNET_CRYPTO_rsa_private_key_free (priv); | 165 | GNUNET_CRYPTO_rsa_private_key_free (priv); |
@@ -210,40 +180,33 @@ test_boolean () | |||
210 | int b1; | 180 | int b1; |
211 | int b2; | 181 | int b2; |
212 | json_t *json; | 182 | json_t *json; |
213 | struct GNUNET_JSON_Specification pspec[] = { | 183 | struct GNUNET_JSON_Specification pspec[] = {GNUNET_JSON_spec_boolean ("b1", |
214 | GNUNET_JSON_spec_boolean ("b1", &b1), | 184 | &b1), |
215 | GNUNET_JSON_spec_boolean ("b2", &b2), | 185 | GNUNET_JSON_spec_boolean ("b2", |
216 | GNUNET_JSON_spec_end() | 186 | &b2), |
217 | }; | 187 | GNUNET_JSON_spec_end ()}; |
218 | 188 | ||
219 | json = json_object (); | 189 | json = json_object (); |
220 | json_object_set_new (json, "b1", json_true ()); | 190 | json_object_set_new (json, "b1", json_true ()); |
221 | json_object_set_new (json, "b2", json_false ()); | 191 | json_object_set_new (json, "b2", json_false ()); |
222 | 192 | ||
223 | GNUNET_assert (GNUNET_OK == | 193 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (json, pspec, NULL, NULL)); |
224 | GNUNET_JSON_parse (json, pspec, | ||
225 | NULL, NULL)); | ||
226 | 194 | ||
227 | GNUNET_assert (GNUNET_YES == b1); | 195 | GNUNET_assert (GNUNET_YES == b1); |
228 | GNUNET_assert (GNUNET_NO == b2); | 196 | GNUNET_assert (GNUNET_NO == b2); |
229 | 197 | ||
230 | json_object_set_new (json, "b1", json_integer (42)); | 198 | json_object_set_new (json, "b1", json_integer (42)); |
231 | 199 | ||
232 | GNUNET_assert (GNUNET_OK != | 200 | GNUNET_assert (GNUNET_OK != GNUNET_JSON_parse (json, pspec, NULL, NULL)); |
233 | GNUNET_JSON_parse (json, pspec, | ||
234 | NULL, NULL)); | ||
235 | 201 | ||
236 | return 0; | 202 | return 0; |
237 | } | 203 | } |
238 | 204 | ||
239 | 205 | ||
240 | int | 206 | int |
241 | main(int argc, | 207 | main (int argc, const char *const argv[]) |
242 | const char *const argv[]) | ||
243 | { | 208 | { |
244 | GNUNET_log_setup ("test-json", | 209 | GNUNET_log_setup ("test-json", "WARNING", NULL); |
245 | "WARNING", | ||
246 | NULL); | ||
247 | if (0 != test_abs_time ()) | 210 | if (0 != test_abs_time ()) |
248 | return 1; | 211 | return 1; |
249 | if (0 != test_rel_time ()) | 212 | if (0 != test_rel_time ()) |