fix and new test
[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 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63 {
64   die_task = GNUNET_SCHEDULER_NO_TASK;
65
66   if (sched_ats != NULL)
67     GNUNET_ATS_scheduling_done (sched_ats);
68   if (perf_ats != NULL)
69     GNUNET_ATS_performance_done (perf_ats);
70   ret = GNUNET_SYSERR;
71 }
72
73
74 static void
75 end ()
76 {
77   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
78   if (die_task != GNUNET_SCHEDULER_NO_TASK)
79   {
80     GNUNET_SCHEDULER_cancel (die_task);
81     die_task = GNUNET_SCHEDULER_NO_TASK;
82   }
83
84   GNUNET_ATS_scheduling_done (sched_ats);
85   sched_ats = NULL;
86   GNUNET_ATS_performance_done (perf_ats);
87   perf_ats = NULL;
88 }
89
90 void address_callback (void *cls,
91                       const struct
92                       GNUNET_HELLO_Address *
93                       address,
94                       struct
95                       GNUNET_BANDWIDTH_Value32NBO
96                       bandwidth_out,
97                       struct
98                       GNUNET_BANDWIDTH_Value32NBO
99                       bandwidth_in,
100                       const struct
101                       GNUNET_ATS_Information *
102                       ats, uint32_t ats_count)
103 {
104   static int counter = 0;
105   if (NULL != address)
106   {
107       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received callback for peer `%s'\n",
108                   GNUNET_i2s (&address->peer));
109       counter ++;
110       return;
111   }
112   if (counter > 0)
113   {
114       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received %u unexpected addresses callbacks\n", counter);
115       ret = counter;
116   }
117   else
118   {
119       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received no unexpected addresses callbacks\n", counter);
120       ret = 0;
121   }
122   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last callback, shutdown\n");
123   GNUNET_SCHEDULER_add_now (&end, NULL);
124 }
125
126
127 static void
128 run (void *cls, 
129      const struct GNUNET_CONFIGURATION_Handle *cfg,
130      struct GNUNET_TESTING_Peer *peer)
131 {
132   struct GNUNET_HELLO_Address hello_address;
133   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
134
135   /* Connect to ATS scheduling */
136   sched_ats = GNUNET_ATS_scheduling_init (cfg, NULL, NULL);
137   if (sched_ats == NULL)
138   {
139     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
140     ret = 1;
141     end ();
142     return;
143   }
144
145   /* Connect to ATS performance */
146   perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL);
147   if (perf_ats == NULL)
148   {
149     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS performance!\n");
150     ret = 1;
151     end ();
152     return;
153   }
154
155   /* Set up peer */
156   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
157   {
158       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
159       ret = GNUNET_SYSERR;
160       end ();
161       return;
162   }
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
164               GNUNET_i2s_full(&p.id));
165
166   test_addr.plugin = "test";
167   test_addr.session = NULL;
168   test_addr.addr = GNUNET_strdup ("test");
169   test_addr.addr_len = 4;
170
171   /* Adding address without session */
172   hello_address.peer = p.id;
173   hello_address.transport_name = test_addr.plugin;
174   hello_address.address = test_addr.addr;
175   hello_address.address_length = test_addr.addr_len;
176   GNUNET_ATS_address_add (sched_ats, &hello_address, test_addr.session, NULL, 0);
177
178   /* Destroying address */
179   GNUNET_ATS_address_destroyed (sched_ats, &hello_address, test_addr.session);
180
181   /* Get list of addresses, expect 0 addresses only NULL callback */
182   GNUNET_ATS_performance_list_addresses (perf_ats, &p.id, GNUNET_YES, address_callback, NULL);
183 }
184
185
186 int
187 main (int argc, char *argv[])
188 {
189   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
190                                     "test_ats_api.conf",
191                                     &run, NULL))
192     return 1;
193   return ret;
194 }
195
196 /* end of file test_ats_api_scheduling_destroy_address.c */