2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
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,
26 * @author Christian Grothoff
27 * @author Matthias Wachs
31 #include "gnunet_ats_service.h"
32 #include "gnunet_testing_lib-new.h"
34 #include "test_ats_api_common.h"
36 static GNUNET_SCHEDULER_TaskIdentifier die_task;
38 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
40 #define WAIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
46 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
56 static struct Test_Address test_addr;
61 static struct PeerContext p;
66 struct GNUNET_HELLO_Address test_hello_address;
71 static void *test_session;
74 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
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;
85 free_test_address (struct Test_Address *dest)
87 GNUNET_free (dest->plugin);
88 GNUNET_free (dest->addr);
93 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 die_task = GNUNET_SCHEDULER_NO_TASK;
97 if (sched_ats != NULL)
98 GNUNET_ATS_scheduling_done (sched_ats);
99 free_test_address (&test_addr);
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)
111 GNUNET_SCHEDULER_cancel (die_task);
112 die_task = GNUNET_SCHEDULER_NO_TASK;
114 free_test_address (&test_addr);
115 GNUNET_ATS_scheduling_done (sched_ats);
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,
127 static int stage = 0;
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Received suggestion for peer `%s'\n",
133 GNUNET_i2s(&address->peer));
135 if (0 != memcmp (&address->peer, &p.id, sizeof (struct GNUNET_PeerIdentity)))
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
140 else if (0 != strcmp (address->transport_name, test_addr.plugin))
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
145 else if (address->address_length != test_addr.addr_len)
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
150 else if (0 != memcmp (address->address, test_addr.plugin, address->address_length))
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
155 else if (test_session != &test_addr)
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session'\n");
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for correct address `%s'\n",
163 GNUNET_i2s (&address->peer));
166 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
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);
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying address for `%s'\n",
177 GNUNET_i2s (&address->peer));
178 /* Destroying session for address */
180 GNUNET_ATS_address_destroyed (sched_ats, &test_hello_address, test_addr.session);
181 /* Request address */
182 GNUNET_ATS_suggest_address (sched_ats, &p.id);
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Received suggestion for peer `%s'\n",
188 GNUNET_i2s(&address->peer));
190 if (0 != memcmp (&address->peer, &p.id, sizeof (struct GNUNET_PeerIdentity)))
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
195 else if (0 != strcmp (address->transport_name, test_addr.plugin))
197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
200 else if (address->address_length != test_addr.addr_len)
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
205 else if (0 != memcmp (address->address, test_addr.plugin, address->address_length))
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
210 else if (session != test_session)
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session %p != %p'\n", test_session, session);
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for correct address `%s'\n",
218 GNUNET_i2s (&address->peer));
221 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
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);
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, &test_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);
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Unexpected address suggestion\n");
248 const struct GNUNET_CONFIGURATION_Handle *cfg,
249 struct GNUNET_TESTING_Peer *peer)
251 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
253 /* Connect to ATS scheduling */
254 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
255 if (sched_ats == NULL)
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
264 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
272 GNUNET_i2s_full(&p.id));
274 /* Adding address with session */
275 create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
276 test_session = &test_addr;
277 test_hello_address.peer = p.id;
278 test_hello_address.transport_name = test_addr.plugin;
279 test_hello_address.address = test_addr.addr;
280 test_hello_address.address_length = test_addr.addr_len;
281 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_addr.session, NULL, 0);
283 /* Request address */
284 GNUNET_ATS_suggest_address (sched_ats, &p.id);
289 main (int argc, char *argv[])
291 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
298 /* end of file test_ats_api_scheduling_destroy_session.c */