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>
5 *
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.
10 *
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.
15 *
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.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #ifdef HAVE_VALGRIND
27 # include <valgrind/valgrind.h>
28 #endif
29
30 #include <gst/check/gstcheck.h>
31
32 #include <gst/check/gstharness.h>
33
GST_START_TEST(test_create_and_unref)34 GST_START_TEST (test_create_and_unref)
35 {
36 GstElement *e;
37
38 e = gst_element_factory_make ("dtlsenc", NULL);
39 fail_unless (e != NULL);
40 gst_element_set_state (e, GST_STATE_NULL);
41 gst_object_unref (e);
42
43 e = gst_element_factory_make ("dtlsdec", NULL);
44 fail_unless (e != NULL);
45 gst_element_set_state (e, GST_STATE_NULL);
46 gst_object_unref (e);
47 }
48
49 GST_END_TEST;
50
51 static GMutex key_lock;
52 static GCond key_cond;
53 static int key_count;
54 static gboolean errored;
55
56 static GstBusSyncReply
bus_msg_handler(GstBus * bus,GstMessage * message,gpointer user_data)57 bus_msg_handler (GstBus * bus, GstMessage * message, gpointer user_data)
58 {
59 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
60 g_mutex_lock (&key_lock);
61 errored = TRUE;
62 g_cond_broadcast (&key_cond);
63 g_mutex_unlock (&key_lock);
64 }
65
66 return GST_BUS_PASS;
67 }
68
69 static void
_on_key_received(GstElement * element,gpointer user_data)70 _on_key_received (GstElement * element, gpointer user_data)
71 {
72 g_mutex_lock (&key_lock);
73 key_count++;
74 g_cond_broadcast (&key_cond);
75 g_mutex_unlock (&key_lock);
76 }
77
78 static void
_wait_for_key_count_to_reach(int n)79 _wait_for_key_count_to_reach (int n)
80 {
81 g_mutex_lock (&key_lock);
82 while (key_count < n) {
83 g_cond_wait (&key_cond, &key_lock);
84
85 /* Check if any errors were posted */
86 if (errored) {
87 g_mutex_unlock (&key_lock);
88 fail ("DTLS element posted an error");
89 g_mutex_lock (&key_lock);
90 }
91 }
92 g_mutex_unlock (&key_lock);
93 }
94
95 static gchar data[] = {
96 0x00, 0x01, 0x02, 0x03,
97 };
98
GST_START_TEST(test_data_transfer)99 GST_START_TEST (test_data_transfer)
100 {
101 GstHarness *server, *client;
102 GstElement *s_enc, *s_dec, *c_enc, *c_dec, *s_bin, *c_bin;
103 GstPad *target, *ghost;
104 GstBuffer *buffer, *buf2;
105 GstBus *bus;
106
107 /* setup a server and client for dtls negotiation */
108 s_bin = gst_bin_new (NULL);
109 c_bin = gst_bin_new (NULL);
110
111 /* XXX: the element set states are needed to avoid a runtime warning:
112 *
113 * 'gst_dtls_connection_process: runtime check failed: (!priv->bio_buffer)'
114 *
115 * where the encoder needs to be started (and SSL initialized) before the
116 * associated decoder receives any data and calls gst_dtls_connection_process().
117 */
118 s_dec = gst_element_factory_make ("dtlsdec", "server_dec");
119 g_object_set (s_dec, "connection-id", "server", NULL);
120 g_signal_connect (s_dec, "on-key-received", G_CALLBACK (_on_key_received),
121 NULL);
122 gst_element_set_state (s_dec, GST_STATE_PAUSED);
123 gst_bin_add (GST_BIN (s_bin), s_dec);
124
125 s_enc = gst_element_factory_make ("dtlsenc", "server_enc");
126 g_object_set (s_enc, "connection-id", "server", NULL);
127 g_signal_connect (s_enc, "on-key-received", G_CALLBACK (_on_key_received),
128 NULL);
129 gst_element_set_state (s_enc, GST_STATE_PAUSED);
130 gst_bin_add (GST_BIN (c_bin), s_enc);
131
132 c_dec = gst_element_factory_make ("dtlsdec", "client_dec");
133 g_object_set (c_dec, "connection-id", "client", NULL);
134 g_signal_connect (c_dec, "on-key-received", G_CALLBACK (_on_key_received),
135 NULL);
136 gst_element_set_state (c_dec, GST_STATE_PAUSED);
137 gst_bin_add (GST_BIN (c_bin), c_dec);
138
139 c_enc = gst_element_factory_make ("dtlsenc", "client_enc");
140 g_object_set (c_enc, "connection-id", "client", "is-client", TRUE, NULL);
141 g_signal_connect (c_enc, "on-key-received", G_CALLBACK (_on_key_received),
142 NULL);
143 gst_bin_add (GST_BIN (s_bin), c_enc);
144
145 gst_element_link_pads (s_enc, "src", c_dec, "sink");
146 gst_element_link_pads (c_enc, "src", s_dec, "sink");
147
148 gst_element_set_state (c_enc, GST_STATE_PAUSED);
149
150 target = gst_element_request_pad_simple (c_dec, "src");
151 ghost = gst_ghost_pad_new ("src", target);
152 gst_element_add_pad (s_bin, ghost);
153 gst_object_unref (target);
154
155 target = gst_element_request_pad_simple (s_enc, "sink");
156 ghost = gst_ghost_pad_new ("sink", target);
157 gst_element_add_pad (s_bin, ghost);
158 gst_object_unref (target);
159
160 target = gst_element_request_pad_simple (s_dec, "src");
161 ghost = gst_ghost_pad_new ("src", target);
162 gst_element_add_pad (c_bin, ghost);
163 gst_object_unref (target);
164
165 target = gst_element_request_pad_simple (c_enc, "sink");
166 ghost = gst_ghost_pad_new ("sink", target);
167 gst_element_add_pad (c_bin, ghost);
168 gst_object_unref (target);
169
170 server = gst_harness_new_with_element (s_bin, "sink", "src");
171 client = gst_harness_new_with_element (c_bin, "sink", "src");
172
173 gst_harness_set_src_caps_str (server, "application/data");
174 gst_harness_set_src_caps_str (client, "application/data");
175
176 bus = gst_bus_new ();
177 gst_bus_set_sync_handler (bus, bus_msg_handler, NULL, NULL);
178
179 gst_element_set_bus (s_bin, bus);
180 gst_element_set_bus (c_bin, bus);
181
182 gst_object_unref (bus);
183
184 _wait_for_key_count_to_reach (4);
185
186 buffer = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, data,
187 G_N_ELEMENTS (data), 0, G_N_ELEMENTS (data), NULL, NULL);
188 gst_harness_push (server, gst_buffer_ref (buffer));
189 buf2 = gst_harness_pull (server);
190 fail_unless_equals_int (0, gst_buffer_memcmp (buf2, 0, data,
191 G_N_ELEMENTS (data)));
192 gst_buffer_unref (buf2);
193
194 gst_harness_play (client);
195 gst_harness_push (client, gst_buffer_ref (buffer));
196 buf2 = gst_harness_pull (client);
197 fail_unless_equals_int (0, gst_buffer_memcmp (buf2, 0, data,
198 G_N_ELEMENTS (data)));
199 gst_buffer_unref (buf2);
200
201 gst_object_unref (s_bin);
202 gst_object_unref (c_bin);
203
204 gst_buffer_unref (buffer);
205 gst_harness_teardown (server);
206 gst_harness_teardown (client);
207 }
208
209 GST_END_TEST;
210
211 static Suite *
dtls_suite(void)212 dtls_suite (void)
213 {
214 Suite *s = suite_create ("dtls");
215 TCase *tc_chain = tcase_create ("general");
216
217 suite_add_tcase (s, tc_chain);
218 tcase_add_test (tc_chain, test_create_and_unref);
219 tcase_add_test (tc_chain, test_data_transfer);
220
221 return s;
222 }
223
224 GST_CHECK_MAIN (dtls);
225