-fixing testcases
[oweals/gnunet.git] / src / ats / test_ats_solver_alternative_after_delete_address.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010-2013 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_solver_alternative_after_delete_address.c
22  * @brief solver test:  add 2 addresses, request address, delete, expect alternative
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "test_ats_api_common.h"
31
32 /**
33  * Timeout task
34  */
35 static struct GNUNET_SCHEDULER_Task * die_task;
36
37 /**
38  * Statistics handle
39  */
40 static struct GNUNET_STATISTICS_Handle *stats;
41
42 /**
43  * Scheduling handle
44  */
45 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
46
47 /**
48  * Return value
49  */
50 static int ret;
51
52 /**
53  * Test address
54  */
55 static struct Test_Address test_addr;
56
57 /**
58  * Alternative test address
59  */
60 static struct Test_Address alt_test_addr;
61
62 /**
63  * Test peer
64  */
65 static struct PeerContext p;
66
67 /**
68  * HELLO address
69  */
70 static struct GNUNET_HELLO_Address test_hello_address;
71
72 /**
73  * HELLO address
74  */
75 static struct GNUNET_HELLO_Address alt_test_hello_address;
76
77 /**
78  * Session
79  */
80 static void *test_session;
81
82 /**
83  * Test ats info
84  */
85 static struct GNUNET_ATS_Information test_ats_info[2];
86
87 /**
88  * Test ats count
89  */
90 static uint32_t test_ats_count;
91
92 /**
93  * Test state
94  */
95 static int addresses_added;
96
97 static int first_address_suggested;
98
99 static int first_address_deleted;
100
101 static int second_address_deleted;
102
103 static int second_address_suggested = GNUNET_YES;
104
105 static struct GNUNET_HELLO_Address *first_suggestion;
106
107 static struct GNUNET_HELLO_Address *second_suggestion;
108
109 /**
110  * 1st Address we will destroy.
111  */
112 static struct GNUNET_ATS_AddressRecord *ar;
113
114 /**
115  * 2nd Address we will destroy.
116  */
117 static struct GNUNET_ATS_AddressRecord *ar2;
118
119
120 static int
121 stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
122          int is_persistent);
123
124
125 static void
126 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 {
128   if (die_task != NULL)
129   {
130     GNUNET_SCHEDULER_cancel (die_task);
131     die_task = NULL;
132   }
133
134   if (NULL != sched_ats)
135   {
136     GNUNET_ATS_scheduling_done (sched_ats);
137     sched_ats = NULL;
138   }
139
140   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
141   if (NULL != stats)
142   {
143     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
144     stats = NULL;
145   }
146
147   free_test_address (&test_addr);
148   GNUNET_free_non_null (first_suggestion);
149   GNUNET_free_non_null (second_suggestion);
150   ret = 0;
151 }
152
153
154 static void
155 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
156 {
157
158   die_task = NULL;
159   end ( NULL, NULL);
160   ret = GNUNET_SYSERR;
161 }
162
163
164 static void
165 end_badly_now ()
166 {
167   if (NULL != die_task)
168   {
169     GNUNET_SCHEDULER_cancel (die_task);
170     die_task = NULL;
171   }
172   GNUNET_SCHEDULER_add_now (&end_badly, NULL);
173 }
174
175
176 static void
177 address_suggest_cb (void *cls,
178                     const struct GNUNET_PeerIdentity *peer,
179                     const struct GNUNET_HELLO_Address *address,
180                     struct Session *session,
181                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
182                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
183 {
184   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
185               "Received a sugggestion for peer `%s' : `%s'\n",
186     GNUNET_i2s (&address->peer), (char *) address->address);
187
188   if (GNUNET_NO == first_address_suggested)
189   {
190     if  (NULL == first_suggestion)
191     {
192       if ((NULL == address) || (NULL != session))
193       {
194         GNUNET_break (0);
195         end_badly_now ();
196         return;
197       }
198       if ((ntohl(bandwidth_in.value__) == 0) ||
199           (ntohl(bandwidth_out.value__) == 0))
200       {
201         GNUNET_break (0);
202         end_badly_now ();
203         return;
204       }
205
206       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
207                   "Received 1st sugggestion for peer `%s' : `%s'\n",
208         GNUNET_i2s (&address->peer), (char *) address->address);
209
210       first_suggestion = GNUNET_HELLO_address_copy (address);
211       first_address_suggested = GNUNET_YES;
212
213
214       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
215                   "Deleting 1st address for peer `%s' : `%s'\n",
216                   GNUNET_i2s (&address->peer),
217                   (char *) address->address);
218       if (0 == (strcmp ((char *) address->address,
219                         "test")))
220       {
221         GNUNET_assert (NULL != ar);
222         GNUNET_ATS_address_destroy (ar);
223         ar = NULL;
224       }
225       else
226       {
227         GNUNET_assert (NULL != ar2);
228         GNUNET_ATS_address_destroy (ar2);
229         ar2 = NULL;
230       }
231
232       first_address_deleted = GNUNET_YES;
233
234       return;
235     }
236   }
237   if (GNUNET_YES == first_address_deleted)
238   {
239     if (NULL == second_suggestion)
240     {
241       if ((NULL == address) || (NULL != session))
242       {
243         GNUNET_break (0);
244         end_badly_now ();
245         return;
246       }
247
248       if (0 != memcmp (address->address, first_suggestion->address,
249           (first_suggestion->address_length < address->address_length) ? first_suggestion->address_length : address->address_length))
250       {
251         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252                     "Received 2nd sugggestion for peer `%s' : `%s'\n",
253                     GNUNET_i2s (&address->peer),
254                     (char *) address->address);
255         second_suggestion = GNUNET_HELLO_address_copy (address);
256         second_address_suggested = GNUNET_YES;
257
258         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
259                     "Deleting 2nd address for peer `%s' : `%s'\n",
260                     GNUNET_i2s (&address->peer),
261                     (char *) address->address);
262         if (NULL != ar)
263         {
264           GNUNET_assert (NULL == ar2);
265           GNUNET_ATS_address_destroy (ar);
266           ar = NULL;
267         }
268         else
269         {
270           GNUNET_assert (NULL != ar2);
271           GNUNET_ATS_address_destroy (ar2);
272           ar2 = NULL;
273         }
274         second_address_deleted = GNUNET_YES;
275         return;
276       }
277     }
278   }
279   if (GNUNET_YES == second_address_deleted)
280   {
281     /* Expecting disconnect */
282     if ((ntohl(bandwidth_in.value__) == 0) &&
283         (ntohl(bandwidth_out.value__) == 0))
284     {
285       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
286                   "ATS tells me to disconnect\n");
287       GNUNET_SCHEDULER_add_now (&end, NULL);
288       return;
289     }
290     else
291     {
292       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
293                   "Expected disconnect but received address `%s' with bandwidth \n",
294                   (char *) address->address);
295     }
296   }
297 }
298
299
300 static int
301 stat_cb(void *cls, const char *subsystem,
302         const char *name, uint64_t value,
303         int is_persistent)
304 {
305   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
306               "ATS statistics: `%s' `%s' %llu\n",
307               subsystem,
308               name,
309               value);
310   if ((GNUNET_NO == addresses_added) && (value == 2))
311   {
312     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
313                 "All addresses added, requesting....\n");
314     /* We have 2 addresses, so we can request */
315     addresses_added = GNUNET_YES;
316     GNUNET_ATS_suggest_address (sched_ats, &p.id);
317   }
318   return GNUNET_OK;
319 }
320
321
322 static void
323 run (void *cls,
324      const struct GNUNET_CONFIGURATION_Handle *mycfg,
325      struct GNUNET_TESTING_Peer *peer)
326 {
327   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
328   stats = GNUNET_STATISTICS_create ("ats", mycfg);
329   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
330
331
332   /* Connect to ATS scheduling */
333   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
334   if (sched_ats == NULL)
335   {
336     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
337                 "Could not connect to ATS scheduling!\n");
338     end_badly_now ();
339     return;
340   }
341
342   /* Set up peer */
343   memset (&p.id, '1', sizeof (p.id));
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
345               GNUNET_i2s_full(&p.id));
346
347   /* Prepare ATS Information */
348   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
349   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
350   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
351   test_ats_info[1].value = htonl(1);
352   test_ats_count = 2;
353
354   /* Adding address without session */
355   test_session = NULL;
356   create_test_address (&test_addr, "test", test_session,
357                        "test", strlen ("test") + 1);
358   test_hello_address.peer = p.id;
359   test_hello_address.transport_name = test_addr.plugin;
360   test_hello_address.address = test_addr.addr;
361   test_hello_address.address_length = test_addr.addr_len;
362
363   /* Adding alternative address without session */
364   test_session = NULL;
365   create_test_address (&alt_test_addr, "test", test_session,
366                        "alt_test", strlen ("alt_test") + 1);
367   alt_test_hello_address.peer = p.id;
368   alt_test_hello_address.transport_name = alt_test_addr.plugin;
369   alt_test_hello_address.address = alt_test_addr.addr;
370   alt_test_hello_address.address_length = alt_test_addr.addr_len;
371
372
373   /* Adding address */
374   ar = GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL,
375                                test_ats_info, test_ats_count);
376   /* Adding alternative address */
377   ar2 = GNUNET_ATS_address_add (sched_ats, &alt_test_hello_address, NULL,
378                                 test_ats_info, test_ats_count);
379 }
380
381
382 int
383 main (int argc, char *argv[])
384 {
385   char *sep;
386   char *src_filename = GNUNET_strdup (__FILE__);
387   char *test_filename = GNUNET_strdup (argv[0]);
388   const char *config_file;
389   char *solver;
390
391   ret = 0;
392
393   if (NULL == (sep  = (strstr (src_filename, ".c"))))
394   {
395     GNUNET_break (0);
396     return -1;
397   }
398   sep[0] = '\0';
399
400   if (NULL != (sep = strstr (test_filename, ".exe")))
401     sep[0] = '\0';
402
403   if (NULL == (solver = strstr (test_filename, src_filename)))
404   {
405     GNUNET_break (0);
406     return -1;
407   }
408   solver += strlen (src_filename) +1;
409
410   if (0 == strcmp(solver, "proportional"))
411   {
412     config_file = "test_ats_solver_proportional.conf";
413   }
414   else if (0 == strcmp(solver, "mlp"))
415   {
416     config_file = "test_ats_solver_mlp.conf";
417   }
418   else if ((0 == strcmp(solver, "ril")))
419   {
420     config_file = "test_ats_solver_ril.conf";
421   }
422   else
423   {
424     GNUNET_break (0);
425     GNUNET_free (src_filename);
426     GNUNET_free (test_filename);
427     return 1;
428   }
429
430   GNUNET_free (src_filename);
431   GNUNET_free (test_filename);
432
433   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
434       config_file, &run, NULL ))
435     return GNUNET_SYSERR;
436
437   return ret;
438 }
439
440 /* end of file test_ats_solver_alternative_after_delete_address.c */