changed testbed API to include convenience callbacks
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_destroy_address.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_address.c
22  * @brief test destroying addresses in automatic transport selection scheduling API
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  *
26  */
27 #include "platform.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_testing_lib-new.h"
30 #include "ats.h"
31
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33
34 static GNUNET_SCHEDULER_TaskIdentifier die_task;
35
36 static struct GNUNET_ATS_SchedulingHandle *ats;
37
38 static int ret;
39
40 static int stage;
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 static struct Address test_addr;
65
66 static struct PeerContext p;
67
68 static struct GNUNET_HELLO_Address hello_address;
69
70
71 static void
72 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 {
74   die_task = GNUNET_SCHEDULER_NO_TASK;
75   if (ats != NULL)
76     GNUNET_ATS_scheduling_done (ats);
77   ret = GNUNET_SYSERR;
78 }
79
80
81 static void
82 end ()
83 {
84   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
85   if (die_task != GNUNET_SCHEDULER_NO_TASK)
86   {
87     GNUNET_SCHEDULER_cancel (die_task);
88     die_task = GNUNET_SCHEDULER_NO_TASK;
89   }
90   GNUNET_ATS_scheduling_done (ats);
91   if (2 == stage)
92     ret = 0;
93   else
94   {
95     GNUNET_break (0);
96     ret = 1;
97   }
98 }
99
100
101 static void
102 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
103                     struct Session *session,
104                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
105                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
106                     const struct GNUNET_ATS_Information *atsi,
107                     uint32_t ats_count)
108 {
109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: ATS suggests address `%s' session %p\n",
110               stage, GNUNET_i2s (&address->peer), session);
111   GNUNET_ATS_reset_backoff(ats, &address->peer);
112
113   GNUNET_assert (0 ==
114                  memcmp (&address->peer, &p.id,
115                          sizeof (struct GNUNET_PeerIdentity)));
116   GNUNET_assert (0 == strcmp (address->transport_name, test_addr.plugin));
117   GNUNET_assert (address->address_length == test_addr.addr_len);
118   GNUNET_assert (0 ==
119                  memcmp (address->address, test_addr.plugin,
120                          address->address_length));
121   GNUNET_assert (test_addr.session == session);
122
123   if (0 == stage)
124   {
125     /* Delete session */
126     GNUNET_ATS_address_destroyed (ats, &hello_address, test_addr.session);
127     test_addr.session = NULL;
128     GNUNET_ATS_suggest_address (ats, &p.id);
129   }
130   if (1 == stage)
131   {
132     /* Delete address */
133     GNUNET_ATS_address_destroyed (ats, &hello_address, test_addr.session);
134     test_addr.session = NULL;
135     GNUNET_ATS_suggest_address (ats, &p.id);
136     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
137   }
138   stage++;
139 }
140
141
142 static void
143 run (void *cls, 
144      const struct GNUNET_CONFIGURATION_Handle *cfg,
145      struct GNUNET_TESTING_Peer *peer)
146 {
147   ret = GNUNET_SYSERR;
148
149   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
150   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
151   if (ats == NULL)
152   {
153     ret = GNUNET_SYSERR;
154     end ();
155     return;
156   }
157
158   /* set up peer */
159   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
160                                     &p.id.hashPubKey);
161   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
162               GNUNET_i2s (&p.id));
163
164   test_addr.plugin = "test";
165   test_addr.session = &test_addr;
166   test_addr.addr = GNUNET_strdup ("test");
167   test_addr.addr_len = 4;
168
169   /* Adding address with session */
170   hello_address.peer = p.id;
171   hello_address.transport_name = test_addr.plugin;
172   hello_address.address = test_addr.addr;
173   hello_address.address_length = test_addr.addr_len;
174   GNUNET_ATS_address_add (ats, &hello_address, test_addr.session, NULL, 0);
175   GNUNET_ATS_suggest_address (ats, &p.id);
176 }
177
178
179 int
180 main (int argc, char *argv[])
181 {
182   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_destroy_address",
183                                     "test_ats_api.conf",
184                                     &run, NULL))
185     return 1;
186   return ret;
187 }
188
189 /* end of file test_ats_api_scheduling_destroy_address.c */