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;
96 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
98 GNUNET_SCHEDULER_cancel (wait_task);
99 wait_task = GNUNET_SCHEDULER_NO_TASK;
101 if (sched_ats != NULL)
102 GNUNET_ATS_scheduling_done (sched_ats);
103 free_test_address (&test_addr);
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)
115 GNUNET_SCHEDULER_cancel (die_task);
116 die_task = GNUNET_SCHEDULER_NO_TASK;
118 free_test_address (&test_addr);
119 GNUNET_ATS_scheduling_done (sched_ats);
124 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
125 const struct GNUNET_HELLO_Address *address2, void *session2)
127 if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
130 return GNUNET_SYSERR;
132 if (0 != strcmp (address1->transport_name, address2->transport_name))
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
135 return GNUNET_SYSERR;
137 if (address1->address_length != address2->address_length)
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
140 return GNUNET_SYSERR;
143 else if (0 != memcmp (address1->address, address2->address, address2->address_length))
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
146 return GNUNET_SYSERR;
148 if (session1 != session2)
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
152 return GNUNET_SYSERR;
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,
167 static int stage = 0;
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Received suggestion for peer `%s'\n",
172 GNUNET_i2s(&address->peer));
174 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
175 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
178 GNUNET_i2s (&address->peer));
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);
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying address for `%s'\n",
191 GNUNET_i2s (&address->peer));
192 /* Destroying session for address */
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);
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Received suggestion for peer `%s'\n",
202 GNUNET_i2s(&address->peer));
205 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
206 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with correct address `%s'\n",
209 GNUNET_i2s (&address->peer));
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);
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);
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Unexpected address suggestion\n");
238 const struct GNUNET_CONFIGURATION_Handle *cfg,
239 struct GNUNET_TESTING_Peer *peer)
241 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
243 /* Connect to ATS scheduling */
244 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
245 if (sched_ats == NULL)
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
254 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
262 GNUNET_i2s_full(&p.id));
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);
273 /* Request address */
274 GNUNET_ATS_suggest_address (sched_ats, &p.id);
279 main (int argc, char *argv[])
281 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
288 /* end of file test_ats_api_scheduling_destroy_session.c */