1 /* GStreamer unit tests for the MIKEY support library
2  *
3  * Copyright (C) 2014 Wim Taymans <wim.taymans@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <gst/check/gstcheck.h>
26 
27 #include <gst/sdp/gstmikey.h>
28 
GST_START_TEST(create_common)29 GST_START_TEST (create_common)
30 {
31   GstMIKEYMessage *msg;
32   const guint8 test_data[] =
33       { 0x01, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00 };
34   const guint8 test_data2[] =
35       { 0x01, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00, 0x00, 0x00,
36     0x02, 0x23, 0x45, 0x67, 0x89, 0x00, 0x00, 0x00, 0x01
37   };
38   GBytes *bytes;
39   const guint8 *data;
40   gsize size;
41   const GstMIKEYMapSRTP *mi;
42   GstMIKEYMapSRTP srtp;
43 
44   msg = gst_mikey_message_new ();
45   fail_unless (msg != NULL);
46 
47   fail_unless (gst_mikey_message_set_info (msg, 1, GST_MIKEY_TYPE_PSK_INIT,
48           FALSE, GST_MIKEY_PRF_MIKEY_1, 0x12345678, GST_MIKEY_MAP_TYPE_SRTP));
49   fail_unless (gst_mikey_message_get_n_cs (msg) == 0);
50 
51   fail_unless (msg->version == 1);
52   fail_unless (msg->type == GST_MIKEY_TYPE_PSK_INIT);
53   fail_unless (msg->V == FALSE);
54   fail_unless (msg->prf_func == GST_MIKEY_PRF_MIKEY_1);
55   fail_unless (msg->CSB_id == 0x12345678);
56   fail_unless (msg->map_type == GST_MIKEY_MAP_TYPE_SRTP);
57 
58   bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
59   data = g_bytes_get_data (bytes, &size);
60   fail_unless (data != NULL);
61   fail_unless (size == 10);
62   fail_unless (memcmp (data, test_data, 10) == 0);
63   g_bytes_unref (bytes);
64 
65   fail_unless (gst_mikey_message_add_cs_srtp (msg, 1, 0x12345678, 0));
66   fail_unless (gst_mikey_message_get_n_cs (msg) == 1);
67   fail_unless (gst_mikey_message_add_cs_srtp (msg, 2, 0x23456789, 1));
68   fail_unless (gst_mikey_message_get_n_cs (msg) == 2);
69 
70   bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
71   data = g_bytes_get_data (bytes, &size);
72   fail_unless (size == 28);
73   fail_unless (memcmp (data + 10, test_data2, 18) == 0);
74   g_bytes_unref (bytes);
75 
76   fail_unless ((mi = gst_mikey_message_get_cs_srtp (msg, 0)) != NULL);
77   fail_unless (mi->policy == 1);
78   fail_unless (mi->ssrc == 0x12345678);
79   fail_unless (mi->roc == 0);
80   fail_unless ((mi = gst_mikey_message_get_cs_srtp (msg, 1)) != NULL);
81   fail_unless (mi->policy == 2);
82   fail_unless (mi->ssrc == 0x23456789);
83   fail_unless (mi->roc == 1);
84 
85   fail_unless (gst_mikey_message_remove_cs_srtp (msg, 0));
86   fail_unless (gst_mikey_message_get_n_cs (msg) == 1);
87   fail_unless ((mi = gst_mikey_message_get_cs_srtp (msg, 0)) != NULL);
88   fail_unless (mi->policy == 2);
89   fail_unless (mi->ssrc == 0x23456789);
90   fail_unless (mi->roc == 1);
91   srtp.policy = 1;
92   srtp.ssrc = 0x12345678;
93   srtp.roc = 0;
94   fail_unless (gst_mikey_message_insert_cs_srtp (msg, 0, &srtp));
95   fail_unless ((mi = gst_mikey_message_get_cs_srtp (msg, 0)) != NULL);
96   fail_unless (mi->policy == 1);
97   fail_unless (mi->ssrc == 0x12345678);
98   fail_unless (mi->roc == 0);
99   fail_unless ((mi = gst_mikey_message_get_cs_srtp (msg, 1)) != NULL);
100   fail_unless (mi->policy == 2);
101   fail_unless (mi->ssrc == 0x23456789);
102   fail_unless (mi->roc == 1);
103 
104   fail_unless (gst_mikey_message_remove_cs_srtp (msg, 1));
105   fail_unless (gst_mikey_message_get_n_cs (msg) == 1);
106   fail_unless (gst_mikey_message_remove_cs_srtp (msg, 0));
107   fail_unless (gst_mikey_message_get_n_cs (msg) == 0);
108 
109   gst_mikey_message_unref (msg);
110 }
111 
112 GST_END_TEST
GST_START_TEST(create_payloads)113 GST_START_TEST (create_payloads)
114 {
115   GstMIKEYMessage *msg;
116   GstMIKEYPayload *payload, *kp;
117   const GstMIKEYPayload *cp, *cp2;
118   const GstMIKEYPayloadKEMAC *p;
119   const GstMIKEYPayloadT *pt;
120   const GstMIKEYPayloadKeyData *pkd;
121   const guint8 ntp_data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
122   const guint8 edata[] = { 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
123     0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0x10
124   };
125   GBytes *bytes;
126   const guint8 *data;
127   gsize size;
128 
129   msg = gst_mikey_message_new ();
130   fail_unless (msg != NULL);
131 
132   fail_unless (gst_mikey_message_set_info (msg, 1, GST_MIKEY_TYPE_PSK_INIT,
133           FALSE, GST_MIKEY_PRF_MIKEY_1, 0x12345678, GST_MIKEY_MAP_TYPE_SRTP));
134   fail_unless (gst_mikey_message_get_n_cs (msg) == 0);
135 
136   fail_unless (gst_mikey_message_get_n_payloads (msg) == 0);
137 
138   payload = gst_mikey_payload_new (GST_MIKEY_PT_T);
139   fail_unless (payload->type == GST_MIKEY_PT_T);
140   fail_unless (payload->len == sizeof (GstMIKEYPayloadT));
141   fail_unless (gst_mikey_payload_t_set (payload, GST_MIKEY_TS_TYPE_NTP,
142           ntp_data));
143   pt = (GstMIKEYPayloadT *) payload;
144   fail_unless (pt->type == GST_MIKEY_TS_TYPE_NTP);
145   fail_unless (memcmp (pt->ts_value, ntp_data, 8) == 0);
146 
147   fail_unless (gst_mikey_message_add_payload (msg, payload));
148   fail_unless (payload->type == GST_MIKEY_PT_T);
149   fail_unless (gst_mikey_message_get_n_payloads (msg) == 1);
150 
151   bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
152   data = g_bytes_get_data (bytes, &size);
153   fail_unless (data != NULL);
154   fail_unless (size == 20);
155   g_bytes_unref (bytes);
156 
157   payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
158   fail_unless (gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL,
159           GST_MIKEY_MAC_NULL));
160   /* add the edata as a key payload */
161   kp = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
162   gst_mikey_payload_key_data_set_key (kp, GST_MIKEY_KD_TEK,
163       sizeof (edata), edata);
164   fail_unless (gst_mikey_payload_kemac_add_sub (payload, kp));
165   fail_unless (gst_mikey_message_add_payload (msg, payload));
166   fail_unless (gst_mikey_message_get_n_payloads (msg) == 2);
167 
168   p = (GstMIKEYPayloadKEMAC *) gst_mikey_message_get_payload (msg, 1);
169   fail_unless (p->enc_alg == GST_MIKEY_ENC_NULL);
170   fail_unless (p->mac_alg == GST_MIKEY_MAC_NULL);
171   fail_unless (gst_mikey_payload_kemac_get_n_sub (&p->pt) == 1);
172 
173   fail_unless ((cp = gst_mikey_message_get_payload (msg, 0)) != NULL);
174   fail_unless (cp->type == GST_MIKEY_PT_T);
175   fail_unless ((cp = gst_mikey_message_get_payload (msg, 1)) != NULL);
176   fail_unless (cp->type == GST_MIKEY_PT_KEMAC);
177 
178   bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
179   gst_mikey_message_unref (msg);
180 
181   msg = gst_mikey_message_new_from_bytes (bytes, NULL, NULL);
182   fail_unless (msg != NULL);
183   g_bytes_unref (bytes);
184   fail_unless (gst_mikey_message_get_n_payloads (msg) == 2);
185   fail_unless ((cp = gst_mikey_message_get_payload (msg, 0)) != NULL);
186   fail_unless (cp->type == GST_MIKEY_PT_T);
187   fail_unless ((cp = gst_mikey_message_get_payload (msg, 1)) != NULL);
188   fail_unless (cp->type == GST_MIKEY_PT_KEMAC);
189 
190   fail_unless ((cp2 = gst_mikey_payload_kemac_get_sub (cp, 0)) != NULL);
191   fail_unless (cp2->type == GST_MIKEY_PT_KEY_DATA);
192   pkd = (GstMIKEYPayloadKeyData *) cp2;
193 
194   fail_unless (pkd->key_type == GST_MIKEY_KD_TEK);
195   fail_unless (pkd->key_len == sizeof (edata));
196   fail_unless (memcmp (pkd->key_data, edata, sizeof (edata)) == 0);
197   fail_unless (pkd->salt_len == 0);
198   fail_unless (pkd->salt_data == 0);
199   fail_unless (pkd->kv_type == GST_MIKEY_KV_NULL);
200 
201 
202   gst_mikey_message_unref (msg);
203 }
204 
205 GST_END_TEST
206 /*
207  * End of test cases
208  */
209 static Suite *
mikey_suite(void)210 mikey_suite (void)
211 {
212   Suite *s = suite_create ("mikey");
213   TCase *tc_chain = tcase_create ("mikey");
214 
215   suite_add_tcase (s, tc_chain);
216   tcase_add_test (tc_chain, create_common);
217   tcase_add_test (tc_chain, create_payloads);
218 
219   return s;
220 }
221 
222 GST_CHECK_MAIN (mikey);
223