-fix testcase assertions
[oweals/gnunet.git] / src / ats / test_ats_solver_request_and_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_request_and_delete_address.c
22  * @brief solver test:  add address, request address, delete address and wait for disconnect
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  * Test peer
59  */
60 static struct PeerContext p;
61
62 /**
63  * HELLO address
64  */
65 static struct GNUNET_HELLO_Address test_hello_address;
66
67 /**
68  * Session
69  */
70 static void *test_session;
71
72 /**
73  * Test ats info
74  */
75 struct GNUNET_ATS_Information test_ats_info[2];
76
77 /**
78  * Test ats count
79  */
80 static uint32_t test_ats_count;
81
82 /**
83  * The address we will delete.
84  */
85 static struct GNUNET_ATS_AddressRecord *ar;
86
87 static int address_deleted = GNUNET_NO;
88
89
90 static int
91 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
92         int is_persistent);
93
94
95 static void
96 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 {
98   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
99   if (die_task != NULL)
100   {
101     GNUNET_SCHEDULER_cancel (die_task);
102     die_task = NULL;
103   }
104
105   if (NULL != sched_ats)
106   {
107     GNUNET_ATS_scheduling_done (sched_ats);
108     sched_ats = NULL;
109   }
110
111   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
112   if (NULL != stats)
113   {
114     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
115     stats = NULL;
116   }
117
118   free_test_address (&test_addr);
119
120   ret = 0;
121 }
122
123
124 static void
125 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
126 {
127   die_task = NULL;
128   end (NULL, NULL);
129   ret = GNUNET_SYSERR;
130 }
131
132
133 static void
134 address_suggest_cb (void *cls,
135                     const struct GNUNET_PeerIdentity *peer,
136                     const struct GNUNET_HELLO_Address *address,
137                     struct Session *session,
138                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
139                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
140 {
141   if (GNUNET_NO == address_deleted)
142   {
143     /* Expected address suggestion */
144     GNUNET_assert (NULL != address);
145     GNUNET_assert (NULL == session);
146     GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
147     GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
148
149     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
150         "Received sugggestion for peer `%s', deleting address\n",
151         GNUNET_i2s (&address->peer));
152     address_deleted = GNUNET_YES;
153     GNUNET_ATS_address_destroy (ar);
154     ar = NULL;
155   }
156   else
157   {
158     /* Expecting disconnect */
159
160     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
161                 "Received sugggestion for peer `%s': %u %u\n",
162                 GNUNET_i2s (&address->peer),
163                 (unsigned int) ntohl (bandwidth_in.value__),
164                 (unsigned int) ntohl (bandwidth_out.value__));
165
166     if ((ntohl(bandwidth_in.value__) == 0) &&
167         (ntohl(bandwidth_out.value__) == 0))
168     {
169       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
170       GNUNET_SCHEDULER_add_now (&end, NULL);
171     }
172   }
173 }
174
175
176 static int
177 stat_cb (void *cls, const char *subsystem,
178          const char *name, uint64_t value,
179          int is_persistent)
180 {
181   static struct GNUNET_ATS_SuggestHandle *sh;
182
183   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
184               "ATS statistics: `%s' `%s' %llu\n",
185               subsystem,
186               name,
187               value);
188   if (NULL == sh)
189     sh = GNUNET_ATS_suggest_address (sched_ats, &p.id);
190   return GNUNET_OK;
191 }
192
193
194 static void
195 run (void *cls,
196      const struct GNUNET_CONFIGURATION_Handle *mycfg,
197      struct GNUNET_TESTING_Peer *peer)
198 {
199   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
200   stats = GNUNET_STATISTICS_create ("ats", mycfg);
201   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
202
203
204   /* Connect to ATS scheduling */
205   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
206   if (sched_ats == NULL)
207   {
208     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
209     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
210     return;
211   }
212
213   /* Set up peer */
214   memset (&p.id, '1', sizeof (p.id));
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
216               GNUNET_i2s_full(&p.id));
217
218   /* Prepare ATS Information */
219   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
220   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
221   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
222   test_ats_info[1].value = htonl(1);
223   test_ats_count = 2;
224
225   /* Adding address without session */
226   test_session = NULL;
227   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
228   test_hello_address.peer = p.id;
229   test_hello_address.transport_name = test_addr.plugin;
230   test_hello_address.address = test_addr.addr;
231   test_hello_address.address_length = test_addr.addr_len;
232
233   /* Adding address */
234   ar = GNUNET_ATS_address_add (sched_ats, &test_hello_address,
235                                NULL,
236                                test_ats_info, test_ats_count);
237 }
238
239
240 int
241 main (int argc, char *argv[])
242 {
243   char *sep;
244   char *src_filename = GNUNET_strdup (__FILE__);
245   char *test_filename = GNUNET_strdup (argv[0]);
246   char *config_file;
247   char *solver;
248
249   ret = 0;
250
251   if (NULL == (sep  = (strstr (src_filename,".c"))))
252   {
253     GNUNET_break (0);
254     return -1;
255   }
256   sep[0] = '\0';
257
258   if (NULL != (sep = strstr (test_filename, ".exe")))
259     sep[0] = '\0';
260
261   if (NULL == (solver = strstr (test_filename, src_filename)))
262   {
263     GNUNET_break (0);
264     return -1;
265   }
266   solver += strlen (src_filename) +1;
267
268   if (0 == strcmp(solver, "proportional"))
269   {
270     config_file = "test_ats_solver_proportional.conf";
271   }
272   else if (0 == strcmp(solver, "mlp"))
273   {
274     config_file = "test_ats_solver_mlp.conf";
275   }
276   else if ((0 == strcmp(solver, "ril")))
277   {
278     config_file = "test_ats_solver_ril.conf";
279   }
280   else
281   {
282     GNUNET_break (0);
283     GNUNET_free (src_filename);
284     GNUNET_free (test_filename);
285     return 1;
286   }
287
288   GNUNET_free (src_filename);
289   GNUNET_free (test_filename);
290
291   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
292       config_file, &run, NULL ))
293     return GNUNET_SYSERR;
294
295   return ret;
296 }
297
298 /* end of file test_ats_solver_add_address.c */