Commit 07a46ede authored by Philip Withnall's avatar Philip Withnall

tests: Fix various memory leaks in the unit tests

The theory being that if we can get check-valgrind to pass, we could
acutally use it to find more memory leaks and other errors in the agent.
parent ec3783f7
...@@ -69,6 +69,8 @@ test_socket_initial_properties (void) ...@@ -69,6 +69,8 @@ test_socket_initial_properties (void)
g_assert_cmpint (sock->addr.s.ip4.sin_addr.s_addr, ==, 0); g_assert_cmpint (sock->addr.s.ip4.sin_addr.s_addr, ==, 0);
// is bound to a particular port // is bound to a particular port
g_assert_cmpuint (nice_address_get_port (&sock->addr), !=, 0); g_assert_cmpuint (nice_address_get_port (&sock->addr), !=, 0);
nice_socket_free (sock);
} }
static void static void
...@@ -84,6 +86,8 @@ test_socket_address_properties (void) ...@@ -84,6 +86,8 @@ test_socket_address_properties (void)
g_assert_cmpuint (nice_address_get_port (&sock->addr), !=, 0); g_assert_cmpuint (nice_address_get_port (&sock->addr), !=, 0);
nice_address_set_port (&tmp, nice_address_get_port (&sock->addr)); nice_address_set_port (&tmp, nice_address_get_port (&sock->addr));
g_assert_cmpuint (nice_address_get_port (&tmp), !=, 0); g_assert_cmpuint (nice_address_get_port (&tmp), !=, 0);
nice_socket_free (sock);
} }
static void static void
...@@ -336,6 +340,25 @@ test_multi_message_recv (guint n_sends, guint n_receives, ...@@ -336,6 +340,25 @@ test_multi_message_recv (guint n_sends, guint n_receives,
} }
g_slice_free1 (expected_recv_buf_len, _expected_recv_buf); g_slice_free1 (expected_recv_buf_len, _expected_recv_buf);
for (i = 0; i < n_receives; i++) {
for (j = 0; j < n_bufs_per_message; j++) {
g_slice_free1 (recv_buf_size,
recv_bufs[i * n_bufs_per_message + j].buffer);
}
}
for (i = 0; i < n_sends; i++) {
for (j = 0; j < n_bufs_per_message; j++) {
g_slice_free1 (send_buf_size,
(gpointer) send_bufs[i * n_bufs_per_message + j].buffer);
}
}
g_free (recv_messages);
g_free (recv_bufs);
g_free (send_messages);
g_free (send_bufs);
} }
nice_socket_free (client); nice_socket_free (client);
......
...@@ -55,6 +55,8 @@ test_invalid_stream (NiceAddress *addr) ...@@ -55,6 +55,8 @@ test_invalid_stream (NiceAddress *addr)
* return G_IO_ERROR_BROKEN_PIPE. */ * return G_IO_ERROR_BROKEN_PIPE. */
io_stream = nice_agent_get_io_stream (agent, 5, 5); io_stream = nice_agent_get_io_stream (agent, 5, 5);
g_assert (io_stream == NULL); g_assert (io_stream == NULL);
g_object_unref (agent);
} }
static void static void
...@@ -351,8 +353,10 @@ test_pollable_cancellation (NiceAddress *addr) ...@@ -351,8 +353,10 @@ test_pollable_cancellation (NiceAddress *addr)
check_pollable_source_cancellation (stream_source, cancellable); check_pollable_source_cancellation (stream_source, cancellable);
g_object_unref (io_stream);
g_source_unref (stream_source); g_source_unref (stream_source);
g_object_unref (cancellable); g_object_unref (cancellable);
g_object_unref (agent);
} }
static void static void
......
...@@ -90,6 +90,7 @@ read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data) ...@@ -90,6 +90,7 @@ read_thread_cb (GInputStream *input_stream, TestIOStreamThreadData *data)
len = g_input_stream_read (input_stream, buf, sizeof (buf), len = g_input_stream_read (input_stream, buf, sizeof (buf),
user_data->cancellable, &error); user_data->cancellable, &error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED); g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
g_error_free (error);
g_assert (len == -1); g_assert (len == -1);
g_main_loop_quit (data->error_loop); g_main_loop_quit (data->error_loop);
...@@ -116,9 +117,13 @@ int main (void) ...@@ -116,9 +117,13 @@ int main (void)
l_data.cancellable = g_cancellable_new (); l_data.cancellable = g_cancellable_new ();
l_data.blocking = FALSE; l_data.blocking = FALSE;
g_cond_init (&l_data.cond);
g_mutex_init (&l_data.mutex);
r_data.cancellable = g_cancellable_new (); r_data.cancellable = g_cancellable_new ();
r_data.blocking = FALSE; r_data.blocking = FALSE;
g_cond_init (&r_data.cond);
g_mutex_init (&r_data.mutex);
l_cancellation_thread = spawn_thread ("libnice L cancel", l_cancellation_thread = spawn_thread ("libnice L cancel",
cancellation_thread_cb, &l_data); cancellation_thread_cb, &l_data);
...@@ -133,6 +138,10 @@ int main (void) ...@@ -133,6 +138,10 @@ int main (void)
/* Free things. */ /* Free things. */
g_object_unref (r_data.cancellable); g_object_unref (r_data.cancellable);
g_object_unref (l_data.cancellable); g_object_unref (l_data.cancellable);
g_cond_clear (&l_data.cond);
g_cond_clear (&r_data.cond);
g_mutex_clear (&l_data.mutex);
g_mutex_clear (&r_data.mutex);
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
WSACleanup (); WSACleanup ();
......
...@@ -49,6 +49,21 @@ GMutex start_mutex; ...@@ -49,6 +49,21 @@ GMutex start_mutex;
GCond start_cond; GCond start_cond;
gboolean started; gboolean started;
/* Waits about 10 seconds for @var to be NULL/FALSE */
#define WAIT_UNTIL_UNSET(var, context) \
if (var) \
{ \
int i; \
\
for (i = 0; i < 13 && (var); i++) \
{ \
g_usleep (1000 * (1 << i)); \
g_main_context_iteration (context, FALSE); \
} \
\
g_assert (!(var)); \
}
static gboolean timer_cb (gpointer pointer) static gboolean timer_cb (gpointer pointer)
{ {
g_debug ("test-thread:%s: %p", G_STRFUNC, pointer); g_debug ("test-thread:%s: %p", G_STRFUNC, pointer);
...@@ -344,6 +359,7 @@ run_io_stream_test (guint deadlock_timeout, gboolean reliable, ...@@ -344,6 +359,7 @@ run_io_stream_test (guint deadlock_timeout, gboolean reliable,
GMutex mutex; GMutex mutex;
GCond cond; GCond cond;
guint start_count = 6; guint start_count = 6;
guint stream_id;
g_mutex_init (&mutex); g_mutex_init (&mutex);
g_cond_init (&cond); g_cond_init (&cond);
...@@ -451,9 +467,26 @@ run_io_stream_test (guint deadlock_timeout, gboolean reliable, ...@@ -451,9 +467,26 @@ run_io_stream_test (guint deadlock_timeout, gboolean reliable,
if (l_data.io_stream != NULL) if (l_data.io_stream != NULL)
g_object_unref (l_data.io_stream); g_object_unref (l_data.io_stream);
stream_id =
GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (r_data.agent), "stream-id"));
if (stream_id != 0)
nice_agent_remove_stream (r_data.agent, stream_id);
stream_id =
GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (l_data.agent), "stream-id"));
if (stream_id != 0)
nice_agent_remove_stream (l_data.agent, stream_id);
g_object_add_weak_pointer (G_OBJECT (r_data.agent),
(gpointer *) &r_data.agent);
g_object_add_weak_pointer (G_OBJECT (l_data.agent),
(gpointer *) &l_data.agent);
g_object_unref (r_data.agent); g_object_unref (r_data.agent);
g_object_unref (l_data.agent); g_object_unref (l_data.agent);
WAIT_UNTIL_UNSET (r_data.agent, r_data.main_context);
WAIT_UNTIL_UNSET (l_data.agent, l_data.main_context);
g_main_loop_unref (r_data.main_loop); g_main_loop_unref (r_data.main_loop);
g_main_loop_unref (l_data.main_loop); g_main_loop_unref (l_data.main_loop);
...@@ -513,6 +546,7 @@ check_for_termination (TestIOStreamThreadData *data, gsize *recv_count, ...@@ -513,6 +546,7 @@ check_for_termination (TestIOStreamThreadData *data, gsize *recv_count,
/* Remove the stream and run away. */ /* Remove the stream and run away. */
nice_agent_remove_stream (data->agent, stream_id); nice_agent_remove_stream (data->agent, stream_id);
g_object_set_data (G_OBJECT (data->agent), "stream-id", GUINT_TO_POINTER (0));
/* If both sides have finished, quit the test main loop. */ /* If both sides have finished, quit the test main loop. */
if (*recv_count > expected_recv_count && if (*recv_count > expected_recv_count &&
......
...@@ -72,6 +72,7 @@ read_stream_cb (GObject *pollable_stream, gpointer _user_data) ...@@ -72,6 +72,7 @@ read_stream_cb (GObject *pollable_stream, gpointer _user_data)
if (len == -1) { if (len == -1) {
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK); g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
g_error_free (error);
return TRUE; return TRUE;
} }
......
...@@ -102,7 +102,9 @@ main (void) ...@@ -102,7 +102,9 @@ main (void)
g_main_loop_run (loop); g_main_loop_run (loop);
nice_agent_remove_stream (agent, stream);
g_object_unref (agent); g_object_unref (agent);
return 0; return 0;
} }
...@@ -89,6 +89,21 @@ static const uint16_t known_attributes[] = { ...@@ -89,6 +89,21 @@ static const uint16_t known_attributes[] = {
0 0
}; };
/* Waits about 10 seconds for @var to be NULL/FALSE */
#define WAIT_UNTIL_UNSET(var, context) \
if (var) \
{ \
int _i; \
\
for (_i = 0; _i < 13 && (var); _i++) \
{ \
g_usleep (1000 * (1 << _i)); \
g_main_context_iteration (context, FALSE); \
} \
\
g_assert (!(var)); \
}
/* /*
* Creates a listening socket * Creates a listening socket
*/ */
...@@ -716,7 +731,6 @@ int main(void) ...@@ -716,7 +731,6 @@ int main(void)
src = g_cancellable_source_new (global_cancellable); src = g_cancellable_source_new (global_cancellable);
g_source_set_dummy_callback (src); g_source_set_dummy_callback (src);
g_source_attach (src, NULL); g_source_attach (src, NULL);
g_source_unref (src);
sock = listen_socket (&stun_port); sock = listen_socket (&stun_port);
...@@ -782,6 +796,9 @@ int main(void) ...@@ -782,6 +796,9 @@ int main(void)
drop_stun_packets = TRUE; drop_stun_packets = TRUE;
send_dummy_data (); send_dummy_data ();
g_object_add_weak_pointer (G_OBJECT (lagent), (gpointer *) &lagent);
g_object_add_weak_pointer (G_OBJECT (ragent), (gpointer *) &ragent);
g_object_unref (lagent); g_object_unref (lagent);
g_object_unref (ragent); g_object_unref (ragent);
...@@ -792,5 +809,11 @@ int main(void) ...@@ -792,5 +809,11 @@ int main(void)
#endif #endif
g_object_unref (global_cancellable); g_object_unref (global_cancellable);
g_source_destroy (src);
g_source_unref (src);
WAIT_UNTIL_UNSET (lagent, NULL);
WAIT_UNTIL_UNSET (ragent, NULL);
return 0; return 0;
} }
...@@ -904,6 +904,7 @@ read_stream_cb (GObject *pollable_stream, gpointer _user_data) ...@@ -904,6 +904,7 @@ read_stream_cb (GObject *pollable_stream, gpointer _user_data)
if (len == -1) { if (len == -1) {
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK); g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
g_error_free (error);
g_free (buf); g_free (buf);
return TRUE; return TRUE;
} }
...@@ -1175,6 +1176,7 @@ main (int argc, char *argv[]) ...@@ -1175,6 +1176,7 @@ main (int argc, char *argv[])
if (!g_option_context_parse (context, &argc, &argv, &error)) { if (!g_option_context_parse (context, &argc, &argv, &error)) {
g_printerr ("Option parsing failed: %s\n", error->message); g_printerr ("Option parsing failed: %s\n", error->message);
g_error_free (error); g_error_free (error);
g_option_context_free (context);
exit (1); exit (1);
} }
...@@ -1307,6 +1309,8 @@ main (int argc, char *argv[]) ...@@ -1307,6 +1309,8 @@ main (int argc, char *argv[])
} }
done: done:
g_option_context_free (context);
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
WSACleanup (); WSACleanup ();
#endif #endif
......
...@@ -54,6 +54,21 @@ volatile gint global_ragent_cands = 0; ...@@ -54,6 +54,21 @@ volatile gint global_ragent_cands = 0;
volatile gint global_lagent_buffers = 0; volatile gint global_lagent_buffers = 0;
volatile gint global_ragent_buffers = 0; volatile gint global_ragent_buffers = 0;
/* Waits about 10 seconds for @var to be NULL/FALSE */
#define WAIT_UNTIL_UNSET(var, context) \
if (var) \
{ \
int _i; \
\
for (_i = 0; _i < 13 && (var); _i++) \
{ \
g_usleep (1000 * (1 << _i)); \
g_main_context_iteration (context, FALSE); \
} \
\
g_assert (!(var)); \
}
static gboolean timer_cb (gpointer pointer) static gboolean timer_cb (gpointer pointer)
{ {
g_debug ("test-thread:%s: %p", G_STRFUNC, pointer); g_debug ("test-thread:%s: %p", G_STRFUNC, pointer);
...@@ -338,9 +353,15 @@ int main (void) ...@@ -338,9 +353,15 @@ int main (void)
g_assert (g_atomic_int_get (&global_lagent_cands) == 1); g_assert (g_atomic_int_get (&global_lagent_cands) == 1);
g_assert (g_atomic_int_get (&global_ragent_cands) == 1); g_assert (g_atomic_int_get (&global_ragent_cands) == 1);
g_object_add_weak_pointer (G_OBJECT (lagent), (gpointer *) &lagent);
g_object_add_weak_pointer (G_OBJECT (ragent), (gpointer *) &ragent);
g_object_unref (lagent); g_object_unref (lagent);
g_object_unref (ragent); g_object_unref (ragent);
WAIT_UNTIL_UNSET (lagent, lmainctx);
WAIT_UNTIL_UNSET (ragent, rmainctx);
g_main_loop_unref (lmainloop); g_main_loop_unref (lmainloop);
g_main_loop_unref (rmainloop); g_main_loop_unref (rmainloop);
g_main_loop_unref (ldmainloop); g_main_loop_unref (ldmainloop);
......
...@@ -43,6 +43,21 @@ ...@@ -43,6 +43,21 @@
#include "agent.h" #include "agent.h"
#include "agent-priv.h" #include "agent-priv.h"
/* Waits about 10 seconds for @var to be NULL/FALSE */
#define WAIT_UNTIL_UNSET(var, context) \
if (var) \
{ \
int _i; \
\
for (_i = 0; _i < 13 && (var); _i++) \
{ \
g_usleep (1000 * (1 << _i)); \
g_main_context_iteration (context, FALSE); \
} \
\
g_assert (!(var)); \
}
gint gint
main (void) main (void)
{ {
...@@ -98,7 +113,12 @@ main (void) ...@@ -98,7 +113,12 @@ main (void)
g_slist_free (candidates); g_slist_free (candidates);
/* clean up */ /* clean up */
nice_agent_remove_stream (agent, stream_id);
g_object_add_weak_pointer (G_OBJECT (agent), (gpointer *) &agent);
g_object_unref (agent); g_object_unref (agent);
WAIT_UNTIL_UNSET (agent, NULL);
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
WSACleanup(); WSACleanup();
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment