ATS test: Give return value a meaning
[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 Indicates the success of the whole test
31  */
32 static int ret;
33
34
35 /**
36  * @brief ATS Application Handle
37  *
38  * Handle to the application-side of ATS.
39  */
40 static struct GNUNET_ATS_ApplicationHandle *ah;
41
42 /**
43  * @brief ATS Transport Handle
44  *
45  * Handle to the transport-side of ATS.
46  */
47 static struct GNUNET_ATS_TransportHandle *th;
48
49 /**
50  * @brief Another (dummy) peer.
51  *
52  * Used as the peer ATS shall allocate bandwidth to.
53  */
54 static struct GNUNET_PeerIdentity other_peer;
55
56 /**
57  * @brief Handle to the session record
58  */
59 static struct GNUNET_ATS_SessionRecord *sr;
60
61 /**
62  * @brief Called whenever allocation changed
63  *
64  * Implements #GNUNET_ATS_AllocationCallback
65  *
66  * @param cls
67  * @param session
68  * @param bandwidth_out
69  * @param bandwidth_in
70  *
71  * @return 
72  */
73 static void
74 allocation_cb (void *cls,
75                struct GNUNET_ATS_Session *session,
76                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
77                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
78 {
79   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "allocation_cb() called\n");
80 }
81
82
83 /**
84  * @brief Called whenever suggestion is made
85  *
86  * Implements #GNUNET_ATS_SuggestionCallback
87  *
88  * @param cls
89  * @param pid
90  * @param address
91  */
92 static void
93 suggestion_cb (void *cls,
94                const struct GNUNET_PeerIdentity *pid,
95                const char *address)
96 {
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggestion_cb() called\n");
98   ret = 0;
99 }
100
101
102 /**
103  * @brief Initialise both 'sides' of ATS
104  *
105  * Initialises the application and transportation side of ATS.
106  */
107 static void
108 init_both (const struct GNUNET_CONFIGURATION_Handle *cfg)
109 {
110   ah = GNUNET_ATS_application_init (cfg);
111   GNUNET_assert (NULL != ah);
112   th = GNUNET_ATS_transport_init (cfg,
113                                   allocation_cb,
114                                   NULL,
115                                   suggestion_cb,
116                                   NULL);
117   GNUNET_assert (NULL != ah);
118 }
119
120
121 /**
122  * @brief Disconnect both 'sides' of ATS
123  */
124 static void
125 finish_both (void)
126 {
127   GNUNET_ATS_application_done (ah);
128   ah = NULL;
129   GNUNET_ATS_transport_done (th);
130   th = NULL;
131 }
132
133
134 /**
135  * @brief Provide information about the start of an imaginary connection
136  */
137 static void
138 provide_info_start (void)
139 {
140   struct GNUNET_ATS_Properties prop =
141   {
142     .delay = GNUNET_TIME_UNIT_FOREVER_REL,
143     .goodput_out = 1048576,
144     .goodput_in = 1048576,
145     .utilization_out = 0,
146     .utilization_in = 0,
147     .distance = 0,
148     .mtu = UINT32_MAX,
149     .nt = GNUNET_NT_UNSPECIFIED,
150     .cc = GNUNET_TRANSPORT_CC_UNKNOWN,
151   };
152
153   sr = GNUNET_ATS_session_add (th,
154                                &other_peer,
155                                "test-address",
156                                NULL,
157                                &prop);
158   GNUNET_assert (NULL != sr);
159 }
160
161
162 /**
163  * @brief Provide information about the end of an imaginary connection
164  */
165 static void
166 provide_info_end (void)
167 {
168   GNUNET_ATS_session_del (sr);
169 }
170
171
172 /**
173  * @brief Inform ATS about the need of a connection towards a peer
174  */
175 static void
176 get_suggestion (void)
177 {
178   struct GNUNET_ATS_ApplicationSuggestHandle *ash;
179
180   ash = GNUNET_ATS_application_suggest (ah,
181                                         &other_peer,
182                                         GNUNET_MQ_PREFERENCE_NONE,
183                                         GNUNET_BANDWIDTH_VALUE_MAX);
184   GNUNET_ATS_application_suggest_cancel (ash);
185 }
186
187
188 /**
189  * Function run once the ATS service has been started.
190  *
191  * @param cls NULL
192  * @param cfg configuration for the testcase
193  * @param peer handle to the peer
194  */
195 static void
196 run (void *cls,
197      const struct GNUNET_CONFIGURATION_Handle *cfg,
198      struct GNUNET_TESTING_Peer *peer)
199 {
200   init_both (cfg);
201   provide_info_start ();
202   get_suggestion ();
203   provide_info_end ();
204   finish_both ();
205 }
206
207
208 /**
209  * @brief Starts the gnunet-testing peer
210  *
211  * @param argc
212  * @param argv[]
213  *
214  * @return 
215  */
216 int
217 main (int argc,
218       char *argv[])
219 {
220   ret = 1;
221   memset (&other_peer, 0, sizeof (struct GNUNET_PeerIdentity));
222   if (0 != GNUNET_TESTING_peer_run ("test-ats2-lib",
223                                     "test_ats2_lib.conf",
224                                     &run, NULL))
225   {
226     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running the testing peer failed.\n");
227     return 1;
228   }
229   return ret;
230 }
231
232
233
234 /* end of test_ats2_lib.c */