changes
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_add_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/test_ats_api_scheduling_add_address.c
22  * @brief test adding addresses in automatic transport selection scheduling API
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib-new.h"
29 #include "ats.h"
30 #include "test_ats_api_common.h"
31
32 static GNUNET_SCHEDULER_TaskIdentifier die_task;
33
34 /**
35  * Scheduling handle
36  */
37 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
38
39 /**
40  * Return value
41  */
42 static int ret;
43
44 /**
45  * Test address
46  */
47 static struct Test_Address test_addr;
48
49 /**
50  * Test peer
51  */
52 static struct PeerContext p;
53
54 /**
55  * HELLO address
56  */
57 struct GNUNET_HELLO_Address test_hello_address;
58
59 /**
60  * Session
61  */
62 static void *test_session;
63
64
65 static void
66 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
67 {
68   dest->plugin = GNUNET_strdup (plugin);
69   dest->session = session;
70   dest->addr = GNUNET_malloc (addrlen);
71   memcpy (dest->addr, addr, addrlen);
72   dest->addr_len = addrlen;
73 }
74
75 static void
76 free_test_address (struct Test_Address *dest)
77 {
78   GNUNET_free (dest->plugin);
79   GNUNET_free (dest->addr);
80 }
81
82
83 static void
84 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 {
86   die_task = GNUNET_SCHEDULER_NO_TASK;
87
88   if (sched_ats != NULL)
89     GNUNET_ATS_scheduling_done (sched_ats);
90   free_test_address (&test_addr);
91   ret = GNUNET_SYSERR;
92 }
93
94
95 static void
96 end ()
97 {
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
99   if (die_task != GNUNET_SCHEDULER_NO_TASK)
100   {
101     GNUNET_SCHEDULER_cancel (die_task);
102     die_task = GNUNET_SCHEDULER_NO_TASK;
103   }
104   GNUNET_ATS_scheduling_done (sched_ats);
105   sched_ats = NULL;
106   free_test_address (&test_addr);
107 }
108
109 static int
110 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
111                    const struct GNUNET_HELLO_Address *address2, void *session2)
112 {
113   if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
114   {
115       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
116       return GNUNET_SYSERR;
117   }
118   if (0 != strcmp (address1->transport_name, address2->transport_name))
119   {
120       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
121       return GNUNET_SYSERR;
122   }
123   if (address1->address_length != address2->address_length)
124   {
125       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
126       return GNUNET_SYSERR;
127
128   }
129   else if (0 != memcmp (address1->address, address2->address, address2->address_length))
130   {
131       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
132       return GNUNET_SYSERR;
133   }
134   if (session1 != session2)
135   {
136       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
137                   session1, session2);
138       return GNUNET_SYSERR;
139
140   }
141   return GNUNET_OK;
142 }
143
144 static void
145 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
146                     struct Session *session,
147                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
148                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
149                     const struct GNUNET_ATS_Information *atsi,
150                     uint32_t ats_count)
151 {
152   if (0 != memcmp (&address->peer, &p.id, sizeof (struct GNUNET_PeerIdentity)))
153   {
154       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
155       ret = 1;
156   }
157   else if (0 != strcmp (address->transport_name, test_addr.plugin))
158   {
159       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
160       ret = 1;
161   }
162   else if (address->address_length != test_addr.addr_len)
163   {
164       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
165       ret = 1;
166   }
167   else if (0 != memcmp (address->address, test_addr.plugin, address->address_length))
168   {
169       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
170       ret = 1;
171   }
172   else
173   {
174
175   }
176
177   if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
178   {
179       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback with correct address `%s'\n",
180                   GNUNET_i2s (&address->peer));
181       ret = 0;
182   }
183   else
184   {
185       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback with invalid address `%s'\n",
186                   GNUNET_i2s (&address->peer));
187       ret = 0;
188   }
189   GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
190   GNUNET_SCHEDULER_add_now (&end, NULL);
191 }
192
193 static void
194 run (void *cls, 
195      const struct GNUNET_CONFIGURATION_Handle *cfg,
196      struct GNUNET_TESTING_Peer *peer)
197 {
198
199   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
200
201   /* Connect to ATS scheduling */
202   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
203   if (sched_ats == NULL)
204   {
205     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
206     ret = 1;
207     end ();
208     return;
209   }
210
211   /* Set up peer */
212   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
213   {
214       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
215       ret = GNUNET_SYSERR;
216       end ();
217       return;
218   }
219
220   GNUNET_assert (0 == strcmp (PEERID, GNUNET_i2s_full (&p.id)));
221
222   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
223               GNUNET_i2s_full(&p.id));
224
225
226   /* Adding address without session */
227   test_session = &test_addr;
228   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
229   test_hello_address.peer = p.id;
230   test_hello_address.transport_name = test_addr.plugin;
231   test_hello_address.address = test_addr.addr;
232   test_hello_address.address_length = test_addr.addr_len;
233   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, NULL, 0);
234
235   GNUNET_ATS_suggest_address (sched_ats, &p.id);
236 }
237
238
239 int
240 main (int argc, char *argv[])
241 {
242   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
243                                     "test_ats_api.conf",
244                                     &run, NULL))
245     return 1;
246   return ret;
247 }
248
249 /* end of file test_ats_api_scheduling_add_address.c */