fix
[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 #include "test_ats_api_common.h"
32
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
34
35 /**
36  * Scheduling handle
37  */
38 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
39
40 /**
41  * Performance handle
42  */
43 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
44
45 /**
46  * Return value
47  */
48 static int ret;
49
50 /**
51  * Test address
52  */
53 static struct Test_Address test_addr;
54
55 /**
56  * Test peer
57  */
58 static struct PeerContext p;
59
60
61 static void
62 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
63 {
64
65   dest->plugin = GNUNET_strdup (plugin);
66   dest->session = session;
67   dest->addr = GNUNET_malloc (addrlen);
68   memcpy (dest->addr, addr, addrlen);
69   dest->addr_len = addrlen;
70 }
71
72 static void
73 free_test_address (struct Test_Address *dest)
74 {
75   GNUNET_free (dest->plugin);
76   GNUNET_free (dest->addr);
77 }
78
79
80 static void
81 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   die_task = GNUNET_SCHEDULER_NO_TASK;
84
85   if (sched_ats != NULL)
86     GNUNET_ATS_scheduling_done (sched_ats);
87   if (perf_ats != NULL)
88     GNUNET_ATS_performance_done (perf_ats);
89   free_test_address (&test_addr);
90   ret = GNUNET_SYSERR;
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   free_test_address (&test_addr);
104   GNUNET_ATS_scheduling_done (sched_ats);
105   sched_ats = NULL;
106   GNUNET_ATS_performance_done (perf_ats);
107   perf_ats = NULL;
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       counter ++;
130       return;
131   }
132   if (counter > 0)
133   {
134       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received %u unexpected addresses callbacks\n", counter);
135       ret = counter;
136   }
137   else
138   {
139       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received no unexpected addresses callbacks\n", counter);
140       ret = 0;
141   }
142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last callback, shutdown\n");
143   GNUNET_SCHEDULER_add_now (&end, NULL);
144 }
145
146
147 static void
148 run (void *cls, 
149      const struct GNUNET_CONFIGURATION_Handle *cfg,
150      struct GNUNET_TESTING_Peer *peer)
151 {
152   struct GNUNET_HELLO_Address hello_address;
153   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
154
155   /* Connect to ATS scheduling */
156   sched_ats = GNUNET_ATS_scheduling_init (cfg, NULL, NULL);
157   if (sched_ats == NULL)
158   {
159     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
160     ret = 1;
161     end ();
162     return;
163   }
164
165   /* Connect to ATS performance */
166   perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL);
167   if (perf_ats == NULL)
168   {
169     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS performance!\n");
170     ret = 1;
171     end ();
172     return;
173   }
174
175   /* Set up peer */
176   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
177   {
178       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
179       ret = GNUNET_SYSERR;
180       end ();
181       return;
182   }
183   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
184               GNUNET_i2s_full(&p.id));
185
186   create_test_address (&test_addr, "test", NULL, "test", strlen ("test") + 1);
187
188   /* Adding address without session */
189   hello_address.peer = p.id;
190   hello_address.transport_name = test_addr.plugin;
191   hello_address.address = test_addr.addr;
192   hello_address.address_length = test_addr.addr_len;
193   GNUNET_ATS_address_add (sched_ats, &hello_address, test_addr.session, NULL, 0);
194
195   /* Destroying address */
196   GNUNET_ATS_address_destroyed (sched_ats, &hello_address, test_addr.session);
197
198   /* Get list of addresses, expect 0 addresses only NULL callback */
199   GNUNET_ATS_performance_list_addresses (perf_ats, &p.id, GNUNET_YES, address_callback, NULL);
200 }
201
202
203 int
204 main (int argc, char *argv[])
205 {
206   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
207                                     "test_ats_api.conf",
208                                     &run, NULL))
209     return 1;
210   return ret;
211 }
212
213 /* end of file test_ats_api_scheduling_destroy_address.c */