changes
[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 with unknown address (address NULL, length 0)
23  *        in automatic transport selection scheduling API
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  *
27  */
28 #include "platform.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_testing_lib-new.h"
31 #include "ats.h"
32 #include "test_ats_api_common.h"
33
34
35 static GNUNET_SCHEDULER_TaskIdentifier die_task;
36
37 static struct GNUNET_ATS_SchedulingHandle *ats;
38
39 static int ret;
40
41 static int stage;
42
43 static struct Test_Address test_addr;
44
45 static struct PeerContext p;
46
47 static struct GNUNET_HELLO_Address hello_address;
48
49
50 static void
51 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
52 {
53   dest->plugin = GNUNET_strdup (plugin);
54   dest->session = session;
55   dest->addr = GNUNET_malloc (addrlen);
56   memcpy (dest->addr, addr, addrlen);
57   dest->addr_len = addrlen;
58 }
59
60 static void
61 free_test_address (struct Test_Address *dest)
62 {
63   GNUNET_free (dest->plugin);
64   GNUNET_free (dest->addr);
65 }
66
67 static void
68 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70   die_task = GNUNET_SCHEDULER_NO_TASK;
71   if (ats != NULL)
72     GNUNET_ATS_scheduling_done (ats);
73   free_test_address (&test_addr);
74   ret = GNUNET_SYSERR;
75 }
76
77
78 static void
79 end ()
80 {
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
82   if (die_task != GNUNET_SCHEDULER_NO_TASK)
83   {
84     GNUNET_SCHEDULER_cancel (die_task);
85     die_task = GNUNET_SCHEDULER_NO_TASK;
86   }
87   GNUNET_ATS_scheduling_done (ats);
88   free_test_address (&test_addr);
89   if (2 == stage)
90     ret = 0;
91   else
92   {
93     GNUNET_break (0);
94     ret = 1;
95   }
96 }
97
98
99 static void
100 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
101                     struct Session *session,
102                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
103                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
104                     const struct GNUNET_ATS_Information *atsi,
105                     uint32_t ats_count)
106 {
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: ATS suggests address `%s' session %p\n",
108               stage, GNUNET_i2s (&address->peer), session);
109   GNUNET_ATS_reset_backoff(ats, &address->peer);
110
111   GNUNET_assert (0 ==
112                  memcmp (&address->peer, &p.id,
113                          sizeof (struct GNUNET_PeerIdentity)));
114   GNUNET_assert (0 == strcmp (address->transport_name, test_addr.plugin));
115   GNUNET_assert (address->address_length == test_addr.addr_len);
116   GNUNET_assert (0 ==
117                  memcmp (address->address, test_addr.plugin,
118                          address->address_length));
119   GNUNET_assert (test_addr.session == session);
120
121   if (0 == stage)
122   {
123     /* Delete session without the address */
124     struct GNUNET_HELLO_Address hello_address_2;
125     hello_address_2.peer = p.id;
126     hello_address_2.transport_name = test_addr.plugin;
127     hello_address_2.address = NULL;
128     hello_address_2.address_length = 0;
129
130     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131                 "Stage %u: Destroying address for peer `%s' address %p length %u session %p\n",
132                 stage,
133                 GNUNET_i2s (&hello_address_2.peer),
134                 hello_address_2.address,
135                 hello_address_2.address_length,
136                 session);
137
138     GNUNET_ATS_address_destroyed (ats, &hello_address_2, test_addr.session);
139     test_addr.session = NULL;
140     GNUNET_ATS_suggest_address (ats, &p.id);
141   }
142   if (1 == stage)
143   {
144     /* End */
145     GNUNET_SCHEDULER_add_now (&end, NULL);
146   }
147   stage++;
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   ret = GNUNET_SYSERR;
157
158   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
159   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
160   if (ats == NULL)
161   {
162     ret = GNUNET_SYSERR;
163     end ();
164     return;
165   }
166
167   /* set up peer */
168   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
169                                     &p.id.hashPubKey);
170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
171               GNUNET_i2s (&p.id));
172
173   create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
174
175   /* Adding address with session */
176   hello_address.peer = p.id;
177   hello_address.transport_name = test_addr.plugin;
178   hello_address.address = test_addr.addr;
179   hello_address.address_length = test_addr.addr_len;
180   GNUNET_ATS_address_add (ats, &hello_address, test_addr.session, NULL, 0);
181   GNUNET_ATS_suggest_address (ats, &p.id);
182 }
183
184
185 int
186 main (int argc, char *argv[])
187 {
188   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_destroy_session",
189                                     "test_ats_api.conf",
190                                     &run, NULL))
191     return 1;
192   return ret;
193 }
194
195 /* end of file test_ats_api_scheduling_destroy_session.c */