diff options
Diffstat (limited to 'src/json/test_json.c')
-rw-r--r-- | src/json/test_json.c | 192 |
1 files changed, 96 insertions, 96 deletions
diff --git a/src/json/test_json.c b/src/json/test_json.c index 40fb7f090..4b9fbc710 100644 --- a/src/json/test_json.c +++ b/src/json/test_json.c | |||
@@ -1,22 +1,22 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | (C) 2015, 2016 GNUnet e.V. | 3 | (C) 2015, 2016 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 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 | 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, | 7 | by the Free Software Foundation, either version 3 of the License, |
8 | or (at your option) any later version. | 8 | or (at your option) any later version. |
9 | 9 | ||
10 | GNUnet is distributed in the hope that it will be useful, but | 10 | GNUnet is distributed in the hope that it will be useful, but |
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 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file json/test_json.c | 22 | * @file json/test_json.c |
@@ -34,32 +34,32 @@ | |||
34 | * @return 0 on success | 34 | * @return 0 on success |
35 | */ | 35 | */ |
36 | static int | 36 | static int |
37 | test_abs_time () | 37 | test_abs_time() |
38 | { | 38 | { |
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[] = {GNUNET_JSON_spec_absolute_time (NULL, | 42 | struct GNUNET_JSON_Specification s1[] = { GNUNET_JSON_spec_absolute_time(NULL, |
43 | &a2), | 43 | &a2), |
44 | GNUNET_JSON_spec_end ()}; | 44 | GNUNET_JSON_spec_end() }; |
45 | struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_absolute_time (NULL, | 45 | struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_absolute_time(NULL, |
46 | &a2), | 46 | &a2), |
47 | GNUNET_JSON_spec_end ()}; | 47 | GNUNET_JSON_spec_end() }; |
48 | 48 | ||
49 | a1 = GNUNET_TIME_absolute_get (); | 49 | a1 = GNUNET_TIME_absolute_get(); |
50 | GNUNET_TIME_round_abs (&a1); | 50 | GNUNET_TIME_round_abs(&a1); |
51 | j = GNUNET_JSON_from_time_abs (a1); | 51 | j = GNUNET_JSON_from_time_abs(a1); |
52 | GNUNET_assert (NULL != j); | 52 | GNUNET_assert(NULL != j); |
53 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); | 53 | GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, s1, NULL, NULL)); |
54 | GNUNET_assert (a1.abs_value_us == a2.abs_value_us); | 54 | GNUNET_assert(a1.abs_value_us == a2.abs_value_us); |
55 | json_decref (j); | 55 | json_decref(j); |
56 | 56 | ||
57 | a1 = GNUNET_TIME_UNIT_FOREVER_ABS; | 57 | a1 = GNUNET_TIME_UNIT_FOREVER_ABS; |
58 | j = GNUNET_JSON_from_time_abs (a1); | 58 | j = GNUNET_JSON_from_time_abs(a1); |
59 | GNUNET_assert (NULL != j); | 59 | GNUNET_assert(NULL != j); |
60 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); | 60 | GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, s2, NULL, NULL)); |
61 | GNUNET_assert (a1.abs_value_us == a2.abs_value_us); | 61 | GNUNET_assert(a1.abs_value_us == a2.abs_value_us); |
62 | json_decref (j); | 62 | json_decref(j); |
63 | return 0; | 63 | return 0; |
64 | } | 64 | } |
65 | 65 | ||
@@ -70,31 +70,31 @@ test_abs_time () | |||
70 | * @return 0 on success | 70 | * @return 0 on success |
71 | */ | 71 | */ |
72 | static int | 72 | static int |
73 | test_rel_time () | 73 | test_rel_time() |
74 | { | 74 | { |
75 | json_t *j; | 75 | json_t *j; |
76 | struct GNUNET_TIME_Relative r1; | 76 | struct GNUNET_TIME_Relative r1; |
77 | struct GNUNET_TIME_Relative r2; | 77 | struct GNUNET_TIME_Relative r2; |
78 | struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_relative_time (NULL, | 78 | struct GNUNET_JSON_Specification s1[] = { GNUNET_JSON_spec_relative_time(NULL, |
79 | &r2), | 79 | &r2), |
80 | GNUNET_JSON_spec_end ()}; | 80 | GNUNET_JSON_spec_end() }; |
81 | struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_relative_time (NULL, | 81 | struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_relative_time(NULL, |
82 | &r2), | 82 | &r2), |
83 | GNUNET_JSON_spec_end ()}; | 83 | GNUNET_JSON_spec_end() }; |
84 | 84 | ||
85 | r1 = GNUNET_TIME_UNIT_SECONDS; | 85 | r1 = GNUNET_TIME_UNIT_SECONDS; |
86 | j = GNUNET_JSON_from_time_rel (r1); | 86 | j = GNUNET_JSON_from_time_rel(r1); |
87 | GNUNET_assert (NULL != j); | 87 | GNUNET_assert(NULL != j); |
88 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); | 88 | GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, s1, NULL, NULL)); |
89 | GNUNET_assert (r1.rel_value_us == r2.rel_value_us); | 89 | GNUNET_assert(r1.rel_value_us == r2.rel_value_us); |
90 | json_decref (j); | 90 | json_decref(j); |
91 | 91 | ||
92 | r1 = GNUNET_TIME_UNIT_FOREVER_REL; | 92 | r1 = GNUNET_TIME_UNIT_FOREVER_REL; |
93 | j = GNUNET_JSON_from_time_rel (r1); | 93 | j = GNUNET_JSON_from_time_rel(r1); |
94 | GNUNET_assert (NULL != j); | 94 | GNUNET_assert(NULL != j); |
95 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); | 95 | GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, s2, NULL, NULL)); |
96 | GNUNET_assert (r1.rel_value_us == r2.rel_value_us); | 96 | GNUNET_assert(r1.rel_value_us == r2.rel_value_us); |
97 | json_decref (j); | 97 | json_decref(j); |
98 | return 0; | 98 | return 0; |
99 | } | 99 | } |
100 | 100 | ||
@@ -105,26 +105,26 @@ test_rel_time () | |||
105 | * @return 0 on success | 105 | * @return 0 on success |
106 | */ | 106 | */ |
107 | static int | 107 | static int |
108 | test_raw () | 108 | test_raw() |
109 | { | 109 | { |
110 | char blob[256]; | 110 | char blob[256]; |
111 | unsigned int i; | 111 | unsigned int i; |
112 | json_t *j; | 112 | json_t *j; |
113 | 113 | ||
114 | for (i = 0; i <= 256; i++) | 114 | for (i = 0; i <= 256; i++) |
115 | { | 115 | { |
116 | char blob2[256]; | 116 | char blob2[256]; |
117 | struct GNUNET_JSON_Specification spec[] = {GNUNET_JSON_spec_fixed (NULL, | 117 | struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_fixed(NULL, |
118 | blob2, | 118 | blob2, |
119 | i), | 119 | i), |
120 | GNUNET_JSON_spec_end ()}; | 120 | GNUNET_JSON_spec_end() }; |
121 | 121 | ||
122 | memset (blob, i, i); | 122 | memset(blob, i, i); |
123 | j = GNUNET_JSON_from_data (blob, i); | 123 | j = GNUNET_JSON_from_data(blob, i); |
124 | GNUNET_assert (NULL != j); | 124 | GNUNET_assert(NULL != j); |
125 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, spec, NULL, NULL)); | 125 | GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, spec, NULL, NULL)); |
126 | GNUNET_assert (0 == memcmp (blob, blob2, i)); | 126 | GNUNET_assert(0 == memcmp(blob, blob2, i)); |
127 | } | 127 | } |
128 | return 0; | 128 | return 0; |
129 | } | 129 | } |
130 | 130 | ||
@@ -135,36 +135,36 @@ test_raw () | |||
135 | * @return 0 on success | 135 | * @return 0 on success |
136 | */ | 136 | */ |
137 | static int | 137 | static int |
138 | test_rsa () | 138 | test_rsa() |
139 | { | 139 | { |
140 | struct GNUNET_CRYPTO_RsaPublicKey *pub; | 140 | struct GNUNET_CRYPTO_RsaPublicKey *pub; |
141 | struct GNUNET_CRYPTO_RsaPublicKey *pub2; | 141 | struct GNUNET_CRYPTO_RsaPublicKey *pub2; |
142 | struct GNUNET_JSON_Specification pspec[] = | 142 | struct GNUNET_JSON_Specification pspec[] = |
143 | {GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), GNUNET_JSON_spec_end ()}; | 143 | { GNUNET_JSON_spec_rsa_public_key(NULL, &pub2), GNUNET_JSON_spec_end() }; |
144 | struct GNUNET_CRYPTO_RsaSignature *sig; | 144 | struct GNUNET_CRYPTO_RsaSignature *sig; |
145 | struct GNUNET_CRYPTO_RsaSignature *sig2; | 145 | struct GNUNET_CRYPTO_RsaSignature *sig2; |
146 | struct GNUNET_JSON_Specification sspec[] = | 146 | struct GNUNET_JSON_Specification sspec[] = |
147 | {GNUNET_JSON_spec_rsa_signature (NULL, &sig2), GNUNET_JSON_spec_end ()}; | 147 | { GNUNET_JSON_spec_rsa_signature(NULL, &sig2), GNUNET_JSON_spec_end() }; |
148 | struct GNUNET_CRYPTO_RsaPrivateKey *priv; | 148 | struct GNUNET_CRYPTO_RsaPrivateKey *priv; |
149 | struct GNUNET_HashCode msg; | 149 | struct GNUNET_HashCode msg; |
150 | json_t *jp; | 150 | json_t *jp; |
151 | json_t *js; | 151 | json_t *js; |
152 | 152 | ||
153 | priv = GNUNET_CRYPTO_rsa_private_key_create (1024); | 153 | priv = GNUNET_CRYPTO_rsa_private_key_create(1024); |
154 | pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); | 154 | pub = GNUNET_CRYPTO_rsa_private_key_get_public(priv); |
155 | memset (&msg, 42, sizeof (msg)); | 155 | memset(&msg, 42, sizeof(msg)); |
156 | sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, &msg); | 156 | sig = GNUNET_CRYPTO_rsa_sign_fdh(priv, &msg); |
157 | GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub))); | 157 | GNUNET_assert(NULL != (jp = GNUNET_JSON_from_rsa_public_key(pub))); |
158 | GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig))); | 158 | GNUNET_assert(NULL != (js = GNUNET_JSON_from_rsa_signature(sig))); |
159 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (jp, pspec, NULL, NULL)); | 159 | GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(jp, pspec, NULL, NULL)); |
160 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (js, sspec, NULL, NULL)); | 160 | GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(js, sspec, NULL, NULL)); |
161 | GNUNET_break (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig2)); | 161 | GNUNET_break(0 == GNUNET_CRYPTO_rsa_signature_cmp(sig, sig2)); |
162 | GNUNET_break (0 == GNUNET_CRYPTO_rsa_public_key_cmp (pub, pub2)); | 162 | GNUNET_break(0 == GNUNET_CRYPTO_rsa_public_key_cmp(pub, pub2)); |
163 | GNUNET_CRYPTO_rsa_signature_free (sig); | 163 | GNUNET_CRYPTO_rsa_signature_free(sig); |
164 | GNUNET_CRYPTO_rsa_signature_free (sig2); | 164 | GNUNET_CRYPTO_rsa_signature_free(sig2); |
165 | GNUNET_CRYPTO_rsa_private_key_free (priv); | 165 | GNUNET_CRYPTO_rsa_private_key_free(priv); |
166 | GNUNET_CRYPTO_rsa_public_key_free (pub); | 166 | GNUNET_CRYPTO_rsa_public_key_free(pub); |
167 | GNUNET_CRYPTO_rsa_public_key_free (pub2); | 167 | GNUNET_CRYPTO_rsa_public_key_free(pub2); |
168 | return 0; | 168 | return 0; |
169 | } | 169 | } |
170 | 170 | ||
@@ -175,47 +175,47 @@ test_rsa () | |||
175 | * @return 0 on success | 175 | * @return 0 on success |
176 | */ | 176 | */ |
177 | static int | 177 | static int |
178 | test_boolean () | 178 | test_boolean() |
179 | { | 179 | { |
180 | int b1; | 180 | int b1; |
181 | int b2; | 181 | int b2; |
182 | json_t *json; | 182 | json_t *json; |
183 | struct GNUNET_JSON_Specification pspec[] = {GNUNET_JSON_spec_boolean ("b1", | 183 | struct GNUNET_JSON_Specification pspec[] = { GNUNET_JSON_spec_boolean("b1", |
184 | &b1), | 184 | &b1), |
185 | GNUNET_JSON_spec_boolean ("b2", | 185 | GNUNET_JSON_spec_boolean("b2", |
186 | &b2), | 186 | &b2), |
187 | GNUNET_JSON_spec_end ()}; | 187 | GNUNET_JSON_spec_end() }; |
188 | 188 | ||
189 | json = json_object (); | 189 | json = json_object(); |
190 | json_object_set_new (json, "b1", json_true ()); | 190 | json_object_set_new(json, "b1", json_true()); |
191 | json_object_set_new (json, "b2", json_false ()); | 191 | json_object_set_new(json, "b2", json_false()); |
192 | 192 | ||
193 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (json, pspec, NULL, NULL)); | 193 | GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(json, pspec, NULL, NULL)); |
194 | 194 | ||
195 | GNUNET_assert (GNUNET_YES == b1); | 195 | GNUNET_assert(GNUNET_YES == b1); |
196 | GNUNET_assert (GNUNET_NO == b2); | 196 | GNUNET_assert(GNUNET_NO == b2); |
197 | 197 | ||
198 | json_object_set_new (json, "b1", json_integer (42)); | 198 | json_object_set_new(json, "b1", json_integer(42)); |
199 | 199 | ||
200 | GNUNET_assert (GNUNET_OK != GNUNET_JSON_parse (json, pspec, NULL, NULL)); | 200 | GNUNET_assert(GNUNET_OK != GNUNET_JSON_parse(json, pspec, NULL, NULL)); |
201 | 201 | ||
202 | return 0; | 202 | return 0; |
203 | } | 203 | } |
204 | 204 | ||
205 | 205 | ||
206 | int | 206 | int |
207 | main (int argc, const char *const argv[]) | 207 | main(int argc, const char *const argv[]) |
208 | { | 208 | { |
209 | GNUNET_log_setup ("test-json", "WARNING", NULL); | 209 | GNUNET_log_setup("test-json", "WARNING", NULL); |
210 | if (0 != test_abs_time ()) | 210 | if (0 != test_abs_time()) |
211 | return 1; | 211 | return 1; |
212 | if (0 != test_rel_time ()) | 212 | if (0 != test_rel_time()) |
213 | return 1; | 213 | return 1; |
214 | if (0 != test_raw ()) | 214 | if (0 != test_raw()) |
215 | return 1; | 215 | return 1; |
216 | if (0 != test_rsa ()) | 216 | if (0 != test_rsa()) |
217 | return 1; | 217 | return 1; |
218 | if (0 != test_boolean ()) | 218 | if (0 != test_boolean()) |
219 | return 1; | 219 | return 1; |
220 | /* FIXME: test EdDSA signature conversion... */ | 220 | /* FIXME: test EdDSA signature conversion... */ |
221 | return 0; | 221 | return 0; |