indentation
[oweals/gnunet.git] / src / core / test_core_api_preferences.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file core/test_core_api_preferences.c
22  * @brief testcase for core_api.c's GNUNET_CORE_change_preference
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_scheduler_lib.h"
32 #include "gnunet_transport_service.h"
33
34 #define VERBOSE GNUNET_NO
35
36 #define START_ARM GNUNET_YES
37
38 #define MTYPE 12345
39
40 struct PeerContext
41 {
42   struct GNUNET_CONFIGURATION_Handle *cfg;
43   struct GNUNET_CORE_Handle *ch;
44   struct GNUNET_PeerIdentity id;
45   struct GNUNET_TRANSPORT_Handle *th;
46   struct GNUNET_MessageHeader *hello;
47   int connect_status;
48 #if START_ARM
49   struct GNUNET_OS_Process *arm_proc;
50 #endif
51 };
52
53 static struct PeerContext p1;
54
55 static struct PeerContext p2;
56
57 static GNUNET_SCHEDULER_TaskIdentifier err_task;
58
59 static int ok;
60
61 #if VERBOSE
62 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
63 #else
64 #define OKPP do { ok++; } while (0)
65 #endif
66
67 static struct GNUNET_CORE_InformationRequestContext *irc;
68
69 static struct GNUNET_CORE_TransmitHandle *th;
70
71 static GNUNET_SCHEDULER_TaskIdentifier irc_task;
72
73 static GNUNET_SCHEDULER_TaskIdentifier ask_task;
74
75 static unsigned int total_reserve = 5;
76
77
78 static void process_hello (void *cls,
79                            const struct GNUNET_MessageHeader *message);
80
81 static void
82 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 {
84 #if VERBOSE
85   fprintf (stderr, "Regular shutdown\n");
86 #endif
87   GNUNET_assert (ok == 6);
88   GNUNET_assert (NULL == irc);
89   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &process_hello, &p1);
90   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &process_hello, &p2);
91   GNUNET_CORE_disconnect (p1.ch);
92   GNUNET_CORE_disconnect (p2.ch);
93   GNUNET_TRANSPORT_disconnect (p1.th);
94   GNUNET_TRANSPORT_disconnect (p2.th);
95   ok = 0;
96 }
97
98
99 static void
100 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   err_task = GNUNET_SCHEDULER_NO_TASK;
103 #if VERBOSE
104   fprintf (stderr, "ENDING ANGRILY %u\n", ok);
105 #endif
106   GNUNET_break (0);
107   if (NULL != irc)
108   {
109     GNUNET_CORE_peer_change_preference_cancel (irc);
110     irc = NULL;
111   }
112   if (GNUNET_SCHEDULER_NO_TASK != irc_task)
113   {
114     GNUNET_SCHEDULER_cancel (irc_task);
115     irc_task = GNUNET_SCHEDULER_NO_TASK;
116   }
117   if (GNUNET_SCHEDULER_NO_TASK != ask_task)
118   {
119     GNUNET_SCHEDULER_cancel (ask_task);
120     ask_task = GNUNET_SCHEDULER_NO_TASK;
121   }
122   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &process_hello, &p1);
123   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &process_hello, &p2);
124   GNUNET_CORE_disconnect (p1.ch);
125   GNUNET_CORE_disconnect (p2.ch);
126   GNUNET_TRANSPORT_disconnect (p1.th);
127   GNUNET_TRANSPORT_disconnect (p2.th);
128   ok = 42;
129 }
130
131
132 static size_t
133 transmit_ready (void *cls, size_t size, void *buf)
134 {
135   struct PeerContext *p = cls;
136   struct GNUNET_MessageHeader *m;
137
138   th = NULL;
139   GNUNET_assert (ok == 4);
140   OKPP;
141   GNUNET_assert (p == &p1);
142   GNUNET_assert (buf != NULL);
143   m = (struct GNUNET_MessageHeader *) buf;
144   m->type = htons (MTYPE);
145   m->size = htons (sizeof (struct GNUNET_MessageHeader));
146   err_task =
147       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
148                                     (GNUNET_TIME_UNIT_SECONDS, 120),
149                                     &terminate_task_error, NULL);
150
151   return sizeof (struct GNUNET_MessageHeader);
152 }
153
154 static void
155 preference_cb (void *cls,
156                const struct
157                GNUNET_PeerIdentity *peer,
158                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
159                int32_t amount,
160                struct GNUNET_TIME_Relative res_delay, uint64_t preference);
161
162 static void
163 do_reserve (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 {
165   struct PeerContext *pc = cls;
166
167   irc_task = GNUNET_SCHEDULER_NO_TASK;
168   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
169   {
170     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171                 "Aborted during attempt to request reservation\n");
172     return;
173   }
174   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175               "Requesting reservatin of 32k from core in 1s!\n");
176   irc = GNUNET_CORE_peer_change_preference (p1.ch,
177                                             &p2.id,
178                                             GNUNET_TIME_UNIT_SECONDS,
179                                             GNUNET_BANDWIDTH_VALUE_MAX,
180                                             1000000 /* bandwidth for 1s */ ,
181                                             0, &preference_cb, pc);
182 }
183
184 static void
185 preference_cb (void *cls,
186                const struct
187                GNUNET_PeerIdentity *peer,
188                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
189                int32_t amount,
190                struct GNUNET_TIME_Relative res_delay, uint64_t preference)
191 {
192   struct PeerContext *pc = cls;
193
194   irc = NULL;
195   if (0 == amount)
196   {
197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198                 "Delaying reservation request by %llu ms!\n",
199                 (unsigned long long) res_delay.rel_value);
200     irc_task = GNUNET_SCHEDULER_add_delayed (res_delay, &do_reserve, pc);
201     return;
202   }
203   total_reserve--;
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation done!\n");
205   if (total_reserve > 0)
206   {
207     irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
208     return;
209   }
210   GNUNET_SCHEDULER_cancel (err_task);
211   err_task = GNUNET_SCHEDULER_NO_TASK;
212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213               "Asking core (1) for transmission to peer `%4s'\n",
214               GNUNET_i2s (&p2.id));
215   if (NULL == (th = GNUNET_CORE_notify_transmit_ready (p1.ch,
216                                                        GNUNET_YES,
217                                                        0,
218                                                        GNUNET_TIME_relative_multiply
219                                                        (GNUNET_TIME_UNIT_SECONDS,
220                                                         45), &p2.id,
221                                                        sizeof (struct
222                                                                GNUNET_MessageHeader),
223                                                        &transmit_ready, &p1)))
224   {
225     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226                 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
227                 GNUNET_i2s (&p2.id));
228   }
229 }
230
231
232 static void
233 connect_notify (void *cls,
234                 const struct GNUNET_PeerIdentity *peer,
235                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
236 {
237   struct PeerContext *pc = cls;
238
239   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
240     return;
241   GNUNET_assert (pc->connect_status == 0);
242   pc->connect_status = 1;
243   if (pc == &p1)
244   {
245     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246                 "Encrypted connection established to peer `%4s'\n",
247                 GNUNET_i2s (peer));
248     if (GNUNET_SCHEDULER_NO_TASK != ask_task)
249     {
250       GNUNET_SCHEDULER_cancel (ask_task);
251       ask_task = GNUNET_SCHEDULER_NO_TASK;
252     }
253     GNUNET_SCHEDULER_cancel (err_task);
254     err_task =
255         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
256                                       (GNUNET_TIME_UNIT_SECONDS, 120),
257                                       &terminate_task_error, NULL);
258     irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
259   }
260 }
261
262
263 static void
264 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
265 {
266   struct PeerContext *pc = cls;
267
268   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
269     return;
270   pc->connect_status = 0;
271   if (GNUNET_SCHEDULER_NO_TASK != irc_task)
272   {
273     GNUNET_SCHEDULER_cancel (irc_task);
274     irc_task = GNUNET_SCHEDULER_NO_TASK;
275   }
276   if (0 == memcmp (peer, &p1.id, sizeof (struct GNUNET_PeerIdentity)))
277   {
278     if (irc != NULL)
279     {
280       GNUNET_CORE_peer_change_preference_cancel (irc);
281       irc = NULL;
282     }
283     if (th != NULL)
284     {
285       GNUNET_CORE_notify_transmit_ready_cancel (th);
286       th = NULL;
287     }
288   }
289   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
291 }
292
293
294 static int
295 inbound_notify (void *cls,
296                 const struct GNUNET_PeerIdentity *other,
297                 const struct GNUNET_MessageHeader *message,
298                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
299 {
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
302   return GNUNET_OK;
303 }
304
305
306 static int
307 outbound_notify (void *cls,
308                  const struct GNUNET_PeerIdentity *other,
309                  const struct GNUNET_MessageHeader *message,
310                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
311 {
312   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313               "Core notifies about outbound data for `%4s'.\n",
314               GNUNET_i2s (other));
315   return GNUNET_OK;
316 }
317
318
319
320 static int
321 process_mtype (void *cls,
322                const struct GNUNET_PeerIdentity *peer,
323                const struct GNUNET_MessageHeader *message,
324                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
325 {
326   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327               "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
328   GNUNET_assert (ok == 5);
329   OKPP;
330   GNUNET_SCHEDULER_cancel (err_task);
331   err_task = GNUNET_SCHEDULER_NO_TASK;
332   GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
333   return GNUNET_OK;
334 }
335
336
337 static struct GNUNET_CORE_MessageHandler handlers[] = {
338   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
339   {NULL, 0, 0}
340 };
341
342
343
344 static void
345 ask_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 {
347   ask_task =
348       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
349                                     &ask_connect_task, NULL);
350   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351               "Asking core (1) AGAIN to connect to peer `%4s'\n",
352               GNUNET_i2s (&p2.id));
353   GNUNET_CORE_peer_request_connect (p1.ch, &p2.id, NULL, NULL);
354 }
355
356
357 static void
358 init_notify (void *cls,
359              struct GNUNET_CORE_Handle *server,
360              const struct GNUNET_PeerIdentity *my_identity,
361              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
362 {
363   struct PeerContext *p = cls;
364
365   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366               "Core connection to `%4s' established\n",
367               GNUNET_i2s (my_identity));
368   GNUNET_assert (server != NULL);
369   p->id = *my_identity;
370   p->ch = server;
371   if (cls == &p1)
372   {
373     GNUNET_assert (ok == 2);
374     OKPP;
375     /* connect p2 */
376     GNUNET_CORE_connect (p2.cfg, 1,
377                          &p2,
378                          &init_notify,
379                          &connect_notify,
380                          &disconnect_notify,
381                          NULL,
382                          &inbound_notify,
383                          GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
384   }
385   else
386   {
387     GNUNET_assert (ok == 3);
388     OKPP;
389     GNUNET_assert (cls == &p2);
390     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391                 "Asking core (1) to connect to peer `%4s'\n",
392                 GNUNET_i2s (&p2.id));
393     err_task =
394         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
395                                       (GNUNET_TIME_UNIT_SECONDS, 60),
396                                       &terminate_task_error, NULL);
397     if (GNUNET_SCHEDULER_NO_TASK != ask_task)
398       GNUNET_SCHEDULER_cancel (ask_task);
399     ask_task =
400         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
401                                       &ask_connect_task, NULL);
402     GNUNET_CORE_peer_request_connect (p1.ch, &p2.id, NULL, NULL);
403   }
404 }
405
406
407 static void
408 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
409 {
410   struct PeerContext *p = cls;
411
412   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413               "Received (my) `%s' from transport service\n", "HELLO");
414   GNUNET_assert (message != NULL);
415   p->hello = GNUNET_malloc (ntohs (message->size));
416   memcpy (p->hello, message, ntohs (message->size));
417   if ((p == &p1) && (p2.th != NULL))
418     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
419   if ((p == &p2) && (p1.th != NULL))
420     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
421
422   if ((p == &p1) && (p2.hello != NULL) && (p1.th != NULL))
423     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
424   if ((p == &p2) && (p1.hello != NULL) && (p2.th != NULL))
425     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
426 }
427
428
429
430 static void
431 setup_peer (struct PeerContext *p, const char *cfgname)
432 {
433   p->cfg = GNUNET_CONFIGURATION_create ();
434 #if START_ARM
435   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
436                                          "gnunet-service-arm",
437 #if VERBOSE
438                                          "-L", "DEBUG",
439 #endif
440                                          "-c", cfgname, NULL);
441 #endif
442   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
443   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
444   GNUNET_assert (p->th != NULL);
445   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
446 }
447
448
449 static void
450 run (void *cls,
451      char *const *args,
452      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
453 {
454   GNUNET_assert (ok == 1);
455   OKPP;
456   setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
457   setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
458   GNUNET_CORE_connect (p1.cfg, 1,
459                        &p1,
460                        &init_notify,
461                        &connect_notify,
462                        &disconnect_notify,
463                        NULL,
464                        &inbound_notify,
465                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
466 }
467
468 static void
469 stop_arm (struct PeerContext *p)
470 {
471 #if START_ARM
472   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
473     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
474   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
475     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
476   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477               "ARM process %u stopped\n",
478               GNUNET_OS_process_get_pid (p->arm_proc));
479   GNUNET_OS_process_close (p->arm_proc);
480   p->arm_proc = NULL;
481 #endif
482   GNUNET_CONFIGURATION_destroy (p->cfg);
483 }
484
485 static int
486 check ()
487 {
488   char *const argv[] = { "test-core-api-preferences",
489     "-c",
490     "test_core_api_data.conf",
491 #if VERBOSE
492     "-L", "DEBUG",
493 #endif
494     NULL
495   };
496   struct GNUNET_GETOPT_CommandLineOption options[] = {
497     GNUNET_GETOPT_OPTION_END
498   };
499   ok = 1;
500   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
501                       argv, "test-core-api-preferences", "nohelp", options,
502                       &run, &ok);
503   stop_arm (&p1);
504   stop_arm (&p2);
505   return ok;
506 }
507
508 int
509 main (int argc, char *argv[])
510 {
511   int ret;
512
513   GNUNET_log_setup ("test-core-api",
514 #if VERBOSE
515                     "DEBUG",
516 #else
517                     "WARNING",
518 #endif
519                     NULL);
520   ret = check ();
521   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
522   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
523
524   return ret;
525 }
526
527 /* end of test_core_api_preferences.c */