fixed tests
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_destroy_session.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_session.c
22  * @brief test destroying sessions: first add an address with a session,
23  *        request the address and compare, delete the session, request and
24  *        compare again, delete whole address, request and wait for timeout,
25  *        shutdown
26  * @author Christian Grothoff
27  * @author Matthias Wachs
28  *
29  */
30 #include "platform.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_testing_lib.h"
33 #include "ats.h"
34 #include "test_ats_api_common.h"
35
36 /**
37  * Timeout task
38  */
39 static GNUNET_SCHEDULER_TaskIdentifier die_task;
40
41 /**
42  * Statistics handle
43  */
44 struct GNUNET_STATISTICS_Handle *stats;
45
46 /**
47  * Scheduling handle
48  */
49 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
50
51 /**
52  * Return value
53  */
54 static int ret;
55
56 /**
57  * Test address
58  */
59 static struct Test_Address test_addr;
60
61 /**
62  * Test peer
63  */
64 static struct PeerContext p;
65
66 /**
67  * HELLO address
68  */
69 struct GNUNET_HELLO_Address test_hello_address;
70
71 /**
72  * Session
73  */
74 static void *test_session;
75
76 /**
77  * Test ats info
78  */
79 struct GNUNET_ATS_Information test_ats_info[2];
80
81 /**
82  * Test ats count
83  */
84 uint32_t test_ats_count;
85
86
87 static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
88 static void end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
89
90 static int
91 stat_cb(void *cls, const char *subsystem,
92         const char *name, uint64_t value,
93         int is_persistent)
94 {
95   static int first_stat_cb = GNUNET_YES;
96
97   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
98       subsystem,name, value);
99   if ((GNUNET_YES == first_stat_cb) && (1 == value))
100   {
101     GNUNET_ATS_address_add (sched_ats, &test_hello_address, (struct Session *) &test_session, test_ats_info, test_ats_count);
102     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
103   }
104   if ((GNUNET_NO == first_stat_cb) && (1 == value))
105   {
106     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS updated existing address\n");
107   }
108   if (value > 1)
109   {
110     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS did not update existing address, but added 2nd address!\n");
111     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
112   }
113
114   return GNUNET_OK;
115 }
116
117
118 static void
119 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 {
121   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
122
123   if (die_task != GNUNET_SCHEDULER_NO_TASK)
124   {
125     GNUNET_SCHEDULER_cancel (die_task);
126     die_task = GNUNET_SCHEDULER_NO_TASK;
127   }
128
129   if (NULL != sched_ats)
130   {
131     GNUNET_ATS_scheduling_done (sched_ats);
132     sched_ats = NULL;
133   }
134
135   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
136   if (NULL != stats)
137   {
138     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
139     stats = NULL;
140   }
141
142   free_test_address (&test_addr);
143
144   ret = 0;
145 }
146
147 static void
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 {
150   die_task = GNUNET_SCHEDULER_NO_TASK;
151   end ( NULL, NULL);
152   ret = GNUNET_SYSERR;
153 }
154
155 static void
156 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
157                     struct Session *session,
158                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
159                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
160                     const struct GNUNET_ATS_Information *atsi,
161                     uint32_t ats_count)
162 {
163   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
164   GNUNET_SCHEDULER_add_now (&end_badly, NULL);
165   return;
166 }
167
168
169 static void
170 run (void *cls, 
171      const struct GNUNET_CONFIGURATION_Handle *cfg,
172      struct GNUNET_TESTING_Peer *peer)
173 {
174   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
175   stats = GNUNET_STATISTICS_create ("ats", cfg);
176   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
177
178
179   /* Connect to ATS scheduling */
180   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
181   if (sched_ats == NULL)
182   {
183     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
184     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
185     return;
186   }
187
188   /* Set up peer */
189   memset (&p.id, '1', sizeof (p.id));
190   /* Prepare ATS Information */
191   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
192   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
193   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
194   test_ats_info[1].value = htonl(1);
195   test_ats_count = 2;
196
197   /* Adding address without session */
198   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
199   test_hello_address.peer = p.id;
200   test_hello_address.transport_name = test_addr.plugin;
201   test_hello_address.address = test_addr.addr;
202   test_hello_address.address_length = test_addr.addr_len;
203
204   /* Adding address */
205   GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
206 }
207
208
209 int
210 main (int argc, char *argv[])
211 {
212   ret = 0;
213   if (0 != GNUNET_TESTING_peer_run ("test-ats-api",
214                                     "test_ats_api.conf",
215                                     &run, NULL))
216     return 1;
217   return ret;
218 }
219 /* end of file test_ats_api_scheduling_destroy_session.c */