changes
[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 "gnunet_testing_lib-new.h"
29 #include "ats.h"
30 #include "test_ats_api_common.h"
31
32 static GNUNET_SCHEDULER_TaskIdentifier die_task;
33
34 /**
35  * Scheduling handle
36  */
37 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
38
39 /**
40  * Performance handle
41  */
42 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
43
44 /**
45  * Return value
46  */
47 static int ret;
48
49 /**
50  * Test address
51  */
52 static struct Test_Address test_addr;
53
54 /**
55  * Test peer
56  */
57 static struct PeerContext p;
58
59
60 static void
61 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
62 {
63   dest->plugin = GNUNET_strdup (plugin);
64   dest->session = session;
65   dest->addr = GNUNET_malloc (addrlen);
66   memcpy (dest->addr, addr, addrlen);
67   dest->addr_len = addrlen;
68 }
69
70 static void
71 free_test_address (struct Test_Address *dest)
72 {
73   GNUNET_free (dest->plugin);
74   GNUNET_free (dest->addr);
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
83   if (sched_ats != NULL)
84     GNUNET_ATS_scheduling_done (sched_ats);
85   if (perf_ats != NULL)
86     GNUNET_ATS_performance_done (perf_ats);
87   free_test_address (&test_addr);
88   ret = GNUNET_SYSERR;
89 }
90
91
92 static void
93 end ()
94 {
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
96   if (die_task != GNUNET_SCHEDULER_NO_TASK)
97   {
98     GNUNET_SCHEDULER_cancel (die_task);
99     die_task = GNUNET_SCHEDULER_NO_TASK;
100   }
101
102   GNUNET_ATS_scheduling_done (sched_ats);
103   sched_ats = NULL;
104   GNUNET_ATS_performance_done (perf_ats);
105   perf_ats = NULL;
106   free_test_address (&test_addr);
107   ret = 0;
108 }
109
110 void address_callback (void *cls,
111                       const struct
112                       GNUNET_HELLO_Address *
113                       address,
114                       struct
115                       GNUNET_BANDWIDTH_Value32NBO
116                       bandwidth_out,
117                       struct
118                       GNUNET_BANDWIDTH_Value32NBO
119                       bandwidth_in,
120                       const struct
121                       GNUNET_ATS_Information *
122                       ats, uint32_t ats_count)
123 {
124   static int counter = 0;
125   if (NULL != address)
126   {
127       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received callback for peer `%s'\n",
128                   GNUNET_i2s (&address->peer));
129       if (0 < counter)
130       {
131         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Too many callbacks!\n");
132         ret = 1;
133       }
134       else if ((0 == memcmp (&address->peer, &p.id, sizeof (struct GNUNET_PeerIdentity))) &&
135           (0 == strcmp (address->transport_name, test_addr.plugin)) &&
136           (address->address_length == test_addr.addr_len) &&
137           (0 == memcmp (address->address, test_addr.plugin, address->address_length)))
138       {
139         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for correct address `%s'\n",
140                     GNUNET_i2s (&address->peer));
141         ret = 0;
142       }
143       counter ++;
144       return;
145   }
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last callback, shutdown\n");
147   GNUNET_SCHEDULER_add_now (&end, NULL);
148 }
149
150
151 static void
152 run (void *cls, 
153      const struct GNUNET_CONFIGURATION_Handle *cfg,
154      struct GNUNET_TESTING_Peer *peer)
155 {
156   struct GNUNET_HELLO_Address hello_address;
157   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
158
159   /* Connect to ATS scheduling */
160   sched_ats = GNUNET_ATS_scheduling_init (cfg, NULL, NULL);
161   if (sched_ats == NULL)
162   {
163     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
164     ret = 1;
165     end ();
166     return;
167   }
168
169   /* Connect to ATS performance */
170   perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL);
171   if (perf_ats == NULL)
172   {
173     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS performance!\n");
174     ret = 1;
175     end ();
176     return;
177   }
178
179   /* Set up peer */
180   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
181   {
182       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
183       ret = GNUNET_SYSERR;
184       end ();
185       return;
186   }
187   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
188               GNUNET_i2s_full(&p.id));
189
190   create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
191   /* Adding address without session */
192   hello_address.peer = p.id;
193   hello_address.transport_name = test_addr.plugin;
194   hello_address.address = test_addr.addr;
195   hello_address.address_length = test_addr.addr_len;
196   GNUNET_ATS_address_add (sched_ats, &hello_address, test_addr.session, NULL, 0);
197
198   GNUNET_ATS_performance_list_addresses (perf_ats, &p.id, GNUNET_YES, address_callback, NULL);
199 }
200
201
202 int
203 main (int argc, char *argv[])
204 {
205   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
206                                     "test_ats_api.conf",
207                                     &run, NULL))
208     return 1;
209   return ret;
210 }
211
212 /* end of file test_ats_api_scheduling_add_address.c */