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_add_address.c
22 * @brief test adding addresses in automatic transport selection scheduling API
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib-new.h"
30 #include "test_ats_api_common.h"
32 static GNUNET_SCHEDULER_TaskIdentifier die_task;
37 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
47 static struct Test_Address test_addr;
52 static struct PeerContext p;
57 struct GNUNET_HELLO_Address test_hello_address;
62 static void *test_session;
66 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
68 dest->plugin = GNUNET_strdup (plugin);
69 dest->session = session;
70 dest->addr = GNUNET_malloc (addrlen);
71 memcpy (dest->addr, addr, addrlen);
72 dest->addr_len = addrlen;
76 free_test_address (struct Test_Address *dest)
78 GNUNET_free (dest->plugin);
79 GNUNET_free (dest->addr);
84 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 die_task = GNUNET_SCHEDULER_NO_TASK;
88 if (sched_ats != NULL)
89 GNUNET_ATS_scheduling_done (sched_ats);
90 free_test_address (&test_addr);
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
99 if (die_task != GNUNET_SCHEDULER_NO_TASK)
101 GNUNET_SCHEDULER_cancel (die_task);
102 die_task = GNUNET_SCHEDULER_NO_TASK;
104 GNUNET_ATS_scheduling_done (sched_ats);
106 free_test_address (&test_addr);
110 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
111 const struct GNUNET_HELLO_Address *address2, void *session2)
113 if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
116 return GNUNET_SYSERR;
118 if (0 != strcmp (address1->transport_name, address2->transport_name))
120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
121 return GNUNET_SYSERR;
123 if (address1->address_length != address2->address_length)
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
126 return GNUNET_SYSERR;
129 else if (0 != memcmp (address1->address, address2->address, address2->address_length))
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
132 return GNUNET_SYSERR;
134 if (session1 != session2)
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
138 return GNUNET_SYSERR;
145 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
146 struct Session *session,
147 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
148 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
149 const struct GNUNET_ATS_Information *atsi,
152 if (0 != memcmp (&address->peer, &p.id, sizeof (struct GNUNET_PeerIdentity)))
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
157 else if (0 != strcmp (address->transport_name, test_addr.plugin))
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
162 else if (address->address_length != test_addr.addr_len)
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
167 else if (0 != memcmp (address->address, test_addr.plugin, address->address_length))
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
177 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback with correct address `%s'\n",
180 GNUNET_i2s (&address->peer));
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback with invalid address `%s'\n",
186 GNUNET_i2s (&address->peer));
189 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
190 GNUNET_SCHEDULER_add_now (&end, NULL);
195 const struct GNUNET_CONFIGURATION_Handle *cfg,
196 struct GNUNET_TESTING_Peer *peer)
199 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
201 /* Connect to ATS scheduling */
202 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
203 if (sched_ats == NULL)
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
212 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
220 GNUNET_assert (0 == strcmp (PEERID, GNUNET_i2s_full (&p.id)));
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
223 GNUNET_i2s_full(&p.id));
226 /* Adding address without session */
227 test_session = &test_addr;
228 create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
229 test_hello_address.peer = p.id;
230 test_hello_address.transport_name = test_addr.plugin;
231 test_hello_address.address = test_addr.addr;
232 test_hello_address.address_length = test_addr.addr_len;
233 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, NULL, 0);
235 GNUNET_ATS_suggest_address (sched_ats, &p.id);
240 main (int argc, char *argv[])
242 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
249 /* end of file test_ats_api_scheduling_add_address.c */