3 * unit test for data protocol
5 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 #include <gst/check/gstcheck.h>
27 #include <gst/dataprotocol/dataprotocol.h>
28 #include "libs/gst/dataprotocol/dp-private.h" /* private header */
30 /* test our method of reading and writing headers using TO/FROM_BE */
31 GST_START_TEST (test_conversion)
34 guint8 write_array[9];
35 guint16 read_two, expect_two;
36 guint32 read_four, expect_four;
37 guint64 read_eight, expect_eight;
40 for (i = 0; i < 9; ++i) {
45 for (i = 0; i < 8; ++i) {
46 read_two = GST_READ_UINT16_BE (array + i);
47 expect_two = array[i] * (1 << 8) + array[i + 1];
48 fail_unless (read_two == expect_two,
49 "GST_READ_UINT16_BE %d: read %d != %d\n", i, read_two, expect_two);
53 for (i = 0; i < 8; ++i) {
54 GST_WRITE_UINT16_BE (&write_array[i], read_two);
55 fail_unless (memcmp (array + 7, write_array + i, 2) == 0,
56 "GST_WRITE_UINT16_BE %d: memcmp failed", i);
60 for (i = 0; i < 5; ++i) {
61 read_four = GST_READ_UINT32_BE (array + i);
62 expect_four = array[i] * (1 << 24) + array[i + 1] * (1 << 16)
63 + array[i + 2] * (1 << 8) + array[i + 3];
64 fail_unless (read_four == expect_four,
65 "GST_READ_UINT32_BE %d: read %d != %d\n", i, read_four, expect_four);
69 for (i = 0; i < 2; ++i) {
70 read_eight = GST_READ_UINT64_BE (array + i);
71 expect_eight = array[i] * (1LL << 56) + array[i + 1] * (1LL << 48)
72 + array[i + 2] * (1LL << 40) + array[i + 3] * (1LL << 32)
73 + array[i + 4] * (1 << 24) + array[i + 5] * (1 << 16)
74 + array[i + 6] * (1 << 8) + array[i + 7];
75 fail_unless (read_eight == expect_eight,
76 "GST_READ_UINT64_BE %d: read %" G_GUINT64_FORMAT
77 " != %" G_GUINT64_FORMAT "\n", i, read_eight, expect_eight);
81 GST_WRITE_UINT64_BE (&write_array[0], read_eight);
82 fail_unless (memcmp (array + 1, write_array, 8) == 0,
83 "GST_WRITE_UINT64_BE: memcmp failed");
88 #ifndef GST_DISABLE_DEPRECATED /* these tests use deprecated API, that we disable by default */
90 #ifndef HAVE_CPU_PPC64 /* this test doesn't work on PPC64. See #348114 */
92 /* test creation of header from buffer and back again */
93 GST_START_TEST (test_buffer)
102 g_message ("Creating a new 8-byte buffer with ts 0.5 sec, dur 1 sec\n");
103 buffer = gst_buffer_new_and_alloc (8);
104 GST_BUFFER_TIMESTAMP (buffer) = (GstClockTime) (GST_SECOND * 0.5);
105 GST_BUFFER_DURATION (buffer) = (GstClockTime) GST_SECOND;
106 GST_BUFFER_OFFSET (buffer) = (guint64) 10;
107 GST_BUFFER_OFFSET_END (buffer) = (guint64) 19;
108 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
109 memmove (GST_BUFFER_DATA (buffer), "a buffer", 8);
111 /* create a buffer with CRC checking */
112 fail_unless (gst_dp_header_from_buffer (buffer, GST_DP_HEADER_FLAG_CRC,
113 &header_length, &header), "Could not create header from buffer.");
115 /* validate the header */
116 fail_unless (gst_dp_validate_header (header_length, header),
117 "Could not validate header");
118 /* create a new, empty buffer with the right size */
119 newbuffer = gst_dp_buffer_from_header (header_length, header);
120 fail_unless (newbuffer != NULL, "Could not create a new buffer from header");
121 fail_unless (GST_IS_BUFFER (newbuffer), "Created buffer is not a GstBuffer");
122 /* read/copy the data */
123 memmove (GST_BUFFER_DATA (newbuffer), GST_BUFFER_DATA (buffer),
124 GST_BUFFER_SIZE (buffer));
125 /* validate the buffer */
126 fail_unless (gst_dp_validate_payload (header_length, header,
127 GST_BUFFER_DATA (newbuffer)), "Could not validate payload");
129 g_message ("new buffer timestamp: %" GST_TIME_FORMAT "\n",
130 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (newbuffer)));
131 g_message ("new buffer duration: %" GST_TIME_FORMAT "\n",
132 GST_TIME_ARGS (GST_BUFFER_DURATION (newbuffer)));
133 g_message ("new buffer offset: %" G_GUINT64_FORMAT "\n",
134 GST_BUFFER_OFFSET (newbuffer));
135 g_message ("new buffer offset_end: %" G_GUINT64_FORMAT "\n",
136 GST_BUFFER_OFFSET_END (newbuffer));
137 fail_unless (GST_BUFFER_TIMESTAMP (newbuffer) ==
138 GST_BUFFER_TIMESTAMP (buffer), "Timestamps don't match !");
139 fail_unless (GST_BUFFER_DURATION (newbuffer) == GST_BUFFER_DURATION (buffer),
140 "Durations don't match !");
141 fail_unless (GST_BUFFER_OFFSET (newbuffer) == GST_BUFFER_OFFSET (buffer),
142 "Offsets don't match !");
143 fail_unless (GST_BUFFER_OFFSET_END (newbuffer) ==
144 GST_BUFFER_OFFSET_END (buffer), "Offset ends don't match !");
145 fail_unless (GST_BUFFER_FLAG_IS_SET (newbuffer, GST_BUFFER_FLAG_IN_CAPS),
146 "GST_BUFFER_IN_CAPS flag should have been copied !");
149 gst_buffer_unref (buffer);
150 gst_buffer_unref (newbuffer);
157 GST_START_TEST (test_caps)
159 gchar *string, *newstring;
160 GstCaps *caps, *newcaps;
163 guint8 *header, *payload;
165 caps = gst_caps_from_string ("audio/x-raw-float, "
166 "rate = (int) [ 11025, 48000 ], "
167 "channels = (int) [ 1, 2 ], " "endianness = (int) BYTE_ORDER, "
168 "width = (int) 32, " "buffer-frames = (int) 0");
169 string = gst_caps_to_string (caps);
170 g_message ("Created caps: %s\n", string);
171 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_length, &header,
172 &payload), "Could not create packet from caps.");
174 /* validate the packet */
175 fail_unless (gst_dp_validate_packet (header_length, header, payload),
176 "Could not validate packet");
177 newcaps = gst_dp_caps_from_packet (header_length, header, payload);
178 fail_unless (newcaps != NULL, "Could not create caps from packet");
179 fail_unless (GST_IS_CAPS (newcaps));
180 newstring = gst_caps_to_string (newcaps);
181 g_message ("Received caps: %s\n", newstring);
182 fail_unless (strcmp (string, newstring) == 0,
183 "Created caps do not match original caps");
186 gst_caps_unref (caps);
187 gst_caps_unref (newcaps);
196 GST_START_TEST (test_event)
201 guint8 *header, *payload;
203 g_message ("Testing EOS event at 1s\n");
204 send = gst_event_new_eos ();
205 GST_EVENT_TIMESTAMP (send) = GST_SECOND;
206 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
207 &header_length, &header, &payload),
208 "Could not create packet from eos event");
210 receive = gst_dp_event_from_packet (header_length, header, payload);
212 g_message ("EOS, timestamp %" GST_TIME_FORMAT "\n",
213 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
214 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_EOS,
215 "Received event is not EOS");
216 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND,
217 "EOS timestamp is not 1.0 sec");
222 gst_event_unref (send);
223 gst_event_unref (receive);
225 g_message ("Testing FLUSH event at 2s\n");
226 send = gst_event_new_flush_start ();
227 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 2;
228 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
229 &header_length, &header, &payload),
230 "Could not create packet from flush event");
232 receive = gst_dp_event_from_packet (header_length, header, payload);
234 g_message ("Flush, timestamp %" GST_TIME_FORMAT "\n",
235 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
236 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_FLUSH_START,
237 "Received event is not flush");
238 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 2,
239 "Flush timestamp is not 2.0 sec");
244 gst_event_unref (send);
245 gst_event_unref (receive);
247 g_message ("Testing SEEK event with 1 second at 3 seconds\n");
249 gst_event_new_seek (1.0, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_SET,
250 GST_SECOND, GST_SEEK_TYPE_NONE, 0);
251 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 3;
252 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
253 &header_length, &header, &payload),
254 "Could not create packet from seek event");
256 receive = gst_dp_event_from_packet (header_length, header, payload);
262 GstSeekType cur_type, stop_type;
265 gst_event_parse_seek (receive, &rate, &format, &flags,
266 &cur_type, &cur, &stop_type, &stop);
268 g_message ("Seek, timestamp %" GST_TIME_FORMAT ", to %" GST_TIME_FORMAT
269 "\n", GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)),
270 GST_TIME_ARGS (cur));
271 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_SEEK,
272 "Returned event is not seek");
273 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 3,
274 "Seek timestamp is not 3.0 sec");
275 fail_unless (format == GST_FORMAT_TIME, "Seek format is not time");
276 fail_unless (cur == GST_SECOND, "Seek cur is not 1.0 sec");
282 gst_event_unref (send);
283 gst_event_unref (receive);
288 /* try to segfault the thing by passing NULLs, short headers, etc.. */
289 GST_START_TEST (test_memory)
299 /* check 0 sized input, data pointer can be NULL or anything. CRC is always 0,
301 fail_if (gst_dp_crc (NULL, 0) != 0);
302 fail_if (gst_dp_crc (foo, 0) != 0);
304 /* this is very invalid input and gives a warning. */
305 ASSERT_CRITICAL (gst_dp_crc (NULL, 1));
306 ASSERT_CRITICAL (gst_dp_header_payload_length (NULL));
307 ASSERT_CRITICAL (gst_dp_header_payload_type (NULL));
310 ASSERT_CRITICAL (gst_dp_header_from_buffer (NULL, 0, NULL, NULL));
312 /* empty buffer has NULL as data pointer */
313 buffer = gst_buffer_new_and_alloc (0);
315 /* no place to store the length and/or header data */
316 ASSERT_CRITICAL (gst_dp_header_from_buffer (buffer, 0, NULL, NULL));
317 ASSERT_CRITICAL (gst_dp_header_from_buffer (buffer, 0, &length, NULL));
319 /* this should work fine */
320 fail_if (gst_dp_header_from_buffer (buffer, 0, &length, &header) != TRUE);
321 fail_unless (length != 0);
322 fail_unless (header != NULL);
324 /* this should validate */
325 fail_if (gst_dp_validate_header (length, header) == FALSE);
327 /* NULL header pointer */
328 ASSERT_CRITICAL (gst_dp_validate_header (length, NULL));
330 ASSERT_CRITICAL (gst_dp_validate_header (5, header));
334 /* this should work and not crash trying to calc a CRC on a 0 sized buffer */
335 fail_if (gst_dp_header_from_buffer (buffer,
336 GST_DP_HEADER_FLAG_CRC_HEADER | GST_DP_HEADER_FLAG_CRC_PAYLOAD,
337 &length, &header) != TRUE);
339 /* this should validate */
340 fail_if (gst_dp_validate_header (length, header) == FALSE);
342 /* there was no payload, NULL as payload data should validate the CRC
344 fail_if (gst_dp_validate_payload (length, header, NULL) == FALSE);
346 /* and the whole packet as well */
347 fail_if (gst_dp_validate_packet (length, header, NULL) == FALSE);
349 /* some bogus length */
350 ASSERT_CRITICAL (gst_dp_validate_packet (5, header, NULL));
351 gst_buffer_unref (buffer);
353 /* create buffer from header data, integrity tested elsewhere */
354 buffer = gst_dp_buffer_from_header (length, header);
355 fail_if (buffer == NULL);
356 gst_buffer_unref (buffer);
359 ASSERT_CRITICAL (gst_dp_packet_from_caps (NULL, 0, NULL, NULL, NULL));
361 /* some caps stuff */
362 caps = gst_caps_new_empty ();
363 ASSERT_CRITICAL (gst_dp_packet_from_caps (caps, 0, NULL, NULL, NULL));
364 ASSERT_CRITICAL (gst_dp_packet_from_caps (caps, 0, &length, NULL, NULL));
365 ASSERT_CRITICAL (gst_dp_packet_from_caps (caps, 0, &length, &header, NULL));
367 fail_if (gst_dp_packet_from_caps (caps, 0, &length, &header,
369 fail_if (strcmp ((const gchar *) payload, "EMPTY") != 0);
370 gst_caps_unref (caps);
372 caps = gst_dp_caps_from_packet (length, header, payload);
373 fail_if (caps == NULL);
374 gst_caps_unref (caps);
379 /* some event stuff */
380 event = gst_event_new_eos ();
381 ASSERT_CRITICAL (gst_dp_packet_from_event (event, 0, NULL, NULL, NULL));
382 ASSERT_CRITICAL (gst_dp_packet_from_event (event, 0, &length, NULL, NULL));
383 ASSERT_CRITICAL (gst_dp_packet_from_event (event, 0, &length, &header, NULL));
385 /* payload is not NULL from previous test and points to freed memory, very
387 fail_if (payload == NULL);
388 fail_if (gst_dp_packet_from_event (event, 0, &length, &header,
391 /* the EOS event has no payload */
392 fail_if (payload != NULL);
393 gst_event_unref (event);
395 event = gst_dp_event_from_packet (length, header, payload);
396 fail_if (event == NULL);
397 fail_if (GST_EVENT_TYPE (event) != GST_EVENT_EOS);
398 gst_event_unref (event);
411 Suite *s = suite_create ("data protocol");
412 TCase *tc_chain = tcase_create ("general");
414 suite_add_tcase (s, tc_chain);
415 tcase_add_test (tc_chain, test_conversion);
416 #ifndef GST_DISABLE_DEPRECATED
417 #ifndef HAVE_CPU_PPC64
418 tcase_add_test (tc_chain, test_buffer);
420 tcase_add_test (tc_chain, test_caps);
421 tcase_add_test (tc_chain, test_event);
422 tcase_add_test (tc_chain, test_memory);
428 GST_CHECK_MAIN (gst_dp);