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_inbound_connection.c
22 * @brief test destroying sessions for inbound connections: 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;
76 struct GNUNET_ATS_Information test_ats_info[2];
81 uint32_t test_ats_count;
85 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 die_task = GNUNET_SCHEDULER_NO_TASK;
88 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
90 GNUNET_SCHEDULER_cancel (wait_task);
91 wait_task = GNUNET_SCHEDULER_NO_TASK;
93 if (sched_ats != NULL)
94 GNUNET_ATS_scheduling_done (sched_ats);
95 free_test_address (&test_addr);
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
104 wait_task = GNUNET_SCHEDULER_NO_TASK;
105 if (die_task != GNUNET_SCHEDULER_NO_TASK)
107 GNUNET_SCHEDULER_cancel (die_task);
108 die_task = GNUNET_SCHEDULER_NO_TASK;
110 free_test_address (&test_addr);
111 GNUNET_ATS_scheduling_done (sched_ats);
117 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
118 struct Session *session,
119 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
120 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
121 const struct GNUNET_ATS_Information *atsi,
124 static int stage = 0;
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Received suggestion for peer `%s'\n",
129 GNUNET_i2s(&address->peer));
131 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
132 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
135 GNUNET_i2s (&address->peer));
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
140 GNUNET_i2s (&address->peer));
141 GNUNET_SCHEDULER_add_now (&end, NULL);
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying address for `%s'\n",
148 GNUNET_i2s (&address->peer));
149 /* Destroying session for address */
151 GNUNET_ATS_address_destroyed (sched_ats, &test_hello_address, test_addr.session);
152 /* Request address */
153 GNUNET_ATS_suggest_address (sched_ats, &p.id);
155 /* Wait for timeout */
156 wait_task = GNUNET_SCHEDULER_add_delayed (WAIT_TIMEOUT, &end, NULL);
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Unexpected address suggestion\n", stage);
166 const struct GNUNET_CONFIGURATION_Handle *cfg,
167 struct GNUNET_TESTING_Peer *peer)
169 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
171 /* Connect to ATS scheduling */
172 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
173 if (sched_ats == NULL)
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
182 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
190 GNUNET_i2s_full(&p.id));
192 /* Prepare ATS Information */
193 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
194 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
195 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
196 test_ats_info[1].value = htonl(1);
199 /* Adding address with session */
200 create_test_address (&test_addr, "test", &test_addr, NULL, 0);
201 test_session = &test_addr;
202 test_hello_address.peer = p.id;
203 test_hello_address.transport_name = test_addr.plugin;
204 test_hello_address.address = test_addr.addr;
205 test_hello_address.address_length = test_addr.addr_len;
206 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_addr.session, test_ats_info, test_ats_count);
208 /* Request address */
209 GNUNET_ATS_suggest_address (sched_ats, &p.id);
214 main (int argc, char *argv[])
216 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
223 /* end of file test_ats_api_scheduling_destroy_inbound_connection.c */