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