- preference mgmt
[oweals/gnunet.git] / src / ats / test_ats_simplistic_change_preference.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_simplistic_change_preference.c
29  * @brief test for changing preferences in 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  * Scheduling handle
50  */
51 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
52
53
54 /**
55  * Return value
56  */
57 static int ret;
58
59 /**
60  * Test address
61  */
62 static struct Test_Address test_addr[2];
63
64 /**
65  * Test peer
66  */
67 static struct PeerContext p[2];
68
69
70 /**
71  * HELLO address
72  */
73 struct GNUNET_HELLO_Address test_hello_address[2];
74
75 /**
76  * Session
77  */
78 static void *test_session[2];
79
80 /**
81  * Test ats info
82  */
83 struct GNUNET_ATS_Information test_ats_info[2];
84
85 /**
86  * Test ats count
87  */
88 uint32_t test_ats_count;
89
90 /**
91  * Configured WAN out quota
92  */
93 unsigned long long wan_quota_out;
94
95 /**
96  * Configured WAN in quota
97  */
98 unsigned long long wan_quota_in;
99
100
101 static void
102 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   die_task = GNUNET_SCHEDULER_NO_TASK;
105
106   if (sched_ats != NULL)
107     GNUNET_ATS_scheduling_done (sched_ats);
108   if (perf_ats != NULL)
109     GNUNET_ATS_performance_done (perf_ats);
110   free_test_address (&test_addr[0]);
111   ret = GNUNET_SYSERR;
112 }
113
114
115 static void
116 end ()
117 {
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
119   if (die_task != GNUNET_SCHEDULER_NO_TASK)
120   {
121     GNUNET_SCHEDULER_cancel (die_task);
122     die_task = GNUNET_SCHEDULER_NO_TASK;
123   }
124   GNUNET_ATS_performance_done (perf_ats);
125   perf_ats = NULL;
126   GNUNET_ATS_scheduling_done (sched_ats);
127   sched_ats = NULL;
128   free_test_address (&test_addr[0]);
129 }
130
131
132 static void
133 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
134                     struct Session *session,
135                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
136                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
137                     const struct GNUNET_ATS_Information *atsi,
138                     uint32_t ats_count)
139 {
140   static int stage = 0;
141   unsigned int bw_in = ntohl(bandwidth_in.value__);
142   unsigned int bw_out = ntohl(bandwidth_out.value__);
143   if (0 == stage)
144   {
145     if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
146     {
147         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
148                     GNUNET_i2s (&address->peer));
149         ret = 0;
150     }
151     else
152     {
153         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
154                     GNUNET_i2s (&address->peer));
155         ret = 1;
156     }
157
158     if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
159     {
160       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
161       ret = 1;
162     }
163
164     if (bw_in > wan_quota_in)
165     {
166         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
167             bw_in, wan_quota_in);
168         ret = 1;
169     }
170     else
171       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
172           bw_in, wan_quota_in);
173
174     if (bw_out > wan_quota_out)
175     {
176         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
177             bw_out, wan_quota_out);
178         ret = 1;
179     }
180     else
181       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
182           bw_out, wan_quota_out);
183
184     if (1 == ret)
185     {
186       GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
187       GNUNET_SCHEDULER_add_now (&end, NULL);
188       return;
189     }
190
191     stage ++;
192
193     /* Change preference */
194     GNUNET_ATS_change_preference (perf_ats,
195         &p[0].id,
196         GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
197
198     /* Request address */
199     GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
200     return;
201   }
202   if (1 == stage)
203   {
204       /* Expecting callback for address[0] with updated quota and no callback for address[1]*/
205       if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
206       {
207           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with correct address `%s'\n",
208                       GNUNET_i2s (&address->peer));
209           ret = 0;
210       }
211       else
212       {
213           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with invalid address `%s'\n",
214                       GNUNET_i2s (&address->peer));
215           ret = 1;
216       }
217
218       if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
219       {
220         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Callback with incorrect ats info \n");
221         ret = 1;
222       }
223
224       if (bw_in > wan_quota_in)
225       {
226           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
227               bw_in, wan_quota_in);
228           ret = 1;
229       }
230       else if (p[0].bw_in_assigned > bw_in)
231       {
232           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
233               bw_in, p[0].bw_in_assigned);
234           ret = 1;
235       }
236       else
237         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
238             bw_in, wan_quota_in);
239
240       if (bw_out > wan_quota_out)
241       {
242           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
243               bw_out, wan_quota_out);
244           ret = 1;
245       }
246       else if (p[0].bw_out_assigned > bw_out)
247       {
248           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
249               bw_out, p[0].bw_out_assigned);
250           ret = 1;
251       }
252       else
253         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
254             bw_out, wan_quota_out);
255
256       if (1 == ret)
257       {
258         GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
259         GNUNET_SCHEDULER_add_now (&end, NULL);
260         return;
261       }
262       stage ++;
263
264       GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
265       GNUNET_SCHEDULER_add_now (&end, NULL);
266       return;
267   }
268
269 }
270
271 static void
272 run (void *cls,
273      const struct GNUNET_CONFIGURATION_Handle *cfg,
274      struct GNUNET_TESTING_Peer *peer)
275 {
276   char *quota_str;
277
278   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_str))
279   {
280       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
281       ret = 1;
282       return;
283   }
284   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
285   {
286       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
287       ret = 1;
288       GNUNET_free (quota_str);
289       return;
290   }
291   GNUNET_free (quota_str);
292   quota_str = NULL;
293
294   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_str))
295   {
296       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
297       ret = 1;
298       return;
299   }
300   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
301   {
302       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
303       GNUNET_free (quota_str);
304       ret = 1;
305       return;
306   }
307   GNUNET_free (quota_str);
308   quota_str = NULL;
309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
310   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
311
312
313   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
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   perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL);
326   if (perf_ats == NULL)
327   {
328     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS performance!\n");
329     ret = 1;
330     end ();
331     return;
332   }
333
334   /* Set up peer 0 */
335   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
336   {
337       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
338       ret = GNUNET_SYSERR;
339       end ();
340       return;
341   }
342
343   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
344
345   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
346               GNUNET_i2s(&p[0].id));
347
348   /* Set up peer 1*/
349   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
350   {
351       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
352       ret = GNUNET_SYSERR;
353       end ();
354       return;
355   }
356
357   GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
358
359   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
360               GNUNET_i2s(&p[1].id));
361
362   /* Prepare ATS Information */
363   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
364   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
365   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
366   test_ats_info[1].value = htonl(1);
367   test_ats_count = 2;
368
369   /* Adding address with session */
370   test_session[0] = &test_addr[0];
371   create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
372   test_hello_address[0].peer = p[0].id;
373   test_hello_address[0].transport_name = test_addr[0].plugin;
374   test_hello_address[0].address = test_addr[0].addr;
375   test_hello_address[0].address_length = test_addr[0].addr_len;
376   GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
377
378   GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
379 }
380
381
382 int
383 main (int argc, char *argv[])
384 {
385   if (0 != GNUNET_TESTING_peer_run ("test_ats_simplistic_change_preference",
386                                     "test_ats_api.conf",
387                                     &run, NULL))
388     return 1;
389   return ret;
390 }
391
392
393 /* end of file test_ats_simplistic_change_preference.c */