add address test
[oweals/gnunet.git] / src / ats / test_ats_api_performance_list_addresses.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_list_addresses.c
22  * @brief test performance API's address listing feature
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 GNUNET_HELLO_Address *address,
133                 int active,
134                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
135                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
136                 const struct GNUNET_ATS_Information *ats,
137                 uint32_t ats_count)
138 {
139   static int cb = 0;
140   int fail = GNUNET_NO;
141
142   if (address != NULL)
143   {
144     if (0 == memcmp (&address->peer, &p[0].id,
145                      sizeof (struct GNUNET_PeerIdentity)))
146     {
147         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148             "Did not expected callback for peer 0 address `%s', got address `%s'!\n",
149             s_ha[0]->address, address->address);
150         GNUNET_ATS_performance_list_addresses_cancel (phal);
151         fail = GNUNET_YES;
152     }
153
154     if (0 == memcmp (&address->peer, &p[1].id,
155                      sizeof (struct GNUNET_PeerIdentity)))
156     {
157         if (0 == strcmp(address->address, s_ha[1]->address))
158         {
159           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160               "Callback for peer 1 suggested address %s\n",
161               s_ha[1]->address);
162           cb ++;
163         }
164         else
165         {
166           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167               "Expected callback for peer 1 address `%s', got address `%s'!\n",
168               s_ha[1]->address, address->address);
169           GNUNET_ATS_performance_list_addresses_cancel (phal);
170           fail = GNUNET_YES;
171         }
172     }
173   }
174   if ((address == NULL) || (GNUNET_YES == fail))
175   {
176       phal = NULL;
177       if ((1 == cb) && (GNUNET_NO == fail))
178       {
179         /* Received all addresses + terminator cb, next stage */
180         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
181         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
182         return;
183       }
184       else
185       {
186         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
187         GNUNET_SCHEDULER_add_now (&end, NULL);
188         ret = 5;
189         return;
190       }
191   }
192 }
193
194 void all_active_addresses_cb (void *cls,
195                                                                                                                         const struct GNUNET_HELLO_Address *address,
196                                                                                                                         int active,
197                                                                                                                         struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
198                                                                                                                         struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
199                                                                                                                         const struct GNUNET_ATS_Information *ats,
200                                                                                                                         uint32_t ats_count)
201 {
202   static int cb = 0;
203   int fail = GNUNET_NO;
204
205   if (address != NULL)
206   {
207     if (0 == memcmp (&address->peer, &p[0].id,
208                      sizeof (struct GNUNET_PeerIdentity)))
209     {
210         if (0 == strcmp(address->address, s_ha[0]->address))
211         {
212           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213               "Callback for peer 0 suggested address %s\n",
214               s_ha[0]->address);
215           cb ++;
216         }
217         else
218         {
219           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220               "Expected callback for peer 0 address `%s', got address `%s'!\n",
221               s_ha[0]->address, address->address);
222           GNUNET_ATS_performance_list_addresses_cancel (phal);
223           fail = GNUNET_YES;
224         }
225     }
226
227     if (0 == memcmp (&address->peer, &p[1].id,
228                      sizeof (struct GNUNET_PeerIdentity)))
229     {
230         if (0 == strcmp(address->address, s_ha[1]->address))
231         {
232           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233               "Callback for peer 1 suggested address %s\n",
234               s_ha[1]->address);
235           cb ++;
236         }
237         else
238         {
239           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240               "Expected callback for peer 1 address `%s', got address `%s'!\n",
241               s_ha[1]->address, address->address);
242           GNUNET_ATS_performance_list_addresses_cancel (phal);
243           fail = GNUNET_YES;
244         }
245     }
246   }
247   if ((address == NULL) || (GNUNET_YES == fail))
248   {
249       phal = NULL;
250       if ((2 == cb) && (GNUNET_NO == fail))
251       {
252         /* Received all addresses + terminator cb, next stage */
253         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
254         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
255         return;
256       }
257       else
258       {
259         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
260         GNUNET_SCHEDULER_add_now (&end, NULL);
261         ret = 5;
262         return;
263       }
264   }
265 }
266
267
268
269 void all_addresses_peer_cb (void *cls,
270                                                                                                                 const struct GNUNET_HELLO_Address *address,
271                                                                                                                 int active,
272                                                                                                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
273                                                                                                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
274                                                                                                                 const struct GNUNET_ATS_Information *ats,
275                                                                                                                 uint32_t ats_count)
276 {
277   static int cb = 0;
278   int fail = GNUNET_NO;
279
280   if (address != NULL)
281   {
282       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283           "Callback for peer `%s'  address `%s'\n",
284            GNUNET_i2s (&address->peer), address->address);
285
286     if (0 != memcmp (&address->peer, &p[1].id,
287                      sizeof (struct GNUNET_PeerIdentity)))
288     {
289         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290             "Stage %i:  Received address for wrong peer\n", stage);
291         GNUNET_ATS_performance_list_addresses_cancel (phal);
292         fail = GNUNET_YES;
293         ret = 4;
294     }
295     cb ++;
296   }
297
298   if ((NULL == address) || (fail))
299   {
300       phal = NULL;
301       if ((2 == cb) && (GNUNET_NO == fail))
302       {
303         /* Received all addresses + terminator cb, next stage */
304         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
305         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
306         return;
307       }
308       else
309       {
310         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
311         GNUNET_SCHEDULER_add_now (&end, NULL);
312         ret = 5;
313         return;
314       }
315   }
316 }
317
318 void all_addresses_cb (void *cls,
319                                                                                         const struct GNUNET_HELLO_Address *address,
320                                                                                         int active,
321                                                                                         struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
322                                                                                         struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
323                                                                                         const struct GNUNET_ATS_Information *ats,
324                                                                                         uint32_t ats_count)
325 {
326   static int cb = 0;
327
328   if (address != NULL)
329   {
330     if (0 == memcmp (&address->peer, &p[0].id,
331                      sizeof (struct GNUNET_PeerIdentity)))
332     {
333       if (0 == strcmp(address->address, p0_addresses[0].addr))
334       {
335         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 0\n");
336         cb |= 1;
337       }
338       if (0 == strcmp(address->address, p0_addresses[1].addr))
339       {
340         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 1\n");
341         cb |= 2;
342       }
343     }
344     if (0 == memcmp (&address->peer, &p[1].id,
345                      sizeof (struct GNUNET_PeerIdentity)))
346     {
347         if (0 == strcmp(address->address, p1_addresses[0].addr))
348         {
349           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 0\n");
350           cb |= 4;
351         }
352         if (0 == strcmp(address->address, p1_addresses[1].addr))
353         {
354           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 1\n");
355           cb |= 8;
356         }
357     }
358   }
359   else
360   {
361       phal = NULL;
362       if (((1 << 4) - 1) == cb)
363       {
364         /* Received all addresses + terminator cb, next stage */
365         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
366         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
367         return;
368       }
369       else
370       {
371         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
372         GNUNET_SCHEDULER_add_now (&end, NULL);
373         ret = 3;
374         return;
375       }
376   }
377 }
378
379 static void
380 test_performance_api (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
381 {
382   if (NULL == ph)
383     ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
384   if (NULL == ph)
385   {
386       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize performance handle\n");
387       ret = 2;
388   }
389   stage++;
390   switch (stage) {
391     case 1: /* Get all peers, all addresses */
392       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 1: \n");
393       phal = GNUNET_ATS_performance_list_addresses (ph,
394                                              NULL,
395                                              GNUNET_YES,
396                                              &all_addresses_cb, NULL);
397       GNUNET_assert (NULL != phal);
398       break;
399     case 2: /* Get specific peer, all addresses */
400       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 2: \n");
401       phal = GNUNET_ATS_performance_list_addresses (ph,
402                                              &p[1].id,
403                                              GNUNET_YES,
404                                              &all_addresses_peer_cb, NULL);
405       GNUNET_assert (NULL != phal);
406       break;
407     case 3: /* Get all peers, active addresses */
408       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 3: \n");
409       phal = GNUNET_ATS_performance_list_addresses (ph,
410                                              NULL,
411                                              GNUNET_NO,
412                                              &all_active_addresses_cb, NULL);
413       GNUNET_assert (NULL != phal);
414       break;
415     case 4: /* Get specific peers, active addresses */
416       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 4: \n");
417       phal = GNUNET_ATS_performance_list_addresses (ph,
418                                              &p[1].id,
419                                              GNUNET_NO,
420                                              &all_active_addresses_peer_cb, NULL);
421       GNUNET_assert (NULL != phal);
422       break;
423     default:
424       /* done */
425       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All tests successful, shutdown... \n");
426       GNUNET_SCHEDULER_add_now (&end, NULL);
427       return;
428   }
429 }
430
431
432 static void
433 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
434                     struct Session *session,
435                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
436                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
437                     const struct GNUNET_ATS_Information *ats,
438                     uint32_t ats_count)
439 {
440   static int suggest_p0 = GNUNET_NO;
441   static int suggest_p1 = GNUNET_NO;
442   static int running = GNUNET_NO;
443
444   if ((GNUNET_NO == suggest_p0) && (0 == memcmp (&address->peer, &p[0].id,
445                    sizeof (struct GNUNET_PeerIdentity))))
446   {
447     suggest_p0 = GNUNET_YES;;
448
449     if (s_ha[0] != NULL)
450       GNUNET_free (s_ha[0]);
451     s_ha[0] = GNUNET_HELLO_address_copy (address);
452     GNUNET_ATS_suggest_address_cancel (atsh, &p[0].id);
453   }
454   if ((GNUNET_NO == suggest_p1) && (0 == memcmp (&address->peer, &p[1].id,
455                    sizeof (struct GNUNET_PeerIdentity))))
456   {
457     suggest_p1 = GNUNET_YES;
458
459     if (s_ha[1] != NULL)
460       GNUNET_free (s_ha[1]);
461     s_ha[1] = GNUNET_HELLO_address_copy (address);
462     GNUNET_ATS_suggest_address_cancel (atsh, &p[1].id);
463   }
464
465
466   if ((GNUNET_NO == running) && (GNUNET_YES == suggest_p0) && (GNUNET_YES == suggest_p1))
467   {
468       running = GNUNET_YES;
469       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have address suggestion for both peers\n");
470       GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
471   }
472
473 }
474
475
476 static void
477 run (void *cls, 
478      const struct GNUNET_CONFIGURATION_Handle *mycfg,
479      struct GNUNET_TESTING_Peer *peer)
480 {
481   ret = 1;
482   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
483   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
484
485
486   /* set up peer 0 */
487   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
488                                     &p[0].id.hashPubKey);
489
490   p0_addresses[0].plugin = "test";
491   p0_addresses[0].session = NULL;
492   p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
493   p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
494
495   p0_ha[0].address = p0_addresses[0].addr;
496   p0_ha[0].address_length = p0_addresses[0].addr_len;
497   p0_ha[0].peer = p[0].id;
498   p0_ha[0].transport_name = p0_addresses[0].plugin;
499
500   p0_addresses[1].plugin = "test";
501   p0_addresses[1].session = NULL;
502   p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
503   p0_addresses[1].addr_len = strlen(p0_addresses[1].addr) + 1;
504
505   p0_ha[1].address = p0_addresses[1].addr;
506   p0_ha[1].address_length = p0_addresses[1].addr_len;
507   p0_ha[1].peer = p[0].id;
508   p0_ha[1].transport_name = p0_addresses[1].plugin;
509
510   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
511               GNUNET_i2s (&p[0].id));
512
513   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
514                                     &p[1].id.hashPubKey);
515
516   p1_addresses[0].plugin = "test";
517   p1_addresses[0].session = NULL;
518   p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
519   p1_addresses[0].addr_len = strlen(p1_addresses[0].addr) + 1;
520
521   p1_ha[0].address = p1_addresses[0].addr;
522   p1_ha[0].address_length = p1_addresses[0].addr_len;
523   p1_ha[0].peer = p[1].id;
524   p1_ha[0].transport_name = p1_addresses[0].plugin;
525
526   p1_addresses[1].plugin = "test";
527   p1_addresses[1].session = NULL;
528   p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
529   p1_addresses[1].addr_len = strlen(p1_addresses[1].addr) + 1;
530
531   p1_ha[1].address = p1_addresses[1].addr;
532   p1_ha[1].address_length = p1_addresses[1].addr_len;
533   p1_ha[1].peer = p[1].id;
534   p1_ha[1].transport_name = p1_addresses[1].plugin;
535
536
537   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
538               GNUNET_i2s (&p[1].id));
539
540
541   /* Add addresses */
542   atsh = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
543   if (atsh == NULL)
544   {
545     ret = GNUNET_SYSERR;
546     end ();
547     return;
548   }
549
550   GNUNET_ATS_address_add (atsh, &p0_ha[0], NULL, NULL, 0);
551   GNUNET_ATS_address_add (atsh, &p0_ha[1], NULL, NULL, 0);
552
553   GNUNET_ATS_address_add (atsh, &p1_ha[0], NULL, NULL, 0);
554   GNUNET_ATS_address_add (atsh, &p1_ha[1], NULL, NULL, 0);
555
556
557   GNUNET_ATS_suggest_address (atsh, &p[0].id);
558   GNUNET_ATS_suggest_address (atsh, &p[1].id);
559 }
560
561
562 int
563 main (int argc, char *argv[])
564 {
565   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance",
566                                     "test_ats_api.conf",
567                                     &run, NULL))
568     return 1;
569   return ret;
570 }
571
572 /* end of file test_ats_api_performance_list_addresses.c */