filesrc: fix buffer leaks in tests
[platform/upstream/gstreamer.git] / tests / check / elements / filesrc.c
1 /* GStreamer
2  *
3  * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #include <gst/check/gstcheck.h>
27
28 static gboolean have_eos = FALSE;
29 static GCond eos_cond;
30 static GMutex event_mutex;
31
32 static GstPad *mysinkpad;
33
34 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS_ANY);
38
39 static gboolean
40 event_func (GstPad * pad, GstObject * parent, GstEvent * event)
41 {
42   gboolean res = TRUE;
43
44   g_mutex_lock (&event_mutex);
45   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
46     have_eos = TRUE;
47     GST_DEBUG ("signal EOS");
48     g_cond_broadcast (&eos_cond);
49   }
50   g_mutex_unlock (&event_mutex);
51
52   gst_event_unref (event);
53
54   return res;
55 }
56
57 static void
58 wait_eos (void)
59 {
60   g_mutex_lock (&event_mutex);
61   GST_DEBUG ("waiting for EOS");
62   while (!have_eos) {
63     g_cond_wait (&eos_cond, &event_mutex);
64   }
65   GST_DEBUG ("received EOS");
66   g_mutex_unlock (&event_mutex);
67 }
68
69 static GstElement *
70 setup_filesrc (void)
71 {
72   GstElement *filesrc;
73
74   GST_DEBUG ("setup_filesrc");
75   filesrc = gst_check_setup_element ("filesrc");
76   mysinkpad = gst_check_setup_sink_pad (filesrc, &sinktemplate);
77   gst_pad_set_event_function (mysinkpad, event_func);
78   gst_pad_set_active (mysinkpad, TRUE);
79
80   return filesrc;
81 }
82
83 static void
84 cleanup_filesrc (GstElement * filesrc)
85 {
86   gst_check_drop_buffers ();
87   gst_pad_set_active (mysinkpad, FALSE);
88   gst_check_teardown_sink_pad (filesrc);
89   gst_check_teardown_element (filesrc);
90 }
91
92 GST_START_TEST (test_seeking)
93 {
94   GstElement *src;
95   GstQuery *seeking_query;
96   gboolean seekable;
97
98 #ifndef TESTFILE
99 #error TESTFILE not defined
100 #endif
101   src = setup_filesrc ();
102
103   g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
104   fail_unless (gst_element_set_state (src,
105           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
106       "could not set to paused");
107
108   /* Test that filesrc is seekable with a file fd */
109   fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
110       != NULL);
111   fail_unless (gst_element_query (src, seeking_query) == TRUE);
112   gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
113   fail_unless (seekable == TRUE);
114   gst_query_unref (seeking_query);
115
116   fail_unless (gst_element_set_state (src,
117           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
118
119   /* cleanup */
120   cleanup_filesrc (src);
121 }
122
123 GST_END_TEST;
124
125 GST_START_TEST (test_reverse)
126 {
127   GstElement *src;
128
129 #ifndef TESTFILE
130 #error TESTFILE not defined
131 #endif
132   src = setup_filesrc ();
133
134   g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
135   /* we're going to perform the seek in ready */
136   fail_unless (gst_element_set_state (src,
137           GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS,
138       "could not set to ready");
139
140   /* reverse seek from end to start */
141   gst_element_seek (src, -1.0, GST_FORMAT_BYTES, 0, GST_SEEK_TYPE_SET, 100,
142       GST_SEEK_TYPE_SET, -1);
143
144   fail_unless (gst_element_set_state (src,
145           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
146       "could not set to paused");
147
148   /* wait for EOS */
149   wait_eos ();
150
151   fail_unless (gst_element_set_state (src,
152           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
153
154   /* cleanup */
155   cleanup_filesrc (src);
156 }
157
158 GST_END_TEST;
159
160 GST_START_TEST (test_pull)
161 {
162   GstElement *src;
163   GstQuery *seeking_query;
164   gboolean res, seekable;
165   gint64 start, stop;
166   GstPad *pad;
167   GstFlowReturn ret;
168   GstBuffer *buffer1, *buffer2;
169   GstMapInfo info1, info2;
170
171   src = setup_filesrc ();
172
173   g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
174   fail_unless (gst_element_set_state (src,
175           GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS,
176       "could not set to ready");
177
178   /* get the source pad */
179   pad = gst_element_get_static_pad (src, "src");
180   fail_unless (pad != NULL);
181
182   /* activate the pad in pull mode */
183   res = gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE);
184   fail_unless (res == TRUE);
185
186   /* not start playing */
187   fail_unless (gst_element_set_state (src,
188           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
189       "could not set to paused");
190
191   /* Test that filesrc is seekable with a file fd */
192   fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
193       != NULL);
194   fail_unless (gst_element_query (src, seeking_query) == TRUE);
195
196   /* get the seeking capabilities */
197   gst_query_parse_seeking (seeking_query, NULL, &seekable, &start, &stop);
198   fail_unless (seekable == TRUE);
199   fail_unless (start == 0);
200   fail_unless (start != -1);
201   gst_query_unref (seeking_query);
202
203   /* do some pulls */
204   buffer1 = NULL;
205   ret = gst_pad_get_range (pad, 0, 100, &buffer1);
206   fail_unless (ret == GST_FLOW_OK);
207   fail_unless (buffer1 != NULL);
208   fail_unless (gst_buffer_get_size (buffer1) == 100);
209
210   buffer2 = NULL;
211   ret = gst_pad_get_range (pad, 0, 50, &buffer2);
212   fail_unless (ret == GST_FLOW_OK);
213   fail_unless (buffer2 != NULL);
214   fail_unless (gst_buffer_get_size (buffer2) == 50);
215
216   /* this should be the same */
217   fail_unless (gst_buffer_map (buffer1, &info1, GST_MAP_READ));
218   fail_unless (gst_buffer_map (buffer2, &info2, GST_MAP_READ));
219   fail_unless (memcmp (info1.data, info2.data, 50) == 0);
220   gst_buffer_unmap (buffer2, &info2);
221
222   gst_buffer_unref (buffer2);
223
224   /* read next 50 bytes */
225   buffer2 = NULL;
226   ret = gst_pad_get_range (pad, 50, 50, &buffer2);
227   fail_unless (ret == GST_FLOW_OK);
228   fail_unless (buffer2 != NULL);
229   fail_unless (gst_buffer_get_size (buffer2) == 50);
230
231   /* compare with previously read data */
232   fail_unless (gst_buffer_map (buffer2, &info2, GST_MAP_READ));
233   fail_unless (memcmp ((guint8 *) info1.data + 50, info2.data, 50) == 0);
234   gst_buffer_unmap (buffer2, &info2);
235
236   gst_buffer_unmap (buffer1, &info1);
237   gst_buffer_unref (buffer1);
238   gst_buffer_unref (buffer2);
239
240   /* read 10 bytes at end-10 should give exactly 10 bytes */
241   buffer1 = NULL;
242   ret = gst_pad_get_range (pad, stop - 10, 10, &buffer1);
243   fail_unless (ret == GST_FLOW_OK);
244   fail_unless (buffer1 != NULL);
245   fail_unless (gst_buffer_get_size (buffer1) == 10);
246   gst_buffer_unref (buffer1);
247
248   /* read 20 bytes at end-10 should give exactly 10 bytes */
249   buffer1 = NULL;
250   ret = gst_pad_get_range (pad, stop - 10, 20, &buffer1);
251   fail_unless (ret == GST_FLOW_OK);
252   fail_unless (buffer1 != NULL);
253   fail_unless (gst_buffer_get_size (buffer1) == 10);
254   gst_buffer_unref (buffer1);
255
256   /* read 0 bytes at end-1 should return 0 bytes */
257   buffer1 = NULL;
258   ret = gst_pad_get_range (pad, stop - 1, 0, &buffer1);
259   fail_unless (ret == GST_FLOW_OK);
260   fail_unless (buffer1 != NULL);
261   fail_unless (gst_buffer_get_size (buffer1) == 0);
262   gst_buffer_unref (buffer1);
263
264   /* read 10 bytes at end-1 should return 1 byte */
265   buffer1 = NULL;
266   ret = gst_pad_get_range (pad, stop - 1, 10, &buffer1);
267   fail_unless (ret == GST_FLOW_OK);
268   fail_unless (buffer1 != NULL);
269   fail_unless (gst_buffer_get_size (buffer1) == 1);
270   gst_buffer_unref (buffer1);
271
272   /* read 0 bytes at end should EOS */
273   buffer1 = NULL;
274   ret = gst_pad_get_range (pad, stop, 0, &buffer1);
275   fail_unless (ret == GST_FLOW_EOS);
276
277   /* read 10 bytes before end should EOS */
278   buffer1 = NULL;
279   ret = gst_pad_get_range (pad, stop, 10, &buffer1);
280   fail_unless (ret == GST_FLOW_EOS);
281
282   /* read 0 bytes after end should EOS */
283   buffer1 = NULL;
284   ret = gst_pad_get_range (pad, stop + 10, 0, &buffer1);
285   fail_unless (ret == GST_FLOW_EOS);
286
287   /* read 10 bytes after end should EOS too */
288   buffer1 = NULL;
289   ret = gst_pad_get_range (pad, stop + 10, 10, &buffer1);
290   fail_unless (ret == GST_FLOW_EOS);
291
292   fail_unless (gst_element_set_state (src,
293           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
294
295   /* cleanup */
296   gst_object_unref (pad);
297   cleanup_filesrc (src);
298 }
299
300 GST_END_TEST;
301
302 GST_START_TEST (test_coverage)
303 {
304   GstElement *src;
305   gchar *location;
306   GstBus *bus;
307   GstMessage *message;
308
309   src = setup_filesrc ();
310   bus = gst_bus_new ();
311
312   gst_element_set_bus (src, bus);
313
314   g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
315   g_object_get (G_OBJECT (src), "location", &location, NULL);
316   fail_unless_equals_string (location, "/i/do/not/exist");
317   g_free (location);
318   fail_unless (gst_element_set_state (src,
319           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE,
320       "could set to playing with wrong location");
321
322   /* a state change and an error */
323   fail_if ((message = gst_bus_pop (bus)) == NULL);
324   gst_message_unref (message);
325   fail_if ((message = gst_bus_pop (bus)) == NULL);
326   fail_unless_message_error (message, RESOURCE, NOT_FOUND);
327   gst_message_unref (message);
328
329   g_object_set (G_OBJECT (src), "location", NULL, NULL);
330   g_object_get (G_OBJECT (src), "location", &location, NULL);
331   fail_if (location);
332
333   /* cleanup */
334   gst_element_set_bus (src, NULL);
335   gst_object_unref (GST_OBJECT (bus));
336   cleanup_filesrc (src);
337 }
338
339 GST_END_TEST;
340
341 GST_START_TEST (test_uri_interface)
342 {
343   GstElement *src;
344   gchar *location;
345   GstBus *bus;
346   GstPad *pad;
347
348   src = setup_filesrc ();
349   bus = gst_bus_new ();
350
351   gst_element_set_bus (src, bus);
352
353   g_object_set (G_OBJECT (src), "location", NULL, NULL);
354   g_object_get (G_OBJECT (src), "location", &location, NULL);
355   fail_unless (location == NULL);
356
357   g_object_set (G_OBJECT (src), "location", "/i/do/not/exist", NULL);
358   g_object_get (G_OBJECT (src), "location", &location, NULL);
359   fail_unless_equals_string (location, "/i/do/not/exist");
360   g_free (location);
361
362   location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
363   fail_unless_equals_string (location, "file:///i/do/not/exist");
364   g_free (location);
365
366   /* should accept file:///foo/bar URIs */
367   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
368           "file:///foo/bar", NULL));
369   location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
370   fail_unless_equals_string (location, "file:///foo/bar");
371   g_free (location);
372   g_object_get (G_OBJECT (src), "location", &location, NULL);
373   fail_unless_equals_string (location, "/foo/bar");
374   g_free (location);
375
376   /* should accept file://localhost/foo/bar URIs */
377   fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
378           "file://localhost/foo/baz", NULL));
379   location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
380   fail_unless_equals_string (location, "file:///foo/baz");
381   g_free (location);
382   g_object_get (G_OBJECT (src), "location", &location, NULL);
383   fail_unless_equals_string (location, "/foo/baz");
384   g_free (location);
385
386   /* should escape non-uri characters for the URI but not for the location */
387   g_object_set (G_OBJECT (src), "location", "/foo/b?r", NULL);
388   g_object_get (G_OBJECT (src), "location", &location, NULL);
389   fail_unless_equals_string (location, "/foo/b?r");
390   g_free (location);
391   location = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
392   fail_unless_equals_string (location, "file:///foo/b%3Fr");
393   g_free (location);
394
395   /* should fail with other hostnames */
396   fail_if (gst_uri_handler_set_uri (GST_URI_HANDLER (src),
397           "file://hostname/foo/foo", NULL));
398
399   g_object_set (G_OBJECT (src), "location", TESTFILE, NULL);
400
401   pad = gst_element_get_static_pad (src, "src");
402   fail_unless (pad != NULL);
403   fail_unless (gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE));
404   gst_object_unref (pad);
405
406   fail_unless (gst_element_set_state (src,
407           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
408       "could not set to playing");
409
410   ASSERT_WARNING (g_object_set (G_OBJECT (src), "location", "/wrong", NULL));
411   g_object_get (G_OBJECT (src), "location", &location, NULL);
412   fail_unless_equals_string (location, TESTFILE);
413   g_free (location);
414
415   fail_unless (gst_element_set_state (src,
416           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
417
418   /* cleanup */
419   gst_element_set_bus (src, NULL);
420   gst_object_unref (GST_OBJECT (bus));
421   cleanup_filesrc (src);
422 }
423
424 GST_END_TEST;
425
426 #ifdef G_OS_UNIX
427 static void
428 check_uri_for_uri (GstElement * e, const gchar * in_uri, const gchar * uri)
429 {
430   GstQuery *query;
431   gchar *query_uri = NULL;
432
433   gst_uri_handler_set_uri (GST_URI_HANDLER (e), in_uri, NULL);
434
435   query = gst_query_new_uri ();
436   fail_unless (gst_element_query (e, query));
437   gst_query_parse_uri (query, &query_uri);
438   gst_query_unref (query);
439
440   if (uri != NULL) {
441     fail_unless_equals_string (query_uri, uri);
442   } else {
443     gchar *fn;
444
445     fail_unless (gst_uri_is_valid (query_uri));
446     fn = g_filename_from_uri (query_uri, NULL, NULL);
447     fail_unless (g_path_is_absolute (fn));
448     fail_unless (fn != NULL);
449     g_free (fn);
450   }
451
452   g_free (query_uri);
453 }
454
455 static void
456 check_uri_for_location (GstElement * e, const gchar * location,
457     const gchar * uri)
458 {
459   GstQuery *query;
460   gchar *query_uri = NULL;
461
462   g_object_set (e, "location", location, NULL);
463   query = gst_query_new_uri ();
464   fail_unless (gst_element_query (e, query));
465   gst_query_parse_uri (query, &query_uri);
466   gst_query_unref (query);
467
468   if (uri != NULL) {
469     fail_unless_equals_string (query_uri, uri);
470   } else {
471     gchar *fn;
472
473     fail_unless (gst_uri_is_valid (query_uri));
474     fn = g_filename_from_uri (query_uri, NULL, NULL);
475     fail_unless (g_path_is_absolute (fn));
476     fail_unless (fn != NULL);
477     g_free (fn);
478   }
479
480   g_free (query_uri);
481 }
482 #endif
483
484 GST_START_TEST (test_uri_query)
485 {
486   GstElement *src;
487
488   src = setup_filesrc ();
489
490 #ifdef G_OS_UNIX
491   {
492     GST_INFO ("*nix");
493     check_uri_for_location (src, "/i/do/not/exist", "file:///i/do/not/exist");
494     check_uri_for_location (src, "/i/do/not/../exist", "file:///i/do/exist");
495     check_uri_for_location (src, "/i/do/not/.././exist", "file:///i/do/exist");
496     check_uri_for_location (src, "/i/./do/not/../exist", "file:///i/do/exist");
497     check_uri_for_location (src, "/i/do/./not/../exist", "file:///i/do/exist");
498     check_uri_for_location (src, "/i/do/not/./../exist", "file:///i/do/exist");
499     check_uri_for_location (src, "/i/./do/./././././exist",
500         "file:///i/do/exist");
501     check_uri_for_location (src, "/i/do/not/../../exist", "file:///i/exist");
502     check_uri_for_location (src, "/i/../not/../exist", "file:///exist");
503     /* hard to test relative URIs, just make sure it returns an URI of sorts */
504     check_uri_for_location (src, "foo", NULL);
505     check_uri_for_location (src, "foo/../bar", NULL);
506     check_uri_for_location (src, "./foo", NULL);
507     check_uri_for_location (src, "../foo", NULL);
508     check_uri_for_location (src, "foo/./bar", NULL);
509     /* make sure non-ASCII characters are escaped properly (U+00F6 here) */
510     check_uri_for_location (src, "/i/./d\303\266/not/../exist",
511         "file:///i/d%C3%B6/exist");
512     /* let's see what happens if we set a malformed URI with ISO-8859-1 chars,
513      * i.e. one that the input characters haven't been escaped properly. We
514      * should get back a properly escaped URI */
515     check_uri_for_uri (src, "file:///M\366t\366r", "file:///M%F6t%F6r");
516   }
517 #endif
518
519   cleanup_filesrc (src);
520 }
521
522 GST_END_TEST;
523
524 static Suite *
525 filesrc_suite (void)
526 {
527   Suite *s = suite_create ("filesrc");
528   TCase *tc_chain = tcase_create ("general");
529
530   suite_add_tcase (s, tc_chain);
531   tcase_add_test (tc_chain, test_seeking);
532   tcase_add_test (tc_chain, test_reverse);
533   tcase_add_test (tc_chain, test_pull);
534   tcase_add_test (tc_chain, test_coverage);
535   tcase_add_test (tc_chain, test_uri_interface);
536   tcase_add_test (tc_chain, test_uri_query);
537
538   return s;
539 }
540
541 GST_CHECK_MAIN (filesrc);