- first test for perf api test
[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);
129
130 void all_addresses_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[0].id,
152                      sizeof (struct GNUNET_PeerIdentity)))
153     {
154       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 `%s'\n", GNUNET_i2s (&address->peer));
155       if (0 == strcmp(address->address, p0_addresses[0].addr))
156       {
157         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 0\n");
158         cb |= 1;
159       }
160       if (0 == strcmp(address->address, p0_addresses[1].addr))
161       {
162         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 1\n");
163         cb |= 2;
164       }
165     }
166     if (0 == memcmp (&address->peer, &p[1].id,
167                      sizeof (struct GNUNET_PeerIdentity)));
168     {
169         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 `%s'\n", GNUNET_i2s (&address->peer));
170         if (0 == strcmp(address->address, p1_addresses[0].addr))
171         {
172           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 0\n");
173           cb |= 4;
174         }
175         if (0 == strcmp(address->address, p1_addresses[1].addr))
176         {
177           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 1\n");
178           cb |= 8;
179         }
180     }
181   }
182   else
183   {
184       phal = NULL;
185       if (((1 << 4) - 1) == cb)
186       {
187         /* Received all addresses + terminator cb, next stage */
188         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
189         test_performance_api ();
190         return;
191       }
192       else
193       {
194         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
195         GNUNET_SCHEDULER_add_now (&end, NULL);
196         ret = 3;
197         return;
198       }
199   }
200
201 }
202
203 static void
204 test_performance_api (void)
205 {
206   if (NULL == ph)
207     ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
208   if (NULL == ph)
209   {
210       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize performance handle\n");
211       ret = 2;
212   }
213   stage++;
214   switch (stage) {
215     case 1:
216       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage 1: \n");
217       phal = GNUNET_ATS_performance_list_addresses (ph,
218                                              NULL,
219                                              GNUNET_YES,
220                                              &all_addresses_cb, NULL);
221       break;
222     default:
223       /* done */
224       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All tests successful, shutdown... \n");
225       GNUNET_SCHEDULER_add_now (&end, NULL);
226       return;
227   }
228 }
229
230
231 static void
232 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
233                     struct Session *session,
234                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
235                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
236                     const struct GNUNET_ATS_Information *ats,
237                     uint32_t ats_count)
238 {
239   static int suggest_p0;
240   static int suggest_p1;
241
242   if (0 == memcmp (&address->peer, &p[0].id,
243                    sizeof (struct GNUNET_PeerIdentity)));
244     suggest_p0++;
245   if (0 == memcmp (&address->peer, &p[1].id,
246                    sizeof (struct GNUNET_PeerIdentity)));
247     suggest_p1++;
248
249   if ((GNUNET_YES >= suggest_p0) && (GNUNET_YES >= suggest_p1))
250   {
251       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have address suggestion for both peers\n");
252       test_performance_api ();
253   }
254 }
255
256
257 static void
258 run (void *cls, 
259      const struct GNUNET_CONFIGURATION_Handle *mycfg,
260      struct GNUNET_TESTING_Peer *peer)
261 {
262   ret = 1;
263   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
264   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
265
266
267   /* set up peer 0 */
268   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
269                                     &p[0].id.hashPubKey);
270
271   p0_addresses[0].plugin = "test";
272   p0_addresses[0].session = NULL;
273   p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
274   p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
275
276   p0_ha[0].address = p0_addresses[0].addr;
277   p0_ha[0].address_length = p0_addresses[0].addr_len;
278   p0_ha[0].peer = p[0].id;
279   p0_ha[0].transport_name = p0_addresses[0].plugin;
280
281   p0_addresses[1].plugin = "test";
282   p0_addresses[1].session = NULL;
283   p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
284   p0_addresses[1].addr_len = strlen(p0_addresses[1].addr) + 1;
285
286   p0_ha[1].address = p0_addresses[1].addr;
287   p0_ha[1].address_length = p0_addresses[1].addr_len;
288   p0_ha[1].peer = p[0].id;
289   p0_ha[1].transport_name = p0_addresses[1].plugin;
290
291   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
292               GNUNET_i2s (&p[0].id));
293
294   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
295                                     &p[1].id.hashPubKey);
296
297   p1_addresses[0].plugin = "test";
298   p1_addresses[0].session = NULL;
299   p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
300   p1_addresses[0].addr_len = strlen(p1_addresses[0].addr) + 1;
301
302   p1_ha[0].address = p1_addresses[0].addr;
303   p1_ha[0].address_length = p1_addresses[0].addr_len;
304   p1_ha[0].peer = p[1].id;
305   p1_ha[0].transport_name = p1_addresses[0].plugin;
306
307   p1_addresses[1].plugin = "test";
308   p1_addresses[1].session = NULL;
309   p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
310   p1_addresses[1].addr_len = strlen(p1_addresses[1].addr) + 1;
311
312   p1_ha[1].address = p1_addresses[1].addr;
313   p1_ha[1].address_length = p1_addresses[1].addr_len;
314   p1_ha[1].peer = p[1].id;
315   p1_ha[1].transport_name = p1_addresses[1].plugin;
316
317
318   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
319               GNUNET_i2s (&p[1].id));
320
321
322   /* Add addresses */
323   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
324   if (ats == NULL)
325   {
326     ret = GNUNET_SYSERR;
327     end ();
328     return;
329   }
330
331   GNUNET_ATS_address_add (ats, &p0_ha[0], NULL, NULL, 0);
332   GNUNET_ATS_address_add (ats, &p0_ha[1], NULL, NULL, 0);
333   GNUNET_ATS_address_add (ats, &p1_ha[0], NULL, NULL, 0);
334   GNUNET_ATS_address_add (ats, &p1_ha[1], NULL, NULL, 0);
335
336   GNUNET_ATS_suggest_address (ats, &p[0].id);
337   GNUNET_ATS_suggest_address (ats, &p[1].id);
338 }
339
340
341 int
342 main (int argc, char *argv[])
343 {
344   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance",
345                                     "test_ats_api.conf",
346                                     &run, NULL))
347     return 1;
348   return ret;
349 }
350
351 /* end of file test_ats_api_performance.c */