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 #ifndef GST_REMOVE_DEPRECATED
28 #undef GST_DISABLE_DEPRECATED
31 #include <gst/dataprotocol/dataprotocol.h>
32 #include "libs/gst/dataprotocol/dp-private.h" /* private header */
34 /* test our method of reading and writing headers using TO/FROM_BE */
35 GST_START_TEST (test_conversion)
38 guint8 write_array[9];
39 guint16 read_two, expect_two;
40 guint32 read_four, expect_four;
41 guint64 read_eight, expect_eight;
44 for (i = 0; i < 9; ++i) {
49 for (i = 0; i < 8; ++i) {
50 read_two = GST_READ_UINT16_BE (array + i);
51 expect_two = array[i] * (1 << 8) + array[i + 1];
52 fail_unless (read_two == expect_two,
53 "GST_READ_UINT16_BE %d: read %d != %d\n", i, read_two, expect_two);
57 for (i = 0; i < 8; ++i) {
58 GST_WRITE_UINT16_BE (&write_array[i], read_two);
59 fail_unless (memcmp (array + 7, write_array + i, 2) == 0,
60 "GST_WRITE_UINT16_BE %d: memcmp failed", i);
64 for (i = 0; i < 5; ++i) {
65 read_four = GST_READ_UINT32_BE (array + i);
66 expect_four = array[i] * (1 << 24) + array[i + 1] * (1 << 16)
67 + array[i + 2] * (1 << 8) + array[i + 3];
68 fail_unless (read_four == expect_four,
69 "GST_READ_UINT32_BE %d: read %d != %d\n", i, read_four, expect_four);
73 for (i = 0; i < 2; ++i) {
74 read_eight = GST_READ_UINT64_BE (array + i);
75 expect_eight = array[i] * (1LL << 56) + array[i + 1] * (1LL << 48)
76 + array[i + 2] * (1LL << 40) + array[i + 3] * (1LL << 32)
77 + array[i + 4] * (1 << 24) + array[i + 5] * (1 << 16)
78 + array[i + 6] * (1 << 8) + array[i + 7];
79 fail_unless (read_eight == expect_eight,
80 "GST_READ_UINT64_BE %d: read %" G_GUINT64_FORMAT
81 " != %" G_GUINT64_FORMAT "\n", i, read_eight, expect_eight);
85 GST_WRITE_UINT64_BE (&write_array[0], read_eight);
86 fail_unless (memcmp (array + 1, write_array, 8) == 0,
87 "GST_WRITE_UINT64_BE: memcmp failed");
92 #ifndef GST_REMOVE_DEPRECATED /* these tests use deprecated API, that we disable by default */
94 #ifndef HAVE_CPU_PPC64 /* this test doesn't work on PPC64. See #348114 */
96 /* test creation of header from buffer and back again */
97 GST_START_TEST (test_buffer)
100 GstBuffer *newbuffer;
106 g_message ("Creating a new 8-byte buffer with ts 0.5 sec, dur 1 sec\n");
107 buffer = gst_buffer_new_and_alloc (8);
108 GST_BUFFER_TIMESTAMP (buffer) = (GstClockTime) (GST_SECOND * 0.5);
109 GST_BUFFER_DURATION (buffer) = (GstClockTime) GST_SECOND;
110 GST_BUFFER_OFFSET (buffer) = (guint64) 10;
111 GST_BUFFER_OFFSET_END (buffer) = (guint64) 19;
112 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
113 memmove (GST_BUFFER_DATA (buffer), "a buffer", 8);
115 /* create a buffer with CRC checking */
116 fail_unless (gst_dp_header_from_buffer (buffer, GST_DP_HEADER_FLAG_CRC,
117 &header_length, &header), "Could not create header from buffer.");
119 /* validate the header */
120 fail_unless (gst_dp_validate_header (header_length, header),
121 "Could not validate header");
122 /* create a new, empty buffer with the right size */
123 newbuffer = gst_dp_buffer_from_header (header_length, header);
124 fail_unless (newbuffer != NULL, "Could not create a new buffer from header");
125 fail_unless (GST_IS_BUFFER (newbuffer), "Created buffer is not a GstBuffer");
126 /* read/copy the data */
127 memmove (GST_BUFFER_DATA (newbuffer), GST_BUFFER_DATA (buffer),
128 GST_BUFFER_SIZE (buffer));
129 /* validate the buffer */
130 fail_unless (gst_dp_validate_payload (header_length, header,
131 GST_BUFFER_DATA (newbuffer)), "Could not validate payload");
133 g_message ("new buffer timestamp: %" GST_TIME_FORMAT "\n",
134 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (newbuffer)));
135 g_message ("new buffer duration: %" GST_TIME_FORMAT "\n",
136 GST_TIME_ARGS (GST_BUFFER_DURATION (newbuffer)));
137 g_message ("new buffer offset: %" G_GUINT64_FORMAT "\n",
138 GST_BUFFER_OFFSET (newbuffer));
139 g_message ("new buffer offset_end: %" G_GUINT64_FORMAT "\n",
140 GST_BUFFER_OFFSET_END (newbuffer));
141 fail_unless (GST_BUFFER_TIMESTAMP (newbuffer) ==
142 GST_BUFFER_TIMESTAMP (buffer), "Timestamps don't match !");
143 fail_unless (GST_BUFFER_DURATION (newbuffer) == GST_BUFFER_DURATION (buffer),
144 "Durations don't match !");
145 fail_unless (GST_BUFFER_OFFSET (newbuffer) == GST_BUFFER_OFFSET (buffer),
146 "Offsets don't match !");
147 fail_unless (GST_BUFFER_OFFSET_END (newbuffer) ==
148 GST_BUFFER_OFFSET_END (buffer), "Offset ends don't match !");
149 fail_unless (GST_BUFFER_FLAG_IS_SET (newbuffer, GST_BUFFER_FLAG_IN_CAPS),
150 "GST_BUFFER_IN_CAPS flag should have been copied !");
153 gst_buffer_unref (buffer);
154 gst_buffer_unref (newbuffer);
161 GST_START_TEST (test_caps)
163 gchar *string, *newstring;
164 GstCaps *caps, *newcaps;
167 guint8 *header, *payload;
169 caps = gst_caps_from_string ("audio/x-raw-float, "
170 "rate = (int) [ 11025, 48000 ], "
171 "channels = (int) [ 1, 2 ], " "endianness = (int) BYTE_ORDER, "
172 "width = (int) 32, " "buffer-frames = (int) 0");
173 string = gst_caps_to_string (caps);
174 g_message ("Created caps: %s\n", string);
175 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_length, &header,
176 &payload), "Could not create packet from caps.");
178 /* validate the packet */
179 fail_unless (gst_dp_validate_packet (header_length, header, payload),
180 "Could not validate packet");
181 newcaps = gst_dp_caps_from_packet (header_length, header, payload);
182 fail_unless (newcaps != NULL, "Could not create caps from packet");
183 fail_unless (GST_IS_CAPS (newcaps));
184 newstring = gst_caps_to_string (newcaps);
185 g_message ("Received caps: %s\n", newstring);
186 fail_unless (strcmp (string, newstring) == 0,
187 "Created caps do not match original caps");
190 gst_caps_unref (caps);
191 gst_caps_unref (newcaps);
200 GST_START_TEST (test_event)
205 guint8 *header, *payload;
207 g_message ("Testing EOS event at 1s\n");
208 send = gst_event_new_eos ();
209 GST_EVENT_TIMESTAMP (send) = GST_SECOND;
210 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
211 &header_length, &header, &payload),
212 "Could not create packet from eos event");
214 receive = gst_dp_event_from_packet (header_length, header, payload);
216 g_message ("EOS, timestamp %" GST_TIME_FORMAT "\n",
217 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
218 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_EOS,
219 "Received event is not EOS");
220 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND,
221 "EOS timestamp is not 1.0 sec");
226 gst_event_unref (send);
227 gst_event_unref (receive);
229 g_message ("Testing FLUSH event at 2s\n");
230 send = gst_event_new_flush_start ();
231 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 2;
232 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
233 &header_length, &header, &payload),
234 "Could not create packet from flush event");
236 receive = gst_dp_event_from_packet (header_length, header, payload);
238 g_message ("Flush, timestamp %" GST_TIME_FORMAT "\n",
239 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
240 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_FLUSH_START,
241 "Received event is not flush");
242 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 2,
243 "Flush timestamp is not 2.0 sec");
248 gst_event_unref (send);
249 gst_event_unref (receive);
251 g_message ("Testing SEEK event with 1 second at 3 seconds\n");
253 gst_event_new_seek (1.0, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_SET,
254 GST_SECOND, GST_SEEK_TYPE_NONE, 0);
255 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 3;
256 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
257 &header_length, &header, &payload),
258 "Could not create packet from seek event");
260 receive = gst_dp_event_from_packet (header_length, header, payload);
266 GstSeekType cur_type, stop_type;
269 gst_event_parse_seek (receive, &rate, &format, &flags,
270 &cur_type, &cur, &stop_type, &stop);
272 g_message ("Seek, timestamp %" GST_TIME_FORMAT ", to %" GST_TIME_FORMAT
273 "\n", GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)),
274 GST_TIME_ARGS (cur));
275 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_SEEK,
276 "Returned event is not seek");
277 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 3,
278 "Seek timestamp is not 3.0 sec");
279 fail_unless (format == GST_FORMAT_TIME, "Seek format is not time");
280 fail_unless (cur == GST_SECOND, "Seek cur is not 1.0 sec");
286 gst_event_unref (send);
287 gst_event_unref (receive);
292 /* try to segfault the thing by passing NULLs, short headers, etc.. */
293 GST_START_TEST (test_memory)
303 /* check 0 sized input, data pointer can be NULL or anything. CRC is always 0,
305 fail_if (gst_dp_crc (NULL, 0) != 0);
306 fail_if (gst_dp_crc (foo, 0) != 0);
308 /* this is very invalid input and gives a warning. */
309 ASSERT_CRITICAL (gst_dp_crc (NULL, 1));
310 ASSERT_CRITICAL (gst_dp_header_payload_length (NULL));
311 ASSERT_CRITICAL (gst_dp_header_payload_type (NULL));
314 ASSERT_CRITICAL (gst_dp_header_from_buffer (NULL, 0, NULL, NULL));
316 /* empty buffer has NULL as data pointer */
317 buffer = gst_buffer_new_and_alloc (0);
319 /* no place to store the length and/or header data */
320 ASSERT_CRITICAL (gst_dp_header_from_buffer (buffer, 0, NULL, NULL));
321 ASSERT_CRITICAL (gst_dp_header_from_buffer (buffer, 0, &length, NULL));
323 /* this should work fine */
324 fail_if (gst_dp_header_from_buffer (buffer, 0, &length, &header) != TRUE);
325 fail_unless (length != 0);
326 fail_unless (header != NULL);
328 /* this should validate */
329 fail_if (gst_dp_validate_header (length, header) == FALSE);
331 /* NULL header pointer */
332 ASSERT_CRITICAL (gst_dp_validate_header (length, NULL));
334 ASSERT_CRITICAL (gst_dp_validate_header (5, header));
338 /* this should work and not crash trying to calc a CRC on a 0 sized buffer */
339 fail_if (gst_dp_header_from_buffer (buffer,
340 GST_DP_HEADER_FLAG_CRC_HEADER | GST_DP_HEADER_FLAG_CRC_PAYLOAD,
341 &length, &header) != TRUE);
343 /* this should validate */
344 fail_if (gst_dp_validate_header (length, header) == FALSE);
346 /* there was no payload, NULL as payload data should validate the CRC
348 fail_if (gst_dp_validate_payload (length, header, NULL) == FALSE);
350 /* and the whole packet as well */
351 fail_if (gst_dp_validate_packet (length, header, NULL) == FALSE);
353 /* some bogus length */
354 ASSERT_CRITICAL (gst_dp_validate_packet (5, header, NULL));
355 gst_buffer_unref (buffer);
357 /* create buffer from header data, integrity tested elsewhere */
358 buffer = gst_dp_buffer_from_header (length, header);
359 fail_if (buffer == NULL);
360 gst_buffer_unref (buffer);
363 ASSERT_CRITICAL (gst_dp_packet_from_caps (NULL, 0, NULL, NULL, NULL));
365 /* some caps stuff */
366 caps = gst_caps_new_empty ();
367 ASSERT_CRITICAL (gst_dp_packet_from_caps (caps, 0, NULL, NULL, NULL));
368 ASSERT_CRITICAL (gst_dp_packet_from_caps (caps, 0, &length, NULL, NULL));
369 ASSERT_CRITICAL (gst_dp_packet_from_caps (caps, 0, &length, &header, NULL));
371 fail_if (gst_dp_packet_from_caps (caps, 0, &length, &header,
373 fail_if (strcmp ((const gchar *) payload, "EMPTY") != 0);
374 gst_caps_unref (caps);
376 caps = gst_dp_caps_from_packet (length, header, payload);
377 fail_if (caps == NULL);
378 gst_caps_unref (caps);
383 /* some event stuff */
384 event = gst_event_new_eos ();
385 ASSERT_CRITICAL (gst_dp_packet_from_event (event, 0, NULL, NULL, NULL));
386 ASSERT_CRITICAL (gst_dp_packet_from_event (event, 0, &length, NULL, NULL));
387 ASSERT_CRITICAL (gst_dp_packet_from_event (event, 0, &length, &header, NULL));
389 /* payload is not NULL from previous test and points to freed memory, very
391 fail_if (payload == NULL);
392 fail_if (gst_dp_packet_from_event (event, 0, &length, &header,
395 /* the EOS event has no payload */
396 fail_if (payload != NULL);
397 gst_event_unref (event);
399 event = gst_dp_event_from_packet (length, header, payload);
400 fail_if (event == NULL);
401 fail_if (GST_EVENT_TYPE (event) != GST_EVENT_EOS);
402 gst_event_unref (event);
415 Suite *s = suite_create ("data protocol");
416 TCase *tc_chain = tcase_create ("general");
418 suite_add_tcase (s, tc_chain);
419 tcase_add_test (tc_chain, test_conversion);
420 #ifndef GST_REMOVE_DEPRECATED
421 #ifndef HAVE_CPU_PPC64
422 tcase_add_test (tc_chain, test_buffer);
424 tcase_add_test (tc_chain, test_caps);
425 tcase_add_test (tc_chain, test_event);
426 tcase_add_test (tc_chain, test_memory);
432 GST_CHECK_MAIN (gst_dp);