add address test
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_destroy_session.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_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,
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 static void
75 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
76 {
77   die_task = GNUNET_SCHEDULER_NO_TASK;
78   if (wait_task != GNUNET_SCHEDULER_NO_TASK)
79   {
80     GNUNET_SCHEDULER_cancel (wait_task);
81     wait_task = GNUNET_SCHEDULER_NO_TASK;
82   }
83   if (sched_ats != NULL)
84     GNUNET_ATS_scheduling_done (sched_ats);
85   free_test_address (&test_addr);
86   ret = GNUNET_SYSERR;
87 }
88
89
90 static void
91 end ()
92 {
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)
96   {
97     GNUNET_SCHEDULER_cancel (die_task);
98     die_task = GNUNET_SCHEDULER_NO_TASK;
99   }
100   free_test_address (&test_addr);
101   GNUNET_ATS_scheduling_done (sched_ats);
102   sched_ats = NULL;
103 }
104
105
106 static void
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,
112                     uint32_t ats_count)
113 {
114   static int stage = 0;
115
116   if (0 == stage)
117   {
118     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Received suggestion for peer `%s'\n",
119                 GNUNET_i2s(&address->peer));
120
121     GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
122     if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
123     {
124         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
125                     GNUNET_i2s (&address->peer));
126     }
127     else
128     {
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);
132         ret = 1;
133         return;
134     }
135     stage ++;
136     ret = 0;
137     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying address for `%s'\n",
138                 GNUNET_i2s (&address->peer));
139     /* Destroying session for address */
140     test_session = NULL;
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);
144     return;
145   }
146   else if (1 == stage)
147   {
148     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Received suggestion for peer `%s'\n",
149                 GNUNET_i2s(&address->peer));
150
151
152     GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
153     if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
154     {
155         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with correct address `%s'\n",
156                     GNUNET_i2s (&address->peer));
157     }
158     else
159     {
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);
163         ret = 1;
164         return;
165     }
166     stage ++;
167     ret = 0;
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 (SUGGESTION_WAIT_TIMEOUT, &end, NULL);
175     return;
176   }
177   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Unexpected address suggestion\n");
178   ret = 1;
179
180 }
181
182
183 static void
184 run (void *cls, 
185      const struct GNUNET_CONFIGURATION_Handle *cfg,
186      struct GNUNET_TESTING_Peer *peer)
187 {
188   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
189
190   /* Connect to ATS scheduling */
191   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
192   if (sched_ats == NULL)
193   {
194     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
195     ret = 1;
196     end ();
197     return;
198   }
199
200   /* Set up peer */
201   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
202   {
203       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
204       ret = GNUNET_SYSERR;
205       end ();
206       return;
207   }
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
209               GNUNET_i2s_full(&p.id));
210
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);
219
220   /* Request address */
221   GNUNET_ATS_suggest_address (sched_ats, &p.id);
222 }
223
224
225 int
226 main (int argc, char *argv[])
227 {
228   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
229                                     "test_ats_api.conf",
230                                     &run, NULL))
231     return 1;
232   return ret;
233 }
234
235 /* end of file test_ats_api_scheduling_destroy_session.c */