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