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