fb2ea70e7ed7a768bdf1d9c8fe6928808b7bd7b6
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_switch_network.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_api_scheduling_update_address.c
22  * @brief test updating networtk type of an address
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib-new.h"
29 #include "ats.h"
30 #include "test_ats_api_common.h"
31
32 #define BIG_M_STRING "unlimited"
33
34
35 static GNUNET_SCHEDULER_TaskIdentifier die_task;
36
37 /**
38  * Scheduling handle
39  */
40 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
41
42 /**
43  * Return value
44  */
45 static int ret;
46
47 /**
48  * Test address
49  */
50 static struct Test_Address test_addr;
51
52 /**
53  * Test peer
54  */
55 static struct PeerContext p;
56
57 /**
58  * HELLO test address
59  */
60
61 struct GNUNET_HELLO_Address test_hello_address;
62
63 /**
64  * Test session
65  */
66 static void *test_session;
67
68 /**
69  * Test ats info
70  */
71 struct GNUNET_ATS_Information test_ats_info[3];
72
73 /**
74  * Test ats count
75  */
76 uint32_t test_ats_count;
77
78 unsigned long long int quota_out[GNUNET_ATS_NetworkTypeCount];
79 unsigned long long int quota_in[GNUNET_ATS_NetworkTypeCount];
80
81 static void
82 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 {
84   die_task = GNUNET_SCHEDULER_NO_TASK;
85
86   if (sched_ats != NULL)
87     GNUNET_ATS_scheduling_done (sched_ats);
88   free_test_address (&test_addr);
89   ret = GNUNET_SYSERR;
90 }
91
92
93 static void
94 end ()
95 {
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
97   if (die_task != GNUNET_SCHEDULER_NO_TASK)
98   {
99     GNUNET_SCHEDULER_cancel (die_task);
100     die_task = GNUNET_SCHEDULER_NO_TASK;
101   }
102   GNUNET_ATS_scheduling_done (sched_ats);
103   sched_ats = NULL;
104   free_test_address (&test_addr);
105 }
106
107
108 static void
109 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
110                     struct Session *session,
111                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
112                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
113                     const struct GNUNET_ATS_Information *atsi,
114                     uint32_t ats_count)
115 {
116   static int stage = 0;
117   int level;
118   char *text;
119   if (0 == stage)
120   {
121     GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
122     if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
123     {
124       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback for correct address `%s'\n",
125                   stage, GNUNET_i2s (&address->peer));
126       ret = 0;
127     }
128     else
129     {
130       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n",
131                   stage, GNUNET_i2s (&address->peer));
132       ret = 1;
133       GNUNET_SCHEDULER_add_now (&end, NULL);
134       return;
135     }
136
137     if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
138     {
139       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n", stage);
140       ret = 1;
141       GNUNET_SCHEDULER_add_now (&end, NULL);
142       return;
143     }
144
145     if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_WAN])
146     {
147         level = GNUNET_ERROR_TYPE_DEBUG;
148         text =  "correct";
149         ret = 0;
150     }
151     else
152     {
153         level = GNUNET_ERROR_TYPE_ERROR;
154         text = "wrong";
155         ret = 1;
156     }
157
158     GNUNET_log (level, "Stage %u: WAN outbound quota out %s: Received %llu, configured %llu\n",
159         stage,
160         text,
161         (unsigned long long int) ntohl(bandwidth_out.value__),
162         quota_out[GNUNET_ATS_NET_WAN]);
163
164     if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_WAN])
165     {
166         level = GNUNET_ERROR_TYPE_DEBUG;
167         text =  "correct";
168         ret = 0;
169     }
170     else
171     {
172         level = GNUNET_ERROR_TYPE_ERROR;
173         text = "wrong";
174         ret = 1;
175     }
176
177
178     GNUNET_log (level, "Stage %u: WAN inbound quota out %s: Received %llu, configured %llu\n",
179         stage,
180         text,
181         (unsigned long long int) ntohl(bandwidth_out.value__),
182         quota_out[GNUNET_ATS_NET_WAN]);
183
184     if (1 == ret)
185     {
186         GNUNET_SCHEDULER_add_now (&end, NULL);
187         return;
188     }
189
190     /* Update address */
191     /* Prepare ATS Information: change network */
192     test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
193     test_ats_info[0].value = htonl(GNUNET_ATS_NET_LAN);
194     test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
195     test_ats_info[1].value = htonl(3);
196     test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
197     test_ats_info[1].value = htonl(30);
198     test_ats_count = 2;
199
200     GNUNET_ATS_address_update (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
201
202     /* Request address */
203     GNUNET_ATS_suggest_address (sched_ats, &p.id);
204     stage ++;
205   }
206   else if (1 == stage)
207   {
208       GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
209       if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
210       {
211         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n", stage,
212                     GNUNET_i2s (&address->peer));
213         ret = 0;
214       }
215       else
216       {
217         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n", stage,
218                     GNUNET_i2s (&address->peer));
219         ret = 1;
220       }
221
222       if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
223       {
224         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n");
225         ret = 1;
226         GNUNET_SCHEDULER_add_now (&end, NULL);
227         return;
228       }
229
230       if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_LAN])
231       {
232           level = GNUNET_ERROR_TYPE_DEBUG;
233           text =  "correct";
234           ret = 0;
235       }
236       else
237       {
238           level = GNUNET_ERROR_TYPE_ERROR;
239           text = "wrong";
240           ret = 1;
241       }
242
243       GNUNET_log (level, "Stage %u: LAN outbound quota out %s: Received %llu, configured %llu\n",
244           stage,
245           text,
246           (unsigned long long int) ntohl(bandwidth_out.value__),
247           quota_out[GNUNET_ATS_NET_LAN]);
248
249       if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_LAN])
250         {
251             level = GNUNET_ERROR_TYPE_DEBUG;
252             text =  "correct";
253             ret = 0;
254         }
255         else
256         {
257             level = GNUNET_ERROR_TYPE_ERROR;
258             text = "wrong";
259             ret = 1;
260         }
261
262         GNUNET_log (level, "Stage %u: LAN inbound quota out %s: Received %llu, configured %llu\n",
263             stage,
264             text,
265             (unsigned long long int) ntohl(bandwidth_out.value__),
266             quota_out[GNUNET_ATS_NET_LAN]);
267
268       GNUNET_SCHEDULER_add_now (&end, NULL);
269   }
270 }
271
272 static unsigned int
273 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
274 {
275   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
276   char * entry_in = NULL;
277   char * entry_out = NULL;
278   char * quota_out_str;
279   char * quota_in_str;
280   int c;
281
282   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
283   {
284     in_dest[c] = 0;
285     out_dest[c] = 0;
286     switch (quotas[c]) {
287       case GNUNET_ATS_NET_UNSPECIFIED:
288         entry_out = "UNSPECIFIED_QUOTA_OUT";
289         entry_in = "UNSPECIFIED_QUOTA_IN";
290         break;
291       case GNUNET_ATS_NET_LOOPBACK:
292         entry_out = "LOOPBACK_QUOTA_OUT";
293         entry_in = "LOOPBACK_QUOTA_IN";
294         break;
295       case GNUNET_ATS_NET_LAN:
296         entry_out = "LAN_QUOTA_OUT";
297         entry_in = "LAN_QUOTA_IN";
298         break;
299       case GNUNET_ATS_NET_WAN:
300         entry_out = "WAN_QUOTA_OUT";
301         entry_in = "WAN_QUOTA_IN";
302         break;
303       case GNUNET_ATS_NET_WLAN:
304         entry_out = "WLAN_QUOTA_OUT";
305         entry_in = "WLAN_QUOTA_IN";
306         break;
307       default:
308         break;
309     }
310
311     if ((entry_in == NULL) || (entry_out == NULL))
312       continue;
313
314     /* quota out */
315     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
316     {
317       if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
318           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
319         out_dest[c] = UINT32_MAX;
320
321       GNUNET_free (quota_out_str);
322       quota_out_str = NULL;
323     }
324     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
325       out_dest[c] = UINT32_MAX;
326     else
327       out_dest[c] = UINT32_MAX;
328
329     /* quota in */
330     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
331     {
332       if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
333           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
334         in_dest[c] = UINT32_MAX;
335
336       GNUNET_free (quota_in_str);
337       quota_in_str = NULL;
338     }
339     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
340     {
341       in_dest[c] = UINT32_MAX;
342     }
343     else
344     {
345         in_dest[c] = UINT32_MAX;
346     }
347     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
348
349   }
350   return GNUNET_ATS_NetworkTypeCount;
351 }
352
353 static void
354 run (void *cls,
355      const struct GNUNET_CONFIGURATION_Handle *cfg,
356      struct GNUNET_TESTING_Peer *peer)
357 {
358   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
359
360   load_quotas (cfg, quota_out, quota_in, GNUNET_ATS_NetworkTypeCount);
361
362   /* Connect to ATS scheduling */
363   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
364   if (sched_ats == NULL)
365   {
366     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
367     ret = 1;
368     end ();
369     return;
370   }
371
372   /* Set up peer */
373   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
374   {
375       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
376       ret = GNUNET_SYSERR;
377       end ();
378       return;
379   }
380
381   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
382
383   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
384               GNUNET_i2s_full(&p.id));
385
386   /* Prepare ATS Information */
387   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
388   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
389   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
390   test_ats_info[1].value = htonl(1);
391   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
392   test_ats_info[1].value = htonl(10);
393   test_ats_count = 2;
394
395   /* Adding address without session */
396   test_session = &test_addr;
397   create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
398   test_hello_address.peer = p.id;
399   test_hello_address.transport_name = test_addr.plugin;
400   test_hello_address.address = test_addr.addr;
401   test_hello_address.address_length = test_addr.addr_len;
402   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
403
404   /* Request address */
405   GNUNET_ATS_suggest_address (sched_ats, &p.id);
406 }
407
408
409 int
410 main (int argc, char *argv[])
411 {
412   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_update_address",
413                                     "test_ats_api.conf",
414                                     &run, NULL))
415     return 1;
416   return ret;
417 }
418
419 /* end of file test_ats_api_scheduling_update_address.c */