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;
75 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77 die_task = GNUNET_SCHEDULER_NO_TASK;
78 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
80 GNUNET_SCHEDULER_cancel (wait_task);
81 wait_task = GNUNET_SCHEDULER_NO_TASK;
83 if (sched_ats != NULL)
84 GNUNET_ATS_scheduling_done (sched_ats);
85 free_test_address (&test_addr);
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
94 wait_task = GNUNET_SCHEDULER_NO_TASK;
95 if (die_task != GNUNET_SCHEDULER_NO_TASK)
97 GNUNET_SCHEDULER_cancel (die_task);
98 die_task = GNUNET_SCHEDULER_NO_TASK;
100 free_test_address (&test_addr);
101 GNUNET_ATS_scheduling_done (sched_ats);
107 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
108 struct Session *session,
109 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
110 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
111 const struct GNUNET_ATS_Information *atsi,
114 static int stage = 0;
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Received suggestion for peer `%s'\n",
119 GNUNET_i2s(&address->peer));
121 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
122 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
125 GNUNET_i2s (&address->peer));
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
130 GNUNET_i2s (&address->peer));
131 GNUNET_SCHEDULER_add_now (&end, NULL);
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying address for `%s'\n",
138 GNUNET_i2s (&address->peer));
139 /* Destroying session for address */
141 GNUNET_ATS_address_destroyed (sched_ats, &test_hello_address, test_addr.session);
142 /* Request address */
143 GNUNET_ATS_suggest_address (sched_ats, &p.id);
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Received suggestion for peer `%s'\n",
149 GNUNET_i2s(&address->peer));
152 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
153 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with correct address `%s'\n",
156 GNUNET_i2s (&address->peer));
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with invalid address `%s'\n",
161 GNUNET_i2s (&address->peer));
162 GNUNET_SCHEDULER_add_now (&end, NULL);
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying address for `%s'\n",
169 GNUNET_i2s (&address->peer));
170 /* Destroying complete address */
171 GNUNET_ATS_address_destroyed (sched_ats, &test_hello_address, session);
172 /* Request address */
173 GNUNET_ATS_suggest_address (sched_ats, &p.id);
174 wait_task = GNUNET_SCHEDULER_add_delayed (WAIT_TIMEOUT, &end, NULL);
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Unexpected address suggestion\n");
185 const struct GNUNET_CONFIGURATION_Handle *cfg,
186 struct GNUNET_TESTING_Peer *peer)
188 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
190 /* Connect to ATS scheduling */
191 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
192 if (sched_ats == NULL)
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
201 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
209 GNUNET_i2s_full(&p.id));
211 /* Adding address with session */
212 create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
213 test_session = &test_addr;
214 test_hello_address.peer = p.id;
215 test_hello_address.transport_name = test_addr.plugin;
216 test_hello_address.address = test_addr.addr;
217 test_hello_address.address_length = test_addr.addr_len;
218 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_addr.session, NULL, 0);
220 /* Request address */
221 GNUNET_ATS_suggest_address (sched_ats, &p.id);
226 main (int argc, char *argv[])
228 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
235 /* end of file test_ats_api_scheduling_destroy_session.c */