diff options
author | Dafydd Harries <dafydd.harries@collabora.co.uk> | 2007-02-13 19:30:00 +0000 |
---|---|---|
committer | Dafydd Harries <dafydd.harries@collabora.co.uk> | 2007-02-13 19:30:00 +0000 |
commit | da51ef41807cdf9a0d48b2796a6ae1167e05bb91 (patch) | |
tree | 87bd6ba7b9c7a7430da4ce4460c863c270b30486 /gst | |
parent | c3d5265f428fe26497c216c6ceb755f3b8b100ad (diff) | |
download | libnice-da51ef41807cdf9a0d48b2796a6ae1167e05bb91.tar.gz |
add test for GStreamer code
darcs-hash:20070213193023-c9803-249e9c2c5c8b0c677a22f5db633d04606c6c6f93.gz
Diffstat (limited to 'gst')
-rw-r--r-- | gst/Makefile.am | 13 | ||||
-rw-r--r-- | gst/test.c | 204 |
2 files changed, 216 insertions, 1 deletions
diff --git a/gst/Makefile.am b/gst/Makefile.am index 2841c6a..fd29c17 100644 --- a/gst/Makefile.am +++ b/gst/Makefile.am @@ -5,7 +5,8 @@ AM_CFLAGS = -Wall -Werror \ -I $(top_srcdir)/udp \ -I $(top_srcdir)/agent \ -I $(top_srcdir)/random \ - -I $(top_srcdir)/address + -I $(top_srcdir)/address \ + -I $(top_srcdir)/stun COMMON_LDADD = \ $(GST_LIBS) \ @@ -29,6 +30,16 @@ libgstnice_la_LDFLAGS = -module -avoid-version # programs +check_PROGRAMS = \ + test + +test_LDADD = \ + $(COMMON_LDADD) \ + ../stun/libstun.la \ + libgstnice.la + +TESTS = $(check_PROGRAMS) + noinst_PROGRAMS = \ jingle-gst-test-server diff --git a/gst/test.c b/gst/test.c new file mode 100644 index 0000000..a82ba28 --- /dev/null +++ b/gst/test.c @@ -0,0 +1,204 @@ + +#include <string.h> + +#include <gst/gst.h> + +#include <nice/nice.h> + +#include "stun.h" + +#include "gstnice.h" + +static GMainLoop *loop = NULL; + +/* XXX: code duplicated from agent/test-send.c */ +static void +send_connectivity_check ( + NiceAgent *agent, + NiceAddress *remote_addr) +{ + NiceUDPSocket *sock; + NiceCandidate *local; + NiceCandidate *remote; + gchar *username; + + g_assert (agent->local_candidates); + g_assert (agent->local_candidates->data); + local = agent->local_candidates->data; + g_assert (local->id == 1); + + g_assert (agent->remote_candidates); + g_assert (agent->remote_candidates->data); + remote = agent->remote_candidates->data; + + sock = &local->sock; + + username = g_strconcat (local->username, remote->username, NULL); + + { + StunMessage *msg; + gchar *packed; + guint len; + + msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 1); + msg->attributes[0] = stun_attribute_username_new (username); + len = stun_message_pack (msg, &packed); + nice_udp_fake_socket_push_recv (sock, remote_addr, len, packed); + g_free (packed); + stun_message_free (msg); + } + + nice_agent_poll_read (agent, NULL, NULL, NULL); + + { + StunMessage *msg; + NiceAddress addr = {0,}; + gchar packed[1024]; + gchar *dump; + guint len; + + len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, packed); + g_assert (nice_address_equal (&addr, remote_addr)); + msg = stun_message_unpack (len, packed); + dump = stun_message_dump (msg); + g_assert (0 == strcmp (dump, + "BINDING-RESPONSE 00000000:00000000:00000000:00000000\n" + " MAPPED-ADDRESS 192.168.0.2:2345\n" + " USERNAME \"S9PObXR5username\"\n")); + g_free (dump); + stun_message_free (msg); + } + + { + StunMessage *msg; + NiceAddress addr = {0,}; + gchar packed[1024]; + gchar *dump; + guint len; + + len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, packed); + g_assert (nice_address_equal (&addr, remote_addr)); + msg = stun_message_unpack (len, packed); + dump = stun_message_dump (msg); + g_assert (0 == strcmp (dump, + "BINDING-REQUEST ac2f75c0:43fbc367:09d315f2:245746d8\n" + " USERNAME \"usernameS9PObXR5\"\n")); + g_free (dump); + stun_message_free (msg); + } + + g_free (username); +} + +static gboolean +recv_cb ( + GIOChannel *source, + GIOCondition condition, + gpointer data) +{ + /* return value is whether to keep the source */ + + NiceAgent *agent = data; + NiceCandidate *candidate = agent->local_candidates->data; + NiceUDPSocket *sock = &candidate->sock; + NiceAddress from; + guint len; + gchar buf[1024]; + + len = nice_udp_fake_socket_pop_send ( + sock, &from, 1024, buf); + + g_assert (len == 6); + g_assert (0 == strncmp (buf, "\x80hello", len)); + + g_main_loop_quit (loop); + return FALSE; +} + +int +main (gint argc, gchar *argv[]) +{ + GstElement *src; + GstElement *sink; + GstElement *pipeline; + NiceAgent *agent; + NiceAddress addr = {0,}; + NiceUDPSocketFactory factory; + + gst_init (&argc, &argv); + + nice_rng_set_new_func (nice_rng_glib_new_predictable); + + // set up agent + + nice_address_set_ipv4 (&addr, 0x7f000001); + nice_udp_fake_socket_factory_init (&factory); + agent = nice_agent_new (&factory); + nice_agent_add_local_address (agent, &addr); + nice_address_set_ipv4 (&addr, 0xc0a80002); + addr.port = 2345; + nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST, + &addr, "username", "password"); + nice_agent_add_stream (agent, 1); + + // send connectivity check so that sending works + + send_connectivity_check (agent, &addr); + + { + GIOChannel *io; + GSource *source; + NiceCandidate *candidate; + NiceUDPSocket *sock; + + candidate = agent->local_candidates->data; + sock = &candidate->sock; + + // send test packet + + nice_udp_fake_socket_push_recv (sock, &addr, 6, "\x80hello"); + + // watch socket for reveived data + + io = g_io_channel_unix_new (nice_udp_fake_socket_get_peer_fd (sock)); + source = g_io_create_watch (io, G_IO_IN); + g_source_set_callback (source, (GSourceFunc) recv_cb, + agent, NULL); + g_source_attach (source, NULL); + } + + // set up pipeline + + src = g_object_new (GST_TYPE_NICE_SRC, + "agent", agent, + "stream", 1, + "component", 1, + NULL); + + sink = g_object_new (GST_TYPE_NICE_SINK, + "agent", agent, + "stream", 1, + "component", 1, + NULL); + + pipeline = gst_pipeline_new (NULL); + gst_bin_add (GST_BIN (pipeline), src); + gst_bin_add (GST_BIN (pipeline), sink); + g_assert (gst_element_link (src, sink)); + gst_element_set_state (pipeline, GST_STATE_PLAYING); + + // loop + + loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (loop); + g_main_loop_unref (loop); + + // clean up + + gst_object_unref (pipeline); + g_object_unref (agent); + nice_udp_socket_factory_close (&factory); + gst_deinit (); + return 0; +} + |