changes
[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 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
97   if (sched_ats != NULL)
98     GNUNET_ATS_scheduling_done (sched_ats);
99   free_test_address (&test_addr);
100   ret = GNUNET_SYSERR;
101 }
102
103
104 static void
105 end ()
106 {
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
108   wait_task = GNUNET_SCHEDULER_NO_TASK;
109   if (die_task != GNUNET_SCHEDULER_NO_TASK)
110   {
111     GNUNET_SCHEDULER_cancel (die_task);
112     die_task = GNUNET_SCHEDULER_NO_TASK;
113   }
114   free_test_address (&test_addr);
115   GNUNET_ATS_scheduling_done (sched_ats);
116   sched_ats = NULL;
117 }
118
119 static void
120 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
121                     struct Session *session,
122                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
123                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
124                     const struct GNUNET_ATS_Information *atsi,
125                     uint32_t ats_count)
126 {
127   static int stage = 0;
128   int res = 0;
129
130   if (0 == stage)
131   {
132     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Received suggestion for peer `%s'\n",
133                 GNUNET_i2s(&address->peer));
134
135     if (0 != memcmp (&address->peer, &p.id, sizeof (struct GNUNET_PeerIdentity)))
136     {
137         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
138         res = 1;
139     }
140     else if (0 != strcmp (address->transport_name, test_addr.plugin))
141     {
142         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
143         res = 1;
144     }
145     else if (address->address_length != test_addr.addr_len)
146     {
147         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
148         res = 1;
149     }
150     else if (0 != memcmp (address->address, test_addr.plugin, address->address_length))
151     {
152         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
153         res = 1;
154     }
155     else if (test_session != &test_addr)
156     {
157         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session'\n");
158         res = 1;
159     }
160     else
161     {
162       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for correct address `%s'\n",
163                   GNUNET_i2s (&address->peer));
164       res = 0;
165     }
166     GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
167     if (1 == res)
168     {
169       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for invalid address `%s'\n",
170                   GNUNET_i2s (&address->peer));
171       GNUNET_SCHEDULER_add_now (&end, NULL);
172       ret = 1;
173     }
174     stage ++;
175     ret = 0;
176     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying address for `%s'\n",
177                 GNUNET_i2s (&address->peer));
178     /* Destroying session for address */
179     test_session = NULL;
180     GNUNET_ATS_address_destroyed (sched_ats, &hello_address, test_addr.session);
181     /* Request address */
182     GNUNET_ATS_suggest_address (sched_ats, &p.id);
183     return;
184   }
185   else if (1 == stage)
186   {
187     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Received suggestion for peer `%s'\n",
188                 GNUNET_i2s(&address->peer));
189
190     if (0 != memcmp (&address->peer, &p.id, sizeof (struct GNUNET_PeerIdentity)))
191     {
192         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
193         res = 1;
194     }
195     else if (0 != strcmp (address->transport_name, test_addr.plugin))
196     {
197         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
198         res = 1;
199     }
200     else if (address->address_length != test_addr.addr_len)
201     {
202         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
203         res = 1;
204     }
205     else if (0 != memcmp (address->address, test_addr.plugin, address->address_length))
206     {
207         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
208         res = 1;
209     }
210     else if (session != test_session)
211     {
212         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session %p != %p'\n", test_session, session);
213         res = 1;
214     }
215     else
216     {
217       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for correct address `%s'\n",
218                   GNUNET_i2s (&address->peer));
219       res = 0;
220     }
221     GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
222     if (1 == res)
223     {
224       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for invalid address `%s'\n",
225                   GNUNET_i2s (&address->peer));
226       GNUNET_SCHEDULER_add_now (&end, NULL);
227       ret = 1;
228     }
229     stage ++;
230     ret = 0;
231     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying address for `%s'\n",
232                 GNUNET_i2s (&address->peer));
233     /* Destroying complete address */
234     GNUNET_ATS_address_destroyed (sched_ats, &hello_address, session);
235     /* Request address */
236     GNUNET_ATS_suggest_address (sched_ats, &p.id);
237     wait_task = GNUNET_SCHEDULER_add_delayed (WAIT_TIMEOUT, &end, NULL);
238     return;
239   }
240   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Unexpected address suggestion\n");
241   ret = 1;
242
243 }
244
245
246 static void
247 run (void *cls, 
248      const struct GNUNET_CONFIGURATION_Handle *cfg,
249      struct GNUNET_TESTING_Peer *peer)
250 {
251   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
252
253   /* Connect to ATS scheduling */
254   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
255   if (sched_ats == NULL)
256   {
257     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
258     ret = 1;
259     end ();
260     return;
261   }
262
263   /* Set up peer */
264   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
265   {
266       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
267       ret = GNUNET_SYSERR;
268       end ();
269       return;
270   }
271   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
272               GNUNET_i2s_full(&p.id));
273
274   /* Adding address with session */
275   create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
276   test_session = &test_addr;
277   hello_address.peer = p.id;
278   hello_address.transport_name = test_addr.plugin;
279   hello_address.address = test_addr.addr;
280   hello_address.address_length = test_addr.addr_len;
281   GNUNET_ATS_address_add (sched_ats, &hello_address, test_addr.session, NULL, 0);
282
283   /* Request address */
284   GNUNET_ATS_suggest_address (sched_ats, &p.id);
285 }
286
287
288 int
289 main (int argc, char *argv[])
290 {
291   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
292                                     "test_ats_api.conf",
293                                     &run, NULL))
294     return 1;
295   return ret;
296 }
297
298 /* end of file test_ats_api_scheduling_destroy_session.c */