1 /* GStreamer unit tests for the srtp elements
2 * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
3 * Copyright (C) 2008 Sebastian Dröge <slomo@circular-chaos.org>
4 * Copyright (C) 2016 Collabora Ltd <vincent.penquerch@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
27 # include <valgrind/valgrind.h>
30 #include <gst/check/gstcheck.h>
32 #include <gst/check/gstharness.h>
34 GST_START_TEST (test_create_and_unref)
38 e = gst_element_factory_make ("dtlsenc", NULL);
39 fail_unless (e != NULL);
40 gst_element_set_state (e, GST_STATE_NULL);
43 e = gst_element_factory_make ("dtlsdec", NULL);
44 fail_unless (e != NULL);
45 gst_element_set_state (e, GST_STATE_NULL);
51 static GMutex key_lock;
52 static GCond key_cond;
56 _on_key_received (GstElement * element, gpointer user_data)
58 g_mutex_lock (&key_lock);
60 g_cond_broadcast (&key_cond);
61 g_mutex_unlock (&key_lock);
65 _wait_for_key_count_to_reach (int n)
67 g_mutex_lock (&key_lock);
69 g_cond_wait (&key_cond, &key_lock);
70 g_mutex_unlock (&key_lock);
73 static gchar data[] = {
74 0x00, 0x01, 0x02, 0x03,
77 GST_START_TEST (test_data_transfer)
79 GstHarness *server, *client;
80 GstElement *s_enc, *s_dec, *c_enc, *c_dec, *s_bin, *c_bin;
81 GstPad *target, *ghost;
82 GstBuffer *buffer, *buf2;
84 /* setup a server and client for dtls negotiation */
85 s_bin = gst_bin_new (NULL);
86 c_bin = gst_bin_new (NULL);
88 /* XXX: the element set states are needed to avoid a runtime warning:
90 * 'gst_dtls_connection_process: runtime check failed: (!priv->bio_buffer)'
92 * where the encoder needs to be started (and SSL initialized) before the
93 * associated decoder receives any data and calls gst_dtls_connection_process().
95 s_dec = gst_element_factory_make ("dtlsdec", "server_dec");
96 g_object_set (s_dec, "connection-id", "server", NULL);
97 g_signal_connect (s_dec, "on-key-received", G_CALLBACK (_on_key_received),
99 gst_element_set_state (s_dec, GST_STATE_PAUSED);
100 gst_bin_add (GST_BIN (s_bin), s_dec);
102 s_enc = gst_element_factory_make ("dtlsenc", "server_enc");
103 g_object_set (s_enc, "connection-id", "server", NULL);
104 g_signal_connect (s_enc, "on-key-received", G_CALLBACK (_on_key_received),
106 gst_element_set_state (s_enc, GST_STATE_PAUSED);
107 gst_bin_add (GST_BIN (c_bin), s_enc);
109 c_dec = gst_element_factory_make ("dtlsdec", "client_dec");
110 g_object_set (c_dec, "connection-id", "client", NULL);
111 g_signal_connect (c_dec, "on-key-received", G_CALLBACK (_on_key_received),
113 gst_element_set_state (c_dec, GST_STATE_PAUSED);
114 gst_bin_add (GST_BIN (c_bin), c_dec);
116 c_enc = gst_element_factory_make ("dtlsenc", "client_enc");
117 g_object_set (c_enc, "connection-id", "client", "is-client", TRUE, NULL);
118 g_signal_connect (c_enc, "on-key-received", G_CALLBACK (_on_key_received),
120 gst_element_set_state (c_enc, GST_STATE_PAUSED);
121 gst_bin_add (GST_BIN (s_bin), c_enc);
123 gst_element_link_pads (s_enc, "src", c_dec, "sink");
124 gst_element_link_pads (c_enc, "src", s_dec, "sink");
126 target = gst_element_get_request_pad (c_dec, "src");
127 ghost = gst_ghost_pad_new ("src", target);
128 gst_element_add_pad (s_bin, ghost);
129 gst_object_unref (target);
131 target = gst_element_get_request_pad (s_enc, "sink");
132 ghost = gst_ghost_pad_new ("sink", target);
133 gst_element_add_pad (s_bin, ghost);
134 gst_object_unref (target);
136 target = gst_element_get_request_pad (s_dec, "src");
137 ghost = gst_ghost_pad_new ("src", target);
138 gst_element_add_pad (c_bin, ghost);
139 gst_object_unref (target);
141 target = gst_element_get_request_pad (c_enc, "sink");
142 ghost = gst_ghost_pad_new ("sink", target);
143 gst_element_add_pad (c_bin, ghost);
144 gst_object_unref (target);
146 server = gst_harness_new_with_element (s_bin, "sink", "src");
147 client = gst_harness_new_with_element (c_bin, "sink", "src");
149 gst_harness_set_src_caps_str (server, "application/data");
150 gst_harness_set_src_caps_str (client, "application/data");
152 _wait_for_key_count_to_reach (4);
154 buffer = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, data,
155 G_N_ELEMENTS (data), 0, G_N_ELEMENTS (data), NULL, NULL);
156 gst_harness_push (server, gst_buffer_ref (buffer));
157 buf2 = gst_harness_pull (server);
158 fail_unless_equals_int (0, gst_buffer_memcmp (buf2, 0, data,
159 G_N_ELEMENTS (data)));
160 gst_buffer_unref (buf2);
162 gst_harness_play (client);
163 gst_harness_push (client, gst_buffer_ref (buffer));
164 buf2 = gst_harness_pull (client);
165 fail_unless_equals_int (0, gst_buffer_memcmp (buf2, 0, data,
166 G_N_ELEMENTS (data)));
167 gst_buffer_unref (buf2);
169 gst_object_unref (s_bin);
170 gst_object_unref (c_bin);
172 gst_buffer_unref (buffer);
173 gst_harness_teardown (server);
174 gst_harness_teardown (client);
182 Suite *s = suite_create ("dtls");
183 TCase *tc_chain = tcase_create ("general");
185 suite_add_tcase (s, tc_chain);
186 tcase_add_test (tc_chain, test_create_and_unref);
187 tcase_add_test (tc_chain, test_data_transfer);
192 GST_CHECK_MAIN (dtls);