- changes related to ATS_address_add + testcases for address deletion and adding
[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 "ats.h"
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
34
35 static struct GNUNET_ATS_SchedulingHandle *ats;
36
37 struct GNUNET_OS_Process *arm_proc;
38
39
40
41 static int ret;
42 static int stage;
43
44 struct Address
45 {
46   char *plugin;
47   size_t plugin_len;
48
49   void *addr;
50   size_t addr_len;
51
52   struct GNUNET_ATS_Information *ats;
53   int ats_count;
54
55   void *session;
56 };
57
58 struct PeerContext
59 {
60   struct GNUNET_PeerIdentity id;
61
62   struct Address *addr;
63 };
64
65 struct Address addr;
66 struct PeerContext p;
67 struct GNUNET_ATS_Information atsi;
68 struct GNUNET_HELLO_Address address0;
69
70 static void
71 stop_arm ()
72 {
73   if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
74     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
75   GNUNET_OS_process_wait (arm_proc);
76   GNUNET_OS_process_destroy (arm_proc);
77   arm_proc = NULL;
78 }
79
80
81 static void
82 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 {
84   die_task = GNUNET_SCHEDULER_NO_TASK;
85   if (ats != NULL)
86     GNUNET_ATS_scheduling_done (ats);
87
88   ret = GNUNET_SYSERR;
89
90   stop_arm ();
91 }
92
93
94 static void
95 end ()
96 {
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
98   if (die_task != GNUNET_SCHEDULER_NO_TASK)
99   {
100     GNUNET_SCHEDULER_cancel (die_task);
101     die_task = GNUNET_SCHEDULER_NO_TASK;
102   }
103
104   GNUNET_ATS_scheduling_done (ats);
105
106   if (2 == stage)
107     ret = 0;
108   else
109   {
110     GNUNET_break (0);
111     ret = 1;
112   }
113
114   stop_arm ();
115 }
116
117
118 static void
119 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
120                     struct Session *session,
121                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
122                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
123                     const struct GNUNET_ATS_Information *atsi,
124                     uint32_t ats_count)
125 {
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: ATS suggests address `%s' session %p\n",
127               stage, GNUNET_i2s (&address->peer), session);
128   GNUNET_ATS_reset_backoff(ats, &address->peer);
129
130   GNUNET_assert (0 ==
131                  memcmp (&address->peer, &p.id,
132                          sizeof (struct GNUNET_PeerIdentity)));
133   GNUNET_assert (0 == strcmp (address->transport_name, addr.plugin));
134   GNUNET_assert (address->address_length == addr.addr_len);
135   GNUNET_assert (0 ==
136                  memcmp (address->address, addr.plugin,
137                          address->address_length));
138   GNUNET_assert (addr.session == session);
139
140   if (0 == stage)
141   {
142     /* Delete session */
143     GNUNET_ATS_address_destroyed (ats, &address0, addr.session);
144     addr.session = NULL;
145     GNUNET_ATS_suggest_address (ats, &p.id);
146   }
147   if (1 == stage)
148   {
149     /* Delete address */
150     GNUNET_ATS_address_destroyed (ats, &address0, addr.session);
151     addr.session = NULL;
152     GNUNET_ATS_suggest_address (ats, &p.id);
153     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
154   }
155   stage++;
156 }
157
158 void
159 start_arm (const char *cfgname)
160 {
161   arm_proc =
162     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
163                                "gnunet-service-arm",
164                                "-c", cfgname, NULL);
165 }
166
167 static void
168 check (void *cls, char *const *args, const char *cfgfile,
169        const struct GNUNET_CONFIGURATION_Handle *cfg)
170 {
171   ret = GNUNET_SYSERR;
172
173   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
174   start_arm (cfgfile);
175
176   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
177
178   if (ats == NULL)
179   {
180     ret = GNUNET_SYSERR;
181     end ();
182     return;
183   }
184
185   /* set up peer */
186   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
187                                     &p.id.hashPubKey);
188   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
189               GNUNET_i2s (&p.id));
190
191   addr.plugin = "test";
192   addr.session = &addr;
193   addr.addr = GNUNET_strdup ("test");
194   addr.addr_len = 4;
195
196   /* Adding address with session */
197   address0.peer = p.id;
198   address0.transport_name = addr.plugin;
199   address0.address = addr.addr;
200   address0.address_length = addr.addr_len;
201   GNUNET_ATS_address_add (ats, &address0, addr.session, NULL, 0);
202
203   GNUNET_ATS_suggest_address (ats, &p.id);
204 }
205
206 int
207 main (int argc, char *argv[])
208 {
209   static char *const argv2[] = { "test_ats_api_scheduling_destroy_address",
210     "-c",
211     "test_ats_api.conf",
212     "-L", "WARNING",
213     NULL
214   };
215
216   static struct GNUNET_GETOPT_CommandLineOption options[] = {
217     GNUNET_GETOPT_OPTION_END
218   };
219
220   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
221                       "test_ats_api_scheduling_destroy_address", "nohelp", options, &check,
222                       NULL);
223
224
225   return ret;
226 }
227
228 /* end of file test_ats_api_scheduling_destroy_address.c */