minor fixes
[oweals/gnunet.git] / src / ats / test_ats_solver_alternative_after_delete_address.c
1 /*
2   if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL)))
3   {
4     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5         "Failed to connect to performance API\n");
6     GNUNET_SCHEDULER_add_now (end_badly, NULL);
7   }
8  This file is part of GNUnet.
9  (C) 2010-2013 Christian Grothoff (and other contributing authors)
10
11  GNUnet is free software; you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published
13  by the Free Software Foundation; either version 3, or (at your
14  option) any later version.
15
16  GNUnet is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with GNUnet; see the file COPYING.  If not, write to the
23  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  Boston, MA 02111-1307, USA.
25  */
26 /**
27  * @file ats/test_ats_solver_add_address.c
28  * @brief solver test:  add 2 addresses, request address, delete, expect alternative
29  * @author Christian Grothoff
30  * @author Matthias Wachs
31  */
32 #include "platform.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_testbed_service.h"
35 #include "gnunet_ats_service.h"
36 #include "test_ats_api_common.h"
37
38 /**
39  * Timeout task
40  */
41 static GNUNET_SCHEDULER_TaskIdentifier die_task;
42
43 /**
44  * Statistics handle
45  */
46 struct GNUNET_STATISTICS_Handle *stats;
47
48 /**
49  * Scheduling handle
50  */
51 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
52
53 /**
54  * Return value
55  */
56 static int ret;
57
58 /**
59  * Test address
60  */
61 static struct Test_Address test_addr;
62
63 /**
64  * Alternative test address
65  */
66 static struct Test_Address alt_test_addr;
67
68 /**
69  * Test peer
70  */
71 static struct PeerContext p;
72
73 /**
74  * HELLO address
75  */
76 static struct GNUNET_HELLO_Address test_hello_address;
77
78 /**
79  * HELLO address
80  */
81 static struct GNUNET_HELLO_Address alt_test_hello_address;
82
83 /**
84  * Session
85  */
86 static void *test_session;
87
88 /**
89  * Test ats info
90  */
91 static struct GNUNET_ATS_Information test_ats_info[2];
92
93 /**
94  * Test ats count
95  */
96 static uint32_t test_ats_count;
97
98 /**
99  * Test state
100  */
101 int addresses_added = GNUNET_NO;
102
103 int first_address_suggested = GNUNET_NO;
104
105 int first_address_deleted = GNUNET_NO;
106
107 int second_address_deleted = GNUNET_NO;
108
109 int second_address_suggested = GNUNET_YES;
110
111 static struct GNUNET_HELLO_Address *first_suggestion = NULL;
112
113 static struct GNUNET_HELLO_Address *second_suggestion = NULL;
114
115
116 static int
117 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
118         int is_persistent);
119
120 static void
121 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 {
123   if (die_task != GNUNET_SCHEDULER_NO_TASK)
124   {
125     GNUNET_SCHEDULER_cancel (die_task);
126     die_task = GNUNET_SCHEDULER_NO_TASK;
127   }
128
129   if (NULL != sched_ats)
130   {
131     GNUNET_ATS_scheduling_done (sched_ats);
132     sched_ats = NULL;
133   }
134
135   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
136   if (NULL != stats)
137   {
138     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
139     stats = NULL;
140   }
141
142   free_test_address (&test_addr);
143   GNUNET_free_non_null (first_suggestion);
144   GNUNET_free_non_null (second_suggestion);
145   ret = 0;
146 }
147
148
149 static void
150 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151 {
152   die_task = GNUNET_SCHEDULER_NO_TASK;
153   end ( NULL, NULL);
154   ret = GNUNET_SYSERR;
155 }
156
157 static void
158 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
159                     struct Session *session,
160                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
161                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
162                     const struct GNUNET_ATS_Information *atsi,
163                     uint32_t ats_count)
164 {
165   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received a sugggestion for peer `%s' : `%s'\n",
166     GNUNET_i2s (&address->peer), (char *) address->address);
167
168   if (GNUNET_NO == first_address_suggested)
169   {
170     if  (NULL == first_suggestion)
171     {
172       if ((NULL == address) || (NULL != session))
173       {
174         GNUNET_break (0);
175         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
176         return;
177       }
178       if ((ntohl(bandwidth_in.value__) == 0) ||
179           (ntohl(bandwidth_out.value__) == 0))
180       {
181         GNUNET_break (0);
182         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
183         return;
184       }
185
186       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received 1st sugggestion for peer `%s' : `%s'\n",
187         GNUNET_i2s (&address->peer), (char *) address->address);
188
189       first_suggestion = GNUNET_HELLO_address_copy (address);
190       first_address_suggested = GNUNET_YES;
191
192
193       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Deleting 1st address for peer `%s' : `%s'\n",
194         GNUNET_i2s (&address->peer), (char *) address->address);
195       GNUNET_ATS_address_destroyed (sched_ats, address, session);
196       first_address_deleted = GNUNET_YES;
197
198       return;
199     }
200   }
201   if (GNUNET_YES == first_address_deleted)
202   {
203     if (NULL == second_suggestion)
204     {
205       GNUNET_assert (NULL != address);
206       GNUNET_assert (NULL == session);
207       GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
208       GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
209
210       if ((NULL == address) || (NULL != session))
211       {
212         GNUNET_break (0);
213         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
214         return;
215       }
216       if ((ntohl(bandwidth_in.value__) == 0) ||
217           (ntohl(bandwidth_out.value__) == 0))
218       {
219         GNUNET_break (0);
220         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
221         return;
222       }
223
224       if (0 == memcmp (address->address, first_suggestion->address,
225           (first_suggestion->address_length < address->address_length) ? first_suggestion->address_length : address->address_length))
226       {
227         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Deleted 1st address for peer `%s' was suggested after deletion: `%s' `%s'\n",
228           GNUNET_i2s (&address->peer), (char *) address->address, first_suggestion->address);
229         GNUNET_break (0);
230         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
231         return;
232       }
233
234       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received 2nd sugggestion for peer `%s' : `%s'\n",
235         GNUNET_i2s (&address->peer), (char *) address->address);
236       second_suggestion = GNUNET_HELLO_address_copy (address);
237       second_address_suggested = GNUNET_YES;
238
239       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Deleting 2nd address for peer `%s' : `%s'\n",
240         GNUNET_i2s (&address->peer), (char *) address->address);
241       GNUNET_ATS_address_destroyed (sched_ats, address, session);
242       second_address_deleted = GNUNET_YES;
243       return;
244     }
245
246   }
247   if (GNUNET_YES == second_address_deleted)
248   {
249     /* Expecting disconnect */
250     if ((ntohl(bandwidth_in.value__) == 0) &&
251         (ntohl(bandwidth_out.value__) == 0))
252     {
253       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
254       GNUNET_SCHEDULER_add_now (&end, NULL);
255       return;
256     }
257     else
258     {
259       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Expected disconnect received address `%s' with bandwidth \n",
260           (char *) address->address);
261     }
262   }
263   return;
264 }
265
266
267 static int
268 stat_cb(void *cls, const char *subsystem,
269         const char *name, uint64_t value,
270         int is_persistent)
271 {
272   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
273       subsystem,name, value);
274   if ((GNUNET_NO == addresses_added) && (value == 2))
275   {
276     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All addresses added, requesting....\n");
277     /* We have 2 addresses, so we can request */
278     addresses_added = GNUNET_YES;
279     GNUNET_ATS_suggest_address (sched_ats, &p.id);
280   }
281   return GNUNET_OK;
282 }
283
284 static void
285 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
286     struct GNUNET_TESTING_Peer *peer)
287 {
288   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
289   stats = GNUNET_STATISTICS_create ("ats", mycfg);
290   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
291
292
293   /* Connect to ATS scheduling */
294   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
295   if (sched_ats == NULL)
296   {
297     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
298     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
299     return;
300   }
301
302   /* Set up peer */
303   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
304   {
305       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
306       GNUNET_SCHEDULER_add_now (&end_badly, NULL);
307       return;
308   }
309   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
310
311   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
312               GNUNET_i2s_full(&p.id));
313
314   /* Prepare ATS Information */
315   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
316   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
317   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
318   test_ats_info[1].value = htonl(1);
319   test_ats_count = 2;
320
321   /* Adding address without session */
322   test_session = NULL;
323   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
324   test_hello_address.peer = p.id;
325   test_hello_address.transport_name = test_addr.plugin;
326   test_hello_address.address = test_addr.addr;
327   test_hello_address.address_length = test_addr.addr_len;
328
329   /* Adding alternative address without session */
330   test_session = NULL;
331   create_test_address (&alt_test_addr, "test", test_session, "alt_test", strlen ("alt_test") + 1);
332   alt_test_hello_address.peer = p.id;
333   alt_test_hello_address.transport_name = alt_test_addr.plugin;
334   alt_test_hello_address.address = alt_test_addr.addr;
335   alt_test_hello_address.address_length = alt_test_addr.addr_len;
336
337
338   /* Adding address */
339   GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
340   /* Adding alternative address */
341   GNUNET_ATS_address_add (sched_ats, &alt_test_hello_address, NULL, test_ats_info, test_ats_count);
342 }
343
344
345 int
346 main (int argc, char *argv[])
347 {
348   char *sep;
349   char *src_filename = GNUNET_strdup (__FILE__);
350   char *test_filename = GNUNET_strdup (argv[0]);
351   char *config_file;
352   char *solver;
353
354   ret = 0;
355
356   if (NULL == (sep  = (strstr (src_filename,".c"))))
357   {
358     GNUNET_break (0);
359     return -1;
360   }
361   sep[0] = '\0';
362
363   if (NULL != (sep = strstr (test_filename, ".exe")))
364     sep[0] = '\0';
365
366   if (NULL == (solver = strstr (test_filename, src_filename)))
367   {
368     GNUNET_break (0);
369     return -1;
370   }
371   solver += strlen (src_filename) +1;
372
373   if (0 == strcmp(solver, "proportional"))
374   {
375     config_file = "test_ats_solver_proportional.conf";
376   }
377   else if (0 == strcmp(solver, "mlp"))
378   {
379     config_file = "test_ats_solver_mlp.conf";
380   }
381   else if ((0 == strcmp(solver, "ril")))
382   {
383     config_file = "test_ats_solver_ril.conf";
384   }
385   else
386   {
387     GNUNET_break (0);
388     GNUNET_free (src_filename);
389     GNUNET_free (test_filename);
390     return 1;
391   }
392
393   GNUNET_free (src_filename);
394   GNUNET_free (test_filename);
395
396   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
397       config_file, &run, NULL ))
398     return GNUNET_SYSERR;
399
400   return ret;
401 }
402
403 /* end of file test_ats_solver_add_address.c */