ATS: Add first stub of test for new ATS API
[oweals/gnunet.git] / src / ats / test_ats2_lib.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file ats/test_ats2_lib.c
20  * @brief test ATS library with a generic interpreter for running ATS tests
21  * @author Julius Bünger
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_ats_application_service.h"
26 #include "gnunet_ats_transport_service.h"
27 #include "gnunet_testing_lib.h"
28
29 /**
30  * @brief ATS Application Handle
31  *
32  * Handle to the application-side of ATS.
33  */
34 static struct GNUNET_ATS_ApplicationHandle *ah;
35
36 /**
37  * @brief ATS Transport Handle
38  *
39  * Handle to the transport-side of ATS.
40  */
41 static struct GNUNET_ATS_TransportHandle *th;
42
43 /**
44  * @brief Another (dummy) peer.
45  *
46  * Used as the peer ATS shall allocate bandwidth to.
47  */
48 static struct GNUNET_PeerIdentity other_peer;
49
50 /**
51  * @brief Handle to the session record
52  */
53 static struct GNUNET_ATS_SessionRecord *sr;
54
55 /**
56  * @brief Called whenever allocation changed
57  *
58  * Implements #GNUNET_ATS_AllocationCallback
59  *
60  * @param cls
61  * @param session
62  * @param bandwidth_out
63  * @param bandwidth_in
64  *
65  * @return 
66  */
67 static void
68 allocation_cb (void *cls,
69                struct GNUNET_ATS_Session *session,
70                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
71                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
72 {
73   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "allocation_cb() called\n");
74 }
75
76
77 /**
78  * @brief Called whenever suggestion is made
79  *
80  * Implements #GNUNET_ATS_SuggestionCallback
81  *
82  * @param cls
83  * @param pid
84  * @param address
85  */
86 static void
87 suggestion_cb (void *cls,
88                const struct GNUNET_PeerIdentity *pid,
89                const char *address)
90 {
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggestion_cb() called\n");
92 }
93
94
95 /**
96  * @brief Initialise both 'sides' of ATS
97  *
98  * Initialises the application and transportation side of ATS.
99  */
100 static void
101 init_both (const struct GNUNET_CONFIGURATION_Handle *cfg)
102 {
103   ah = GNUNET_ATS_application_init (cfg);
104   GNUNET_assert (NULL != ah);
105   th = GNUNET_ATS_transport_init (cfg,
106                                   allocation_cb,
107                                   NULL,
108                                   suggestion_cb,
109                                   NULL);
110   GNUNET_assert (NULL != ah);
111 }
112
113
114 /**
115  * @brief Disconnect both 'sides' of ATS
116  */
117 static void
118 finish_both (void)
119 {
120   GNUNET_ATS_application_done (ah);
121   ah = NULL;
122   GNUNET_ATS_transport_done (th);
123   th = NULL;
124 }
125
126
127 /**
128  * @brief Provide information about the start of an imaginary connection
129  */
130 static void
131 provide_info_start (void)
132 {
133   struct GNUNET_ATS_Properties prop =
134   {
135     .delay = GNUNET_TIME_UNIT_FOREVER_REL,
136     .goodput_out = 1048576,
137     .goodput_in = 1048576,
138     .utilization_out = 0,
139     .utilization_in = 0,
140     .distance = 0,
141     .mtu = UINT32_MAX,
142     .nt = GNUNET_NT_UNSPECIFIED,
143     .cc = GNUNET_TRANSPORT_CC_UNKNOWN,
144   };
145
146   sr = GNUNET_ATS_session_add (th,
147                                &other_peer,
148                                "test-address",
149                                NULL,
150                                &prop);
151 }
152
153
154 /**
155  * @brief Provide information about the end of an imaginary connection
156  */
157 static void
158 provide_info_end (void)
159 {
160   GNUNET_ATS_session_del (sr);
161 }
162
163
164 /**
165  * @brief Inform ATS about the need of a connection towards a peer
166  */
167 static void
168 get_suggestion (void)
169 {
170   struct GNUNET_ATS_ApplicationSuggestHandle *ash;
171
172   ash = GNUNET_ATS_application_suggest (ah,
173                                         &other_peer,
174                                         GNUNET_MQ_PREFERENCE_NONE,
175                                         GNUNET_BANDWIDTH_VALUE_MAX);
176   GNUNET_ATS_application_suggest_cancel (ash);
177 }
178
179
180 /**
181  * Function run once the ATS service has been started.
182  *
183  * @param cls NULL
184  * @param cfg configuration for the testcase
185  * @param peer handle to the peer
186  */
187 static void
188 run (void *cls,
189      const struct GNUNET_CONFIGURATION_Handle *cfg,
190      struct GNUNET_TESTING_Peer *peer)
191 {
192   init_both (cfg);
193   provide_info_start ();
194   get_suggestion ();
195   provide_info_end ();
196   finish_both ();
197 }
198
199
200 /**
201  * @brief Starts the gnunet-testing peer
202  *
203  * @param argc
204  * @param argv[]
205  *
206  * @return 
207  */
208 int
209 main (int argc,
210       char *argv[])
211 {
212   memset (&other_peer, 0, sizeof (struct GNUNET_PeerIdentity));
213   return GNUNET_TESTING_peer_run ("test-ats2-lib",
214                                   "test_ats2_lib.conf",
215                                   &run, NULL);
216 }
217
218
219
220 /* end of test_ats2_lib.c */