diff options
Diffstat (limited to 'src/core/test_core_api.c')
-rw-r--r-- | src/core/test_core_api.c | 343 |
1 files changed, 173 insertions, 170 deletions
diff --git a/src/core/test_core_api.c b/src/core/test_core_api.c index d29a5f04d..feeea7e9f 100644 --- a/src/core/test_core_api.c +++ b/src/core/test_core_api.c | |||
@@ -32,7 +32,8 @@ | |||
32 | 32 | ||
33 | #define MTYPE 12345 | 33 | #define MTYPE 12345 |
34 | 34 | ||
35 | struct PeerContext { | 35 | struct PeerContext |
36 | { | ||
36 | struct GNUNET_CONFIGURATION_Handle *cfg; | 37 | struct GNUNET_CONFIGURATION_Handle *cfg; |
37 | struct GNUNET_CORE_Handle *ch; | 38 | struct GNUNET_CORE_Handle *ch; |
38 | struct GNUNET_PeerIdentity id; | 39 | struct GNUNET_PeerIdentity id; |
@@ -55,18 +56,18 @@ static int ok; | |||
55 | 56 | ||
56 | #define OKPP \ | 57 | #define OKPP \ |
57 | do \ | 58 | do \ |
58 | { \ | 59 | { \ |
59 | ok++; \ | 60 | ok++; \ |
60 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, \ | 61 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, \ |
61 | "Now at stage %u at %s:%u\n", \ | 62 | "Now at stage %u at %s:%u\n", \ |
62 | ok, \ | 63 | ok, \ |
63 | __FILE__, \ | 64 | __FILE__, \ |
64 | __LINE__); \ | 65 | __LINE__); \ |
65 | } while (0) | 66 | } while (0) |
66 | 67 | ||
67 | 68 | ||
68 | static void | 69 | static void |
69 | offer_hello_done(void *cls) | 70 | offer_hello_done (void *cls) |
70 | { | 71 | { |
71 | struct PeerContext *p = cls; | 72 | struct PeerContext *p = cls; |
72 | 73 | ||
@@ -75,264 +76,266 @@ offer_hello_done(void *cls) | |||
75 | 76 | ||
76 | 77 | ||
77 | static void | 78 | static void |
78 | process_hello(void *cls, const struct GNUNET_MessageHeader *message) | 79 | process_hello (void *cls, const struct GNUNET_MessageHeader *message) |
79 | { | 80 | { |
80 | struct PeerContext *p = cls; | 81 | struct PeerContext *p = cls; |
81 | 82 | ||
82 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 83 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
83 | "Received (my) HELLO from transport service\n"); | 84 | "Received (my) HELLO from transport service\n"); |
84 | GNUNET_assert(message != NULL); | 85 | GNUNET_assert (message != NULL); |
85 | if ((p == &p1) && (NULL == p2.oh)) | 86 | if ((p == &p1) && (NULL == p2.oh)) |
86 | p2.oh = | 87 | p2.oh = |
87 | GNUNET_TRANSPORT_offer_hello(p2.cfg, message, &offer_hello_done, &p2); | 88 | GNUNET_TRANSPORT_offer_hello (p2.cfg, message, &offer_hello_done, &p2); |
88 | if ((p == &p2) && (NULL == p1.oh)) | 89 | if ((p == &p2) && (NULL == p1.oh)) |
89 | p1.oh = | 90 | p1.oh = |
90 | GNUNET_TRANSPORT_offer_hello(p1.cfg, message, &offer_hello_done, &p1); | 91 | GNUNET_TRANSPORT_offer_hello (p1.cfg, message, &offer_hello_done, &p1); |
91 | } | 92 | } |
92 | 93 | ||
93 | 94 | ||
94 | static void | 95 | static void |
95 | terminate_peer(struct PeerContext *p) | 96 | terminate_peer (struct PeerContext *p) |
96 | { | 97 | { |
97 | if (NULL != p->ch) | 98 | if (NULL != p->ch) |
98 | { | 99 | { |
99 | GNUNET_CORE_disconnect(p->ch); | 100 | GNUNET_CORE_disconnect (p->ch); |
100 | p->ch = NULL; | 101 | p->ch = NULL; |
101 | } | 102 | } |
102 | if (NULL != p->ghh) | 103 | if (NULL != p->ghh) |
103 | { | 104 | { |
104 | GNUNET_TRANSPORT_hello_get_cancel(p->ghh); | 105 | GNUNET_TRANSPORT_hello_get_cancel (p->ghh); |
105 | p->ghh = NULL; | 106 | p->ghh = NULL; |
106 | } | 107 | } |
107 | if (NULL != p->oh) | 108 | if (NULL != p->oh) |
108 | { | 109 | { |
109 | GNUNET_TRANSPORT_offer_hello_cancel(p->oh); | 110 | GNUNET_TRANSPORT_offer_hello_cancel (p->oh); |
110 | p->oh = NULL; | 111 | p->oh = NULL; |
111 | } | 112 | } |
112 | if (NULL != p->ats_sh) | 113 | if (NULL != p->ats_sh) |
113 | { | 114 | { |
114 | GNUNET_ATS_connectivity_suggest_cancel(p->ats_sh); | 115 | GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh); |
115 | p->ats_sh = NULL; | 116 | p->ats_sh = NULL; |
116 | } | 117 | } |
117 | if (NULL != p->ats) | 118 | if (NULL != p->ats) |
118 | { | 119 | { |
119 | GNUNET_ATS_connectivity_done(p->ats); | 120 | GNUNET_ATS_connectivity_done (p->ats); |
120 | p->ats = NULL; | 121 | p->ats = NULL; |
121 | } | 122 | } |
122 | } | 123 | } |
123 | 124 | ||
124 | 125 | ||
125 | static void | 126 | static void |
126 | terminate_task(void *cls) | 127 | terminate_task (void *cls) |
127 | { | 128 | { |
128 | GNUNET_assert(ok == 6); | 129 | GNUNET_assert (ok == 6); |
129 | terminate_peer(&p1); | 130 | terminate_peer (&p1); |
130 | terminate_peer(&p2); | 131 | terminate_peer (&p2); |
131 | ok = 0; | 132 | ok = 0; |
132 | } | 133 | } |
133 | 134 | ||
134 | 135 | ||
135 | static void | 136 | static void |
136 | terminate_task_error(void *cls) | 137 | terminate_task_error (void *cls) |
137 | { | 138 | { |
138 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "ENDING ANGRILY %u\n", ok); | 139 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ENDING ANGRILY %u\n", ok); |
139 | GNUNET_break(0); | 140 | GNUNET_break (0); |
140 | terminate_peer(&p1); | 141 | terminate_peer (&p1); |
141 | terminate_peer(&p2); | 142 | terminate_peer (&p2); |
142 | ok = 42; | 143 | ok = 42; |
143 | } | 144 | } |
144 | 145 | ||
145 | 146 | ||
146 | static void * | 147 | static void * |
147 | connect_notify(void *cls, | 148 | connect_notify (void *cls, |
148 | const struct GNUNET_PeerIdentity *peer, | 149 | const struct GNUNET_PeerIdentity *peer, |
149 | struct GNUNET_MQ_Handle *mq) | 150 | struct GNUNET_MQ_Handle *mq) |
150 | { | 151 | { |
151 | struct PeerContext *pc = cls; | 152 | struct PeerContext *pc = cls; |
152 | struct GNUNET_MQ_Envelope *env; | 153 | struct GNUNET_MQ_Envelope *env; |
153 | struct GNUNET_MessageHeader *msg; | 154 | struct GNUNET_MessageHeader *msg; |
154 | 155 | ||
155 | if (0 == memcmp(&pc->id, peer, sizeof(struct GNUNET_PeerIdentity))) | 156 | if (0 == memcmp (&pc->id, peer, sizeof(struct GNUNET_PeerIdentity))) |
156 | return (void *)peer; | 157 | return (void *) peer; |
157 | GNUNET_assert(pc->connect_status == 0); | 158 | GNUNET_assert (pc->connect_status == 0); |
158 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 159 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
159 | "Encrypted connection established to peer `%s'\n", | 160 | "Encrypted connection established to peer `%s'\n", |
160 | GNUNET_i2s(peer)); | 161 | GNUNET_i2s (peer)); |
161 | pc->connect_status = 1; | 162 | pc->connect_status = 1; |
162 | if (pc == &p1) | 163 | if (pc == &p1) |
163 | { | 164 | { |
164 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 165 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
165 | "Asking core (1) for transmission to peer `%s'\n", | 166 | "Asking core (1) for transmission to peer `%s'\n", |
166 | GNUNET_i2s(&p2.id)); | 167 | GNUNET_i2s (&p2.id)); |
167 | env = GNUNET_MQ_msg(msg, MTYPE); | 168 | env = GNUNET_MQ_msg (msg, MTYPE); |
168 | /* enable corking for this test */ | 169 | /* enable corking for this test */ |
169 | GNUNET_MQ_env_set_options(env, | 170 | GNUNET_MQ_env_set_options (env, |
170 | GNUNET_MQ_PRIO_BEST_EFFORT | | 171 | GNUNET_MQ_PRIO_BEST_EFFORT |
171 | GNUNET_MQ_PREF_CORK_ALLOWED); | 172 | | GNUNET_MQ_PREF_CORK_ALLOWED); |
172 | /* now actually transmit message */ | 173 | /* now actually transmit message */ |
173 | GNUNET_assert(ok == 4); | 174 | GNUNET_assert (ok == 4); |
174 | OKPP; | 175 | OKPP; |
175 | GNUNET_MQ_send(mq, env); | 176 | GNUNET_MQ_send (mq, env); |
176 | } | 177 | } |
177 | return (void *)peer; | 178 | return (void *) peer; |
178 | } | 179 | } |
179 | 180 | ||
180 | 181 | ||
181 | static void | 182 | static void |
182 | disconnect_notify(void *cls, | 183 | disconnect_notify (void *cls, |
183 | const struct GNUNET_PeerIdentity *peer, | 184 | const struct GNUNET_PeerIdentity *peer, |
184 | void *internal_cls) | 185 | void *internal_cls) |
185 | { | 186 | { |
186 | struct PeerContext *pc = cls; | 187 | struct PeerContext *pc = cls; |
187 | 188 | ||
188 | if (0 == memcmp(&pc->id, peer, sizeof(struct GNUNET_PeerIdentity))) | 189 | if (0 == memcmp (&pc->id, peer, sizeof(struct GNUNET_PeerIdentity))) |
189 | return; | 190 | return; |
190 | pc->connect_status = 0; | 191 | pc->connect_status = 0; |
191 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 192 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
192 | "Encrypted connection to `%s' cut\n", | 193 | "Encrypted connection to `%s' cut\n", |
193 | GNUNET_i2s(peer)); | 194 | GNUNET_i2s (peer)); |
194 | } | 195 | } |
195 | 196 | ||
196 | 197 | ||
197 | static void | 198 | static void |
198 | handle_test(void *cls, const struct GNUNET_MessageHeader *message) | 199 | handle_test (void *cls, const struct GNUNET_MessageHeader *message) |
199 | { | 200 | { |
200 | const struct GNUNET_PeerIdentity *peer = cls; | 201 | const struct GNUNET_PeerIdentity *peer = cls; |
201 | 202 | ||
202 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 203 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
203 | "Receiving message from `%s'.\n", | 204 | "Receiving message from `%s'.\n", |
204 | GNUNET_i2s(peer)); | 205 | GNUNET_i2s (peer)); |
205 | GNUNET_assert(ok == 5); | 206 | GNUNET_assert (ok == 5); |
206 | OKPP; | 207 | OKPP; |
207 | GNUNET_SCHEDULER_cancel(err_task); | 208 | GNUNET_SCHEDULER_cancel (err_task); |
208 | err_task = GNUNET_SCHEDULER_add_now(&terminate_task, NULL); | 209 | err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL); |
209 | } | 210 | } |
210 | 211 | ||
211 | 212 | ||
212 | static void | 213 | static void |
213 | init_notify(void *cls, const struct GNUNET_PeerIdentity *my_identity) | 214 | init_notify (void *cls, const struct GNUNET_PeerIdentity *my_identity) |
214 | { | 215 | { |
215 | struct PeerContext *p = cls; | 216 | struct PeerContext *p = cls; |
216 | struct GNUNET_MQ_MessageHandler handlers[] = | 217 | struct GNUNET_MQ_MessageHandler handlers[] = |
217 | { GNUNET_MQ_hd_fixed_size(test, MTYPE, struct GNUNET_MessageHeader, NULL), | 218 | { GNUNET_MQ_hd_fixed_size (test, MTYPE, struct GNUNET_MessageHeader, NULL), |
218 | GNUNET_MQ_handler_end() }; | 219 | GNUNET_MQ_handler_end () }; |
219 | 220 | ||
220 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 221 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
221 | "Core connection to `%s' established\n", | 222 | "Core connection to `%s' established\n", |
222 | GNUNET_i2s(my_identity)); | 223 | GNUNET_i2s (my_identity)); |
223 | p->id = *my_identity; | 224 | p->id = *my_identity; |
224 | if (cls == &p1) | 225 | if (cls == &p1) |
225 | { | 226 | { |
226 | GNUNET_assert(ok == 2); | 227 | GNUNET_assert (ok == 2); |
227 | OKPP; | 228 | OKPP; |
228 | /* connect p2 */ | 229 | /* connect p2 */ |
229 | p2.ch = GNUNET_CORE_connect(p2.cfg, | 230 | p2.ch = GNUNET_CORE_connect (p2.cfg, |
230 | &p2, | 231 | &p2, |
231 | &init_notify, | 232 | &init_notify, |
232 | &connect_notify, | 233 | &connect_notify, |
233 | &disconnect_notify, | 234 | &disconnect_notify, |
234 | handlers); | 235 | handlers); |
235 | } | 236 | } |
236 | else | 237 | else |
237 | { | 238 | { |
238 | GNUNET_assert(ok == 3); | 239 | GNUNET_assert (ok == 3); |
239 | OKPP; | 240 | OKPP; |
240 | GNUNET_assert(cls == &p2); | 241 | GNUNET_assert (cls == &p2); |
241 | p1.ats_sh = GNUNET_ATS_connectivity_suggest(p1.ats, &p2.id, 1); | 242 | p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats, &p2.id, 1); |
242 | } | 243 | } |
243 | } | 244 | } |
244 | 245 | ||
245 | 246 | ||
246 | static void | 247 | static void |
247 | setup_peer(struct PeerContext *p, const char *cfgname) | 248 | setup_peer (struct PeerContext *p, const char *cfgname) |
248 | { | 249 | { |
249 | char *binary; | 250 | char *binary; |
250 | 251 | ||
251 | binary = GNUNET_OS_get_libexec_binary_path("gnunet-service-arm"); | 252 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm"); |
252 | p->cfg = GNUNET_CONFIGURATION_create(); | 253 | p->cfg = GNUNET_CONFIGURATION_create (); |
253 | p->arm_proc = GNUNET_OS_start_process(GNUNET_YES, | 254 | p->arm_proc = GNUNET_OS_start_process (GNUNET_YES, |
254 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | 255 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, |
255 | NULL, | 256 | NULL, |
256 | NULL, | 257 | NULL, |
257 | NULL, | 258 | NULL, |
258 | binary, | 259 | binary, |
259 | "gnunet-service-arm", | 260 | "gnunet-service-arm", |
260 | "-c", | 261 | "-c", |
261 | cfgname, | 262 | cfgname, |
262 | NULL); | 263 | NULL); |
263 | GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(p->cfg, cfgname)); | 264 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); |
264 | p->ats = GNUNET_ATS_connectivity_init(p->cfg); | 265 | p->ats = GNUNET_ATS_connectivity_init (p->cfg); |
265 | GNUNET_assert(NULL != p->ats); | 266 | GNUNET_assert (NULL != p->ats); |
266 | p->ghh = GNUNET_TRANSPORT_hello_get(p->cfg, | 267 | p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg, |
267 | GNUNET_TRANSPORT_AC_ANY, | 268 | GNUNET_TRANSPORT_AC_ANY, |
268 | &process_hello, | 269 | &process_hello, |
269 | p); | 270 | p); |
270 | GNUNET_free(binary); | 271 | GNUNET_free (binary); |
271 | } | 272 | } |
272 | 273 | ||
273 | 274 | ||
274 | static void | 275 | static void |
275 | run(void *cls, | 276 | run (void *cls, |
276 | char *const *args, | 277 | char *const *args, |
277 | const char *cfgfile, | 278 | const char *cfgfile, |
278 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 279 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
279 | { | 280 | { |
280 | struct GNUNET_MQ_MessageHandler handlers[] = | 281 | struct GNUNET_MQ_MessageHandler handlers[] = |
281 | { GNUNET_MQ_hd_fixed_size(test, MTYPE, struct GNUNET_MessageHeader, NULL), | 282 | { GNUNET_MQ_hd_fixed_size (test, MTYPE, struct GNUNET_MessageHeader, NULL), |
282 | GNUNET_MQ_handler_end() }; | 283 | GNUNET_MQ_handler_end () }; |
283 | 284 | ||
284 | GNUNET_assert(ok == 1); | 285 | GNUNET_assert (ok == 1); |
285 | OKPP; | 286 | OKPP; |
286 | setup_peer(&p1, "test_core_api_peer1.conf"); | 287 | setup_peer (&p1, "test_core_api_peer1.conf"); |
287 | setup_peer(&p2, "test_core_api_peer2.conf"); | 288 | setup_peer (&p2, "test_core_api_peer2.conf"); |
288 | err_task = GNUNET_SCHEDULER_add_delayed( | 289 | err_task = GNUNET_SCHEDULER_add_delayed ( |
289 | GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 300), | 290 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300), |
290 | &terminate_task_error, | 291 | &terminate_task_error, |
291 | NULL); | 292 | NULL); |
292 | p1.ch = GNUNET_CORE_connect(p1.cfg, | 293 | p1.ch = GNUNET_CORE_connect (p1.cfg, |
293 | &p1, | 294 | &p1, |
294 | &init_notify, | 295 | &init_notify, |
295 | &connect_notify, | 296 | &connect_notify, |
296 | &disconnect_notify, | 297 | &disconnect_notify, |
297 | handlers); | 298 | handlers); |
298 | } | 299 | } |
299 | 300 | ||
300 | 301 | ||
301 | static void | 302 | static void |
302 | stop_arm(struct PeerContext *p) | 303 | stop_arm (struct PeerContext *p) |
303 | { | 304 | { |
304 | if (0 != GNUNET_OS_process_kill(p->arm_proc, GNUNET_TERM_SIG)) | 305 | if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG)) |
305 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); | 306 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); |
306 | if (GNUNET_OK != GNUNET_OS_process_wait(p->arm_proc)) | 307 | if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc)) |
307 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "waitpid"); | 308 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); |
308 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 309 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
309 | "ARM process %u stopped\n", | 310 | "ARM process %u stopped\n", |
310 | GNUNET_OS_process_get_pid(p->arm_proc)); | 311 | GNUNET_OS_process_get_pid (p->arm_proc)); |
311 | GNUNET_OS_process_destroy(p->arm_proc); | 312 | GNUNET_OS_process_destroy (p->arm_proc); |
312 | p->arm_proc = NULL; | 313 | p->arm_proc = NULL; |
313 | GNUNET_CONFIGURATION_destroy(p->cfg); | 314 | GNUNET_CONFIGURATION_destroy (p->cfg); |
314 | } | 315 | } |
315 | 316 | ||
316 | 317 | ||
317 | int | 318 | int |
318 | main(int argc, char *argv1[]) | 319 | main (int argc, char *argv1[]) |
319 | { | 320 | { |
320 | char *const argv[] = { "test-core-api", "-c", "test_core_api_data.conf", NULL }; | 321 | char *const argv[] = { "test-core-api", "-c", "test_core_api_data.conf", |
321 | struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END }; | 322 | NULL }; |
323 | struct GNUNET_GETOPT_CommandLineOption options[] = | ||
324 | { GNUNET_GETOPT_OPTION_END }; | ||
322 | 325 | ||
323 | ok = 1; | 326 | ok = 1; |
324 | GNUNET_log_setup("test-core-api", "WARNING", NULL); | 327 | GNUNET_log_setup ("test-core-api", "WARNING", NULL); |
325 | GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *)) - 1, | 328 | GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, |
326 | argv, | 329 | argv, |
327 | "test-core-api", | 330 | "test-core-api", |
328 | "nohelp", | 331 | "nohelp", |
329 | options, | 332 | options, |
330 | &run, | 333 | &run, |
331 | &ok); | 334 | &ok); |
332 | stop_arm(&p1); | 335 | stop_arm (&p1); |
333 | stop_arm(&p2); | 336 | stop_arm (&p2); |
334 | GNUNET_DISK_directory_remove("/tmp/test-gnunet-core-peer-1"); | 337 | GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1"); |
335 | GNUNET_DISK_directory_remove("/tmp/test-gnunet-core-peer-2"); | 338 | GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2"); |
336 | 339 | ||
337 | return ok; | 340 | return ok; |
338 | } | 341 | } |