a734a22206c63d2c89622ffcc83087f53fb9c04d
[platform/upstream/gstreamer.git] / tests / check / libs / gdp.c
1 /* GStreamer
2  *
3  * unit test for data protocol
4  *
5  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include "config.h"
24
25 #include <gst/check/gstcheck.h>
26
27 #include <gst/dataprotocol/dataprotocol.h>
28 #include "libs/gst/dataprotocol/dp-private.h"   /* private header */
29
30 /* test our method of reading and writing headers using TO/FROM_BE */
31 GST_START_TEST (test_conversion)
32 {
33   guint8 array[9];
34   guint8 write_array[9];
35   guint16 read_two, expect_two;
36   guint32 read_four, expect_four;
37   guint64 read_eight, expect_eight;
38   int i;
39
40   for (i = 0; i < 9; ++i) {
41     array[i] = i * 0x10;
42   }
43
44   /* read 8 16 bits */
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);
50   }
51
52   /* write 8 16 bits */
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);
57   }
58
59   /* read 5 32 bits */
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);
66   }
67
68   /* read 2 64 bits */
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);
78   }
79
80   /* write 1 64 bit */
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");
84 }
85
86 GST_END_TEST;
87
88 #ifndef GST_DISABLE_DEPRECATED  /* these tests use deprecated API, that we disable by default */
89
90 #ifndef HAVE_CPU_PPC64          /* this test doesn't work on PPC64. See #348114 */
91
92 /* test creation of header from buffer and back again */
93 GST_START_TEST (test_buffer)
94 {
95   GstBuffer *buffer;
96   GstBuffer *newbuffer;
97
98   guint header_length;
99   guint8 *header;
100
101   /* create 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);
110
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.");
114
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");
128
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 !");
147
148   /* clean up */
149   gst_buffer_unref (buffer);
150   gst_buffer_unref (newbuffer);
151   g_free (header);
152 }
153
154 GST_END_TEST;
155 #endif
156
157 GST_START_TEST (test_caps)
158 {
159   gchar *string, *newstring;
160   GstCaps *caps, *newcaps;
161
162   guint header_length;
163   guint8 *header, *payload;
164
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.");
173
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");
184
185   /* cleanup */
186   gst_caps_unref (caps);
187   gst_caps_unref (newcaps);
188   g_free (header);
189   g_free (payload);
190   g_free (string);
191   g_free (newstring);
192 }
193
194 GST_END_TEST;
195
196 GST_START_TEST (test_event)
197 {
198   GstEvent *send;
199   GstEvent *receive;
200   guint header_length;
201   guint8 *header, *payload;
202
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");
209
210   receive = gst_dp_event_from_packet (header_length, header, payload);
211
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");
218
219   /* clean up */
220   g_free (header);
221   g_free (payload);
222   gst_event_unref (send);
223   gst_event_unref (receive);
224
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");
231
232   receive = gst_dp_event_from_packet (header_length, header, payload);
233
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");
240
241   /* clean up */
242   g_free (header);
243   g_free (payload);
244   gst_event_unref (send);
245   gst_event_unref (receive);
246
247   g_message ("Testing SEEK event with 1 second at 3 seconds\n");
248   send =
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");
255
256   receive = gst_dp_event_from_packet (header_length, header, payload);
257
258   {
259     gdouble rate;
260     GstFormat format;
261     GstSeekFlags flags;
262     GstSeekType cur_type, stop_type;
263     gint64 cur, stop;
264
265     gst_event_parse_seek (receive, &rate, &format, &flags,
266         &cur_type, &cur, &stop_type, &stop);
267
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");
277   }
278
279   /* clean up */
280   g_free (header);
281   g_free (payload);
282   gst_event_unref (send);
283   gst_event_unref (receive);
284 }
285
286 GST_END_TEST;
287
288 /* try to segfault the thing by passing NULLs, short headers, etc.. */
289 GST_START_TEST (test_memory)
290 {
291   guint8 foo[5];
292   GstBuffer *buffer;
293   GstCaps *caps;
294   GstEvent *event;
295   guint length;
296   guint8 *header;
297   guint8 *payload;
298
299   /* check 0 sized input, data pointer can be NULL or anything. CRC is always 0,
300    * though. */
301   fail_if (gst_dp_crc (NULL, 0) != 0);
302   fail_if (gst_dp_crc (foo, 0) != 0);
303
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));
308
309   /* wrong */
310   ASSERT_CRITICAL (gst_dp_header_from_buffer (NULL, 0, NULL, NULL));
311
312   /* empty buffer has NULL as data pointer */
313   buffer = gst_buffer_new_and_alloc (0);
314
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));
318
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);
323
324   /* this should validate */
325   fail_if (gst_dp_validate_header (length, header) == FALSE);
326
327   /* NULL header pointer */
328   ASSERT_CRITICAL (gst_dp_validate_header (length, NULL));
329   /* short header */
330   ASSERT_CRITICAL (gst_dp_validate_header (5, header));
331
332   g_free (header);
333
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);
338
339   /* this should validate */
340   fail_if (gst_dp_validate_header (length, header) == FALSE);
341
342   /* there was no payload, NULL as payload data should validate the CRC
343    * checks and all. */
344   fail_if (gst_dp_validate_payload (length, header, NULL) == FALSE);
345
346   /* and the whole packet as well */
347   fail_if (gst_dp_validate_packet (length, header, NULL) == FALSE);
348
349   /* some bogus length */
350   ASSERT_CRITICAL (gst_dp_validate_packet (5, header, NULL));
351   gst_buffer_unref (buffer);
352
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);
357   g_free (header);
358
359   ASSERT_CRITICAL (gst_dp_packet_from_caps (NULL, 0, NULL, NULL, NULL));
360
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));
366
367   fail_if (gst_dp_packet_from_caps (caps, 0, &length, &header,
368           &payload) != TRUE);
369   fail_if (strcmp ((const gchar *) payload, "EMPTY") != 0);
370   gst_caps_unref (caps);
371
372   caps = gst_dp_caps_from_packet (length, header, payload);
373   fail_if (caps == NULL);
374   gst_caps_unref (caps);
375
376   g_free (header);
377   g_free (payload);
378
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));
384
385   /* payload is not NULL from previous test and points to freed memory, very
386    * invalid. */
387   fail_if (payload == NULL);
388   fail_if (gst_dp_packet_from_event (event, 0, &length, &header,
389           &payload) != TRUE);
390
391   /* the EOS event has no payload */
392   fail_if (payload != NULL);
393   gst_event_unref (event);
394
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);
399
400   g_free (header);
401   g_free (payload);
402 }
403
404 GST_END_TEST;
405
406 #endif
407
408 Suite *
409 gst_dp_suite (void)
410 {
411   Suite *s = suite_create ("data protocol");
412   TCase *tc_chain = tcase_create ("general");
413
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);
419 #endif
420   tcase_add_test (tc_chain, test_caps);
421   tcase_add_test (tc_chain, test_event);
422   tcase_add_test (tc_chain, test_memory);
423 #endif
424
425   return s;
426 }
427
428 GST_CHECK_MAIN (gst_dp);