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