diff options
author | Tim-Philipp Müller <tim@centricular.com> | 2014-09-09 18:31:32 +0400 |
---|---|---|
committer | Tim-Philipp Müller <tim@centricular.com> | 2014-09-09 20:42:02 +0400 |
commit | 7b1774513ef80d05336134812170a5ada576dacb (patch) | |
tree | bab299cbc0121facd6e6b2f581890f1cc1085e34 /tests/check/elements | |
parent | e6f77948acb5fdd5bac673a4ad669ed4360a6952 (diff) |
tests: udpsrc: split out socket setup
Diffstat (limited to 'tests/check/elements')
-rw-r--r-- | tests/check/elements/udpsrc.c | 128 |
1 files changed, 70 insertions, 58 deletions
diff --git a/tests/check/elements/udpsrc.c b/tests/check/elements/udpsrc.c index 1a0ab3cae..944ec8a40 100644 --- a/tests/check/elements/udpsrc.c +++ b/tests/check/elements/udpsrc.c @@ -26,85 +26,97 @@ static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_ALWAYS, GST_STATIC_CAPS_ANY); -GST_START_TEST (test_udpsrc_empty_packet) +static gboolean +udpsrc_setup (GstElement ** udpsrc, GSocket ** socket, + GstPad ** sinkpad, GSocketAddress ** sa) { - GstElement *udpsrc; - GSocket *socket; - GstPad *sinkpad; + GInetAddress *ia; int port = 0; + gchar *s; - udpsrc = gst_check_setup_element ("udpsrc"); - fail_unless (udpsrc != NULL); - g_object_set (udpsrc, "port", 0, NULL); + *udpsrc = gst_check_setup_element ("udpsrc"); + fail_unless (*udpsrc != NULL); + g_object_set (*udpsrc, "port", 0, NULL); - sinkpad = gst_check_setup_sink_pad_by_name (udpsrc, &sinktemplate, "src"); - fail_unless (sinkpad != NULL); - gst_pad_set_active (sinkpad, TRUE); + *sinkpad = gst_check_setup_sink_pad_by_name (*udpsrc, &sinktemplate, "src"); + fail_unless (*sinkpad != NULL); + gst_pad_set_active (*sinkpad, TRUE); - gst_element_set_state (udpsrc, GST_STATE_PLAYING); - g_object_get (udpsrc, "port", &port, NULL); + gst_element_set_state (*udpsrc, GST_STATE_PLAYING); + g_object_get (*udpsrc, "port", &port, NULL); GST_INFO ("udpsrc port = %d", port); - socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, + *socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, NULL); - if (socket != NULL) { - GSocketAddress *sa; - GInetAddress *ia; - gchar *s; - - ia = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4); - s = g_inet_address_to_string (ia); - GST_LOG ("inet address %s", s); - g_free (s); - sa = g_inet_socket_address_new (ia, port); - - if (g_socket_send_to (socket, sa, "HeLL0", 0, NULL, NULL) == 0) { - GST_INFO ("sent 0 bytes"); - if (g_socket_send_to (socket, sa, "HeLL0", 6, NULL, NULL) == 6) { - GstMapInfo map; - GstBuffer *buf; - guint len; - - GST_INFO ("sent 6 bytes"); - - g_usleep (G_USEC_PER_SEC / 2); - - len = g_list_length (buffers); - GST_INFO ("%u buffers", len); - fail_unless (len == 1 || len == 2); - - /* last buffer should be our HeLL0 string */ - buf = GST_BUFFER (g_list_nth_data (buffers, len - 1)); - gst_buffer_map (buf, &map, GST_MAP_READ); - fail_unless_equals_int (map.size, 6); - fail_unless_equals_string ((gchar *) map.data, "HeLL0"); - gst_buffer_unmap (buf, &map); - - /* if there's another buffer, it should be 0 bytes */ - if (len == 2) { - buf = GST_BUFFER (g_list_nth_data (buffers, 0)); - fail_unless_equals_int (gst_buffer_get_size (buf), 0); - } - } else { - GST_WARNING ("send_to(6 bytes) failed"); + if (*socket == NULL) { + GST_WARNING ("Could not create IPv4 UDP socket for unit test"); + return FALSE; + } + + ia = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4); + s = g_inet_address_to_string (ia); + GST_LOG ("inet address %s", s); + g_free (s); + *sa = g_inet_socket_address_new (ia, port); + g_object_unref (ia); + + return TRUE; +} + +GST_START_TEST (test_udpsrc_empty_packet) +{ + GSocketAddress *sa = NULL; + GstElement *udpsrc = NULL; + GSocket *socket = NULL; + GstPad *sinkpad = NULL; + + if (!udpsrc_setup (&udpsrc, &socket, &sinkpad, &sa)) + goto no_socket; + + if (g_socket_send_to (socket, sa, "HeLL0", 0, NULL, NULL) == 0) { + GST_INFO ("sent 0 bytes"); + if (g_socket_send_to (socket, sa, "HeLL0", 6, NULL, NULL) == 6) { + GstMapInfo map; + GstBuffer *buf; + guint len; + + GST_INFO ("sent 6 bytes"); + + g_usleep (G_USEC_PER_SEC / 2); + + len = g_list_length (buffers); + GST_INFO ("%u buffers", len); + fail_unless (len == 1 || len == 2); + + /* last buffer should be our HeLL0 string */ + buf = GST_BUFFER (g_list_nth_data (buffers, len - 1)); + gst_buffer_map (buf, &map, GST_MAP_READ); + fail_unless_equals_int (map.size, 6); + fail_unless_equals_string ((gchar *) map.data, "HeLL0"); + gst_buffer_unmap (buf, &map); + + /* if there's another buffer, it should be 0 bytes */ + if (len == 2) { + buf = GST_BUFFER (g_list_nth_data (buffers, 0)); + fail_unless_equals_int (gst_buffer_get_size (buf), 0); } } else { - GST_WARNING ("send_to(0 bytes) failed"); + GST_WARNING ("send_to(6 bytes) failed"); } - - g_object_unref (sa); - g_object_unref (ia); } else { - GST_WARNING ("Could not create IPv4 UDP socket for unit test"); + GST_WARNING ("send_to(0 bytes) failed"); } +no_socket: + gst_element_set_state (udpsrc, GST_STATE_NULL); gst_check_teardown_pad_by_name (udpsrc, "src"); gst_check_teardown_element (udpsrc); g_object_unref (socket); + g_object_unref (sa); } GST_END_TEST; |