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