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 HAVE_CPU_PPC64 /* this test doesn't work on PPC64. See #348114 */
90 /* test creation of header from buffer and back again */
91 GST_START_TEST (test_buffer)
100 g_message ("Creating a new 8-byte buffer with ts 0.5 sec, dur 1 sec\n");
101 buffer = gst_buffer_new_and_alloc (8);
102 GST_BUFFER_TIMESTAMP (buffer) = (GstClockTime) (GST_SECOND * 0.5);
103 GST_BUFFER_DURATION (buffer) = (GstClockTime) GST_SECOND;
104 GST_BUFFER_OFFSET (buffer) = (guint64) 10;
105 GST_BUFFER_OFFSET_END (buffer) = (guint64) 19;
106 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
107 memmove (GST_BUFFER_DATA (buffer), "a buffer", 8);
109 /* create a buffer with CRC checking */
110 fail_unless (gst_dp_header_from_buffer (buffer, GST_DP_HEADER_FLAG_CRC,
111 &header_length, &header), "Could not create header from buffer.");
113 /* validate the header */
114 fail_unless (gst_dp_validate_header (header_length, header),
115 "Could not validate header");
116 /* create a new, empty buffer with the right size */
117 newbuffer = gst_dp_buffer_from_header (header_length, header);
118 fail_unless (newbuffer != NULL, "Could not create a new buffer from header");
119 fail_unless (GST_IS_BUFFER (newbuffer), "Created buffer is not a GstBuffer");
120 /* read/copy the data */
121 memmove (GST_BUFFER_DATA (newbuffer), GST_BUFFER_DATA (buffer),
122 GST_BUFFER_SIZE (buffer));
123 /* validate the buffer */
124 fail_unless (gst_dp_validate_payload (header_length, header,
125 GST_BUFFER_DATA (newbuffer)), "Could not validate payload");
127 g_message ("new buffer timestamp: %" GST_TIME_FORMAT "\n",
128 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (newbuffer)));
129 g_message ("new buffer duration: %" GST_TIME_FORMAT "\n",
130 GST_TIME_ARGS (GST_BUFFER_DURATION (newbuffer)));
131 g_message ("new buffer offset: %" G_GUINT64_FORMAT "\n",
132 GST_BUFFER_OFFSET (newbuffer));
133 g_message ("new buffer offset_end: %" G_GUINT64_FORMAT "\n",
134 GST_BUFFER_OFFSET_END (newbuffer));
135 fail_unless (GST_BUFFER_TIMESTAMP (newbuffer) ==
136 GST_BUFFER_TIMESTAMP (buffer), "Timestamps don't match !");
137 fail_unless (GST_BUFFER_DURATION (newbuffer) == GST_BUFFER_DURATION (buffer),
138 "Durations don't match !");
139 fail_unless (GST_BUFFER_OFFSET (newbuffer) == GST_BUFFER_OFFSET (buffer),
140 "Offsets don't match !");
141 fail_unless (GST_BUFFER_OFFSET_END (newbuffer) ==
142 GST_BUFFER_OFFSET_END (buffer), "Offset ends don't match !");
143 fail_unless (GST_BUFFER_FLAG_IS_SET (newbuffer, GST_BUFFER_FLAG_IN_CAPS),
144 "GST_BUFFER_IN_CAPS flag should have been copied !");
147 gst_buffer_unref (buffer);
148 gst_buffer_unref (newbuffer);
156 GST_START_TEST (test_caps)
158 gchar *string, *newstring;
159 GstCaps *caps, *newcaps;
162 guint8 *header, *payload;
164 caps = gst_caps_from_string ("audio/x-raw-float, "
165 "rate = (int) [ 11025, 48000 ], "
166 "channels = (int) [ 1, 2 ], " "endianness = (int) BYTE_ORDER, "
167 "width = (int) 32, " "buffer-frames = (int) 0");
168 string = gst_caps_to_string (caps);
169 g_message ("Created caps: %s\n", string);
170 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_length, &header,
171 &payload), "Could not create packet from caps.");
173 /* validate the packet */
174 fail_unless (gst_dp_validate_packet (header_length, header, payload),
175 "Could not validate packet");
176 newcaps = gst_dp_caps_from_packet (header_length, header, payload);
177 fail_unless (newcaps != NULL, "Could not create caps from packet");
178 fail_unless (GST_IS_CAPS (newcaps));
179 newstring = gst_caps_to_string (newcaps);
180 g_message ("Received caps: %s\n", newstring);
181 fail_unless (strcmp (string, newstring) == 0,
182 "Created caps do not match original caps");
185 gst_caps_unref (caps);
186 gst_caps_unref (newcaps);
195 GST_START_TEST (test_event)
200 guint8 *header, *payload;
202 g_message ("Testing EOS event at 1s\n");
203 send = gst_event_new_eos ();
204 GST_EVENT_TIMESTAMP (send) = GST_SECOND;
205 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
206 &header_length, &header, &payload),
207 "Could not create packet from eos event");
209 receive = gst_dp_event_from_packet (header_length, header, payload);
211 g_message ("EOS, timestamp %" GST_TIME_FORMAT "\n",
212 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
213 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_EOS,
214 "Received event is not EOS");
215 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND,
216 "EOS timestamp is not 1.0 sec");
221 gst_event_unref (send);
222 gst_event_unref (receive);
224 g_message ("Testing FLUSH event at 2s\n");
225 send = gst_event_new_flush_start ();
226 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 2;
227 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
228 &header_length, &header, &payload),
229 "Could not create packet from flush event");
231 receive = gst_dp_event_from_packet (header_length, header, payload);
233 g_message ("Flush, timestamp %" GST_TIME_FORMAT "\n",
234 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
235 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_FLUSH_START,
236 "Received event is not flush");
237 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 2,
238 "Flush timestamp is not 2.0 sec");
243 gst_event_unref (send);
244 gst_event_unref (receive);
246 g_message ("Testing SEEK event with 1 second at 3 seconds\n");
248 gst_event_new_seek (1.0, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_SET,
249 GST_SECOND, GST_SEEK_TYPE_NONE, 0);
250 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 3;
251 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
252 &header_length, &header, &payload),
253 "Could not create packet from seek event");
255 receive = gst_dp_event_from_packet (header_length, header, payload);
261 GstSeekType cur_type, stop_type;
264 gst_event_parse_seek (receive, &rate, &format, &flags,
265 &cur_type, &cur, &stop_type, &stop);
267 g_message ("Seek, timestamp %" GST_TIME_FORMAT ", to %" GST_TIME_FORMAT
268 "\n", GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)),
269 GST_TIME_ARGS (cur));
270 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_SEEK,
271 "Returned event is not seek");
272 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 3,
273 "Seek timestamp is not 3.0 sec");
274 fail_unless (format == GST_FORMAT_TIME, "Seek format is not time");
275 fail_unless (cur == GST_SECOND, "Seek cur is not 1.0 sec");
281 gst_event_unref (send);
282 gst_event_unref (receive);
287 /* try to segfault the thing by passing NULLs, short headers, etc.. */
288 GST_START_TEST (test_memory)
298 /* check 0 sized input, data pointer can be NULL or anything. CRC is always 0,
300 fail_if (gst_dp_crc (NULL, 0) != 0);
301 fail_if (gst_dp_crc (foo, 0) != 0);
303 /* this is very invalid input and gives a warning. */
304 ASSERT_CRITICAL (gst_dp_crc (NULL, 1));
305 ASSERT_CRITICAL (gst_dp_header_payload_length (NULL));
306 ASSERT_CRITICAL (gst_dp_header_payload_type (NULL));
309 ASSERT_CRITICAL (gst_dp_header_from_buffer (NULL, 0, NULL, NULL));
311 /* empty buffer has NULL as data pointer */
312 buffer = gst_buffer_new_and_alloc (0);
314 /* no place to store the length and/or header data */
315 ASSERT_CRITICAL (gst_dp_header_from_buffer (buffer, 0, NULL, NULL));
316 ASSERT_CRITICAL (gst_dp_header_from_buffer (buffer, 0, &length, NULL));
318 /* this should work fine */
319 fail_if (gst_dp_header_from_buffer (buffer, 0, &length, &header) != TRUE);
320 fail_unless (length != 0);
321 fail_unless (header != NULL);
323 /* this should validate */
324 fail_if (gst_dp_validate_header (length, header) == FALSE);
326 /* NULL header pointer */
327 ASSERT_CRITICAL (gst_dp_validate_header (length, NULL));
329 ASSERT_CRITICAL (gst_dp_validate_header (5, header));
333 /* this should work and not crash trying to calc a CRC on a 0 sized buffer */
334 fail_if (gst_dp_header_from_buffer (buffer,
335 GST_DP_HEADER_FLAG_CRC_HEADER | GST_DP_HEADER_FLAG_CRC_PAYLOAD,
336 &length, &header) != TRUE);
338 /* this should validate */
339 fail_if (gst_dp_validate_header (length, header) == FALSE);
341 /* there was no payload, NULL as payload data should validate the CRC
343 fail_if (gst_dp_validate_payload (length, header, NULL) == FALSE);
345 /* and the whole packet as well */
346 fail_if (gst_dp_validate_packet (length, header, NULL) == FALSE);
348 /* some bogus length */
349 ASSERT_CRITICAL (gst_dp_validate_packet (5, header, NULL));
350 gst_buffer_unref (buffer);
352 /* create buffer from header data, integrity tested elsewhere */
353 buffer = gst_dp_buffer_from_header (length, header);
354 fail_if (buffer == NULL);
355 gst_buffer_unref (buffer);
358 ASSERT_CRITICAL (gst_dp_packet_from_caps (NULL, 0, NULL, NULL, NULL));
360 /* some caps stuff */
361 caps = gst_caps_new_empty ();
362 ASSERT_CRITICAL (gst_dp_packet_from_caps (caps, 0, NULL, NULL, NULL));
363 ASSERT_CRITICAL (gst_dp_packet_from_caps (caps, 0, &length, NULL, NULL));
364 ASSERT_CRITICAL (gst_dp_packet_from_caps (caps, 0, &length, &header, NULL));
366 fail_if (gst_dp_packet_from_caps (caps, 0, &length, &header,
368 fail_if (strcmp ((const gchar *) payload, "EMPTY") != 0);
369 gst_caps_unref (caps);
371 caps = gst_dp_caps_from_packet (length, header, payload);
372 fail_if (caps == NULL);
373 gst_caps_unref (caps);
378 /* some event stuff */
379 event = gst_event_new_eos ();
380 ASSERT_CRITICAL (gst_dp_packet_from_event (event, 0, NULL, NULL, NULL));
381 ASSERT_CRITICAL (gst_dp_packet_from_event (event, 0, &length, NULL, NULL));
382 ASSERT_CRITICAL (gst_dp_packet_from_event (event, 0, &length, &header, NULL));
384 /* payload is not NULL from previous test and points to freed memory, very
386 fail_if (payload == NULL);
387 fail_if (gst_dp_packet_from_event (event, 0, &length, &header,
390 /* the EOS event has no payload */
391 fail_if (payload != NULL);
392 gst_event_unref (event);
394 event = gst_dp_event_from_packet (length, header, payload);
395 fail_if (event == NULL);
396 fail_if (GST_EVENT_TYPE (event) != GST_EVENT_EOS);
397 gst_event_unref (event);
408 Suite *s = suite_create ("data protocol");
409 TCase *tc_chain = tcase_create ("general");
411 suite_add_tcase (s, tc_chain);
412 tcase_add_test (tc_chain, test_conversion);
413 #ifndef HAVE_CPU_PPC64
414 tcase_add_test (tc_chain, test_buffer);
416 tcase_add_test (tc_chain, test_caps);
417 tcase_add_test (tc_chain, test_event);
418 tcase_add_test (tc_chain, test_memory);
423 GST_CHECK_MAIN (gst_dp);