1 /*
2 * This file is part of the Nice GLib ICE library.
3 *
4 * (C) 2013 Collabora Ltd.
5 * Contact: Philip Withnall
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is the Nice GLib ICE library.
18 *
19 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20 * Corporation. All Rights Reserved.
21 *
22 * Contributors:
23 * Philip Withnall, Collabora Ltd.
24 *
25 * Alternatively, the contents of this file may be used under the terms of the
26 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27 * case the provisions of LGPL are applicable instead of those above. If you
28 * wish to allow use of your version of this file only under the terms of the
29 * LGPL and not to allow others to use your version of this file under the
30 * MPL, indicate your decision by deleting the provisions above and replace
31 * them with the notice and other provisions required by the LGPL. If you do
32 * not delete the provisions above, a recipient may use your version of this
33 * file under either the MPL or the LGPL.
34 */
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 #include <string.h>
40
41 #include "agent.h"
42
43 #include "iostream.h"
44
45 #include "test-io-stream-common.h"
46
47 static void
test_invalid_stream(NiceAddress * addr)48 test_invalid_stream (NiceAddress *addr)
49 {
50 NiceAgent *agent;
51 GIOStream *io_stream;
52
53 agent = nice_agent_new_reliable (NULL, NICE_COMPATIBILITY_RFC5245);
54 g_object_set (G_OBJECT (agent), "upnp", FALSE, NULL);
55 nice_agent_add_local_address (agent, addr);
56
57 /* Try building an I/O stream for an invalid stream. All its operations should
58 * return G_IO_ERROR_BROKEN_PIPE. */
59 io_stream = nice_agent_get_io_stream (agent, 5, 5);
60 g_assert (io_stream == NULL);
61
62 g_object_unref (agent);
63 }
64
65 static void
test_io_stream_properties(NiceAddress * addr)66 test_io_stream_properties (NiceAddress *addr)
67 {
68 NiceAgent *agent;
69 guint stream_id;
70 GIOStream *io_stream;
71 GInputStream *input_stream;
72 GOutputStream *output_stream;
73
74 agent = nice_agent_new_reliable (NULL, NICE_COMPATIBILITY_RFC5245);
75 g_object_set (G_OBJECT (agent), "upnp", FALSE, NULL);
76 nice_agent_add_local_address (agent, addr);
77
78 stream_id = nice_agent_add_stream (agent, 1);
79
80 /* Try building an I/O stream around it. */
81 io_stream = nice_agent_get_io_stream (agent, stream_id, 1);
82 g_assert (G_IS_IO_STREAM (io_stream));
83 g_assert (NICE_IS_IO_STREAM (io_stream));
84
85 /* Check various initial properties. */
86 g_assert (!g_io_stream_is_closed (G_IO_STREAM (io_stream)));
87 g_assert (!g_io_stream_has_pending (G_IO_STREAM (io_stream)));
88
89 /* Check the input stream’s properties. */
90 input_stream = g_io_stream_get_input_stream (G_IO_STREAM (io_stream));
91 g_assert (G_IS_INPUT_STREAM (input_stream));
92 g_assert (NICE_IS_INPUT_STREAM (input_stream));
93
94 g_assert (!g_input_stream_is_closed (input_stream));
95 g_assert (!g_input_stream_has_pending (input_stream));
96
97 /* Check the output stream’s properties. */
98 output_stream = g_io_stream_get_output_stream (G_IO_STREAM (io_stream));
99 g_assert (G_IS_OUTPUT_STREAM (output_stream));
100 g_assert (NICE_IS_OUTPUT_STREAM (output_stream));
101
102 g_assert (!g_output_stream_is_closing (output_stream));
103 g_assert (!g_output_stream_is_closed (output_stream));
104 g_assert (!g_output_stream_has_pending (output_stream));
105
106 /* Remove the component and check that the I/O streams close. */
107 nice_agent_remove_stream (agent, stream_id);
108
109 g_assert (g_io_stream_is_closed (G_IO_STREAM (io_stream)));
110 g_assert (g_input_stream_is_closed (input_stream));
111 g_assert (g_output_stream_is_closed (output_stream));
112
113 g_object_unref (io_stream);
114 g_object_unref (agent);
115 }
116
117 static void
test_pollable_properties(NiceAddress * addr)118 test_pollable_properties (NiceAddress *addr)
119 {
120 NiceAgent *agent;
121 guint stream_id;
122 GIOStream *io_stream;
123 GInputStream *input_stream;
124 GOutputStream *output_stream;
125 GPollableInputStream *pollable_input_stream;
126 GPollableOutputStream *pollable_output_stream;
127 guint8 buf[65536];
128 GError *error = NULL;
129 GSource *stream_source;
130
131 agent = nice_agent_new_reliable (NULL, NICE_COMPATIBILITY_RFC5245);
132 g_object_set (G_OBJECT (agent), "upnp", FALSE, NULL);
133 nice_agent_add_local_address (agent, addr);
134
135 /* Add a stream. */
136 stream_id = nice_agent_add_stream (agent, 1);
137
138 /* Try building an I/O stream around it. */
139 io_stream = nice_agent_get_io_stream (agent, stream_id, 1);
140 g_assert (G_IS_IO_STREAM (io_stream));
141 g_assert (NICE_IS_IO_STREAM (io_stream));
142
143 /* Check the input stream’s properties. */
144 input_stream = g_io_stream_get_input_stream (G_IO_STREAM (io_stream));
145 g_assert (G_IS_POLLABLE_INPUT_STREAM (input_stream));
146 pollable_input_stream = G_POLLABLE_INPUT_STREAM (input_stream);
147
148 g_assert (g_pollable_input_stream_can_poll (pollable_input_stream));
149 g_assert (!g_pollable_input_stream_is_readable (pollable_input_stream));
150
151 g_assert (
152 g_pollable_input_stream_read_nonblocking (pollable_input_stream,
153 buf, sizeof (buf), NULL, &error) == -1);
154 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
155 g_clear_error (&error);
156
157 stream_source =
158 g_pollable_input_stream_create_source (pollable_input_stream, NULL);
159 g_assert (stream_source != NULL);
160 g_source_unref (stream_source);
161
162 /* Check the output stream’s properties. */
163 output_stream = g_io_stream_get_output_stream (G_IO_STREAM (io_stream));
164 g_assert (G_IS_POLLABLE_OUTPUT_STREAM (output_stream));
165 pollable_output_stream = G_POLLABLE_OUTPUT_STREAM (output_stream);
166
167 g_assert (g_pollable_output_stream_can_poll (pollable_output_stream));
168 g_assert (!g_pollable_output_stream_is_writable (pollable_output_stream));
169
170 g_assert (
171 g_pollable_output_stream_write_nonblocking (pollable_output_stream,
172 buf, sizeof (buf), NULL, &error) == -1);
173 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
174 g_clear_error (&error);
175
176 stream_source =
177 g_pollable_output_stream_create_source (pollable_output_stream, NULL);
178 g_assert (stream_source != NULL);
179 g_source_unref (stream_source);
180
181 /* Remove the component and check that the I/O streams close. */
182 nice_agent_remove_stream (agent, stream_id);
183
184 g_assert (!g_pollable_input_stream_is_readable (pollable_input_stream));
185 g_assert (!g_pollable_output_stream_is_writable (pollable_output_stream));
186
187 g_assert (
188 g_pollable_input_stream_read_nonblocking (pollable_input_stream,
189 buf, sizeof (buf), NULL, &error) == 0);
190 g_assert_no_error (error);
191
192 g_assert (
193 g_pollable_output_stream_write_nonblocking (pollable_output_stream,
194 buf, sizeof (buf), NULL, &error) == -1);
195 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
196 g_clear_error (&error);
197
198 g_object_unref (io_stream);
199 g_object_unref (agent);
200 }
201
202 static gboolean
source_cancel_cb(gpointer user_data)203 source_cancel_cb (gpointer user_data)
204 {
205 GCancellable *cancellable = user_data;
206
207 g_cancellable_cancel (cancellable);
208
209 return FALSE;
210 }
211
212 static gboolean
source_cancelled_cb(GObject * pollable_stream,gpointer user_data)213 source_cancelled_cb (GObject *pollable_stream, gpointer user_data)
214 {
215 GMainLoop *main_loop = user_data;
216
217 /* Try and check that the callback was invoked due to cancellation rather than
218 * a poll() event on the socket itself. */
219 if (G_IS_POLLABLE_INPUT_STREAM (pollable_stream)) {
220 g_assert (
221 !g_pollable_input_stream_is_readable (
222 G_POLLABLE_INPUT_STREAM (pollable_stream)));
223 } else {
224 g_assert (
225 !g_pollable_output_stream_is_writable (
226 G_POLLABLE_OUTPUT_STREAM (pollable_stream)));
227 }
228
229 g_main_loop_quit (main_loop);
230
231 return FALSE;
232 }
233
234 static gboolean
source_timeout_cb(gpointer user_data)235 source_timeout_cb (gpointer user_data)
236 {
237 g_error ("check_pollable_source_cancellation() took too long. Aborting.");
238
239 return FALSE;
240 }
241
242 /* Check that cancelling a GCancellable which is associated with a pollable
243 * stream’s GSource invokes a callback from that source in the main loop. This
244 * uses a main context with three sources: the pollable source, an idle source
245 * to trigger the cancellation, and a timeout source to fail the test if it
246 * takes too long. */
247 static void
check_pollable_source_cancellation(GSource * pollable_source,GCancellable * cancellable)248 check_pollable_source_cancellation (GSource *pollable_source,
249 GCancellable *cancellable)
250 {
251 GMainContext *main_context;
252 GMainLoop *main_loop;
253 GSource *idle_source, *timeout_source;
254
255 main_context = g_main_context_new ();
256 main_loop = g_main_loop_new (main_context, FALSE);
257
258 /* Set up the pollable source. */
259 g_source_set_callback (pollable_source, G_SOURCE_FUNC (source_cancelled_cb),
260 main_loop, NULL);
261 g_source_attach (pollable_source, main_context);
262
263 /* Idle source to cancel the cancellable. */
264 idle_source = g_idle_source_new ();
265 g_source_set_callback (idle_source, (GSourceFunc) source_cancel_cb,
266 cancellable, NULL);
267 g_source_attach (idle_source, main_context);
268 g_source_unref (idle_source);
269
270 /* Timeout. */
271 timeout_source = g_timeout_source_new (30000);
272 g_source_set_callback (timeout_source, (GSourceFunc) source_timeout_cb,
273 NULL, NULL);
274 g_source_attach (timeout_source, main_context);
275 g_source_unref (timeout_source);
276
277 /* Run the main loop and expect to quit it immediately as the pollable source
278 * is cancelled. */
279 g_main_loop_run (main_loop);
280
281 g_assert (g_cancellable_is_cancelled (cancellable));
282
283 g_main_loop_unref (main_loop);
284 g_main_context_unref (main_context);
285 }
286
287 static void
test_pollable_cancellation(NiceAddress * addr)288 test_pollable_cancellation (NiceAddress *addr)
289 {
290 NiceAgent *agent;
291 guint stream_id;
292 GIOStream *io_stream;
293 GInputStream *input_stream;
294 GOutputStream *output_stream;
295 GPollableInputStream *pollable_input_stream;
296 GPollableOutputStream *pollable_output_stream;
297 guint8 buf[65536];
298 GError *error = NULL;
299 GSource *stream_source;
300 GCancellable *cancellable;
301
302 agent = nice_agent_new_reliable (NULL, NICE_COMPATIBILITY_RFC5245);
303 g_object_set (G_OBJECT (agent), "upnp", FALSE, NULL);
304 nice_agent_add_local_address (agent, addr);
305
306 /* Add a stream. */
307 stream_id = nice_agent_add_stream (agent, 1);
308
309 /* Try building an I/O stream around it. */
310 io_stream = nice_agent_get_io_stream (agent, stream_id, 1);
311 g_assert (G_IS_IO_STREAM (io_stream));
312 g_assert (NICE_IS_IO_STREAM (io_stream));
313
314 /* Grab the input and output streams. */
315 input_stream = g_io_stream_get_input_stream (G_IO_STREAM (io_stream));
316 g_assert (G_IS_POLLABLE_INPUT_STREAM (input_stream));
317 pollable_input_stream = G_POLLABLE_INPUT_STREAM (input_stream);
318
319 output_stream = g_io_stream_get_output_stream (G_IO_STREAM (io_stream));
320 g_assert (G_IS_POLLABLE_OUTPUT_STREAM (output_stream));
321 pollable_output_stream = G_POLLABLE_OUTPUT_STREAM (output_stream);
322
323 /* Check the non-blocking read() and write() return immediately if called with
324 * a cancelled cancellable. */
325 cancellable = g_cancellable_new ();
326 g_cancellable_cancel (cancellable);
327
328 g_assert (
329 g_pollable_input_stream_read_nonblocking (pollable_input_stream,
330 buf, sizeof (buf), cancellable, &error) == -1);
331 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
332 g_clear_error (&error);
333
334 g_assert (
335 g_pollable_output_stream_write_nonblocking (pollable_output_stream,
336 buf, sizeof (buf), cancellable, &error) == -1);
337 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
338 g_clear_error (&error);
339
340 g_object_unref (cancellable);
341
342 /* Check the GSources invoke a callback when run with the cancellable
343 * cancelled. */
344 cancellable = g_cancellable_new ();
345 stream_source =
346 g_pollable_input_stream_create_source (pollable_input_stream,
347 cancellable);
348
349 check_pollable_source_cancellation (stream_source, cancellable);
350
351 g_source_unref (stream_source);
352 g_object_unref (cancellable);
353
354 /* And for the output stream. */
355 cancellable = g_cancellable_new ();
356 stream_source =
357 g_pollable_output_stream_create_source (pollable_output_stream,
358 cancellable);
359
360 check_pollable_source_cancellation (stream_source, cancellable);
361
362 g_object_unref (io_stream);
363 g_source_unref (stream_source);
364 g_object_unref (cancellable);
365 g_object_unref (agent);
366 }
367
368 static void
test_zero_length_reads_writes(NiceAddress * addr)369 test_zero_length_reads_writes (NiceAddress *addr)
370 {
371 NiceAgent *agent;
372 guint stream_id;
373 GIOStream *io_stream;
374 GInputStream *input_stream;
375 GOutputStream *output_stream;
376 GPollableInputStream *pollable_input_stream;
377 GPollableOutputStream *pollable_output_stream;
378 GError *error = NULL;
379 guint8 buf[1]; /* should never be accessed */
380
381 agent = nice_agent_new_reliable (NULL, NICE_COMPATIBILITY_RFC5245);
382 g_object_set (G_OBJECT (agent), "upnp", FALSE, NULL);
383 nice_agent_add_local_address (agent, addr);
384
385 /* Add a stream. */
386 stream_id = nice_agent_add_stream (agent, 1);
387
388 /* Try building an I/O stream around it. */
389 io_stream = nice_agent_get_io_stream (agent, stream_id, 1);
390 g_assert (G_IS_IO_STREAM (io_stream));
391 g_assert (NICE_IS_IO_STREAM (io_stream));
392
393 input_stream = g_io_stream_get_input_stream (G_IO_STREAM (io_stream));
394 output_stream = g_io_stream_get_output_stream (G_IO_STREAM (io_stream));
395 pollable_input_stream = G_POLLABLE_INPUT_STREAM (input_stream);
396 pollable_output_stream = G_POLLABLE_OUTPUT_STREAM (output_stream);
397
398 /* Check zero-length reads and writes complete immediately without error. */
399 g_assert_cmpint (g_input_stream_read (input_stream, buf, 0, NULL, &error), ==, 0);
400 g_assert_no_error (error);
401
402 g_assert_cmpint (g_output_stream_write (output_stream, buf, 0, NULL, &error), ==, 0);
403 g_assert_no_error (error);
404
405 g_assert (
406 g_pollable_input_stream_read_nonblocking (pollable_input_stream,
407 buf, 0, NULL, &error) == 0);
408 g_assert_no_error (error);
409
410 g_assert (
411 g_pollable_output_stream_write_nonblocking (pollable_output_stream,
412 buf, 0, NULL, &error) == 0);
413 g_assert_no_error (error);
414
415 /* Remove the component and check that zero-length reads and writes still
416 * result in a 0 response, rather than any error. */
417 nice_agent_remove_stream (agent, stream_id);
418 g_assert (g_io_stream_is_closed (G_IO_STREAM (io_stream)));
419
420 g_assert_cmpint (g_input_stream_read (input_stream, buf, 0, NULL, &error), ==, 0);
421 g_assert_no_error (error);
422
423 g_assert_cmpint (g_output_stream_write (output_stream, buf, 0, NULL, &error), ==, 0);
424 g_assert_no_error (error);
425
426 g_assert (
427 g_pollable_input_stream_read_nonblocking (pollable_input_stream,
428 buf, 0, NULL, &error) == 0);
429 g_assert_no_error (error);
430
431 g_assert (
432 g_pollable_output_stream_write_nonblocking (pollable_output_stream,
433 buf, 0, NULL, &error) == 0);
434 g_assert_no_error (error);
435
436 g_object_unref (io_stream);
437 g_object_unref (agent);
438 }
439
440 int
main(void)441 main (void)
442 {
443 NiceAddress addr;
444
445 #ifdef G_OS_WIN32
446 WSADATA w;
447 WSAStartup (0x0202, &w);
448 #endif
449 nice_address_init (&addr);
450
451 g_assert (nice_address_set_from_string (&addr, "127.0.0.1"));
452
453 test_invalid_stream (&addr);
454 test_io_stream_properties (&addr);
455 test_pollable_properties (&addr);
456 test_pollable_cancellation (&addr);
457 test_zero_length_reads_writes (&addr);
458
459 #ifdef G_OS_WIN32
460 WSACleanup ();
461 #endif
462
463 return 0;
464 }
465
466