changed testbed API to include convenience callbacks
[oweals/gnunet.git] / src / ats / test_ats_api_reset_backoff.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_reset_backoff.c
22  * @brief test case for block reset api
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib-new.h"
29 #include "ats.h"
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32 #define ATS_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 90)
33
34 static GNUNET_SCHEDULER_TaskIdentifier die_task;
35
36 static GNUNET_SCHEDULER_TaskIdentifier suggest_timeout_task;
37
38 static struct GNUNET_ATS_SchedulingHandle *ats;
39
40 static int ret;
41
42
43 struct Address
44 {
45   char *plugin;
46   size_t plugin_len;
47
48   void *addr;
49   size_t addr_len;
50
51   struct GNUNET_ATS_Information *ats;
52   int ats_count;
53
54   void *session;
55 };
56
57 struct PeerContext
58 {
59   struct GNUNET_PeerIdentity id;
60
61   struct Address *addr;
62 };
63
64 struct GNUNET_HELLO_Address hello_addr;
65
66 struct Address address;
67
68 struct PeerContext peer;
69
70 struct GNUNET_ATS_Information atsi[2];
71
72
73 static void
74 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75 {
76   die_task = GNUNET_SCHEDULER_NO_TASK;
77   if (suggest_timeout_task != GNUNET_SCHEDULER_NO_TASK)
78   {
79     GNUNET_SCHEDULER_cancel (suggest_timeout_task);
80     suggest_timeout_task = GNUNET_SCHEDULER_NO_TASK;
81   }
82   if (ats != NULL)
83   {
84     GNUNET_ATS_scheduling_done (ats);
85     ats = NULL;
86   }
87   ret = GNUNET_SYSERR;
88 }
89
90
91 static void
92 end ()
93 {
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
95   if (die_task != GNUNET_SCHEDULER_NO_TASK)
96   {
97     GNUNET_SCHEDULER_cancel (die_task);
98     die_task = GNUNET_SCHEDULER_NO_TASK;
99   }
100
101   if (suggest_timeout_task != GNUNET_SCHEDULER_NO_TASK)
102   {
103     GNUNET_SCHEDULER_cancel (suggest_timeout_task);
104     suggest_timeout_task = GNUNET_SCHEDULER_NO_TASK;
105   }
106   GNUNET_ATS_scheduling_done (ats);
107   ret = 0;
108 }
109
110
111 static void
112 suggest_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 {
114   suggest_timeout_task = GNUNET_SCHEDULER_NO_TASK;
115   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting address for peer timed out\n");
116   if (die_task != GNUNET_SCHEDULER_NO_TASK)
117   {
118     GNUNET_SCHEDULER_cancel (die_task);
119     die_task = GNUNET_SCHEDULER_NO_TASK;
120   }
121   die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
122 }
123
124
125 static void
126 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *a,
127                     struct Session *session,
128                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
129                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
130                     const struct GNUNET_ATS_Information *atsi,
131                     uint32_t ats_count)
132 {
133   static int suggestions;
134
135   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggests address `%s'\n",
136               GNUNET_i2s (&a->peer));
137
138   if (0 != memcmp (&a->peer, &peer.id,
139                          sizeof (struct GNUNET_PeerIdentity)))
140   {
141    GNUNET_break (0);
142    if (die_task != GNUNET_SCHEDULER_NO_TASK)
143    {
144      GNUNET_SCHEDULER_cancel (die_task);
145      die_task = GNUNET_SCHEDULER_NO_TASK;
146    }
147    GNUNET_SCHEDULER_add_now (&end_badly, NULL);
148    return;
149   }
150
151   if (0 != strcmp (a->transport_name, address.plugin))
152   {
153    GNUNET_break (0);
154    if (die_task != GNUNET_SCHEDULER_NO_TASK)
155    {
156      GNUNET_SCHEDULER_cancel (die_task);
157      die_task = GNUNET_SCHEDULER_NO_TASK;
158    }
159    GNUNET_SCHEDULER_add_now (&end_badly, NULL);
160    return;
161   }
162
163   if (a->address_length != address.addr_len)
164   {
165    GNUNET_break (0);
166    if (die_task != GNUNET_SCHEDULER_NO_TASK)
167    {
168      GNUNET_SCHEDULER_cancel (die_task);
169      die_task = GNUNET_SCHEDULER_NO_TASK;
170    }
171    GNUNET_SCHEDULER_add_now (&end_badly, NULL);
172    return;
173   }
174
175   if (0 != memcmp (a->address, address.addr,
176       a->address_length))
177   {
178    GNUNET_break (0);
179    if (die_task != GNUNET_SCHEDULER_NO_TASK)
180    {
181      GNUNET_SCHEDULER_cancel (die_task);
182      die_task = GNUNET_SCHEDULER_NO_TASK;
183    }
184    GNUNET_SCHEDULER_add_now (&end_badly, NULL);
185    return;
186   }
187
188   if (session != address.session)
189   {
190    GNUNET_break (0);
191    if (die_task != GNUNET_SCHEDULER_NO_TASK)
192    {
193      GNUNET_SCHEDULER_cancel (die_task);
194      die_task = GNUNET_SCHEDULER_NO_TASK;
195    }
196    GNUNET_SCHEDULER_add_now (&end_badly, NULL);
197    return;
198   }
199
200   suggestions ++;
201
202   if (2 == suggestions)
203   {
204     GNUNET_SCHEDULER_add_now(&end, NULL);
205     return;
206   }
207
208   if (suggest_timeout_task != GNUNET_SCHEDULER_NO_TASK)
209   {
210     GNUNET_SCHEDULER_cancel (suggest_timeout_task);
211     suggest_timeout_task = GNUNET_SCHEDULER_NO_TASK;
212   }
213   suggest_timeout_task = GNUNET_SCHEDULER_add_delayed(ATS_TIMEOUT, &suggest_timeout, NULL);
214 }
215
216
217 static void
218 run (void *cls, 
219      const struct GNUNET_CONFIGURATION_Handle *cfg,
220      struct GNUNET_TESTING_Peer *peerx)
221 {
222   ret = GNUNET_SYSERR;
223   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
224   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
225   if (ats == NULL)
226   {
227     ret = GNUNET_SYSERR;
228     end ();
229     return;
230   }
231
232   /* set up peer */
233   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
234                                     &peer.id.hashPubKey);
235   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n", GNUNET_i2s (&peer.id));
236
237   address.plugin = "test";
238   address.session = NULL;
239   address.addr = GNUNET_strdup ("test");
240   address.addr_len = 4;
241
242   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding address\n");
243
244   hello_addr.peer = peer.id;
245   hello_addr.transport_name = address.plugin;
246   hello_addr.address = address.addr;
247   hello_addr.address_length = address.addr_len;
248   GNUNET_ATS_address_add (ats, &hello_addr, address.session, NULL, 0);
249   GNUNET_ATS_address_update (ats, &hello_addr, address.session, NULL, 0);
250
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting address for peer `%s'\n",
252               GNUNET_i2s (&peer.id));
253   /* Increase block timout far beyond ATS_TIMEOUT */
254   GNUNET_ATS_suggest_address (ats, &peer.id);
255   GNUNET_ATS_reset_backoff(ats, &peer.id);
256   GNUNET_ATS_suggest_address (ats, &peer.id);
257 }
258
259
260 int
261 main (int argc, char *argv[])
262 {
263   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_reset_backoff",
264                                     "test_ats_api.conf",
265                                     &run, NULL))
266     return 1;
267   return ret;
268 }
269 /* end of file test_ats_api_reset_backoff.c */