changes
[oweals/gnunet.git] / src / ats / test_ats_simplistic.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_mlp.c
22  * @brief test for the MLP solver
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25
26  */
27 /**
28  * @file ats/test_ats_api_scheduling_add_address.c
29  * @brief test for ats simplistic solver
30  * @author Christian Grothoff
31  * @author Matthias Wachs
32  */
33 #include "platform.h"
34 #include "gnunet_ats_service.h"
35 #include "gnunet_testing_lib-new.h"
36 #include "ats.h"
37 #include "test_ats_api_common.h"
38
39 #define DEBUG_ATS_INFO GNUNET_NO
40
41 static GNUNET_SCHEDULER_TaskIdentifier die_task;
42
43 /**
44  * Scheduling handle
45  */
46 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
47
48 /**
49  * Return value
50  */
51 static int ret;
52
53 /**
54  * Test address
55  */
56 static struct Test_Address test_addr[2];
57
58 /**
59  * Test peer
60  */
61 static struct PeerContext p[2];
62
63
64 /**
65  * HELLO address
66  */
67 struct GNUNET_HELLO_Address test_hello_address[2];
68
69 /**
70  * Session
71  */
72 static void *test_session[2];
73
74 /**
75  * Test ats info
76  */
77 struct GNUNET_ATS_Information test_ats_info[2];
78
79 /**
80  * Test ats count
81  */
82 uint32_t test_ats_count;
83
84 /**
85  * Configured WAN out quota
86  */
87 unsigned long long wan_quota_out;
88
89 /**
90  * Configured WAN in quota
91  */
92 unsigned long long wan_quota_in;
93
94 static void
95 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
96 {
97   dest->plugin = GNUNET_strdup (plugin);
98   dest->session = session;
99   dest->addr = GNUNET_malloc (addrlen);
100   memcpy (dest->addr, addr, addrlen);
101   dest->addr_len = addrlen;
102 }
103
104 static void
105 free_test_address (struct Test_Address *dest)
106 {
107   GNUNET_free (dest->plugin);
108   GNUNET_free (dest->addr);
109 }
110
111
112 static void
113 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 {
115   die_task = GNUNET_SCHEDULER_NO_TASK;
116
117   if (sched_ats != NULL)
118     GNUNET_ATS_scheduling_done (sched_ats);
119   free_test_address (&test_addr[0]);
120   ret = GNUNET_SYSERR;
121 }
122
123
124 static void
125 end ()
126 {
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
128   if (die_task != GNUNET_SCHEDULER_NO_TASK)
129   {
130     GNUNET_SCHEDULER_cancel (die_task);
131     die_task = GNUNET_SCHEDULER_NO_TASK;
132   }
133   GNUNET_ATS_scheduling_done (sched_ats);
134   sched_ats = NULL;
135   free_test_address (&test_addr[0]);
136 }
137
138 static int
139 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
140                    const struct GNUNET_HELLO_Address *address2, void *session2)
141 {
142   if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
143   {
144       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
145       return GNUNET_SYSERR;
146   }
147   if (0 != strcmp (address1->transport_name, address2->transport_name))
148   {
149       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
150       return GNUNET_SYSERR;
151   }
152   if (address1->address_length != address2->address_length)
153   {
154       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
155       return GNUNET_SYSERR;
156
157   }
158   else if (0 != memcmp (address1->address, address2->address, address2->address_length))
159   {
160       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
161       return GNUNET_SYSERR;
162   }
163   if (session1 != session2)
164   {
165       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
166                   session1, session2);
167       return GNUNET_SYSERR;
168
169   }
170   return GNUNET_OK;
171 }
172
173
174 static int
175 compare_ats (const struct GNUNET_ATS_Information *ats_is, uint32_t ats_count_is,
176              const struct GNUNET_ATS_Information *ats_should, uint32_t ats_count_should)
177 {
178   unsigned int c_o;
179   unsigned int c_i;
180   char *prop[] = GNUNET_ATS_PropertyStrings;
181   uint32_t type1;
182   uint32_t type2;
183   uint32_t val1;
184   uint32_t val2;
185   int res = GNUNET_OK;
186
187   for (c_o = 0; c_o < ats_count_is; c_o++)
188   {
189     for (c_i = 0; c_i < ats_count_should; c_i++)
190     {
191         type1 = ntohl(ats_is[c_o].type);
192         type2 = ntohl(ats_should[c_i].type);
193         if (type1 == type2)
194         {
195 #if DEBUG_ATS_INFO
196             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS type `%s'\n",
197                         prop[type1]);
198 #endif
199             val1 = ntohl(ats_is[c_o].value);
200             val2 = ntohl(ats_should[c_i].value);
201             if (val1 != val2)
202             {
203 #if DEBUG_ATS_INFO
204                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS value `%s' not equal: %u != %u\n",
205                             prop[type1],
206                             val1, val2);
207 #endif
208                 res = GNUNET_SYSERR;
209             }
210             else
211             {
212 #if DEBUG_ATS_INFO
213               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS value `%s' equal: %u == %u\n",
214                           prop[type1],
215                           val1, val2);
216 #endif
217             }
218         }
219     }
220   }
221   return res;
222 }
223
224 static void
225 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
226                     struct Session *session,
227                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
228                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
229                     const struct GNUNET_ATS_Information *atsi,
230                     uint32_t ats_count)
231 {
232   static int stage = 0;
233   unsigned int bw_in = ntohl(bandwidth_in.value__);
234   unsigned int bw_out = ntohl(bandwidth_out.value__);
235   if (0 == stage)
236   {
237     if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
238     {
239         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
240                     GNUNET_i2s (&address->peer));
241         ret = 0;
242     }
243     else
244     {
245         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
246                     GNUNET_i2s (&address->peer));
247         ret = 1;
248     }
249
250     if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
251     {
252       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
253       ret = 1;
254     }
255
256     if (bw_in > wan_quota_in)
257     {
258         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
259             bw_in, wan_quota_in);
260         ret = 1;
261     }
262     else
263       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
264           bw_in, wan_quota_in);
265
266     if (bw_out > wan_quota_out)
267     {
268         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
269             bw_out, wan_quota_out);
270         ret = 1;
271     }
272     else
273       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
274           bw_out, wan_quota_out);
275
276     if (1 == ret)
277     {
278       GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
279       GNUNET_SCHEDULER_add_now (&end, NULL);
280       return;
281     }
282     p[0].bw_out_assigned = bw_out;
283     p[0].bw_in_assigned = bw_in;
284     stage ++;
285
286     /* Add a 2nd address */
287     /* Prepare ATS Information */
288     test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
289     test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
290     test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
291     test_ats_info[1].value = htonl(1);
292     test_ats_count = 2;
293
294     /* Adding address with session */
295     test_session[1] = &test_addr[1];
296     create_test_address (&test_addr[1], "test1", test_session[1], "test1", strlen ("test1") + 1);
297     test_hello_address[1].peer = p[1].id;
298     test_hello_address[1].transport_name = test_addr[1].plugin;
299     test_hello_address[1].address = test_addr[1].addr;
300     test_hello_address[1].address_length = test_addr[1].addr_len;
301     GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], test_session[1], test_ats_info, test_ats_count);
302   }
303   if (1 == stage)
304   {
305       /* Expecting callback for address[0] with updated quota and no callback for address[1]*/
306       if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
307       {
308           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with correct address `%s'\n",
309                       GNUNET_i2s (&address->peer));
310           ret = 0;
311       }
312       else
313       {
314           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with invalid address `%s'\n",
315                       GNUNET_i2s (&address->peer));
316           ret = 1;
317       }
318
319       if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
320       {
321         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Callback with incorrect ats info \n");
322         ret = 1;
323       }
324
325       if (bw_in > wan_quota_in)
326       {
327           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
328               bw_in, wan_quota_in);
329           ret = 1;
330       }
331       else if (p[0].bw_in_assigned > bw_in)
332       {
333           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
334               bw_in, p[0].bw_in_assigned);
335           ret = 1;
336       }
337       else
338         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
339             bw_in, wan_quota_in);
340
341       if (bw_out > wan_quota_out)
342       {
343           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
344               bw_out, wan_quota_out);
345           ret = 1;
346       }
347       else if (p[0].bw_out_assigned > bw_out)
348       {
349           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
350               bw_out, p[0].bw_out_assigned);
351           ret = 1;
352       }
353       else
354         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
355             bw_out, wan_quota_out);
356
357       if (1 == ret)
358       {
359         GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
360         GNUNET_SCHEDULER_add_now (&end, NULL);
361         return;
362       }
363       stage ++;
364
365       GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
366       GNUNET_SCHEDULER_add_now (&end, NULL);
367       return;
368   }
369
370 }
371
372 static void
373 run (void *cls,
374      const struct GNUNET_CONFIGURATION_Handle *cfg,
375      struct GNUNET_TESTING_Peer *peer)
376 {
377   char *quota_str;
378
379   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_str))
380   {
381       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
382       ret = 1;
383       return;
384   }
385   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
386   {
387       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
388       ret = 1;
389       GNUNET_free (quota_str);
390       return;
391   }
392   GNUNET_free (quota_str);
393   quota_str = NULL;
394
395   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_str))
396   {
397       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
398       ret = 1;
399       return;
400   }
401   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
402   {
403       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
404       GNUNET_free (quota_str);
405       ret = 1;
406       return;
407   }
408   GNUNET_free (quota_str);
409   quota_str = NULL;
410   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
411   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
412
413
414   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
415
416   /* Connect to ATS scheduling */
417   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
418   if (sched_ats == NULL)
419   {
420     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
421     ret = 1;
422     end ();
423     return;
424   }
425
426   /* Set up peer 0 */
427   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
428   {
429       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
430       ret = GNUNET_SYSERR;
431       end ();
432       return;
433   }
434
435   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
436
437   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
438               GNUNET_i2s(&p[0].id));
439
440   /* Set up peer 1*/
441   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
442   {
443       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
444       ret = GNUNET_SYSERR;
445       end ();
446       return;
447   }
448
449   GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
450
451   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
452               GNUNET_i2s(&p[1].id));
453
454   /* Prepare ATS Information */
455   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
456   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
457   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
458   test_ats_info[1].value = htonl(1);
459   test_ats_count = 2;
460
461   /* Adding address with session */
462   test_session[0] = &test_addr[0];
463   create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
464   test_hello_address[0].peer = p[0].id;
465   test_hello_address[0].transport_name = test_addr[0].plugin;
466   test_hello_address[0].address = test_addr[0].addr;
467   test_hello_address[0].address_length = test_addr[0].addr_len;
468   GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
469
470   GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
471 }
472
473
474 int
475 main (int argc, char *argv[])
476 {
477   if (0 != GNUNET_TESTING_peer_run ("test_ats_simplististic",
478                                     "test_ats_api.conf",
479                                     &run, NULL))
480     return 1;
481   return ret;
482 }
483
484
485 /* end of file test_ats_api_bandwidth_consumption.c */