-fix leak
[oweals/gnunet.git] / src / ats / test_ats_api_performance_list_peer_addresses.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_performance_list_addresses.c
22  * @brief test performance API's address listing feature
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
29 #include "ats.h"
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
34
35 /**
36  * Statistics handle
37  */
38 struct GNUNET_STATISTICS_Handle *stats;
39
40 /**
41  * Configuration handle
42  */
43 struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 /**
46  * ATS scheduling handle
47  */
48 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
49
50 /**
51  * ATS performance handle
52  */
53 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
54
55 struct GNUNET_ATS_AddressListHandle* phal;
56
57 static int ret;
58
59
60 struct Address
61 {
62   char *plugin;
63   size_t plugin_len;
64
65   void *addr;
66   size_t addr_len;
67
68   struct GNUNET_ATS_Information *ats;
69   int ats_count;
70
71   void *session;
72 };
73
74
75 struct PeerContext
76 {
77   struct GNUNET_PeerIdentity id;
78
79   struct Address *addr;
80 };
81
82
83 static struct PeerContext p[2];
84
85 static struct Address p0_addresses[2];
86 static struct Address p1_addresses[2];
87
88 struct GNUNET_HELLO_Address p0_ha[2];
89 struct GNUNET_HELLO_Address p1_ha[2];
90 struct GNUNET_HELLO_Address *s_ha[2];
91
92
93 static void
94 end (void *cls,
95      const struct GNUNET_SCHEDULER_TaskContext *tc);
96
97
98 static void
99 ats_perf_cb (void *cls,
100             const struct GNUNET_HELLO_Address *address,
101             int address_active,
102             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
103             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
104             const struct GNUNET_ATS_Information *ats,
105             uint32_t ats_count)
106 {
107   static int counter = 0;
108
109   if (NULL == address)
110   {
111     phal = NULL;
112     if (2 == counter)
113     {
114       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
115                   "Done\n");
116       GNUNET_SCHEDULER_add_now (&end,
117                                 NULL);
118     }
119     else
120     {
121       fprintf (stderr,
122                "Expected 2 addresses, got %d\n",
123                counter);
124       GNUNET_break (0);
125       GNUNET_SCHEDULER_shutdown ();
126     }
127     return;
128   }
129   else
130   {
131     if (0 != memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
132     {
133       GNUNET_break (0);
134       GNUNET_SCHEDULER_shutdown ();
135       return;
136     }
137   }
138   counter ++;
139 }
140
141
142 static int
143 stat_cb (void *cls, const char *subsystem,
144          const char *name, uint64_t value,
145          int is_persistent)
146 {
147
148   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
149               "ATS statistics: `%s' `%s' %llu\n",
150               subsystem,
151               name,
152               value);
153   if (4 == value)
154   {
155     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
156                 "All addresses added, connecting to performance\n");
157     if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL)))
158     {
159       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160                   "Failed to connect to performance API\n");
161       GNUNET_SCHEDULER_shutdown ();
162       return GNUNET_SYSERR;
163     }
164     phal = GNUNET_ATS_performance_list_addresses (perf_ats,
165                                                   &p[0].id,
166                                                   GNUNET_YES,
167                                                   &ats_perf_cb, NULL);
168   }
169   return GNUNET_OK;
170 }
171
172
173 static void
174 end (void *cls,
175      const struct GNUNET_SCHEDULER_TaskContext *tc)
176 {
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178               "Shutting down\n");
179   if (die_task != GNUNET_SCHEDULER_NO_TASK )
180   {
181     GNUNET_SCHEDULER_cancel (die_task);
182     die_task = GNUNET_SCHEDULER_NO_TASK;
183   }
184   if (NULL != sched_ats)
185   {
186     GNUNET_ATS_scheduling_done (sched_ats);
187     sched_ats = NULL;
188   }
189   if (NULL != phal)
190   {
191     GNUNET_ATS_performance_list_addresses_cancel (phal);
192     phal = NULL;
193   }
194   if (NULL != perf_ats)
195   {
196     GNUNET_ATS_performance_done (perf_ats);
197     perf_ats = NULL;
198   }
199
200   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
201   if (NULL != stats)
202   {
203     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
204     stats = NULL;
205   }
206   GNUNET_free_non_null (p0_addresses[0].addr);
207   GNUNET_free_non_null (p0_addresses[1].addr);
208   GNUNET_free_non_null (p1_addresses[0].addr);
209   GNUNET_free_non_null (p1_addresses[1].addr);
210
211   ret = 0;
212 }
213
214
215 static void
216 end_badly (void *cls,
217            const struct GNUNET_SCHEDULER_TaskContext *tc)
218 {
219   die_task = GNUNET_SCHEDULER_NO_TASK;
220   end (NULL, NULL);
221   ret = GNUNET_SYSERR;
222 }
223
224
225 static void
226 address_suggest_cb (void *cls,
227                     const struct GNUNET_PeerIdentity *peer,
228                     const struct GNUNET_HELLO_Address *address,
229                     struct Session *session,
230                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
231                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
232                     const struct GNUNET_ATS_Information *atsi,
233                     uint32_t ats_count)
234 {
235   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236               "Did not expect suggestion callback!\n");
237   GNUNET_SCHEDULER_shutdown ();
238 }
239
240
241 static void
242 run (void *cls,
243      const struct GNUNET_CONFIGURATION_Handle *mycfg,
244      struct GNUNET_TESTING_Peer *peer)
245 {
246   ret = 1;
247   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
248   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
249
250   stats = GNUNET_STATISTICS_create ("ats", cfg);
251   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
252
253   /* set up peer 0 */
254   memset (&p[0].id, '1', sizeof (p[0].id));
255   p0_addresses[0].plugin = "test";
256   p0_addresses[0].session = NULL;
257   p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
258   p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
259
260   p0_ha[0].address = p0_addresses[0].addr;
261   p0_ha[0].address_length = p0_addresses[0].addr_len;
262   p0_ha[0].peer = p[0].id;
263   p0_ha[0].transport_name = p0_addresses[0].plugin;
264
265   p0_addresses[1].plugin = "test";
266   p0_addresses[1].session = NULL;
267   p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
268   p0_addresses[1].addr_len = strlen (p0_addresses[1].addr) + 1;
269
270   p0_ha[1].address = p0_addresses[1].addr;
271   p0_ha[1].address_length = p0_addresses[1].addr_len;
272   p0_ha[1].peer = p[0].id;
273   p0_ha[1].transport_name = p0_addresses[1].plugin;
274
275   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
276              "Created peer 0: `%s'\n",
277              GNUNET_i2s (&p[0].id));
278
279   memset (&p[1].id, '2', sizeof (p[1].id));
280   p1_addresses[0].plugin = "test";
281   p1_addresses[0].session = NULL;
282   p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
283   p1_addresses[0].addr_len = strlen (p1_addresses[0].addr) + 1;
284
285   p1_ha[0].address = p1_addresses[0].addr;
286   p1_ha[0].address_length = p1_addresses[0].addr_len;
287   p1_ha[0].peer = p[1].id;
288   p1_ha[0].transport_name = p1_addresses[0].plugin;
289
290   p1_addresses[1].plugin = "test";
291   p1_addresses[1].session = NULL;
292   p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
293   p1_addresses[1].addr_len = strlen (p1_addresses[1].addr) + 1;
294
295   p1_ha[1].address = p1_addresses[1].addr;
296   p1_ha[1].address_length = p1_addresses[1].addr_len;
297   p1_ha[1].peer = p[1].id;
298   p1_ha[1].transport_name = p1_addresses[1].plugin;
299
300   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
301              "Created peer 1: `%s'\n",
302              GNUNET_i2s (&p[1].id));
303
304   /* Add addresses */
305   sched_ats = GNUNET_ATS_scheduling_init (cfg,
306                                           &address_suggest_cb, NULL);
307   if (NULL == sched_ats)
308   {
309     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310                 "Could not setup peer!\n");
311     GNUNET_SCHEDULER_shutdown ();
312     return;
313   }
314
315   GNUNET_ATS_address_add (sched_ats, &p0_ha[0], NULL, NULL, 0);
316   GNUNET_ATS_address_add (sched_ats, &p0_ha[1], NULL, NULL, 0);
317
318   GNUNET_ATS_address_add (sched_ats, &p1_ha[0], NULL, NULL, 0);
319   GNUNET_ATS_address_add (sched_ats, &p1_ha[1], NULL, NULL, 0);
320 }
321
322
323 int
324 main (int argc, char *argv[])
325 {
326   if (0 !=
327       GNUNET_TESTING_peer_run ("test_ats_api_performance",
328                                "test_ats_api.conf",
329                                &run, NULL))
330     return 1;
331   return ret;
332 }
333
334 /* end of file test_ats_api_performance_list_addresses.c */