diff options
author | Bart Polot <bart.polot+voyager@gmail.com> | 2017-02-08 19:43:33 +0100 |
---|---|---|
committer | Bart Polot <bart.polot+voyager@gmail.com> | 2017-02-08 19:43:33 +0100 |
commit | a03d291e3253572b19712918759011198118b3b2 (patch) | |
tree | 01234f2808bdc110839112956f4b7995f1248473 /src/cadet/test_cadet_local_mq.c | |
parent | ec05b7e9cef7322f3291c96c278056f387ba574b (diff) | |
download | gnunet-a03d291e3253572b19712918759011198118b3b2.tar.gz gnunet-a03d291e3253572b19712918759011198118b3b2.zip |
Add mq test
Diffstat (limited to 'src/cadet/test_cadet_local_mq.c')
-rw-r--r-- | src/cadet/test_cadet_local_mq.c | 331 |
1 files changed, 331 insertions, 0 deletions
diff --git a/src/cadet/test_cadet_local_mq.c b/src/cadet/test_cadet_local_mq.c new file mode 100644 index 000000000..988ec725e --- /dev/null +++ b/src/cadet/test_cadet_local_mq.c | |||
@@ -0,0 +1,331 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011 GNUnet e.V. | ||
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., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file cadet/test_cadet_local.c | ||
23 | * @brief test cadet local: test of cadet channels with just one peer | ||
24 | * @author Bartlomiej Polot | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_dht_service.h" | ||
30 | #include "gnunet_testing_lib.h" | ||
31 | #include "gnunet_cadet_service.h" | ||
32 | |||
33 | #define TEST_MESSAGE_TYPE 1 | ||
34 | #define TEST_PORT_ID 1 | ||
35 | |||
36 | /** | ||
37 | * Test message structure. | ||
38 | */ | ||
39 | struct GNUNET_CADET_TestMsg | ||
40 | { | ||
41 | /** | ||
42 | * Type: #TEST_MESSAGE_TYPE | ||
43 | * | ||
44 | * Size: sizeof(struct GNUNET_CADET_TestMsg) | ||
45 | */ | ||
46 | struct GNUNET_MessageHeader header; | ||
47 | |||
48 | /** | ||
49 | * Test payload. | ||
50 | */ | ||
51 | uint64_t payload; | ||
52 | }; | ||
53 | |||
54 | struct GNUNET_TESTING_Peer *me; | ||
55 | |||
56 | static struct GNUNET_CADET_Handle *cadet_peer_1; | ||
57 | |||
58 | static struct GNUNET_CADET_Handle *cadet_peer_2; | ||
59 | |||
60 | static struct GNUNET_CADET_Channel *ch; | ||
61 | |||
62 | static int result = GNUNET_OK; | ||
63 | |||
64 | static int got_data = GNUNET_NO; | ||
65 | |||
66 | static struct GNUNET_SCHEDULER_Task *abort_task; | ||
67 | |||
68 | static struct GNUNET_SCHEDULER_Task *connect_task; | ||
69 | |||
70 | |||
71 | /** | ||
72 | * Connect to other client and send data | ||
73 | * | ||
74 | * @param cls Closue (unused). | ||
75 | */ | ||
76 | static void | ||
77 | do_connect (void *cls); | ||
78 | |||
79 | |||
80 | /** | ||
81 | * Shutdown nicely | ||
82 | */ | ||
83 | static void | ||
84 | do_shutdown (void *cls) | ||
85 | { | ||
86 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
87 | "shutdown\n"); | ||
88 | if (NULL != abort_task) | ||
89 | { | ||
90 | GNUNET_SCHEDULER_cancel (abort_task); | ||
91 | abort_task = NULL; | ||
92 | } | ||
93 | if (NULL != ch) | ||
94 | { | ||
95 | GNUNET_CADET_channel_destroy (ch); | ||
96 | ch = NULL; | ||
97 | } | ||
98 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
99 | "Disconnect client 1\n"); | ||
100 | if (NULL != cadet_peer_1) | ||
101 | { | ||
102 | GNUNET_CADET_disconnect (cadet_peer_1); | ||
103 | cadet_peer_1 = NULL; | ||
104 | } | ||
105 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
106 | "Disconnect client 2\n"); | ||
107 | if (NULL != cadet_peer_2) | ||
108 | { | ||
109 | GNUNET_CADET_disconnect (cadet_peer_2); | ||
110 | cadet_peer_2 = NULL; | ||
111 | } | ||
112 | if (NULL != connect_task) | ||
113 | { | ||
114 | GNUNET_SCHEDULER_cancel (connect_task); | ||
115 | connect_task = NULL; | ||
116 | } | ||
117 | } | ||
118 | |||
119 | |||
120 | /** | ||
121 | * Something went wrong and timed out. Kill everything and set error flag | ||
122 | */ | ||
123 | static void | ||
124 | do_abort (void *cls) | ||
125 | { | ||
126 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ABORT from line %ld\n", (long) cls); | ||
127 | result = GNUNET_SYSERR; | ||
128 | abort_task = NULL; | ||
129 | GNUNET_SCHEDULER_shutdown (); | ||
130 | } | ||
131 | |||
132 | /** | ||
133 | * Method called whenever a peer connects to a port in MQ-based CADET. | ||
134 | * | ||
135 | * @param cls Closure from #GNUNET_CADET_open_porT. | ||
136 | * @param channel New handle to the channel. | ||
137 | * @param source Peer that started this channel. | ||
138 | * @return Closure for the incoming @a channel. It's given to: | ||
139 | * - The #GNUNET_CADET_DisconnectEventHandler (given to | ||
140 | * #GNUNET_CADET_open_porT) when the channel dies. | ||
141 | * - Each the #GNUNET_MQ_MessageCallback handlers for each message | ||
142 | * received on the @a channel. | ||
143 | */ | ||
144 | static void * | ||
145 | connected (void *cls, | ||
146 | struct GNUNET_CADET_Channel *channel, | ||
147 | const struct GNUNET_PeerIdentity *source) | ||
148 | { | ||
149 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
150 | "connected %s, cls: %p\n", | ||
151 | GNUNET_i2s(source), cls); | ||
152 | return channel; | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * Function called whenever an MQ-channel is destroyed, even if the destruction | ||
157 | * was requested by #GNUNET_CADET_channel_destroy. | ||
158 | * It must NOT call #GNUNET_CADET_channel_destroy on the channel. | ||
159 | * | ||
160 | * It should clean up any associated state, including cancelling any pending | ||
161 | * transmission on this channel. | ||
162 | * | ||
163 | * @param cls Channel closure. | ||
164 | * @param channel Connection to the other end (henceforth invalid). | ||
165 | */ | ||
166 | static void | ||
167 | disconnected (void *cls, | ||
168 | const struct GNUNET_CADET_Channel *channel) | ||
169 | { | ||
170 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
171 | "disconnected cls: %p\n", | ||
172 | cls); | ||
173 | if (channel == ch) | ||
174 | ch = NULL; | ||
175 | } | ||
176 | |||
177 | |||
178 | /** | ||
179 | * Handle test data | ||
180 | * | ||
181 | * @param h The cadet handle | ||
182 | * @param msg A message with the details of the new incoming channel | ||
183 | */ | ||
184 | static void | ||
185 | handle_data_received (void *cls, | ||
186 | const struct GNUNET_CADET_TestMsg *msg) | ||
187 | { | ||
188 | uint64_t payload; | ||
189 | |||
190 | payload = GNUNET_ntohll (msg->payload); | ||
191 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
192 | "Data callback payload %lu with cls: %p! Shutting down.\n", | ||
193 | payload, cls); | ||
194 | GNUNET_assert (42 == payload); | ||
195 | got_data = GNUNET_YES; | ||
196 | GNUNET_SCHEDULER_shutdown (); | ||
197 | } | ||
198 | |||
199 | |||
200 | /** | ||
201 | * Signature of the main function of a task. | ||
202 | * | ||
203 | * @param cls Closure (unused). | ||
204 | */ | ||
205 | static void | ||
206 | message_sent (void *cls) | ||
207 | { | ||
208 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message sent\n"); | ||
209 | } | ||
210 | |||
211 | |||
212 | /** | ||
213 | * Connect to other client and send data | ||
214 | * | ||
215 | * @param cls Closure (unused). | ||
216 | */ | ||
217 | static void | ||
218 | do_connect (void *cls) | ||
219 | { | ||
220 | struct GNUNET_PeerIdentity id; | ||
221 | struct GNUNET_MQ_Handle *mq; | ||
222 | struct GNUNET_MQ_Envelope *env; | ||
223 | struct GNUNET_CADET_TestMsg *msg; | ||
224 | |||
225 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
226 | GNUNET_MQ_hd_fixed_size (data_received, | ||
227 | TEST_MESSAGE_TYPE, | ||
228 | struct GNUNET_CADET_TestMsg, | ||
229 | cadet_peer_1), | ||
230 | GNUNET_MQ_handler_end () | ||
231 | }; | ||
232 | |||
233 | connect_task = NULL; | ||
234 | GNUNET_TESTING_peer_get_identity (me, &id); | ||
235 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
236 | "creating channel\n"); | ||
237 | ch = GNUNET_CADET_channel_creatE (cadet_peer_1, /* cadet handle */ | ||
238 | NULL, /* channel cls */ | ||
239 | &id, /* destination */ | ||
240 | GC_u2h (TEST_MESSAGE_TYPE), /* port */ | ||
241 | GNUNET_CADET_OPTION_DEFAULT, /* opt */ | ||
242 | NULL, /* window change */ | ||
243 | &disconnected, /* disconnect handler */ | ||
244 | handlers /* traffic handlers */ | ||
245 | ); | ||
246 | env = GNUNET_MQ_msg (msg, TEST_MESSAGE_TYPE); | ||
247 | msg->payload = GNUNET_htonll (42); | ||
248 | mq = GNUNET_CADET_get_mq (ch); | ||
249 | GNUNET_MQ_notify_sent (env, &message_sent, NULL); | ||
250 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
251 | "sending message\n"); | ||
252 | GNUNET_MQ_send (mq, env); | ||
253 | } | ||
254 | |||
255 | |||
256 | /** | ||
257 | * Initialize framework and start test | ||
258 | * | ||
259 | * @param cls Closure (unused). | ||
260 | * @param cfg Configuration handle. | ||
261 | * @param peer Testing peer handle. | ||
262 | */ | ||
263 | static void | ||
264 | run (void *cls, | ||
265 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
266 | struct GNUNET_TESTING_Peer *peer) | ||
267 | { | ||
268 | struct GNUNET_TIME_Relative start_delay; | ||
269 | me = peer; | ||
270 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, | ||
271 | NULL); | ||
272 | abort_task = | ||
273 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
274 | (GNUNET_TIME_UNIT_SECONDS, 15), | ||
275 | &do_abort, | ||
276 | (void *) (long) __LINE__); | ||
277 | cadet_peer_1 = GNUNET_CADET_connecT (cfg); | ||
278 | cadet_peer_2 = GNUNET_CADET_connecT (cfg); | ||
279 | |||
280 | if ( (NULL == cadet_peer_1) || | ||
281 | (NULL == cadet_peer_2) ) | ||
282 | { | ||
283 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
284 | "Couldn't connect to cadet\n"); | ||
285 | result = GNUNET_SYSERR; | ||
286 | GNUNET_SCHEDULER_shutdown (); | ||
287 | return; | ||
288 | } | ||
289 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 1: %p\n", cadet_peer_1); | ||
290 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 2: %p\n", cadet_peer_2); | ||
291 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
292 | GNUNET_MQ_hd_fixed_size (data_received, | ||
293 | TEST_MESSAGE_TYPE, | ||
294 | struct GNUNET_CADET_TestMsg, | ||
295 | cadet_peer_2), | ||
296 | GNUNET_MQ_handler_end () | ||
297 | }; | ||
298 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handlers 2: %p\n", handlers); | ||
299 | GNUNET_CADET_open_porT (cadet_peer_2, /* cadet handle */ | ||
300 | GC_u2h (TEST_PORT_ID), /* port id */ | ||
301 | &connected, /* connect handler */ | ||
302 | (void *) 2L, /* handle for #connected */ | ||
303 | NULL, /* window size handler */ | ||
304 | &disconnected, /* disconnect handler */ | ||
305 | handlers); /* traffic handlers */ | ||
306 | start_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2); | ||
307 | if (NULL == connect_task) | ||
308 | connect_task = GNUNET_SCHEDULER_add_delayed (start_delay, | ||
309 | &do_connect, | ||
310 | NULL); | ||
311 | } | ||
312 | |||
313 | |||
314 | /** | ||
315 | * Main | ||
316 | */ | ||
317 | int | ||
318 | main (int argc, char *argv[]) | ||
319 | { | ||
320 | if (0 != GNUNET_TESTING_peer_run ("test-cadet-local", | ||
321 | "test_cadet.conf", | ||
322 | &run, NULL)) | ||
323 | { | ||
324 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n"); | ||
325 | return 2; | ||
326 | } | ||
327 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result); | ||
328 | return (result == GNUNET_OK) ? 0 : 1; | ||
329 | } | ||
330 | |||
331 | /* end of test_cadet_local_1.c */ | ||