ats_ril: - bug fixed in address_add()
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_destroy_inbound_connection.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
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,
25  *        shutdown
26  * @author Christian Grothoff
27  * @author Matthias Wachs
28  *
29  */
30 #include "platform.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_testing_lib.h"
33 #include "ats.h"
34 #include "test_ats_api_common.h"
35
36 static GNUNET_SCHEDULER_TaskIdentifier die_task;
37
38 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
39
40 #define SUGGESTION_WAIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
41
42
43 /**
44  * Scheduling handle
45  */
46 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
47
48 /**
49  * Return value
50  */
51 static int ret;
52
53 /**
54  * Test address
55  */
56 static struct Test_Address test_addr;
57
58 /**
59  * Test peer
60  */
61 static struct PeerContext p;
62
63 /**
64  * HELLO address
65  */
66 struct GNUNET_HELLO_Address test_hello_address;
67
68 /**
69  * Session
70  */
71 static void *test_session;
72
73 /**
74  * Test ats info
75  */
76 struct GNUNET_ATS_Information test_ats_info[2];
77
78 /**
79  * Test ats count
80  */
81 uint32_t test_ats_count;
82
83
84 static void
85 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 {
87   die_task = GNUNET_SCHEDULER_NO_TASK;
88   if (wait_task != GNUNET_SCHEDULER_NO_TASK)
89   {
90     GNUNET_SCHEDULER_cancel (wait_task);
91     wait_task = GNUNET_SCHEDULER_NO_TASK;
92   }
93   if (sched_ats != NULL)
94     GNUNET_ATS_scheduling_done (sched_ats);
95   free_test_address (&test_addr);
96   ret = GNUNET_SYSERR;
97 }
98
99
100 static void
101 end ()
102 {
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)
106   {
107     GNUNET_SCHEDULER_cancel (die_task);
108     die_task = GNUNET_SCHEDULER_NO_TASK;
109   }
110   free_test_address (&test_addr);
111   GNUNET_ATS_scheduling_done (sched_ats);
112   sched_ats = NULL;
113 }
114
115
116 static void
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,
122                     uint32_t ats_count)
123 {
124   static int stage = 0;
125
126   if (0 == stage)
127   {
128     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Received suggestion for peer `%s'\n",
129                 GNUNET_i2s(&address->peer));
130
131     GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
132     if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
133     {
134         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
135                     GNUNET_i2s (&address->peer));
136     }
137     else
138     {
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);
142         ret = 1;
143         return;
144     }
145     stage ++;
146     ret = 0;
147     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying address for `%s'\n",
148                 GNUNET_i2s (&address->peer));
149     /* Destroying session for address */
150     test_session = NULL;
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);
154
155     /* Wait for timeout */
156     wait_task = GNUNET_SCHEDULER_add_delayed (SUGGESTION_WAIT_TIMEOUT, &end, NULL);
157     return;
158   }
159   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Unexpected address suggestion\n", stage);
160   ret = 1;
161 }
162
163
164 static void
165 run (void *cls,
166      const struct GNUNET_CONFIGURATION_Handle *cfg,
167      struct GNUNET_TESTING_Peer *peer)
168 {
169   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
170
171   /* Connect to ATS scheduling */
172   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
173   if (sched_ats == NULL)
174   {
175     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
176     ret = 1;
177     end ();
178     return;
179   }
180
181   /* Set up peer */
182   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
183   {
184       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
185       ret = GNUNET_SYSERR;
186       end ();
187       return;
188   }
189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
190               GNUNET_i2s_full(&p.id));
191
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);
197   test_ats_count = 2;
198
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);
207
208   /* Request address */
209   GNUNET_ATS_suggest_address (sched_ats, &p.id);
210 }
211
212
213 int
214 main (int argc, char *argv[])
215 {
216   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
217                                     "test_ats_api.conf",
218                                     &run, NULL))
219     return 1;
220   return ret;
221 }
222
223 /* end of file test_ats_api_scheduling_destroy_inbound_connection.c */