5e8448880c53784711be98c7a27211c4987b85e9
[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 GNUNET_SCHEDULER_TaskIdentifier irc_task;
70
71 static GNUNET_SCHEDULER_TaskIdentifier ask_task;
72
73 static unsigned int total_reserve = 5;
74
75
76
77 static void
78 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80 #if VERBOSE
81   fprintf(stderr, "Regular shutdown\n");
82 #endif
83   GNUNET_assert (ok == 6);
84   GNUNET_assert (NULL == irc);
85   GNUNET_CORE_disconnect (p1.ch);
86   GNUNET_CORE_disconnect (p2.ch);
87   GNUNET_TRANSPORT_disconnect (p1.th);
88   GNUNET_TRANSPORT_disconnect (p2.th);
89   ok = 0;
90 }
91
92
93 static void
94 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   err_task = GNUNET_SCHEDULER_NO_TASK;
97 #if VERBOSE
98   fprintf(stderr, "ENDING ANGRILY %u\n", ok);
99 #endif
100   GNUNET_break (0);
101   if (NULL != irc)
102     GNUNET_CORE_peer_change_preference_cancel (irc);
103   if (GNUNET_SCHEDULER_NO_TASK != irc_task)
104     GNUNET_SCHEDULER_cancel (irc_task);
105   if (GNUNET_SCHEDULER_NO_TASK != ask_task)
106     {
107       GNUNET_SCHEDULER_cancel (ask_task);
108       ask_task = GNUNET_SCHEDULER_NO_TASK;
109     }
110   GNUNET_CORE_disconnect (p1.ch);
111   GNUNET_CORE_disconnect (p2.ch);
112   GNUNET_TRANSPORT_disconnect (p1.th);
113   GNUNET_TRANSPORT_disconnect (p2.th);
114   ok = 42;
115 }
116
117
118 static size_t
119 transmit_ready (void *cls, size_t size, void *buf)
120 {
121   struct PeerContext *p = cls;
122   struct GNUNET_MessageHeader *m;
123
124   GNUNET_assert (ok == 4);
125   OKPP;
126   GNUNET_assert (p == &p1);
127   GNUNET_assert (buf != NULL);
128   m = (struct GNUNET_MessageHeader *) buf;
129   m->type = htons (MTYPE);
130   m->size = htons (sizeof (struct GNUNET_MessageHeader));
131   err_task = 
132     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120), 
133                                   &terminate_task_error, NULL);
134
135   return sizeof (struct GNUNET_MessageHeader);
136 }
137
138 static void
139 preference_cb (void *cls,
140                const struct
141                GNUNET_PeerIdentity *peer,
142                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
143                int32_t amount,
144                struct GNUNET_TIME_Relative res_delay,
145                uint64_t preference);
146
147 static void
148 do_reserve (void *cls,
149             const struct GNUNET_SCHEDULER_TaskContext *tc)
150 {
151   struct PeerContext *pc = cls;
152
153   irc_task = GNUNET_SCHEDULER_NO_TASK;
154   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
155     {
156       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157                   "Aborted during attempt to request reservation\n");
158       return;
159     }
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161               "Requesting reservatin of 32k from core in 1s!\n");
162   irc = GNUNET_CORE_peer_change_preference (p1.ch,
163                                             &p2.id,
164                                             GNUNET_TIME_UNIT_SECONDS,
165                                             GNUNET_BANDWIDTH_VALUE_MAX,
166                                             1000000 /* bandwidth for 1s */,
167                                             0,
168                                             &preference_cb,
169                                             pc);
170 }
171
172 static void
173 preference_cb (void *cls,
174                const struct
175                GNUNET_PeerIdentity *peer,
176                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
177                int32_t amount,
178                struct GNUNET_TIME_Relative res_delay,
179                uint64_t preference)
180 {
181   struct PeerContext *pc = cls;
182
183   irc = NULL;
184   if (0 == amount)
185     {
186       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187                   "Delaying reservation request by %llu ms!\n",
188                   (unsigned long long) res_delay.rel_value);
189       irc_task = GNUNET_SCHEDULER_add_delayed (res_delay,
190                                                &do_reserve, pc);
191       return;
192     }
193   total_reserve--;
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195               "Reservation done!\n");
196   if (total_reserve > 0)
197     {
198       irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
199       return;
200     }
201   GNUNET_SCHEDULER_cancel (err_task);
202   err_task = GNUNET_SCHEDULER_NO_TASK;
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204               "Asking core (1) for transmission to peer `%4s'\n",
205               GNUNET_i2s (&p2.id));
206   if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
207                                                  GNUNET_YES,
208                                                  0,
209                                                  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
210                                                  &p2.id,
211                                                  sizeof (struct GNUNET_MessageHeader),
212                                                  &transmit_ready, &p1))
213     {
214       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215                   "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
216                   GNUNET_i2s (&p2.id));
217     }    
218 }
219
220
221 static void
222 connect_notify (void *cls,
223                 const struct GNUNET_PeerIdentity *peer,
224                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
225 {
226   struct PeerContext *pc = cls;
227
228   if (0 == memcmp (&pc->id,
229                    peer,
230                    sizeof (struct GNUNET_PeerIdentity)))
231     return;
232   GNUNET_assert (pc->connect_status == 0);
233   pc->connect_status = 1;
234   if (pc == &p1)
235     {
236       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237                   "Encrypted connection established to peer `%4s'\n",
238                   GNUNET_i2s (peer));
239       if (GNUNET_SCHEDULER_NO_TASK != ask_task)
240         {
241           GNUNET_SCHEDULER_cancel (ask_task);
242           ask_task = GNUNET_SCHEDULER_NO_TASK;
243         }
244       GNUNET_SCHEDULER_cancel (err_task);
245       err_task = 
246         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120), 
247                                       &terminate_task_error, NULL);
248       irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
249     }
250 }
251
252
253 static void
254 disconnect_notify (void *cls,
255                    const struct GNUNET_PeerIdentity *peer)
256 {
257   struct PeerContext *pc = cls;
258
259   if (0 == memcmp (&pc->id,
260                    peer,
261                    sizeof (struct GNUNET_PeerIdentity)))
262     return;
263   pc->connect_status = 0;
264   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
266 }
267
268
269 static int
270 inbound_notify (void *cls,
271                 const struct GNUNET_PeerIdentity *other,
272                 const struct GNUNET_MessageHeader *message,
273                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
274 {
275   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
277   return GNUNET_OK;
278 }
279
280
281 static int
282 outbound_notify (void *cls,
283                  const struct GNUNET_PeerIdentity *other,
284                  const struct GNUNET_MessageHeader *message,
285                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
286 {
287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288               "Core notifies about outbound data for `%4s'.\n",
289               GNUNET_i2s (other));
290   return GNUNET_OK;
291 }
292
293
294
295 static int
296 process_mtype (void *cls,
297                const struct GNUNET_PeerIdentity *peer,
298                const struct GNUNET_MessageHeader *message,
299                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
300 {
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302               "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
303   GNUNET_assert (ok == 5);
304   OKPP;
305   GNUNET_SCHEDULER_cancel (err_task);
306   err_task = GNUNET_SCHEDULER_NO_TASK;
307   GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
308   return GNUNET_OK;
309 }
310
311
312 static struct GNUNET_CORE_MessageHandler handlers[] = {
313   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
314   {NULL, 0, 0}
315 };
316
317
318
319 static void 
320 ask_connect_task (void *cls,
321                   const struct GNUNET_SCHEDULER_TaskContext *tc)
322 {
323   if (GNUNET_SCHEDULER_NO_TASK != ask_task)
324     GNUNET_SCHEDULER_cancel (ask_task);
325   ask_task =
326     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
327                                   &ask_connect_task, NULL);
328   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329               "Asking core (1) AGAIN to connect to peer `%4s'\n",
330               GNUNET_i2s (&p2.id));
331   GNUNET_CORE_peer_request_connect (p1.ch,
332                                     &p2.id,
333                                     NULL, NULL);
334 }
335
336
337 static void
338 init_notify (void *cls,
339              struct GNUNET_CORE_Handle *server,
340              const struct GNUNET_PeerIdentity *my_identity,
341              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
342 {
343   struct PeerContext *p = cls;
344
345   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346               "Core connection to `%4s' established\n",
347               GNUNET_i2s (my_identity));
348   GNUNET_assert (server != NULL);
349   p->id = *my_identity;
350   p->ch = server;
351   if (cls == &p1)
352     {
353       GNUNET_assert (ok == 2);
354       OKPP;
355       /* connect p2 */
356       GNUNET_CORE_connect (p2.cfg, 1,
357                            &p2,
358                            &init_notify,                         
359                            &connect_notify,
360                            &disconnect_notify,
361                            NULL,
362                            &inbound_notify,
363                            GNUNET_YES,
364                            &outbound_notify, GNUNET_YES, handlers);
365     }
366   else
367     {
368       GNUNET_assert (ok == 3);
369       OKPP;
370       GNUNET_assert (cls == &p2);
371       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372                   "Asking core (1) to connect to peer `%4s'\n",
373                   GNUNET_i2s (&p2.id));
374       err_task = 
375         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), 
376                                       &terminate_task_error, NULL);
377       if (GNUNET_SCHEDULER_NO_TASK != ask_task)
378         GNUNET_SCHEDULER_cancel (ask_task);
379       ask_task =
380         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
381                                       &ask_connect_task, NULL);
382       GNUNET_CORE_peer_request_connect (p1.ch,
383                                         &p2.id,
384                                         NULL, NULL);
385     }
386 }
387
388
389 static void
390 process_hello (void *cls,
391                const struct GNUNET_MessageHeader *message)
392 {
393   struct PeerContext *p = cls;
394
395   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
396   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397               "Received (my) `%s' from transport service\n",
398               "HELLO");
399   GNUNET_assert (message != NULL);
400   p->hello = GNUNET_malloc (ntohs (message->size));
401   memcpy (p->hello, message, ntohs (message->size));
402   if ((p == &p1) && (p2.th != NULL))
403     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
404   if ((p == &p2) && (p1.th != NULL))
405     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
406
407   if ((p == &p1) && (p2.hello != NULL))
408     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
409   if ((p == &p2) && (p1.hello != NULL))
410     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
411 }
412
413
414
415 static void
416 setup_peer (struct PeerContext *p, const char *cfgname)
417 {
418   p->cfg = GNUNET_CONFIGURATION_create ();
419 #if START_ARM
420   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
421                                         "gnunet-service-arm",
422 #if VERBOSE
423                                         "-L", "DEBUG",
424 #endif
425                                         "-c", cfgname, NULL);
426 #endif
427   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
428   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
429   GNUNET_assert (p->th != NULL);
430   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
431 }
432
433
434 static void
435 run (void *cls,
436      char *const *args,
437      const char *cfgfile,
438      const struct GNUNET_CONFIGURATION_Handle *cfg)
439 {
440   GNUNET_assert (ok == 1);
441   OKPP;
442   setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
443   setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
444   GNUNET_CORE_connect (p1.cfg, 1,
445                        &p1,
446                        &init_notify,
447                        &connect_notify,
448                        &disconnect_notify,
449                        NULL,
450                        &inbound_notify,
451                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
452 }
453
454 static void
455 stop_arm (struct PeerContext *p)
456 {
457 #if START_ARM
458   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
459     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
460   if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
461     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
462   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
464   GNUNET_OS_process_close (p->arm_proc);
465   p->arm_proc = NULL;
466 #endif
467   GNUNET_CONFIGURATION_destroy (p->cfg);
468 }
469
470 static int
471 check ()
472 {
473   char *const argv[] = { "test-core-api-preferences",
474     "-c",
475     "test_core_api_data.conf",
476 #if VERBOSE
477     "-L", "DEBUG",
478 #endif
479     NULL
480   };
481   struct GNUNET_GETOPT_CommandLineOption options[] = {
482     GNUNET_GETOPT_OPTION_END
483   };
484   ok = 1;
485   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
486                       argv, "test-core-api-preferences", "nohelp", options, &run, &ok);
487   stop_arm (&p1);
488   stop_arm (&p2);
489   return ok;
490 }
491
492 int
493 main (int argc, char *argv[])
494 {
495   int ret;
496
497   GNUNET_log_setup ("test-core-api",
498 #if VERBOSE
499                     "DEBUG",
500 #else
501                     "WARNING",
502 #endif
503                     NULL);
504   ret = check ();
505   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1"); 
506   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
507
508   return ret;
509 }
510
511 /* end of test_core_api_preferences.c */