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