modifications
[oweals/gnunet.git] / src / ats / test_ats_api_performance.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 ats/test_ats_api_performance.c
22  * @brief test adding addresses in automatic transport selection performance API
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
29 #include "ats.h"
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
34
35 struct GNUNET_CONFIGURATION_Handle *cfg;
36
37 static struct GNUNET_ATS_SchedulingHandle *atsh;
38 static struct GNUNET_ATS_PerformanceHandle *ph;
39 struct GNUNET_ATS_AddressListHandle* phal;
40
41 static int ret;
42
43 struct Address
44 {
45   char *plugin;
46   size_t plugin_len;
47
48   void *addr;
49   size_t addr_len;
50
51   struct GNUNET_ATS_Information *ats;
52   int ats_count;
53
54   void *session;
55 };
56
57 struct PeerContext
58 {
59   struct GNUNET_PeerIdentity id;
60
61   struct Address *addr;
62 };
63
64
65
66 static struct PeerContext p[2];
67
68 static struct Address p0_addresses[2];
69 static struct Address p1_addresses[2];
70
71 struct GNUNET_HELLO_Address p0_ha[2];
72 struct GNUNET_HELLO_Address p1_ha[2];
73 struct GNUNET_HELLO_Address *s_ha[2];
74
75 static unsigned int stage = 0;
76
77 static void
78 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80   die_task = GNUNET_SCHEDULER_NO_TASK;
81
82   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout in stage %u\n", stage);
83
84   if (NULL != atsh)
85   GNUNET_ATS_scheduling_done (atsh);
86   if (phal != NULL)
87     GNUNET_ATS_performance_list_addresses_cancel (phal);
88   phal = NULL;
89   if (ph != NULL)
90     GNUNET_ATS_performance_done (ph);
91   ph = NULL;
92
93   GNUNET_free_non_null (p0_addresses[0].addr);
94   GNUNET_free_non_null (p0_addresses[1].addr);
95   GNUNET_free_non_null (p1_addresses[0].addr);
96   GNUNET_free_non_null (p1_addresses[1].addr);
97
98   ret = GNUNET_SYSERR;
99 }
100
101
102 static void
103 end ()
104 {
105   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
106   if (die_task != GNUNET_SCHEDULER_NO_TASK)
107   {
108     GNUNET_SCHEDULER_cancel (die_task);
109     die_task = GNUNET_SCHEDULER_NO_TASK;
110   }
111   if (NULL != atsh)
112   GNUNET_ATS_scheduling_done (atsh);
113   if (phal != NULL)
114     GNUNET_ATS_performance_list_addresses_cancel (phal);
115   phal = NULL;
116   if (ph != NULL)
117     GNUNET_ATS_performance_done (ph);
118   ph = NULL;
119
120   GNUNET_free_non_null (p0_addresses[0].addr);
121   GNUNET_free_non_null (p0_addresses[1].addr);
122   GNUNET_free_non_null (p1_addresses[0].addr);
123   GNUNET_free_non_null (p1_addresses[1].addr);
124
125   ret = 0;
126 }
127
128 static void
129 test_performance_api (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
130
131 void all_active_addresses_peer_cb (void *cls,
132                       const struct
133                       GNUNET_HELLO_Address *
134                       address,
135                       struct
136                       GNUNET_BANDWIDTH_Value32NBO
137                       bandwidth_out,
138                       struct
139                       GNUNET_BANDWIDTH_Value32NBO
140                       bandwidth_in,
141                       const struct
142                       GNUNET_ATS_Information *
143                       ats, uint32_t ats_count)
144 {
145   static int cb = 0;
146   int fail = GNUNET_NO;
147
148   if (address != NULL)
149   {
150     if (0 == memcmp (&address->peer, &p[0].id,
151                      sizeof (struct GNUNET_PeerIdentity)))
152     {
153         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154             "Did not expected callback for peer 0 address `%s', got address `%s'!\n",
155             s_ha[0]->address, address->address);
156         GNUNET_ATS_performance_list_addresses_cancel (phal);
157         fail = GNUNET_YES;
158     }
159
160     if (0 == memcmp (&address->peer, &p[1].id,
161                      sizeof (struct GNUNET_PeerIdentity)))
162     {
163         if (0 == strcmp(address->address, s_ha[1]->address))
164         {
165           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166               "Callback for peer 1 suggested address %s\n",
167               s_ha[1]->address);
168           cb ++;
169         }
170         else
171         {
172           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
173               "Expected callback for peer 1 address `%s', got address `%s'!\n",
174               s_ha[1]->address, address->address);
175           GNUNET_ATS_performance_list_addresses_cancel (phal);
176           fail = GNUNET_YES;
177         }
178     }
179   }
180   if ((address == NULL) || (GNUNET_YES == fail))
181   {
182       phal = NULL;
183       if ((1 == cb) && (GNUNET_NO == fail))
184       {
185         /* Received all addresses + terminator cb, next stage */
186         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
187         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
188         return;
189       }
190       else
191       {
192         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
193         GNUNET_SCHEDULER_add_now (&end, NULL);
194         ret = 5;
195         return;
196       }
197   }
198 }
199
200 void all_active_addresses_cb (void *cls,
201                       const struct
202                       GNUNET_HELLO_Address *
203                       address,
204                       struct
205                       GNUNET_BANDWIDTH_Value32NBO
206                       bandwidth_out,
207                       struct
208                       GNUNET_BANDWIDTH_Value32NBO
209                       bandwidth_in,
210                       const struct
211                       GNUNET_ATS_Information *
212                       ats, uint32_t ats_count)
213 {
214   static int cb = 0;
215   int fail = GNUNET_NO;
216
217   if (address != NULL)
218   {
219     if (0 == memcmp (&address->peer, &p[0].id,
220                      sizeof (struct GNUNET_PeerIdentity)))
221     {
222         if (0 == strcmp(address->address, s_ha[0]->address))
223         {
224           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225               "Callback for peer 0 suggested address %s\n",
226               s_ha[0]->address);
227           cb ++;
228         }
229         else
230         {
231           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232               "Expected callback for peer 0 address `%s', got address `%s'!\n",
233               s_ha[0]->address, address->address);
234           GNUNET_ATS_performance_list_addresses_cancel (phal);
235           fail = GNUNET_YES;
236         }
237     }
238
239     if (0 == memcmp (&address->peer, &p[1].id,
240                      sizeof (struct GNUNET_PeerIdentity)))
241     {
242         if (0 == strcmp(address->address, s_ha[1]->address))
243         {
244           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245               "Callback for peer 1 suggested address %s\n",
246               s_ha[1]->address);
247           cb ++;
248         }
249         else
250         {
251           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252               "Expected callback for peer 1 address `%s', got address `%s'!\n",
253               s_ha[1]->address, address->address);
254           GNUNET_ATS_performance_list_addresses_cancel (phal);
255           fail = GNUNET_YES;
256         }
257     }
258   }
259   if ((address == NULL) || (GNUNET_YES == fail))
260   {
261       phal = NULL;
262       if ((2 == cb) && (GNUNET_NO == fail))
263       {
264         /* Received all addresses + terminator cb, next stage */
265         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
266         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
267         return;
268       }
269       else
270       {
271         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
272         GNUNET_SCHEDULER_add_now (&end, NULL);
273         ret = 5;
274         return;
275       }
276   }
277 }
278
279
280
281 void all_addresses_peer_cb (void *cls,
282                       const struct
283                       GNUNET_HELLO_Address *
284                       address,
285                       struct
286                       GNUNET_BANDWIDTH_Value32NBO
287                       bandwidth_out,
288                       struct
289                       GNUNET_BANDWIDTH_Value32NBO
290                       bandwidth_in,
291                       const struct
292                       GNUNET_ATS_Information *
293                       ats, uint32_t ats_count)
294 {
295   static int cb = 0;
296   int fail = GNUNET_NO;
297
298   if (address != NULL)
299   {
300       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301           "Callback for peer `%s'  address `%s'\n",
302            GNUNET_i2s (&address->peer), address->address);
303
304     if (0 != memcmp (&address->peer, &p[1].id,
305                      sizeof (struct GNUNET_PeerIdentity)))
306     {
307         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
308             "Stage %i:  Received address for wrong peer\n", stage);
309         GNUNET_ATS_performance_list_addresses_cancel (phal);
310         fail = GNUNET_YES;
311         ret = 4;
312     }
313     cb ++;
314   }
315
316   if ((NULL == address) || (fail))
317   {
318       phal = NULL;
319       if ((2 == cb) && (GNUNET_NO == fail))
320       {
321         /* Received all addresses + terminator cb, next stage */
322         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
323         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
324         return;
325       }
326       else
327       {
328         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
329         GNUNET_SCHEDULER_add_now (&end, NULL);
330         ret = 5;
331         return;
332       }
333   }
334 }
335
336 void all_addresses_cb (void *cls,
337                               const struct
338                               GNUNET_HELLO_Address *
339                               address,
340                               struct
341                               GNUNET_BANDWIDTH_Value32NBO
342                               bandwidth_out,
343                               struct
344                               GNUNET_BANDWIDTH_Value32NBO
345                               bandwidth_in,
346                               const struct
347                               GNUNET_ATS_Information *
348                               ats, uint32_t ats_count)
349 {
350   static int cb = 0;
351
352   if (address != NULL)
353   {
354     if (0 == memcmp (&address->peer, &p[0].id,
355                      sizeof (struct GNUNET_PeerIdentity)))
356     {
357       if (0 == strcmp(address->address, p0_addresses[0].addr))
358       {
359         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 0\n");
360         cb |= 1;
361       }
362       if (0 == strcmp(address->address, p0_addresses[1].addr))
363       {
364         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 1\n");
365         cb |= 2;
366       }
367     }
368     if (0 == memcmp (&address->peer, &p[1].id,
369                      sizeof (struct GNUNET_PeerIdentity)))
370     {
371         if (0 == strcmp(address->address, p1_addresses[0].addr))
372         {
373           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 0\n");
374           cb |= 4;
375         }
376         if (0 == strcmp(address->address, p1_addresses[1].addr))
377         {
378           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 1\n");
379           cb |= 8;
380         }
381     }
382   }
383   else
384   {
385       phal = NULL;
386       if (((1 << 4) - 1) == cb)
387       {
388         /* Received all addresses + terminator cb, next stage */
389         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
390         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
391         return;
392       }
393       else
394       {
395         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
396         GNUNET_SCHEDULER_add_now (&end, NULL);
397         ret = 3;
398         return;
399       }
400   }
401 }
402
403 static void
404 test_performance_api (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
405 {
406   if (NULL == ph)
407     ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
408   if (NULL == ph)
409   {
410       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize performance handle\n");
411       ret = 2;
412   }
413   stage++;
414   switch (stage) {
415     case 1: /* Get all peers, all addresses */
416       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 1: \n");
417       phal = GNUNET_ATS_performance_list_addresses (ph,
418                                              NULL,
419                                              GNUNET_YES,
420                                              &all_addresses_cb, NULL);
421       GNUNET_assert (NULL != phal);
422       break;
423     case 2: /* Get specific peer, all addresses */
424       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 2: \n");
425       phal = GNUNET_ATS_performance_list_addresses (ph,
426                                              &p[1].id,
427                                              GNUNET_YES,
428                                              &all_addresses_peer_cb, NULL);
429       GNUNET_assert (NULL != phal);
430       break;
431     case 3: /* Get all peers, active addresses */
432       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 3: \n");
433       phal = GNUNET_ATS_performance_list_addresses (ph,
434                                              NULL,
435                                              GNUNET_NO,
436                                              &all_active_addresses_cb, NULL);
437       GNUNET_assert (NULL != phal);
438       break;
439     case 4: /* Get specific peers, active addresses */
440       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 4: \n");
441       phal = GNUNET_ATS_performance_list_addresses (ph,
442                                              &p[1].id,
443                                              GNUNET_NO,
444                                              &all_active_addresses_peer_cb, NULL);
445       GNUNET_assert (NULL != phal);
446       break;
447     default:
448       /* done */
449       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All tests successful, shutdown... \n");
450       GNUNET_SCHEDULER_add_now (&end, NULL);
451       return;
452   }
453 }
454
455
456 static void
457 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
458                     struct Session *session,
459                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
460                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
461                     const struct GNUNET_ATS_Information *ats,
462                     uint32_t ats_count)
463 {
464   static int suggest_p0 = GNUNET_NO;
465   static int suggest_p1 = GNUNET_NO;
466   static int running = GNUNET_NO;
467
468   if ((GNUNET_NO == suggest_p0) && (0 == memcmp (&address->peer, &p[0].id,
469                    sizeof (struct GNUNET_PeerIdentity))))
470   {
471     suggest_p0 = GNUNET_YES;;
472
473     if (s_ha[0] != NULL)
474       GNUNET_free (s_ha[0]);
475     s_ha[0] = GNUNET_HELLO_address_copy (address);
476     GNUNET_ATS_suggest_address_cancel (atsh, &p[0].id);
477   }
478   if ((GNUNET_NO == suggest_p1) && (0 == memcmp (&address->peer, &p[1].id,
479                    sizeof (struct GNUNET_PeerIdentity))))
480   {
481     suggest_p1 = GNUNET_YES;
482
483     if (s_ha[1] != NULL)
484       GNUNET_free (s_ha[1]);
485     s_ha[1] = GNUNET_HELLO_address_copy (address);
486     GNUNET_ATS_suggest_address_cancel (atsh, &p[1].id);
487   }
488
489
490   if ((GNUNET_NO == running) && (GNUNET_YES == suggest_p0) && (GNUNET_YES == suggest_p1))
491   {
492       running = GNUNET_YES;
493       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have address suggestion for both peers\n");
494       GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
495   }
496
497 }
498
499
500 static void
501 run (void *cls, 
502      const struct GNUNET_CONFIGURATION_Handle *mycfg,
503      struct GNUNET_TESTING_Peer *peer)
504 {
505   ret = 1;
506   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
507   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
508
509
510   /* set up peer 0 */
511   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
512                                     &p[0].id.hashPubKey);
513
514   p0_addresses[0].plugin = "test";
515   p0_addresses[0].session = NULL;
516   p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
517   p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
518
519   p0_ha[0].address = p0_addresses[0].addr;
520   p0_ha[0].address_length = p0_addresses[0].addr_len;
521   p0_ha[0].peer = p[0].id;
522   p0_ha[0].transport_name = p0_addresses[0].plugin;
523
524   p0_addresses[1].plugin = "test";
525   p0_addresses[1].session = NULL;
526   p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
527   p0_addresses[1].addr_len = strlen(p0_addresses[1].addr) + 1;
528
529   p0_ha[1].address = p0_addresses[1].addr;
530   p0_ha[1].address_length = p0_addresses[1].addr_len;
531   p0_ha[1].peer = p[0].id;
532   p0_ha[1].transport_name = p0_addresses[1].plugin;
533
534   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
535               GNUNET_i2s (&p[0].id));
536
537   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
538                                     &p[1].id.hashPubKey);
539
540   p1_addresses[0].plugin = "test";
541   p1_addresses[0].session = NULL;
542   p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
543   p1_addresses[0].addr_len = strlen(p1_addresses[0].addr) + 1;
544
545   p1_ha[0].address = p1_addresses[0].addr;
546   p1_ha[0].address_length = p1_addresses[0].addr_len;
547   p1_ha[0].peer = p[1].id;
548   p1_ha[0].transport_name = p1_addresses[0].plugin;
549
550   p1_addresses[1].plugin = "test";
551   p1_addresses[1].session = NULL;
552   p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
553   p1_addresses[1].addr_len = strlen(p1_addresses[1].addr) + 1;
554
555   p1_ha[1].address = p1_addresses[1].addr;
556   p1_ha[1].address_length = p1_addresses[1].addr_len;
557   p1_ha[1].peer = p[1].id;
558   p1_ha[1].transport_name = p1_addresses[1].plugin;
559
560
561   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
562               GNUNET_i2s (&p[1].id));
563
564
565   /* Add addresses */
566   atsh = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
567   if (atsh == NULL)
568   {
569     ret = GNUNET_SYSERR;
570     end ();
571     return;
572   }
573
574   GNUNET_ATS_address_add (atsh, &p0_ha[0], NULL, NULL, 0);
575   GNUNET_ATS_address_add (atsh, &p0_ha[1], NULL, NULL, 0);
576
577   GNUNET_ATS_address_add (atsh, &p1_ha[0], NULL, NULL, 0);
578   GNUNET_ATS_address_add (atsh, &p1_ha[1], NULL, NULL, 0);
579
580
581   GNUNET_ATS_suggest_address (atsh, &p[0].id);
582   GNUNET_ATS_suggest_address (atsh, &p[1].id);
583 }
584
585
586 int
587 main (int argc, char *argv[])
588 {
589   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance",
590                                     "test_ats_api.conf",
591                                     &run, NULL))
592     return 1;
593   return ret;
594 }
595
596 /* end of file test_ats_api_performance.c */