Commit 0655aab0 authored by Philip Withnall's avatar Philip Withnall Committed by Olivier Crête

tests: Add tests for the I/O stream classes

parent d285b5a5
......@@ -129,9 +129,15 @@ tests/test
tests/test-add-remove-stream
tests/test-address
tests/test-bsd
tests/test-build-io-stream
tests/test-dribble
tests/test-fallback
tests/test-fullmode
tests/test-io-stream-cancelling
tests/test-io-stream-closing
tests/test-io-stream-thread
tests/test-io-stream-pollable
tests/test-send-recv
tests/test-mainloop
tests/test-priority
tests/test-pseudotcp
......
......@@ -26,6 +26,12 @@ check_PROGRAMS = \
test \
test-address \
test-add-remove-stream \
test-build-io-stream \
test-io-stream-thread \
test-io-stream-closing \
test-io-stream-cancelling \
test-io-stream-pollable \
test-send-recv \
test-priority \
test-mainloop \
test-fullmode \
......@@ -53,6 +59,23 @@ test_address_LDADD = $(COMMON_LDADD)
test_add_remove_stream_LDADD = $(COMMON_LDADD)
test_build_io_stream_LDADD = $(COMMON_LDADD)
test_io_stream_thread_SOURCES = test-io-stream-thread.c test-io-stream-common.c
test_io_stream_thread_LDADD = $(COMMON_LDADD)
test_io_stream_closing_SOURCES = test-io-stream-closing.c test-io-stream-common.c
test_io_stream_closing_LDADD = $(COMMON_LDADD)
test_io_stream_cancelling_SOURCES = test-io-stream-cancelling.c test-io-stream-common.c
test_io_stream_cancelling_LDADD = $(COMMON_LDADD)
test_io_stream_pollable_SOURCES = test-io-stream-pollable.c test-io-stream-common.c
test_io_stream_pollable_LDADD = $(COMMON_LDADD)
test_send_recv_SOURCES = test-send-recv.c test-io-stream-common.c
test_send_recv_LDADD = $(COMMON_LDADD)
test_priority_LDADD = $(COMMON_LDADD)
test_mainloop_LDADD = $(COMMON_LDADD)
......
This diff is collapsed.
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2014 Collabora Ltd.
* Contact: Philip Withnall
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Philip Withnall, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "agent.h"
#include "test-io-stream-common.h"
#include <stdlib.h>
#include <string.h>
#ifndef G_OS_WIN32
#include <unistd.h>
#endif
typedef struct {
GCancellable *cancellable; /* owned */
GCond cond;
GMutex mutex;
gboolean blocking; /* protected by @mutex */
} CancellationData;
static gpointer
cancellation_thread_cb (gpointer user_data)
{
CancellationData *data = user_data;
/* Wait to be signalled from read_thread_cb(). */
g_mutex_lock (&data->mutex);
while (!data->blocking)
g_cond_wait (&data->cond, &data->mutex);
g_mutex_unlock (&data->mutex);
/* Try and ensure we cancel part-way through the read, rather than before the
* read function is called. */
g_usleep (100000);
g_cancellable_cancel (data->cancellable);
return NULL;
}
static void
read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
{
CancellationData *user_data = data->user_data;
GError *error = NULL;
guint8 buf[MESSAGE_SIZE];
gssize len;
/* Block on receiving some data or cancellation. */
g_mutex_lock (&user_data->mutex);
user_data->blocking = TRUE;
g_cond_signal (&user_data->cond);
g_mutex_unlock (&user_data->mutex);
len = g_input_stream_read (input_stream, buf, sizeof (buf),
user_data->cancellable, &error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
g_assert (len == -1);
g_main_loop_quit (data->error_loop);
}
int main (void)
{
GThread *l_cancellation_thread, *r_cancellation_thread;
CancellationData *l_data, *r_data;
const TestIOStreamCallbacks callbacks = {
read_thread_cb,
NULL,
NULL,
NULL,
};
#ifdef G_OS_WIN32
WSADATA w;
WSAStartup (0x0202, &w);
#endif
#if !GLIB_CHECK_VERSION(2, 36, 0)
g_type_init ();
#endif
#if !GLIB_CHECK_VERSION(2, 31, 8)
g_thread_init (NULL);
#endif
l_data = g_malloc0 (sizeof (CancellationData));
l_data->cancellable = g_cancellable_new ();
l_data->blocking = FALSE;
r_data = g_malloc0 (sizeof (CancellationData));
r_data->cancellable = g_cancellable_new ();
r_data->blocking = FALSE;
l_cancellation_thread = spawn_thread ("libnice L cancel",
cancellation_thread_cb, l_data);
r_cancellation_thread = spawn_thread ("libnice R cancel",
cancellation_thread_cb, r_data);
run_io_stream_test (30, TRUE, &callbacks, l_data, NULL, r_data, NULL);
g_thread_join (l_cancellation_thread);
g_thread_join (r_cancellation_thread);
/* Free things. */
g_object_unref (r_data->cancellable);
g_free (r_data);
g_object_unref (l_data->cancellable);
g_free (l_data);
#ifdef G_OS_WIN32
WSACleanup ();
#endif
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2014 Collabora Ltd.
* Contact: Philip Withnall
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Philip Withnall, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "agent.h"
#include "test-io-stream-common.h"
#include <stdlib.h>
#include <string.h>
#ifndef G_OS_WIN32
#include <unistd.h>
#endif
typedef struct {
gint recv_count;
gint *other_recv_count;
} ClosingData;
#define NUM_MESSAGES 10
static void
read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
{
ClosingData *user_data = data->user_data;
GOutputStream *output_stream;
gpointer tmp;
guint stream_id;
GError *error = NULL;
while (user_data->recv_count < NUM_MESSAGES) {
gchar expected_data[MESSAGE_SIZE];
guint8 buf[MESSAGE_SIZE];
gssize len;
gsize offset;
/* Block on receiving some data. */
len = g_input_stream_read (input_stream, buf, sizeof (buf), NULL, &error);
g_assert_no_error (error);
offset = 0;
while (len > 0) {
g_assert (len == MESSAGE_SIZE);
g_assert (user_data->recv_count < NUM_MESSAGES);
memset (expected_data, user_data->recv_count + '1', MESSAGE_SIZE);
g_assert (
memcmp (buf + offset, expected_data, sizeof (expected_data)) == 0);
user_data->recv_count++;
len -= MESSAGE_SIZE;
offset += MESSAGE_SIZE;
}
g_assert (len == 0);
}
/* Signal completion. */
output_stream = g_io_stream_get_output_stream (data->io_stream);
g_output_stream_write (output_stream, "Done", strlen ("Done"), NULL, &error);
g_assert_no_error (error);
/* Wait for a done packet. */
while (TRUE) {
guint8 buf[4];
gssize len;
len = g_input_stream_read (input_stream, buf, sizeof (buf), NULL, &error);
g_assert_no_error (error);
g_assert (len == 4);
g_assert (memcmp (buf, "Done", strlen ("Done")) == 0);
break;
}
user_data->recv_count = -1;
tmp = g_object_get_data (G_OBJECT (data->agent), "stream-id");
stream_id = GPOINTER_TO_UINT (tmp);
nice_agent_remove_stream (data->agent, stream_id);
/* Have both threads finished? */
if (user_data->recv_count == -1 &&
*user_data->other_recv_count == -1) {
g_main_loop_quit (data->error_loop);
}
}
static void
write_thread_cb (GOutputStream *output_stream, TestIOStreamThreadData *data)
{
gchar buf[MESSAGE_SIZE];
guint i;
for (i = 0; i < NUM_MESSAGES; i++) {
GError *error = NULL;
memset (buf, i + '1', MESSAGE_SIZE);
g_output_stream_write (output_stream, buf, sizeof (buf), NULL, &error);
g_assert_no_error (error);
}
}
int main (void)
{
ClosingData *l_data, *r_data;
const TestIOStreamCallbacks callbacks = {
read_thread_cb,
write_thread_cb,
NULL,
NULL,
};
#ifdef G_OS_WIN32
WSADATA w;
WSAStartup (0x0202, &w);
#endif
#if !GLIB_CHECK_VERSION(2, 36, 0)
g_type_init ();
#endif
#if !GLIB_CHECK_VERSION(2, 31, 8)
g_thread_init (NULL);
#endif
l_data = g_malloc0 (sizeof (ClosingData));
r_data = g_malloc0 (sizeof (ClosingData));
l_data->recv_count = 0;
l_data->other_recv_count = &r_data->recv_count;
r_data->recv_count = 0;
r_data->other_recv_count = &l_data->recv_count;
run_io_stream_test (30, TRUE, &callbacks, l_data, NULL, r_data, NULL);
g_free (r_data);
g_free (l_data);
#ifdef G_OS_WIN32
WSACleanup ();
#endif
return 0;
}
This diff is collapsed.
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2014 Collabora Ltd.
* Contact: Philip Withnall
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Philip Withnall, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "agent.h"
#include <stdlib.h>
#include <string.h>
#ifndef G_OS_WIN32
#include <unistd.h>
#endif
/* Make the message sufficiently large to not hit Nagle’s algorithm in the
* pseudo-TCP implementation, and hence run really slowly. */
#define MESSAGE_SIZE 1284 /* bytes */
typedef struct _TestIOStreamThreadData TestIOStreamThreadData;
typedef struct {
void (*read_thread) (GInputStream *input_stream,
TestIOStreamThreadData *data);
void (*write_thread) (GOutputStream *output_stream,
TestIOStreamThreadData *data);
void (*reliable_transport_writable) (GOutputStream *output_stream,
NiceAgent *agent, guint stream_id, guint component_id,
TestIOStreamThreadData *data);
void (*new_selected_pair) (NiceAgent *agent, guint stream_id,
guint component_id, gchar *lfoundation, gchar *rfoundation,
TestIOStreamThreadData *data);
} TestIOStreamCallbacks;
struct _TestIOStreamThreadData {
NiceAgent *agent;
GIOStream *io_stream;
gboolean reliable;
GMainLoop *main_loop;
GMainLoop *error_loop;
GMainContext *main_context;
GMainContext *write_context;
GMainContext *read_context;
gpointer user_data;
GDestroyNotify user_data_free;
/*< private >*/
const TestIOStreamCallbacks *callbacks;
/* Condition signalling for the stream being open/writeable. */
gboolean stream_open;
gboolean stream_ready;
GCond write_cond;
GMutex write_mutex;
};
GThread *spawn_thread (const gchar *thread_name, GThreadFunc thread_func,
gpointer user_data);
void run_io_stream_test (guint deadlock_timeout, gboolean reliable,
const TestIOStreamCallbacks *callbacks,
gpointer l_user_data, GDestroyNotify l_user_data_free,
gpointer r_user_data, GDestroyNotify r_user_data_free);
void check_for_termination (TestIOStreamThreadData *data, gsize *recv_count,
gsize *other_recv_count, gsize *send_count, gsize expected_recv_count);
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2014 Collabora Ltd.
* Contact: Philip Withnall
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Philip Withnall, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "agent.h"
#include "test-io-stream-common.h"
#include <stdlib.h>
#include <string.h>
#ifndef G_OS_WIN32
#include <unistd.h>
#endif
typedef struct {
GMainLoop *read_loop; /* unowned */
gsize recv_count;
gsize *other_recv_count;
gsize send_count;
gsize *other_send_count;
} ThreadData;
static gboolean
read_stream_cb (GObject *pollable_stream, gpointer _user_data)
{
TestIOStreamThreadData *data = _user_data;
ThreadData *user_data = data->user_data;
gchar expected_data[MESSAGE_SIZE];
GError *error = NULL;
guint8 buf[MESSAGE_SIZE];
gssize len;
/* Try to receive some data. */
len = g_pollable_input_stream_read_nonblocking (
G_POLLABLE_INPUT_STREAM (pollable_stream), buf, sizeof (buf), NULL,
&error);
if (len == -1) {
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
return TRUE;
}
g_assert_cmpint (len, ==, MESSAGE_SIZE);
memset (expected_data, user_data->recv_count + '1', sizeof (expected_data));
g_assert (memcmp (buf, expected_data, sizeof (expected_data)) == 0);
user_data->recv_count++;
if (user_data->recv_count == 10) {
g_main_loop_quit (user_data->read_loop);
return FALSE;
}
return TRUE;
}
static void
read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
{
GMainContext *main_context;
GMainLoop *main_loop;
GSource *stream_source;
ThreadData *user_data = data->user_data;
main_context = g_main_context_new ();
main_loop = g_main_loop_new (main_context, FALSE);
g_main_context_push_thread_default (main_context);
stream_source =
g_pollable_input_stream_create_source (
G_POLLABLE_INPUT_STREAM (input_stream), NULL);
g_source_set_callback (stream_source, (GSourceFunc) read_stream_cb,
data, NULL);
g_source_attach (stream_source, main_context);
g_source_unref (stream_source);
/* Run the main loop. */
user_data->read_loop = main_loop;
g_main_loop_run (main_loop);
g_main_context_pop_thread_default (main_context);
g_main_loop_unref (main_loop);
g_main_context_unref (main_context);
check_for_termination (data, &user_data->recv_count,
user_data->other_recv_count, &user_data->send_count, 10);
}
static void
write_thread_cb (GOutputStream *output_stream, TestIOStreamThreadData *data)
{
ThreadData *user_data = data->user_data;
guint8 buf[MESSAGE_SIZE];
for (user_data->send_count = 0;
user_data->send_count < 10;
user_data->send_count++) {
GError *error = NULL;
memset (buf, user_data->send_count + '1', MESSAGE_SIZE);
g_pollable_output_stream_write_nonblocking (
G_POLLABLE_OUTPUT_STREAM (output_stream), buf, sizeof (buf), NULL,
&error);
g_assert_no_error (error);
}
}
int main (void)
{
ThreadData *l_data, *r_data;
const TestIOStreamCallbacks callbacks = {
read_thread_cb,
write_thread_cb,
NULL,
NULL,
};
#ifdef G_OS_WIN32
WSADATA w;
WSAStartup (0x0202, &w);
#endif
#if !GLIB_CHECK_VERSION(2, 36, 0)
g_type_init ();
#endif
#if !GLIB_CHECK_VERSION(2,31,8)
g_thread_init (NULL);
#endif
l_data = g_malloc0 (sizeof (ThreadData));
r_data = g_malloc0 (sizeof (ThreadData));
l_data->recv_count = 0;
l_data->send_count = 0;
l_data->other_recv_count = &r_data->recv_count;
l_data->other_send_count = &r_data->send_count;
r_data->recv_count = 0;
r_data->send_count = 0;
r_data->other_recv_count = &l_data->recv_count;
r_data->other_send_count = &l_data->send_count;
run_io_stream_test (30, TRUE, &callbacks, l_data, NULL, r_data, NULL);
g_free (r_data);
g_free (l_data);
#ifdef G_OS_WIN32
WSACleanup ();
#endif
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2013 Collabora Ltd.
* Contact: Philip Withnall
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Philip Withnall, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "agent.h"
#include "test-io-stream-common.h"
#include <stdlib.h>
#include <string.h>
#ifndef G_OS_WIN32
#include <unistd.h>
#endif
typedef struct {
guint cand_count;
guint *other_cand_count;
gsize recv_count;
gsize *other_recv_count;
gsize send_count;
gsize *other_send_count;
} ThreadData;
static void
read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
{
ThreadData *user_data = data->user_data;
for (user_data->recv_count = 0;
user_data->recv_count < 10;
user_data->recv_count++) {
guint8 expected_data[MESSAGE_SIZE];
GError *error = NULL;
guint8 buf[MESSAGE_SIZE];
gssize len;