- next stage
[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-new.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 *ats;
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
74 static unsigned int stage = 0;
75
76 static void
77 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 {
79   die_task = GNUNET_SCHEDULER_NO_TASK;
80
81   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout in stage %u\n", stage);
82
83   if (NULL != ats)
84   GNUNET_ATS_scheduling_done (ats);
85   if (phal != NULL)
86     GNUNET_ATS_performance_list_addresses_cancel (phal);
87   phal = NULL;
88   if (ph != NULL)
89     GNUNET_ATS_performance_done (ph);
90   ph = NULL;
91
92   GNUNET_free_non_null (p0_addresses[0].addr);
93   GNUNET_free_non_null (p0_addresses[1].addr);
94   GNUNET_free_non_null (p1_addresses[0].addr);
95   GNUNET_free_non_null (p1_addresses[1].addr);
96
97   ret = GNUNET_SYSERR;
98 }
99
100
101 static void
102 end ()
103 {
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
105   if (die_task != GNUNET_SCHEDULER_NO_TASK)
106   {
107     GNUNET_SCHEDULER_cancel (die_task);
108     die_task = GNUNET_SCHEDULER_NO_TASK;
109   }
110   if (NULL != ats)
111   GNUNET_ATS_scheduling_done (ats);
112   if (phal != NULL)
113     GNUNET_ATS_performance_list_addresses_cancel (phal);
114   phal = NULL;
115   if (ph != NULL)
116     GNUNET_ATS_performance_done (ph);
117   ph = NULL;
118
119   GNUNET_free_non_null (p0_addresses[0].addr);
120   GNUNET_free_non_null (p0_addresses[1].addr);
121   GNUNET_free_non_null (p1_addresses[0].addr);
122   GNUNET_free_non_null (p1_addresses[1].addr);
123
124   ret = 0;
125 }
126
127 static void
128 test_performance_api (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
129
130 void all_addresses_peer_cb (void *cls,
131                       const struct
132                       GNUNET_HELLO_Address *
133                       address,
134                       struct
135                       GNUNET_BANDWIDTH_Value32NBO
136                       bandwidth_out,
137                       struct
138                       GNUNET_BANDWIDTH_Value32NBO
139                       bandwidth_in,
140                       const struct
141                       GNUNET_ATS_Information *
142                       ats, uint32_t ats_count)
143 {
144   static int cb = 0;
145
146   if (address != NULL)
147   {
148       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'  address `%s'\n",
149            GNUNET_i2s (&address->peer), address->address);
150
151     if (0 != memcmp (&address->peer, &p[1].id,
152                      sizeof (struct GNUNET_PeerIdentity)))
153     {
154         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  Received address for wrong peer\n", stage);
155         GNUNET_ATS_performance_list_addresses_cancel (phal);
156         phal = NULL;
157         GNUNET_SCHEDULER_add_now (&end, NULL);
158         ret = 4;
159         return;
160     }
161     cb ++;
162   }
163   else
164   {
165       phal = NULL;
166       if (2 == cb)
167       {
168         /* Received all addresses + terminator cb, next stage */
169         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
170         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
171         return;
172       }
173       else
174       {
175         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
176         GNUNET_SCHEDULER_add_now (&end, NULL);
177         ret = 5;
178         return;
179       }
180   }
181 }
182
183 void all_addresses_cb (void *cls,
184                               const struct
185                               GNUNET_HELLO_Address *
186                               address,
187                               struct
188                               GNUNET_BANDWIDTH_Value32NBO
189                               bandwidth_out,
190                               struct
191                               GNUNET_BANDWIDTH_Value32NBO
192                               bandwidth_in,
193                               const struct
194                               GNUNET_ATS_Information *
195                               ats, uint32_t ats_count)
196 {
197   static int cb = 0;
198
199   if (address != NULL)
200   {
201       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'  address `%s'\n",
202            GNUNET_i2s (&address->peer), address->address);
203
204     if (0 == memcmp (&address->peer, &p[0].id,
205                      sizeof (struct GNUNET_PeerIdentity)))
206     {
207       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 `%s'\n", GNUNET_i2s (&address->peer));
208       if (0 == strcmp(address->address, p0_addresses[0].addr))
209       {
210         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 0\n");
211         cb |= 1;
212       }
213       if (0 == strcmp(address->address, p0_addresses[1].addr))
214       {
215         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 1\n");
216         cb |= 2;
217       }
218     }
219     if (0 == memcmp (&address->peer, &p[1].id,
220                      sizeof (struct GNUNET_PeerIdentity)));
221     {
222         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 `%s'\n", GNUNET_i2s (&address->peer));
223         if (0 == strcmp(address->address, p1_addresses[0].addr))
224         {
225           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 0\n");
226           cb |= 4;
227         }
228         if (0 == strcmp(address->address, p1_addresses[1].addr))
229         {
230           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 1\n");
231           cb |= 8;
232         }
233     }
234   }
235   else
236   {
237       phal = NULL;
238       if (((1 << 4) - 1) == cb)
239       {
240         /* Received all addresses + terminator cb, next stage */
241         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
242         GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
243         return;
244       }
245       else
246       {
247         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
248         GNUNET_SCHEDULER_add_now (&end, NULL);
249         ret = 3;
250         return;
251       }
252   }
253 }
254
255 static void
256 test_performance_api (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
257 {
258   if (NULL == ph)
259     ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
260   if (NULL == ph)
261   {
262       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize performance handle\n");
263       ret = 2;
264   }
265   stage++;
266   switch (stage) {
267     case 1:
268       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage 1: \n");
269       phal = GNUNET_ATS_performance_list_addresses (ph,
270                                              NULL,
271                                              GNUNET_YES,
272                                              &all_addresses_cb, NULL);
273       GNUNET_assert (NULL != phal);
274       break;
275     case 2:
276       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage 2: \n");
277       phal = GNUNET_ATS_performance_list_addresses (ph,
278                                              &p[1].id,
279                                              GNUNET_YES,
280                                              &all_addresses_peer_cb, NULL);
281       GNUNET_assert (NULL != phal);
282       break;
283     default:
284       /* done */
285       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All tests successful, shutdown... \n");
286       GNUNET_SCHEDULER_add_now (&end, NULL);
287       return;
288   }
289 }
290
291
292 static void
293 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
294                     struct Session *session,
295                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
296                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
297                     const struct GNUNET_ATS_Information *ats,
298                     uint32_t ats_count)
299 {
300   static int suggest_p0;
301   static int suggest_p1;
302
303   if (0 == memcmp (&address->peer, &p[0].id,
304                    sizeof (struct GNUNET_PeerIdentity)));
305     suggest_p0++;
306   if (0 == memcmp (&address->peer, &p[1].id,
307                    sizeof (struct GNUNET_PeerIdentity)));
308     suggest_p1++;
309
310   if ((GNUNET_YES >= suggest_p0) && (GNUNET_YES >= suggest_p1))
311   {
312       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have address suggestion for both peers\n");
313       test_performance_api(NULL, NULL);
314       //GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
315   }
316 }
317
318
319 static void
320 run (void *cls, 
321      const struct GNUNET_CONFIGURATION_Handle *mycfg,
322      struct GNUNET_TESTING_Peer *peer)
323 {
324   ret = 1;
325   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
326   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
327
328
329   /* set up peer 0 */
330   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
331                                     &p[0].id.hashPubKey);
332
333   p0_addresses[0].plugin = "test";
334   p0_addresses[0].session = NULL;
335   p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
336   p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
337
338   p0_ha[0].address = p0_addresses[0].addr;
339   p0_ha[0].address_length = p0_addresses[0].addr_len;
340   p0_ha[0].peer = p[0].id;
341   p0_ha[0].transport_name = p0_addresses[0].plugin;
342
343   p0_addresses[1].plugin = "test";
344   p0_addresses[1].session = NULL;
345   p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
346   p0_addresses[1].addr_len = strlen(p0_addresses[1].addr) + 1;
347
348   p0_ha[1].address = p0_addresses[1].addr;
349   p0_ha[1].address_length = p0_addresses[1].addr_len;
350   p0_ha[1].peer = p[0].id;
351   p0_ha[1].transport_name = p0_addresses[1].plugin;
352
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
354               GNUNET_i2s (&p[0].id));
355
356   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
357                                     &p[1].id.hashPubKey);
358
359   p1_addresses[0].plugin = "test";
360   p1_addresses[0].session = NULL;
361   p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
362   p1_addresses[0].addr_len = strlen(p1_addresses[0].addr) + 1;
363
364   p1_ha[0].address = p1_addresses[0].addr;
365   p1_ha[0].address_length = p1_addresses[0].addr_len;
366   p1_ha[0].peer = p[1].id;
367   p1_ha[0].transport_name = p1_addresses[0].plugin;
368
369   p1_addresses[1].plugin = "test";
370   p1_addresses[1].session = NULL;
371   p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
372   p1_addresses[1].addr_len = strlen(p1_addresses[1].addr) + 1;
373
374   p1_ha[1].address = p1_addresses[1].addr;
375   p1_ha[1].address_length = p1_addresses[1].addr_len;
376   p1_ha[1].peer = p[1].id;
377   p1_ha[1].transport_name = p1_addresses[1].plugin;
378
379
380   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
381               GNUNET_i2s (&p[1].id));
382
383
384   /* Add addresses */
385   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
386   if (ats == NULL)
387   {
388     ret = GNUNET_SYSERR;
389     end ();
390     return;
391   }
392
393   GNUNET_ATS_address_add (ats, &p0_ha[0], NULL, NULL, 0);
394
395   GNUNET_ATS_address_add (ats, &p0_ha[1], NULL, NULL, 0);
396   GNUNET_ATS_address_in_use (ats, &p0_ha[1], NULL, GNUNET_YES);
397
398   GNUNET_ATS_address_add (ats, &p1_ha[0], NULL, NULL, 0);
399   GNUNET_ATS_address_in_use (ats, &p1_ha[0], NULL, GNUNET_YES);
400
401   GNUNET_ATS_address_add (ats, &p1_ha[1], NULL, NULL, 0);
402
403   GNUNET_ATS_suggest_address (ats, &p[0].id);
404   GNUNET_ATS_suggest_address (ats, &p[1].id);
405 }
406
407
408 int
409 main (int argc, char *argv[])
410 {
411   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance",
412                                     "test_ats_api.conf",
413                                     &run, NULL))
414     return 1;
415   return ret;
416 }
417
418 /* end of file test_ats_api_performance.c */