- changes
[oweals/gnunet.git] / src / ats / test_ats_api_update_address.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/ats_api.c
22  * @brief automatic transport selection API
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  *
26  * TODO:
27  * - write test case
28  * - extend API to get performance data
29  * - implement simplistic strategy based on say 'lowest latency' or strict ordering
30  * - extend API to get peer preferences, implement proportional bandwidth assignment
31  * - re-implement API against a real ATS service (!)
32  */
33 #include "platform.h"
34 #include "gnunet_ats_service.h"
35 #include "gnunet_transport_service.h"
36
37
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
39
40 #define VALID GNUNET_TIME_UNIT_FOREVER_ABS
41
42 static struct GNUNET_ATS_SchedulingHandle *ats;
43
44 static struct GNUNET_ATS_SuggestionContext *asc;
45
46 static struct GNUNET_PeerIdentity peer;
47
48 static GNUNET_SCHEDULER_TaskIdentifier end_task;
49
50 static struct AllocationRecord *ar;
51
52 static int result;
53
54 struct ExpectedValues
55 {
56   int expected_ats_count;
57
58   int expected_ats_type;
59
60   int expected_ats_value;
61
62   int expected_in_index;
63 };
64
65 struct AllocationRecord
66 {
67
68   /**
69    * Performance information associated with this address (array).
70    */
71   struct GNUNET_ATS_Information *ats;
72
73   /**
74    * Name of the plugin
75    */
76   char *plugin_name;
77
78   /**
79    * Address this record represents, allocated at the end of this struct.
80    */
81   const void *plugin_addr;
82
83   /**
84    * Session associated with this record.
85    */
86   struct Session *session;
87
88   /**
89    * Number of bytes in plugin_addr.
90    */
91   size_t plugin_addr_len;
92
93   /**
94    * Number of entries in 'ats'.
95    */
96   uint32_t ats_count;
97 };
98
99 static void
100 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutdown\n");
103   if (asc != NULL)
104   {
105     GNUNET_ATS_suggest_address_cancel (asc);
106     asc = NULL;
107   }
108   GNUNET_ATS_shutdown (ats);
109
110   GNUNET_array_grow (ar->ats, ar->ats_count, 0);
111   GNUNET_free (ar);
112 }
113
114 void
115 suggest_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
116             const char *plugin_name, const void *plugin_addr,
117             size_t plugin_addr_len, struct Session *session,
118             struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
119             const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
120 {
121   struct ExpectedValues *ex = cls;
122
123   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
124               "ATS suggested address for peer `%s': `%s' `%s'\n",
125               GNUNET_i2s (peer), plugin_name, plugin_addr);
126   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS count %u\n", ats_count);
127
128   int c = 0;
129
130   while (c < ats_count)
131   {
132     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ats[%u]: type %u value %u\n", c,
133                 ntohl (ats[c].type), ntohl (ats[c].value));
134
135     c++;
136   }
137
138   if (ex->expected_ats_count != GNUNET_SYSERR)
139     GNUNET_assert (ex->expected_ats_count == ats_count);
140
141   if ((ex->expected_ats_value != GNUNET_SYSERR) &&
142       (ex->expected_in_index != GNUNET_SYSERR))
143     GNUNET_assert (ex->expected_ats_value ==
144                    ntohl (ats[ex->expected_in_index].value));
145
146   if ((ex->expected_ats_type != GNUNET_SYSERR) &&
147       (ex->expected_in_index != GNUNET_SYSERR))
148     GNUNET_assert (ex->expected_ats_type ==
149                    ntohl (ats[ex->expected_in_index].type));
150
151
152 }
153
154 static void
155 check (void *cls, char *const *args, const char *cfgfile,
156        const struct GNUNET_CONFIGURATION_Handle *cfg)
157 {
158   struct ExpectedValues ex;
159
160   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
161                                     &peer.hashPubKey);
162   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created peer identity `%s'\n",
163               GNUNET_i2s (&peer));
164
165   ats = GNUNET_ATS_init (cfg, NULL, NULL);
166   GNUNET_assert (ats != NULL);
167
168   end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
169
170   ar = GNUNET_malloc (sizeof (struct AllocationRecord));
171
172   ar->plugin_name = "test";
173   ar->session = NULL;
174   ar->plugin_addr = "address1";
175   ar->plugin_addr_len = strlen (ar->plugin_addr) + 1;
176   ar->ats = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information));
177
178   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initial ATS information\n");
179   ar->ats_count = 1;
180   ar->ats[0].type = htonl (0);
181   ar->ats[0].value = htonl (0);
182
183   ex.expected_ats_count = 1;
184   ex.expected_ats_type = 0;
185   ex.expected_ats_value = 0;
186   ex.expected_in_index = 0;
187
188   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
189                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
190                              ar->ats_count);
191   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
192
193   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending empty ATS information\n");
194
195   GNUNET_array_grow (ar->ats, ar->ats_count, ar->ats_count + 1);
196   ar->ats[0].type = htonl (1);
197   ar->ats[0].value = htonl (1);
198   ar->ats[1].type = htonl (0);
199   ar->ats[1].value = htonl (0);
200
201   ex.expected_ats_count = 2;
202   ex.expected_ats_type = 1;
203   ex.expected_ats_value = 1;
204   ex.expected_in_index = 0;
205
206   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
207                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
208                              ar->ats_count);
209   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
210
211   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
212
213   ar->ats[0].type = htonl (1);
214   ar->ats[0].value = htonl (2);
215   ar->ats[1].type = htonl (0);
216   ar->ats[1].value = htonl (0);
217
218   ex.expected_ats_count = 2;
219   ex.expected_ats_type = 1;
220   ex.expected_ats_value = 2;
221   ex.expected_in_index = 0;
222
223   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
224                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
225                              ar->ats_count);
226   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
227
228   /* Extending existing ATS information */
229   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending existing ATS information\n");
230
231
232   ar->ats[0].type = htonl (2);
233   ar->ats[0].value = htonl (2);
234   ar->ats[1].type = htonl (0);
235   ar->ats[1].value = htonl (0);
236
237   ex.expected_ats_count = 3;
238   ex.expected_ats_type = 2;
239   ex.expected_ats_value = 2;
240   ex.expected_in_index = 1;
241
242   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
243                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
244                              ar->ats_count);
245   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
246
247   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
248
249   ar->ats[0].type = htonl (2);
250   ar->ats[0].value = htonl (3);
251   ar->ats[1].type = htonl (0);
252   ar->ats[1].value = htonl (0);
253
254   ex.expected_ats_count = 3;
255   ex.expected_ats_type = 2;
256   ex.expected_ats_value = 3;
257   ex.expected_in_index = 1;
258
259   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
260                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
261                              ar->ats_count);
262   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
263
264   if (end_task != GNUNET_SCHEDULER_NO_TASK)
265     GNUNET_SCHEDULER_cancel (end_task);
266   end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
267 }
268
269 int
270 main (int argc, char *argv1[])
271 {
272   static char *const argv[] = { "test_ats_api_update_address",
273     "-c",
274     "test_ats_api.conf",
275     "-L", "WARNING",
276     NULL
277   };
278
279   static struct GNUNET_GETOPT_CommandLineOption options[] = {
280     GNUNET_GETOPT_OPTION_END
281   };
282
283   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
284                       "test_ats_api_update_address", "nohelp", options, &check,
285                       NULL);
286
287   return result;
288 }