changes
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_block_and_reset.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_reset_backoff.c
22  * @brief test case for blocking suggests and blocking reset API
23  *        measure duration of initial suggest, measure blocking duration,
24  *        reset block, measure suggest, compare time
25  * @author Christian Grothoff
26  * @author Matthias Wachs
27  */
28 #include "platform.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_testing_lib-new.h"
31 #include "ats.h"
32 #include "test_ats_api_common.h"
33
34 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 10)
35
36 static GNUNET_SCHEDULER_TaskIdentifier die_task;
37
38 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
39
40 /**
41  * Scheduling handle
42  */
43 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
44
45 /**
46  * Return value
47  */
48 static int ret;
49
50 /**
51  * Test address
52  */
53 static struct Test_Address test_addr;
54
55 /**
56  * Test peer
57  */
58 static struct PeerContext p;
59
60 /**
61  * HELLO address
62  */
63 struct GNUNET_HELLO_Address test_hello_address;
64
65 /**
66  * Session
67  */
68 static void *test_session;
69
70 /**
71  * Test ats info
72  */
73 struct GNUNET_ATS_Information test_ats_info[2];
74
75 /**
76  * Test ats count
77  */
78 uint32_t test_ats_count;
79
80
81 struct GNUNET_TIME_Absolute initial_start;
82
83 struct GNUNET_TIME_Relative initial_duration;
84
85 /**
86  * Blocking start
87  */
88 struct GNUNET_TIME_Absolute block_start;
89
90 struct GNUNET_TIME_Relative block_duration;
91
92 struct GNUNET_TIME_Absolute reset_block_start;
93
94 struct GNUNET_TIME_Relative reset_block_duration;
95
96 static void
97 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
98 {
99   dest->plugin = GNUNET_strdup (plugin);
100   dest->session = session;
101   dest->addr = GNUNET_malloc (addrlen);
102   memcpy (dest->addr, addr, addrlen);
103   dest->addr_len = addrlen;
104 }
105
106 static void
107 free_test_address (struct Test_Address *dest)
108 {
109   GNUNET_free (dest->plugin);
110   GNUNET_free (dest->addr);
111 }
112
113
114 static void
115 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   die_task = GNUNET_SCHEDULER_NO_TASK;
118   if (wait_task != GNUNET_SCHEDULER_NO_TASK)
119   {
120     GNUNET_SCHEDULER_cancel (wait_task);
121     wait_task = GNUNET_SCHEDULER_NO_TASK;
122   }
123   if (sched_ats != NULL)
124     GNUNET_ATS_scheduling_done (sched_ats);
125   free_test_address (&test_addr);
126   ret = GNUNET_SYSERR;
127 }
128
129
130 static void
131 end ()
132 {
133   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
134   if (die_task != GNUNET_SCHEDULER_NO_TASK)
135   {
136     GNUNET_SCHEDULER_cancel (die_task);
137     die_task = GNUNET_SCHEDULER_NO_TASK;
138   }
139   if (wait_task != GNUNET_SCHEDULER_NO_TASK)
140   {
141     GNUNET_SCHEDULER_cancel (wait_task);
142     wait_task = GNUNET_SCHEDULER_NO_TASK;
143   }
144   GNUNET_ATS_scheduling_done (sched_ats);
145   sched_ats = NULL;
146   free_test_address (&test_addr);
147 }
148
149 static int
150 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
151                    const struct GNUNET_HELLO_Address *address2, void *session2)
152 {
153   if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
154   {
155       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
156       return GNUNET_SYSERR;
157   }
158   if (0 != strcmp (address1->transport_name, address2->transport_name))
159   {
160       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
161       return GNUNET_SYSERR;
162   }
163   if (address1->address_length != address2->address_length)
164   {
165       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
166       return GNUNET_SYSERR;
167
168   }
169   else if (0 != memcmp (address1->address, address2->address, address2->address_length))
170   {
171       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
172       return GNUNET_SYSERR;
173   }
174   if (session1 != session2)
175   {
176       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
177                   session1, session2);
178       return GNUNET_SYSERR;
179
180   }
181   return GNUNET_OK;
182 }
183
184
185 static int
186 compare_ats (const struct GNUNET_ATS_Information *ats_is, uint32_t ats_count_is,
187              const struct GNUNET_ATS_Information *ats_should, uint32_t ats_count_should)
188 {
189   unsigned int c_o;
190   unsigned int c_i;
191   char *prop[] = GNUNET_ATS_PropertyStrings;
192   uint32_t type1;
193   uint32_t type2;
194   uint32_t val1;
195   uint32_t val2;
196   int res = GNUNET_OK;
197
198   for (c_o = 0; c_o < ats_count_is; c_o++)
199   {
200     for (c_i = 0; c_i < ats_count_should; c_i++)
201     {
202         type1 = ntohl(ats_is[c_o].type);
203         type2 = ntohl(ats_should[c_i].type);
204         if (type1 == type2)
205         {
206             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS type `%s'\n",
207                         prop[type1]);
208             val1 = ntohl(ats_is[c_o].value);
209             val2 = ntohl(ats_should[c_i].value);
210             if (val1 != val2)
211             {
212                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS value `%s' not equal: %u != %u\n",
213                             prop[type1],
214                             val1, val2);
215                 res = GNUNET_SYSERR;
216             }
217             else
218             {
219               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS value `%s' equal: %u == %u\n",
220                           prop[type1],
221                           val1, val2);
222             }
223         }
224     }
225   }
226   return res;
227 }
228
229 static void
230 request_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
231 {
232   wait_task = GNUNET_SCHEDULER_NO_TASK;
233   GNUNET_ATS_suggest_address (sched_ats, &p.id);
234   wait_task = GNUNET_SCHEDULER_add_delayed (WAIT, &request_task, NULL);
235 }
236
237 static void
238 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
239                     struct Session *session,
240                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
241                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
242                     const struct GNUNET_ATS_Information *atsi,
243                     uint32_t ats_count)
244 {
245   static int stage = 0;
246
247   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u\n", stage);
248   if (2 == stage)
249   {
250       /* Suggestion after resetting block interval */
251       reset_block_duration = GNUNET_TIME_absolute_get_difference(reset_block_start, GNUNET_TIME_absolute_get());
252       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Address suggestion after resetting blocking took about %llu ms!\n",
253                   (long long unsigned int) reset_block_duration.rel_value);
254       if ((block_duration.rel_value <= (initial_duration.rel_value * 3)) ||
255           (initial_duration.rel_value <= (block_duration.rel_value * 3)))
256       {
257         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
258                     "Address suggestion after resetting blocking (%llu ms) took about the same as initial suggestion (%llu ms)\n",
259                     (long long unsigned int) reset_block_duration.rel_value,
260                     (long long unsigned int) initial_duration.rel_value);
261         ret = 0;
262       }
263       else
264       {
265         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Address suggestion after resetting blocking (%llu ms) has too big difference to initial suggestion (%llu ms)\n",
266                     (long long unsigned int) reset_block_duration.rel_value,
267                     (long long unsigned int) initial_duration.rel_value);
268         ret = 1;
269         GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
270         GNUNET_SCHEDULER_add_now (&end, NULL);
271         return;
272       }
273
274       if (((initial_duration.rel_value * 3) <= block_duration.rel_value ) &&
275           ((reset_block_duration.rel_value * 3) <= block_duration.rel_value))
276       {
277         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Address suggestion after resetting blocking (%llu ms) and initial suggestion (%llu ms) much faster than with blocking (%llu ms)\n",
278                     (long long unsigned int) reset_block_duration.rel_value,
279                     (long long unsigned int) initial_duration.rel_value,
280                     (long long unsigned int) block_duration.rel_value);
281         ret = 0;
282       }
283       else
284       {
285         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Address suggestion after resetting blocking (%llu ms) and initial suggestion (%llu ms) not faster than with blocking (%llu ms)\n",
286                     (long long unsigned int) reset_block_duration.rel_value,
287                     (long long unsigned int) initial_duration.rel_value,
288                     (long long unsigned int) block_duration.rel_value);
289         ret = 1;
290       }
291
292
293       GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
294       GNUNET_SCHEDULER_add_now (&end, NULL);
295
296   }
297   if (1 == stage)
298   {
299       /* Suggestion after block*/
300       block_duration = GNUNET_TIME_absolute_get_difference(block_start, GNUNET_TIME_absolute_get());
301       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Address suggestion was blocked for about %llu ms!\n",
302                   (long long unsigned int) block_duration.rel_value);
303
304       if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
305       {
306           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
307                       GNUNET_i2s (&address->peer));
308           ret = 0;
309       }
310       else
311       {
312           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
313                       GNUNET_i2s (&address->peer));
314           GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
315           GNUNET_SCHEDULER_add_now (&end, NULL);
316           ret = 1;
317       }
318
319       if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
320       {
321         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
322         GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
323         GNUNET_SCHEDULER_add_now (&end, NULL);
324         ret = 1;
325       }
326       stage ++;
327
328       /* Reset block interval */
329       GNUNET_ATS_reset_backoff (sched_ats, &address->peer);
330       reset_block_start = GNUNET_TIME_absolute_get();
331       GNUNET_ATS_suggest_address (sched_ats, &p.id);
332   }
333   if (0 == stage)
334   {
335     /* Initial suggestion */
336     if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
337     {
338         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
339                     GNUNET_i2s (&address->peer));
340         ret = 0;
341     }
342     else
343     {
344         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
345                     GNUNET_i2s (&address->peer));
346         GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
347         GNUNET_SCHEDULER_add_now (&end, NULL);
348         ret = 1;
349     }
350
351     if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
352     {
353       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
354       GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
355       GNUNET_SCHEDULER_add_now (&end, NULL);
356       ret = 1;
357     }
358     stage ++;
359     initial_duration = GNUNET_TIME_absolute_get_difference(initial_start, GNUNET_TIME_absolute_get());
360     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initial suggestion took about %llu ms\n",
361                 (long long unsigned int) block_duration.rel_value);
362
363     block_start = GNUNET_TIME_absolute_get();
364     wait_task = GNUNET_SCHEDULER_add_delayed (WAIT, &request_task, NULL);
365   }
366 }
367
368
369 static void
370 run (void *cls, 
371      const struct GNUNET_CONFIGURATION_Handle *cfg,
372      struct GNUNET_TESTING_Peer *peer)
373 {
374
375   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
376
377   /* Connect to ATS scheduling */
378   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
379   if (sched_ats == NULL)
380   {
381     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
382     ret = 1;
383     end ();
384     return;
385   }
386
387   /* Set up peer */
388   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
389   {
390       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
391       ret = GNUNET_SYSERR;
392       end ();
393       return;
394   }
395   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
396
397   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
398               GNUNET_i2s_full(&p.id));
399
400   /* Prepare ATS Information */
401   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
402   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
403   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
404   test_ats_info[1].value = htonl(1);
405   test_ats_count = 2;
406
407   /* Adding address without session */
408   test_session = &test_addr;
409   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
410   test_hello_address.peer = p.id;
411   test_hello_address.transport_name = test_addr.plugin;
412   test_hello_address.address = test_addr.addr;
413   test_hello_address.address_length = test_addr.addr_len;
414   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
415
416   initial_start = GNUNET_TIME_absolute_get();
417   GNUNET_ATS_suggest_address (sched_ats, &p.id);
418 }
419
420
421 int
422 main (int argc, char *argv[])
423 {
424   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
425                                     "test_ats_api.conf",
426                                     &run, NULL))
427     return 1;
428   return ret;
429 }
430 /* end of file test_ats_api_scheduling_reset_backoff.c */