2 * This file is part of the Nice GLib ICE library.
4 * (C) 2013 Collabora Ltd.
5 * Contact: Philip Withnall
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/
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
17 * The Original Code is the Nice GLib ICE library.
19 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20 * Corporation. All Rights Reserved.
23 * Philip Withnall, Collabora Ltd.
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.
45 #include "test-io-stream-common.h"
48 test_invalid_stream (NiceAddress *addr)
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);
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);
62 g_object_unref (agent);
66 test_io_stream_properties (NiceAddress *addr)
71 GInputStream *input_stream;
72 GOutputStream *output_stream;
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);
78 stream_id = nice_agent_add_stream (agent, 1);
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));
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)));
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));
94 g_assert (!g_input_stream_is_closed (input_stream));
95 g_assert (!g_input_stream_has_pending (input_stream));
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));
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));
106 /* Remove the component and check that the I/O streams close. */
107 nice_agent_remove_stream (agent, stream_id);
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));
113 g_object_unref (io_stream);
114 g_object_unref (agent);
118 test_pollable_properties (NiceAddress *addr)
122 GIOStream *io_stream;
123 GInputStream *input_stream;
124 GOutputStream *output_stream;
125 GPollableInputStream *pollable_input_stream;
126 GPollableOutputStream *pollable_output_stream;
128 GError *error = NULL;
129 GSource *stream_source;
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);
136 stream_id = nice_agent_add_stream (agent, 1);
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));
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);
148 g_assert (g_pollable_input_stream_can_poll (pollable_input_stream));
149 g_assert (!g_pollable_input_stream_is_readable (pollable_input_stream));
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);
158 g_pollable_input_stream_create_source (pollable_input_stream, NULL);
159 g_assert (stream_source != NULL);
160 g_source_unref (stream_source);
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);
167 g_assert (g_pollable_output_stream_can_poll (pollable_output_stream));
168 g_assert (!g_pollable_output_stream_is_writable (pollable_output_stream));
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);
177 g_pollable_output_stream_create_source (pollable_output_stream, NULL);
178 g_assert (stream_source != NULL);
179 g_source_unref (stream_source);
181 /* Remove the component and check that the I/O streams close. */
182 nice_agent_remove_stream (agent, stream_id);
184 g_assert (!g_pollable_input_stream_is_readable (pollable_input_stream));
185 g_assert (!g_pollable_output_stream_is_writable (pollable_output_stream));
188 g_pollable_input_stream_read_nonblocking (pollable_input_stream,
189 buf, sizeof (buf), NULL, &error) == 0);
190 g_assert_no_error (error);
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);
198 g_object_unref (io_stream);
199 g_object_unref (agent);
203 source_cancel_cb (gpointer user_data)
205 GCancellable *cancellable = user_data;
207 g_cancellable_cancel (cancellable);
213 source_cancelled_cb (GObject *pollable_stream, gpointer user_data)
215 GMainLoop *main_loop = user_data;
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)) {
221 !g_pollable_input_stream_is_readable (
222 G_POLLABLE_INPUT_STREAM (pollable_stream)));
225 !g_pollable_output_stream_is_writable (
226 G_POLLABLE_OUTPUT_STREAM (pollable_stream)));
229 g_main_loop_quit (main_loop);
235 source_timeout_cb (gpointer user_data)
237 g_error ("check_pollable_source_cancellation() took too long. Aborting.");
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
248 check_pollable_source_cancellation (GSource *pollable_source,
249 GCancellable *cancellable)
251 GMainContext *main_context;
252 GMainLoop *main_loop;
253 GSource *idle_source, *timeout_source;
255 main_context = g_main_context_new ();
256 main_loop = g_main_loop_new (main_context, FALSE);
258 /* Set up the pollable source. */
259 g_source_set_callback (pollable_source, G_SOURCE_FUNC (source_cancelled_cb),
261 g_source_attach (pollable_source, main_context);
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,
267 g_source_attach (idle_source, main_context);
268 g_source_unref (idle_source);
271 timeout_source = g_timeout_source_new (30000);
272 g_source_set_callback (timeout_source, (GSourceFunc) source_timeout_cb,
274 g_source_attach (timeout_source, main_context);
275 g_source_unref (timeout_source);
277 /* Run the main loop and expect to quit it immediately as the pollable source
279 g_main_loop_run (main_loop);
281 g_assert (g_cancellable_is_cancelled (cancellable));
283 g_main_loop_unref (main_loop);
284 g_main_context_unref (main_context);
288 test_pollable_cancellation (NiceAddress *addr)
292 GIOStream *io_stream;
293 GInputStream *input_stream;
294 GOutputStream *output_stream;
295 GPollableInputStream *pollable_input_stream;
296 GPollableOutputStream *pollable_output_stream;
298 GError *error = NULL;
299 GSource *stream_source;
300 GCancellable *cancellable;
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);
307 stream_id = nice_agent_add_stream (agent, 1);
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));
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);
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);
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);
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);
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);
340 g_object_unref (cancellable);
342 /* Check the GSources invoke a callback when run with the cancellable
344 cancellable = g_cancellable_new ();
346 g_pollable_input_stream_create_source (pollable_input_stream,
349 check_pollable_source_cancellation (stream_source, cancellable);
351 g_source_unref (stream_source);
352 g_object_unref (cancellable);
354 /* And for the output stream. */
355 cancellable = g_cancellable_new ();
357 g_pollable_output_stream_create_source (pollable_output_stream,
360 check_pollable_source_cancellation (stream_source, cancellable);
362 g_object_unref (io_stream);
363 g_source_unref (stream_source);
364 g_object_unref (cancellable);
365 g_object_unref (agent);
369 test_zero_length_reads_writes (NiceAddress *addr)
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 */
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);
386 stream_id = nice_agent_add_stream (agent, 1);
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));
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);
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);
402 g_assert_cmpint (g_output_stream_write (output_stream, buf, 0, NULL, &error), ==, 0);
403 g_assert_no_error (error);
406 g_pollable_input_stream_read_nonblocking (pollable_input_stream,
407 buf, 0, NULL, &error) == 0);
408 g_assert_no_error (error);
411 g_pollable_output_stream_write_nonblocking (pollable_output_stream,
412 buf, 0, NULL, &error) == 0);
413 g_assert_no_error (error);
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)));
420 g_assert_cmpint (g_input_stream_read (input_stream, buf, 0, NULL, &error), ==, 0);
421 g_assert_no_error (error);
423 g_assert_cmpint (g_output_stream_write (output_stream, buf, 0, NULL, &error), ==, 0);
424 g_assert_no_error (error);
427 g_pollable_input_stream_read_nonblocking (pollable_input_stream,
428 buf, 0, NULL, &error) == 0);
429 g_assert_no_error (error);
432 g_pollable_output_stream_write_nonblocking (pollable_output_stream,
433 buf, 0, NULL, &error) == 0);
434 g_assert_no_error (error);
436 g_object_unref (io_stream);
437 g_object_unref (agent);
447 WSAStartup (0x0202, &w);
449 nice_address_init (&addr);
451 g_assert (nice_address_set_from_string (&addr, "127.0.0.1"));
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);