changed testbed API to include convenience callbacks
[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
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
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 struct Address
44 {
45   char *plugin;
46   size_t plugin_len;
47
48   void *addr;
49   size_t addr_len;
50
51   struct GNUNET_ATS_Information *ats;
52   int ats_count;
53
54   void *session;
55 };
56
57 struct PeerContext
58 {
59   struct GNUNET_PeerIdentity id;
60
61   struct Address *addr;
62 };
63
64
65 static struct Address test_addr;
66
67 static struct PeerContext p;
68
69 static struct GNUNET_HELLO_Address hello_address;
70
71
72 static void
73 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   die_task = GNUNET_SCHEDULER_NO_TASK;
76   if (ats != NULL)
77     GNUNET_ATS_scheduling_done (ats);
78   ret = GNUNET_SYSERR;
79 }
80
81
82 static void
83 end ()
84 {
85   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
86   if (die_task != GNUNET_SCHEDULER_NO_TASK)
87   {
88     GNUNET_SCHEDULER_cancel (die_task);
89     die_task = GNUNET_SCHEDULER_NO_TASK;
90   }
91   GNUNET_ATS_scheduling_done (ats);
92   if (2 == stage)
93     ret = 0;
94   else
95   {
96     GNUNET_break (0);
97     ret = 1;
98   }
99 }
100
101
102 static void
103 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
104                     struct Session *session,
105                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
106                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
107                     const struct GNUNET_ATS_Information *atsi,
108                     uint32_t ats_count)
109 {
110   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: ATS suggests address `%s' session %p\n",
111               stage, GNUNET_i2s (&address->peer), session);
112   GNUNET_ATS_reset_backoff(ats, &address->peer);
113
114   GNUNET_assert (0 ==
115                  memcmp (&address->peer, &p.id,
116                          sizeof (struct GNUNET_PeerIdentity)));
117   GNUNET_assert (0 == strcmp (address->transport_name, test_addr.plugin));
118   GNUNET_assert (address->address_length == test_addr.addr_len);
119   GNUNET_assert (0 ==
120                  memcmp (address->address, test_addr.plugin,
121                          address->address_length));
122   GNUNET_assert (test_addr.session == session);
123
124   if (0 == stage)
125   {
126     /* Delete session without the address */
127     struct GNUNET_HELLO_Address hello_address_2;
128     hello_address_2.peer = p.id;
129     hello_address_2.transport_name = test_addr.plugin;
130     hello_address_2.address = NULL;
131     hello_address_2.address_length = 0;
132
133     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
134                 "Stage %u: Destroying address for peer `%s' address %p length %u session %p\n",
135                 stage,
136                 GNUNET_i2s (&hello_address_2.peer),
137                 hello_address_2.address,
138                 hello_address_2.address_length,
139                 session);
140
141     GNUNET_ATS_address_destroyed (ats, &hello_address_2, test_addr.session);
142     test_addr.session = NULL;
143     GNUNET_ATS_suggest_address (ats, &p.id);
144   }
145   if (1 == stage)
146   {
147     /* End */
148     GNUNET_SCHEDULER_add_now (&end, NULL);
149   }
150   stage++;
151 }
152
153
154 static void
155 run (void *cls, 
156      const struct GNUNET_CONFIGURATION_Handle *cfg,
157      struct GNUNET_TESTING_Peer *peer)
158 {
159   ret = GNUNET_SYSERR;
160
161   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
162   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
163   if (ats == NULL)
164   {
165     ret = GNUNET_SYSERR;
166     end ();
167     return;
168   }
169
170   /* set up peer */
171   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
172                                     &p.id.hashPubKey);
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
174               GNUNET_i2s (&p.id));
175
176   test_addr.plugin = "test";
177   test_addr.session = &test_addr;
178   test_addr.addr = GNUNET_strdup ("test");
179   test_addr.addr_len = 4;
180
181   /* Adding address with session */
182   hello_address.peer = p.id;
183   hello_address.transport_name = test_addr.plugin;
184   hello_address.address = test_addr.addr;
185   hello_address.address_length = test_addr.addr_len;
186   GNUNET_ATS_address_add (ats, &hello_address, test_addr.session, NULL, 0);
187   GNUNET_ATS_suggest_address (ats, &p.id);
188 }
189
190
191 int
192 main (int argc, char *argv[])
193 {
194   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_destroy_session",
195                                     "test_ats_api.conf",
196                                     &run, NULL))
197     return 1;
198   return ret;
199 }
200
201 /* end of file test_ats_api_scheduling_destroy_session.c */