pre lunch commit
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_check_min_bw_alt.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_check_min_bw_alt.c
29  * @brief alternative suggestion on network change if no bw is available
30  * @author Christian Grothoff
31  * @author Matthias Wachs
32  */
33 #include "platform.h"
34 #include "gnunet_ats_service.h"
35 #include "gnunet_testing_lib.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][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
95 static void
96 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 {
98   die_task = GNUNET_SCHEDULER_NO_TASK;
99
100   if (sched_ats != NULL)
101     GNUNET_ATS_scheduling_done (sched_ats);
102   free_test_address (&test_addr[0]);
103   free_test_address (&test_addr[1]);
104   ret = GNUNET_SYSERR;
105 }
106
107
108 static void
109 end ()
110 {
111   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
112   if (die_task != GNUNET_SCHEDULER_NO_TASK)
113   {
114     GNUNET_SCHEDULER_cancel (die_task);
115     die_task = GNUNET_SCHEDULER_NO_TASK;
116   }
117   GNUNET_ATS_scheduling_done (sched_ats);
118   sched_ats = NULL;
119   free_test_address (&test_addr[0]);
120   free_test_address (&test_addr[1]);
121 }
122
123
124 static void
125 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
126                     struct Session *session,
127                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
128                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
129                     const struct GNUNET_ATS_Information *atsi,
130                     uint32_t ats_count)
131 {
132   static int stage = 0;
133   unsigned int bw_in = ntohl(bandwidth_in.value__);
134   unsigned int bw_out = ntohl(bandwidth_out.value__);
135
136   if (0 == stage)
137   {
138     /* Initial suggestion for 1st address */
139     if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
140     {
141         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n",
142                     stage, GNUNET_i2s (&address->peer));
143         ret = 0;
144     }
145     else
146     {
147         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with invalid address `%s'\n",
148             stage, GNUNET_i2s (&address->peer));
149         ret = 1;
150     }
151
152     if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info[0], test_ats_count))
153     {
154       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n");
155       ret = 1;
156     }
157
158     if (bw_in > wan_quota_in)
159     {
160         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
161             stage, bw_in, wan_quota_in);
162         ret = 1;
163     }
164     else
165       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Suggested WAN inbound quota %u, allowed quota %llu \n",
166           stage, bw_in, wan_quota_in);
167
168     if (bw_out > wan_quota_out)
169     {
170         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
171             stage, bw_out, wan_quota_out);
172         ret = 1;
173     }
174     else
175       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Suggested WAN outbound quota %u, allowed quota %llu \n",
176           stage, bw_out, wan_quota_out);
177
178     if (1 == ret)
179     {
180       GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
181       GNUNET_SCHEDULER_add_now (&end, NULL);
182       return;
183     }
184
185     p[0].bw_out_assigned = bw_out;
186     p[0].bw_in_assigned = bw_in;
187     stage ++;
188
189     /* Add a 2nd address to give ATS an suggestion alternative */
190     /* Prepare ATS Information */
191     test_ats_info[1][0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
192     test_ats_info[1][0].value = htonl(GNUNET_ATS_NET_LAN);
193     test_ats_info[1][1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
194     test_ats_info[1][1].value = htonl(1);
195     test_ats_count = 2;
196
197     /* Adding address with session */
198     test_session[1] = &test_addr[1];
199     create_test_address (&test_addr[1], "test1", test_session[1], "test1", strlen ("test1") + 1);
200     test_hello_address[1].peer = p[0].id;
201     test_hello_address[1].transport_name = test_addr[1].plugin;
202     test_hello_address[1].address = test_addr[1].addr;
203     test_hello_address[1].address_length = test_addr[1].addr_len;
204     GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], test_session[1],
205         test_ats_info[1], test_ats_count);
206
207
208     /* Changing 1st address to network with now bw available (WLAN) */
209     test_ats_info[0][0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
210     test_ats_info[0][0].value = htonl(GNUNET_ATS_NET_WLAN);
211     test_ats_info[0][1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
212     test_ats_info[0][1].value = htonl(1);
213     test_ats_count = 2;
214
215     GNUNET_ATS_address_update (sched_ats, &test_hello_address[0], test_session[0],
216         test_ats_info[0], test_ats_count);
217     return;
218   }
219   if (1 == stage)
220   {
221     /* Bandwidth update to (in/out) 0/0 for 1st address */
222     if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
223     {
224         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n",
225                     stage, GNUNET_i2s (&address->peer));
226         ret = 0;
227     }
228     else
229     {
230         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with invalid address `%s'\n",
231             stage, GNUNET_i2s (&address->peer));
232         ret = 1;
233     }
234
235     if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info[0], test_ats_count))
236     {
237       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n");
238       ret = 1;
239     }
240
241     if ((bw_in != 0) || (bw_out != 0))
242     {
243         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: ATS did not set bandwidth to 0 but instead to %u/%u \n",
244             stage, bw_in, wan_quota_in);
245         ret = 1;
246     }
247
248     if (1 == ret)
249     {
250       GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
251       GNUNET_SCHEDULER_add_now (&end, NULL);
252       return;
253     }
254     p[0].bw_out_assigned = bw_out;
255     p[0].bw_in_assigned = bw_in;
256     stage ++;
257     return;
258   }
259   if (2 == stage)
260   {
261     /* Expecting suggestion of alternative 2nd address*/
262     if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[1], test_session[1]))
263     {
264         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n",
265                     stage, GNUNET_i2s (&address->peer));
266         ret = 0;
267     }
268     else
269     {
270         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with invalid address `%s'\n",
271             stage, GNUNET_i2s (&address->peer));
272         ret = 1;
273     }
274
275     if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info[1], test_ats_count))
276     {
277       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n",
278           stage);
279       ret = 1;
280     }
281
282     if ((bw_in == 0) || (bw_out == 0))
283     {
284         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: ATS did not set bandwidth correctly \n",
285             stage);
286         ret = 1;
287     }
288
289     if (1 == ret)
290     {
291       GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
292       GNUNET_SCHEDULER_add_now (&end, NULL);
293       return;
294     }
295
296     GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
297     GNUNET_SCHEDULER_add_now (&end, NULL);
298     ret = 0;
299     return;
300   }
301 }
302
303 static void
304 run (void *cls,
305      const struct GNUNET_CONFIGURATION_Handle *cfg,
306      struct GNUNET_TESTING_Peer *peer)
307 {
308   char *quota_str;
309
310   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_str))
311   {
312       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
313       ret = 1;
314       return;
315   }
316   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
317   {
318       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
319       ret = 1;
320       GNUNET_free (quota_str);
321       return;
322   }
323   GNUNET_free (quota_str);
324   quota_str = NULL;
325
326   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_str))
327   {
328       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
329       ret = 1;
330       return;
331   }
332   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
333   {
334       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
335       GNUNET_free (quota_str);
336       ret = 1;
337       return;
338   }
339   GNUNET_free (quota_str);
340   quota_str = NULL;
341   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
342   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
343
344
345   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
346
347   /* Connect to ATS scheduling */
348   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
349   if (sched_ats == NULL)
350   {
351     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
352     ret = 1;
353     end ();
354     return;
355   }
356
357   /* Set up peer 0 */
358   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
359   {
360       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
361       ret = GNUNET_SYSERR;
362       end ();
363       return;
364   }
365
366   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
367
368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
369               GNUNET_i2s(&p[0].id));
370
371   /* Set up peer 1*/
372   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
373   {
374       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
375       ret = GNUNET_SYSERR;
376       end ();
377       return;
378   }
379
380   GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
381
382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
383               GNUNET_i2s(&p[1].id));
384
385   /* Prepare ATS Information */
386   test_ats_info[0][0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
387   test_ats_info[0][0].value = htonl(GNUNET_ATS_NET_WAN);
388   test_ats_info[0][1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
389   test_ats_info[0][1].value = htonl(1);
390   test_ats_count = 2;
391
392   /* Adding address with session */
393   test_session[0] = &test_addr[0];
394   create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
395   test_hello_address[0].peer = p[0].id;
396   test_hello_address[0].transport_name = test_addr[0].plugin;
397   test_hello_address[0].address = test_addr[0].addr;
398   test_hello_address[0].address_length = test_addr[0].addr_len;
399   GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0],
400       test_ats_info[0], test_ats_count);
401
402   GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
403 }
404
405
406 int
407 main (int argc, char *argv[])
408 {
409   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_check_min_bw_alt",
410                                     "test_ats_api.conf",
411                                     &run, NULL))
412     return 1;
413   return ret;
414 }
415
416
417 /* end of file test_ats_api_scheduling_check_min_bw_alt.c */