-report error if starting openssl failed
[oweals/gnunet.git] / src / ats / test_ats_simplistic_switch_networks.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.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 static uint32_t
108 find_ats_value (const struct GNUNET_ATS_Information *atsi,
109                 uint32_t ats_count,
110                 uint32_t value)
111 {
112   int c;
113   for (c = 0; c < ats_count; c ++)
114   {
115       if (ntohl(atsi[c].type) == value)
116           return ntohl (atsi[c].value);
117   }
118   GNUNET_break (0);
119   return UINT32_MAX;
120 }
121
122
123 static void
124 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
125                     struct Session *session,
126                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
127                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
128                     const struct GNUNET_ATS_Information *atsi,
129                     uint32_t ats_count)
130 {
131   static int stage = 0;
132   int level;
133   char *text;
134   if (0 == stage)
135   {
136     //GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
137     if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
138     {
139       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback for correct address `%s'\n",
140                   stage, GNUNET_i2s (&address->peer));
141       ret = 0;
142     }
143     else
144     {
145       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n",
146                   stage, GNUNET_i2s (&address->peer));
147       ret = 1;
148       GNUNET_SCHEDULER_add_now (&end, NULL);
149       return;
150     }
151
152     if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
153     {
154       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n", stage);
155       ret = 1;
156       GNUNET_SCHEDULER_add_now (&end, NULL);
157       return;
158     }
159
160     if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_WAN])
161     {
162         level = GNUNET_ERROR_TYPE_DEBUG;
163         text =  "correct";
164         ret = 0;
165     }
166     else
167     {
168         level = GNUNET_ERROR_TYPE_ERROR;
169         text = "wrong";
170         ret = 1;
171     }
172
173     GNUNET_log (level, "Stage %u: WAN outbound quota out %s: Received %llu, configured %llu\n",
174         stage,
175         text,
176         (unsigned long long int) ntohl(bandwidth_out.value__),
177         quota_out[GNUNET_ATS_NET_WAN]);
178
179     if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_WAN])
180     {
181         level = GNUNET_ERROR_TYPE_DEBUG;
182         text =  "correct";
183         ret = 0;
184     }
185     else
186     {
187         level = GNUNET_ERROR_TYPE_ERROR;
188         text = "wrong";
189         ret = 1;
190     }
191
192     GNUNET_log (level, "Stage %u: WAN inbound quota out %s: Received %llu, configured %llu\n",
193         stage,
194         text,
195         (unsigned long long int) ntohl(bandwidth_out.value__),
196         quota_out[GNUNET_ATS_NET_WAN]);
197
198     if (GNUNET_ATS_NET_WAN != find_ats_value (atsi, ats_count, GNUNET_ATS_NETWORK_TYPE))
199     {
200         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Incorrect network type, exptected %s, got %s \n",
201             stage,
202             GNUNET_ATS_print_network_type(GNUNET_ATS_NET_WAN),
203             GNUNET_ATS_print_network_type(find_ats_value (atsi, ats_count, GNUNET_ATS_NETWORK_TYPE)));
204         ret = 1;
205     }
206
207     if (1 == ret)
208     {
209         GNUNET_SCHEDULER_add_now (&end, NULL);
210         return;
211     }
212
213     /* Update address */
214     /* Prepare ATS Information: change network */
215     test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
216     test_ats_info[0].value = htonl(GNUNET_ATS_NET_LAN);
217     test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
218     test_ats_info[1].value = htonl(3);
219     test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
220     test_ats_info[1].value = htonl(30);
221     test_ats_count = 2;
222
223     GNUNET_ATS_address_update (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
224
225     /* Request address */
226     GNUNET_ATS_suggest_address (sched_ats, &p.id);
227     stage ++;
228   }
229   else if (1 == stage)
230   {
231       GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
232       if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
233       {
234         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n", stage,
235                     GNUNET_i2s (&address->peer));
236         ret = 0;
237       }
238       else
239       {
240         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n", stage,
241                     GNUNET_i2s (&address->peer));
242         ret = 1;
243       }
244
245       if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
246       {
247         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n");
248         ret = 1;
249         GNUNET_SCHEDULER_add_now (&end, NULL);
250         return;
251       }
252
253       if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_LAN])
254       {
255           level = GNUNET_ERROR_TYPE_DEBUG;
256           text =  "correct";
257           ret = 0;
258       }
259       else
260       {
261           level = GNUNET_ERROR_TYPE_ERROR;
262           text = "wrong";
263           ret = 1;
264       }
265
266       GNUNET_log (level, "Stage %u: LAN outbound quota out %s: Received %llu, configured %llu\n",
267           stage,
268           text,
269           (unsigned long long int) ntohl(bandwidth_out.value__),
270           quota_out[GNUNET_ATS_NET_LAN]);
271
272       if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_LAN])
273         {
274             level = GNUNET_ERROR_TYPE_DEBUG;
275             text =  "correct";
276             ret = 0;
277         }
278         else
279         {
280             level = GNUNET_ERROR_TYPE_ERROR;
281             text = "wrong";
282             ret = 1;
283         }
284
285         GNUNET_log (level, "Stage %u: LAN inbound quota out %s: Received %llu, configured %llu\n",
286             stage,
287             text,
288             (unsigned long long int) ntohl(bandwidth_out.value__),
289             quota_out[GNUNET_ATS_NET_LAN]);
290
291         if (GNUNET_ATS_NET_LAN != find_ats_value (atsi, ats_count, GNUNET_ATS_NETWORK_TYPE))
292         {
293             GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Incorrect network type, exptected %s, got %s \n",
294                 stage,
295                 GNUNET_ATS_print_network_type(GNUNET_ATS_NET_LAN),
296                 GNUNET_ATS_print_network_type(find_ats_value (atsi, ats_count, GNUNET_ATS_NETWORK_TYPE)));
297             ret = 1;
298         }
299
300       GNUNET_SCHEDULER_add_now (&end, NULL);
301   }
302 }
303
304
305 static void
306 run (void *cls,
307      const struct GNUNET_CONFIGURATION_Handle *cfg,
308      struct GNUNET_TESTING_Peer *peer)
309 {
310   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
311
312   load_quotas (cfg, quota_out, quota_in, GNUNET_ATS_NetworkTypeCount);
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 */
325   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.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.id)));
334
335   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
336               GNUNET_i2s_full(&p.id));
337
338   /* Prepare ATS Information */
339   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
340   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
341   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
342   test_ats_info[1].value = htonl(1);
343   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
344   test_ats_info[1].value = htonl(10);
345   test_ats_count = 2;
346
347   /* Adding address without session */
348   test_session = &test_addr;
349   create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
350   test_hello_address.peer = p.id;
351   test_hello_address.transport_name = test_addr.plugin;
352   test_hello_address.address = test_addr.addr;
353   test_hello_address.address_length = test_addr.addr_len;
354   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
355
356   /* Request address */
357   GNUNET_ATS_suggest_address (sched_ats, &p.id);
358 }
359
360
361 int
362 main (int argc, char *argv[])
363 {
364   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_update_address",
365                                     "test_ats_api.conf",
366                                     &run, NULL))
367     return 1;
368   return ret;
369 }
370
371 /* end of file test_ats_api_scheduling_update_address.c */