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