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 
55 static void
_on_key_received(GstElement * element,gpointer user_data)56 _on_key_received (GstElement * element, gpointer user_data)
57 {
58   g_mutex_lock (&key_lock);
59   key_count++;
60   g_cond_broadcast (&key_cond);
61   g_mutex_unlock (&key_lock);
62 }
63 
64 static void
_wait_for_key_count_to_reach(int n)65 _wait_for_key_count_to_reach (int n)
66 {
67   g_mutex_lock (&key_lock);
68   while (key_count < n)
69     g_cond_wait (&key_cond, &key_lock);
70   g_mutex_unlock (&key_lock);
71 }
72 
73 static gchar data[] = {
74   0x00, 0x01, 0x02, 0x03,
75 };
76 
GST_START_TEST(test_data_transfer)77 GST_START_TEST (test_data_transfer)
78 {
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;
83 
84   /* setup a server and client for dtls negotiation */
85   s_bin = gst_bin_new (NULL);
86   c_bin = gst_bin_new (NULL);
87 
88   /* XXX: the element set states are needed to avoid a runtime warning:
89    *
90    *   'gst_dtls_connection_process: runtime check failed: (!priv->bio_buffer)'
91    *
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().
94    */
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),
98       NULL);
99   gst_element_set_state (s_dec, GST_STATE_PAUSED);
100   gst_bin_add (GST_BIN (s_bin), s_dec);
101 
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),
105       NULL);
106   gst_element_set_state (s_enc, GST_STATE_PAUSED);
107   gst_bin_add (GST_BIN (c_bin), s_enc);
108 
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),
112       NULL);
113   gst_element_set_state (c_dec, GST_STATE_PAUSED);
114   gst_bin_add (GST_BIN (c_bin), c_dec);
115 
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),
119       NULL);
120   gst_element_set_state (c_enc, GST_STATE_PAUSED);
121   gst_bin_add (GST_BIN (s_bin), c_enc);
122 
123   gst_element_link_pads (s_enc, "src", c_dec, "sink");
124   gst_element_link_pads (c_enc, "src", s_dec, "sink");
125 
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);
130 
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);
135 
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);
140 
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);
145 
146   server = gst_harness_new_with_element (s_bin, "sink", "src");
147   client = gst_harness_new_with_element (c_bin, "sink", "src");
148 
149   gst_harness_set_src_caps_str (server, "application/data");
150   gst_harness_set_src_caps_str (client, "application/data");
151 
152   _wait_for_key_count_to_reach (4);
153 
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);
161 
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);
168 
169   gst_object_unref (s_bin);
170   gst_object_unref (c_bin);
171 
172   gst_buffer_unref (buffer);
173   gst_harness_teardown (server);
174   gst_harness_teardown (client);
175 }
176 
177 GST_END_TEST;
178 
179 static Suite *
dtls_suite(void)180 dtls_suite (void)
181 {
182   Suite *s = suite_create ("dtls");
183   TCase *tc_chain = tcase_create ("general");
184 
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);
188 
189   return s;
190 }
191 
192 GST_CHECK_MAIN (dtls);
193