-add newline
[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
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_ATS_suggest_address_cancel (sched_ats, &p[1].id);
258         GNUNET_SCHEDULER_add_now (&end, NULL);
259         return;
260       }
261       stage ++;
262
263       GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
264       GNUNET_SCHEDULER_add_now (&end, NULL);
265       return;
266   }
267
268 }
269
270 static void
271 run (void *cls,
272      const struct GNUNET_CONFIGURATION_Handle *cfg,
273      struct GNUNET_TESTING_Peer *peer)
274 {
275   char *quota_str;
276
277   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_str))
278   {
279       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
280       ret = 1;
281       return;
282   }
283   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
284   {
285       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
286       ret = 1;
287       GNUNET_free (quota_str);
288       return;
289   }
290   GNUNET_free (quota_str);
291   quota_str = NULL;
292
293   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_str))
294   {
295       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
296       ret = 1;
297       return;
298   }
299   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
300   {
301       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
302       GNUNET_free (quota_str);
303       ret = 1;
304       return;
305   }
306   GNUNET_free (quota_str);
307   quota_str = NULL;
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
310
311
312   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
313
314   /* Connect to ATS scheduling */
315   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
316   if (sched_ats == NULL)
317   {
318     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
319     ret = 1;
320     end ();
321     return;
322   }
323
324   /* Set up peer 0 */
325   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
326   {
327       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
328       ret = GNUNET_SYSERR;
329       end ();
330       return;
331   }
332
333   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
334
335   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
336               GNUNET_i2s(&p[0].id));
337
338   /* Set up peer 1*/
339   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
340   {
341       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
342       ret = GNUNET_SYSERR;
343       end ();
344       return;
345   }
346
347   GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
348
349   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
350               GNUNET_i2s(&p[1].id));
351
352   /* Prepare ATS Information */
353   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
354   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
355   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
356   test_ats_info[1].value = htonl(1);
357   test_ats_count = 2;
358
359   /* Adding address with session */
360   test_session[0] = &test_addr[0];
361   create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
362   test_hello_address[0].peer = p[0].id;
363   test_hello_address[0].transport_name = test_addr[0].plugin;
364   test_hello_address[0].address = test_addr[0].addr;
365   test_hello_address[0].address_length = test_addr[0].addr_len;
366   GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
367
368   GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
369 }
370
371
372 int
373 main (int argc, char *argv[])
374 {
375   if (0 != GNUNET_TESTING_peer_run ("test_ats_simplististic",
376                                     "test_ats_api.conf",
377                                     &run, NULL))
378     return 1;
379   return ret;
380 }
381
382
383 /* end of file test_ats_api_bandwidth_consumption.c */