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