test and change
[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 static GNUNET_SCHEDULER_TaskIdentifier die_task;
40
41 /**
42  * Scheduling handle
43  */
44 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
45
46 /**
47  * Return value
48  */
49 static int ret;
50
51 /**
52  * Test address
53  */
54 static struct Test_Address test_addr[2];
55
56 /**
57  * Test peer
58  */
59 static struct PeerContext p;
60
61 /**
62  * HELLO address
63  */
64 struct GNUNET_HELLO_Address test_hello_address[2];
65
66 /**
67  * Session
68  */
69 static void *test_session[2];
70
71 /**
72  * Test ats info
73  */
74 struct GNUNET_ATS_Information test_ats_info[2];
75
76 /**
77  * Test ats count
78  */
79 uint32_t test_ats_count;
80
81 /**
82  * Configured WAN out quota
83  */
84 unsigned long long wan_quota_out;
85
86 /**
87  * Configured WAN in quota
88  */
89 unsigned long long wan_quota_in;
90
91 static void
92 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
93 {
94   dest->plugin = GNUNET_strdup (plugin);
95   dest->session = session;
96   dest->addr = GNUNET_malloc (addrlen);
97   memcpy (dest->addr, addr, addrlen);
98   dest->addr_len = addrlen;
99 }
100
101 static void
102 free_test_address (struct Test_Address *dest)
103 {
104   GNUNET_free (dest->plugin);
105   GNUNET_free (dest->addr);
106 }
107
108
109 static void
110 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   die_task = GNUNET_SCHEDULER_NO_TASK;
113
114   if (sched_ats != NULL)
115     GNUNET_ATS_scheduling_done (sched_ats);
116   free_test_address (&test_addr[0]);
117   ret = GNUNET_SYSERR;
118 }
119
120
121 static void
122 end ()
123 {
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
125   if (die_task != GNUNET_SCHEDULER_NO_TASK)
126   {
127     GNUNET_SCHEDULER_cancel (die_task);
128     die_task = GNUNET_SCHEDULER_NO_TASK;
129   }
130   GNUNET_ATS_scheduling_done (sched_ats);
131   sched_ats = NULL;
132   free_test_address (&test_addr[0]);
133 }
134
135 static int
136 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
137                    const struct GNUNET_HELLO_Address *address2, void *session2)
138 {
139   if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
140   {
141       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
142       return GNUNET_SYSERR;
143   }
144   if (0 != strcmp (address1->transport_name, address2->transport_name))
145   {
146       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
147       return GNUNET_SYSERR;
148   }
149   if (address1->address_length != address2->address_length)
150   {
151       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
152       return GNUNET_SYSERR;
153
154   }
155   else if (0 != memcmp (address1->address, address2->address, address2->address_length))
156   {
157       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
158       return GNUNET_SYSERR;
159   }
160   if (session1 != session2)
161   {
162       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
163                   session1, session2);
164       return GNUNET_SYSERR;
165
166   }
167   return GNUNET_OK;
168 }
169
170
171 static int
172 compare_ats (const struct GNUNET_ATS_Information *ats_is, uint32_t ats_count_is,
173              const struct GNUNET_ATS_Information *ats_should, uint32_t ats_count_should)
174 {
175   unsigned int c_o;
176   unsigned int c_i;
177   char *prop[] = GNUNET_ATS_PropertyStrings;
178   uint32_t type1;
179   uint32_t type2;
180   uint32_t val1;
181   uint32_t val2;
182   int res = GNUNET_OK;
183
184   for (c_o = 0; c_o < ats_count_is; c_o++)
185   {
186     for (c_i = 0; c_i < ats_count_should; c_i++)
187     {
188         type1 = ntohl(ats_is[c_o].type);
189         type2 = ntohl(ats_should[c_i].type);
190         if (type1 == type2)
191         {
192             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS type `%s'\n",
193                         prop[type1]);
194             val1 = ntohl(ats_is[c_o].value);
195             val2 = ntohl(ats_should[c_i].value);
196             if (val1 != val2)
197             {
198                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS value `%s' not equal: %u != %u\n",
199                             prop[type1],
200                             val1, val2);
201                 res = GNUNET_SYSERR;
202             }
203             else
204             {
205               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS value `%s' equal: %u == %u\n",
206                           prop[type1],
207                           val1, val2);
208             }
209         }
210     }
211   }
212   return res;
213 }
214
215 static void
216 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
217                     struct Session *session,
218                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
219                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
220                     const struct GNUNET_ATS_Information *atsi,
221                     uint32_t ats_count)
222 {
223   if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
224   {
225       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
226                   GNUNET_i2s (&address->peer));
227       ret = 0;
228   }
229   else
230   {
231       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
232                   GNUNET_i2s (&address->peer));
233       ret = 1;
234   }
235
236   if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
237   {
238     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
239     ret = 1;
240   }
241
242   if (ntohl(bandwidth_in.value__) > wan_quota_in)
243   {
244       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
245           (unsigned int) ntohl(bandwidth_in.value__), wan_quota_in);
246       ret = 1;
247   }
248   else
249     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
250         (unsigned int) ntohl(bandwidth_in.value__), wan_quota_in);
251
252   if (ntohl(bandwidth_out.value__) > wan_quota_out)
253   {
254       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
255           (unsigned int) ntohl(bandwidth_out.value__), wan_quota_out);
256       ret = 1;
257   }
258   else
259     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
260         (unsigned int) ntohl(bandwidth_out.value__), wan_quota_out);
261
262
263
264
265   GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
266   GNUNET_SCHEDULER_add_now (&end, NULL);
267 }
268
269 static void
270 run (void *cls,
271      const struct GNUNET_CONFIGURATION_Handle *cfg,
272      struct GNUNET_TESTING_Peer *peer)
273 {
274   char *quota_str;
275
276   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_str))
277   {
278       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
279       ret = 1;
280       return;
281   }
282   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
283   {
284       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
285       ret = 1;
286       GNUNET_free (quota_str);
287       return;
288   }
289   GNUNET_free (quota_str);
290   quota_str = NULL;
291
292   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_str))
293   {
294       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
295       ret = 1;
296       return;
297   }
298   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
299   {
300       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
301       GNUNET_free (quota_str);
302       ret = 1;
303       return;
304   }
305   GNUNET_free (quota_str);
306   quota_str = NULL;
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
309
310
311   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
312
313   /* Connect to ATS scheduling */
314   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
315   if (sched_ats == NULL)
316   {
317     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
318     ret = 1;
319     end ();
320     return;
321   }
322
323   /* Set up peer */
324   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
325   {
326       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
327       ret = GNUNET_SYSERR;
328       end ();
329       return;
330   }
331
332   GNUNET_assert (0 == strcmp (PEERID, GNUNET_i2s_full (&p.id)));
333
334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
335               GNUNET_i2s(&p.id));
336
337   /* Prepare ATS Information */
338   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
339   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
340   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
341   test_ats_info[1].value = htonl(1);
342   test_ats_count = 2;
343
344   /* Adding address without session */
345   test_session[0] = &test_addr[0];
346   create_test_address (&test_addr[0], "test", test_session, "test", strlen ("test") + 1);
347   test_hello_address[0].peer = p.id;
348   test_hello_address[0].transport_name = test_addr[0].plugin;
349   test_hello_address[0].address = test_addr[0].addr;
350   test_hello_address[0].address_length = test_addr[0].addr_len;
351   GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
352
353   GNUNET_ATS_suggest_address (sched_ats, &p.id);
354 }
355
356
357 int
358 main (int argc, char *argv[])
359 {
360   if (0 != GNUNET_TESTING_peer_run ("test_ats_simplististic",
361                                     "test_ats_api.conf",
362                                     &run, NULL))
363     return 1;
364   return ret;
365 }
366
367
368 /* end of file test_ats_api_bandwidth_consumption.c */