-test cleanup
[oweals/gnunet.git] / src / ats / test_ats_solver_add_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_add_address.c
22  * @brief solver test: add address
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 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 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 uint32_t test_ats_count;
81
82
83 static int
84 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
85         int is_persistent);
86
87 static void
88 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89 {
90   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
91
92   if (die_task != NULL)
93   {
94     GNUNET_SCHEDULER_cancel (die_task);
95     die_task = NULL;
96   }
97
98   if (NULL != sched_ats)
99   {
100     GNUNET_ATS_scheduling_done (sched_ats);
101     sched_ats = NULL;
102   }
103
104   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
105   if (NULL != stats)
106   {
107     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
108     stats = NULL;
109   }
110
111   free_test_address (&test_addr);
112
113   ret = 0;
114 }
115
116
117 static void
118 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 {
120   die_task = NULL;
121   end ( NULL, NULL);
122   ret = GNUNET_SYSERR;
123 }
124
125 static void
126 address_suggest_cb (void *cls,
127                     const struct GNUNET_PeerIdentity *peer,
128                     const struct GNUNET_HELLO_Address *address,
129                     struct Session *session,
130                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
131                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
132                     const struct GNUNET_ATS_Information *atsi,
133                     uint32_t ats_count)
134 {
135   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
136   GNUNET_SCHEDULER_add_now (&end_badly, NULL);
137   return;
138 }
139
140
141 static int
142 stat_cb(void *cls, const char *subsystem,
143         const char *name, uint64_t value,
144         int is_persistent)
145 {
146
147   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
148       subsystem,name, value);
149   if (1 == value)
150   {
151     GNUNET_SCHEDULER_add_now (&end, NULL);
152   }
153   return GNUNET_OK;
154 }
155
156 static void
157 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
158     struct GNUNET_TESTING_Peer *peer)
159 {
160   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
161   stats = GNUNET_STATISTICS_create ("ats", mycfg);
162   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
163
164
165   /* Connect to ATS scheduling */
166   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
167   if (sched_ats == NULL)
168   {
169     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
170     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
171     return;
172   }
173
174   /* Set up peer */
175   memset (&p.id, '1', sizeof (p.id));
176
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
178               GNUNET_i2s_full(&p.id));
179
180   /* Prepare ATS Information */
181   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
182   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
183   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
184   test_ats_info[1].value = htonl(1);
185   test_ats_count = 2;
186
187   /* Adding address without session */
188   test_session = NULL;
189   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
190   test_hello_address.peer = p.id;
191   test_hello_address.transport_name = test_addr.plugin;
192   test_hello_address.address = test_addr.addr;
193   test_hello_address.address_length = test_addr.addr_len;
194
195   /* Adding address */
196   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
197 }
198
199
200 int
201 main (int argc, char *argv[])
202 {
203   char *sep;
204   char *src_filename = GNUNET_strdup (__FILE__);
205   char *test_filename = GNUNET_strdup (argv[0]);
206   char *config_file;
207   char *solver;
208
209   ret = 0;
210
211   if (NULL == (sep  = (strstr (src_filename,".c"))))
212   {
213     GNUNET_break (0);
214     return -1;
215   }
216   sep[0] = '\0';
217
218   if (NULL != (sep = strstr (test_filename, ".exe")))
219     sep[0] = '\0';
220
221   if (NULL == (solver = strstr (test_filename, src_filename)))
222   {
223     GNUNET_break (0);
224     return -1;
225   }
226   solver += strlen (src_filename) +1;
227
228   if (0 == strcmp(solver, "proportional"))
229   {
230     config_file = "test_ats_solver_proportional.conf";
231   }
232   else if (0 == strcmp(solver, "mlp"))
233   {
234     config_file = "test_ats_solver_mlp.conf";
235   }
236   else if ((0 == strcmp(solver, "ril")))
237   {
238     config_file = "test_ats_solver_ril.conf";
239   }
240   else
241   {
242     GNUNET_break (0);
243     GNUNET_free (src_filename);
244     GNUNET_free (test_filename);
245     return 1;
246   }
247
248   GNUNET_free (src_filename);
249   GNUNET_free (test_filename);
250
251   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
252       config_file, &run, NULL ))
253     return GNUNET_SYSERR;
254
255   return ret;
256 }
257
258 /* end of file test_ats_solver_add_address.c */