3 * unit test for avisubtitle
5 * Copyright (C) <2007> Thijs Vermeir <thijsvermeir@gmail.com>
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.
22 /* Element-Checklist-Version: 5 */
27 #include <gst/check/gstcheck.h>
32 guint8 avisub_utf_8_with_bom[] = {
33 0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
34 0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
35 0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
36 0x00, 0x00, 0x00, 0x04, 0x00, 0x8e, 0x00, 0x00,
37 0x00, 0xef, 0xbb, 0xbf, 0x31, 0x0d, 0x0a, 0x30,
38 0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x2c,
39 0x31, 0x30, 0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20,
40 0x30, 0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32,
41 0x2c, 0x30, 0x30, 0x30, 0x0d, 0x0a, 0x3c, 0x62,
42 0x3e, 0x41, 0x6e, 0x20, 0x55, 0x54, 0x46, 0x38,
43 0x20, 0x53, 0x75, 0x62, 0x74, 0x69, 0x74, 0x6c,
44 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x42,
45 0x4f, 0x4d, 0x3c, 0x2f, 0x62, 0x3e, 0x0d, 0x0a,
46 0x0d, 0x0a, 0x32, 0x0d, 0x0a, 0x30, 0x30, 0x3a,
47 0x30, 0x30, 0x3a, 0x30, 0x32, 0x2c, 0x31, 0x30,
48 0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20, 0x30, 0x30,
49 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x34, 0x2c, 0x30,
50 0x30, 0x30, 0x0d, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
51 0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x6e, 0x6f,
52 0x6e, 0x41, 0x53, 0x43, 0x49, 0x49, 0x20, 0x2d,
53 0x20, 0xc2, 0xb5, 0xc3, 0xb6, 0xc3, 0xa4, 0xc3,
54 0xbc, 0xc3, 0x9f, 0x0d, 0x0a, 0x0d, 0x0a
57 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("application/x-subtitle")
63 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
66 GST_STATIC_CAPS ("application/x-subtitle-avi")
70 setup_avisubtitle (void)
72 GstElement *avisubtitle;
75 GST_DEBUG ("setup_avisubtitle");
76 avisubtitle = gst_check_setup_element ("avisubtitle");
77 caps = gst_caps_new_simple ("application/x-subtitle", NULL);
78 mysinkpad = gst_check_setup_sink_pad (avisubtitle, &sink_template, caps);
79 gst_caps_unref (caps);
80 caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
81 mysrcpad = gst_check_setup_src_pad (avisubtitle, &src_template, caps);
82 gst_caps_unref (caps);
83 gst_pad_set_active (mysinkpad, TRUE);
84 gst_pad_set_active (mysrcpad, TRUE);
89 cleanup_avisubtitle (GstElement * avisubtitle)
91 gst_pad_set_active (mysinkpad, FALSE);
92 gst_pad_set_active (mysrcpad, FALSE);
93 gst_check_teardown_sink_pad (avisubtitle);
94 gst_check_teardown_src_pad (avisubtitle);
95 gst_check_teardown_element (avisubtitle);
99 check_wrong_buffer (guint8 * data, guint length)
101 GstBuffer *buffer = gst_buffer_new ();
102 GstElement *avisubtitle = setup_avisubtitle ();
104 gst_buffer_set_data (buffer, data, length);
105 fail_unless (gst_element_set_state (avisubtitle,
106 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
107 "could not set to playing");
108 gst_buffer_ref (buffer);
109 ASSERT_BUFFER_REFCOUNT (buffer, "inbuffer", 2);
110 /* push the broken buffer */
111 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_ERROR,
112 "accepted a broken buffer");
113 /* check if we have unreffed this buffer on failure */
114 ASSERT_BUFFER_REFCOUNT (buffer, "inbuffer", 1);
115 gst_buffer_unref (buffer);
116 fail_unless (gst_element_set_state (avisubtitle,
117 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
118 cleanup_avisubtitle (avisubtitle);
122 check_correct_buffer (guint8 * src_data, guint src_size, guint8 * dst_data,
125 GstBuffer *buffer = gst_buffer_new ();
126 GstBuffer *newBuffer;
127 GstElement *avisubtitle = setup_avisubtitle ();
130 fail_unless (g_list_length (buffers) == 0, "Buffers list needs to be empty");
131 gst_buffer_set_data (buffer, src_data, src_size);
132 fail_unless (gst_element_set_state (avisubtitle,
133 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
134 "could not set to playing");
135 ASSERT_BUFFER_REFCOUNT (buffer, "inbuffer", 1);
136 event = gst_event_new_seek (1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
137 GST_SEEK_TYPE_SET, 2 * GST_SECOND, GST_SEEK_TYPE_SET, 5 * GST_SECOND);
138 fail_unless (gst_element_send_event (avisubtitle, event) == FALSE,
139 "Seeking is not possible when there is no buffer yet");
140 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK,
141 "not accepted a correct buffer");
142 /* we gave away our reference to the buffer, don't assume anything */
144 /* a new buffer is created in the list */
145 fail_unless (g_list_length (buffers) == 1,
146 "No new buffer in the buffers list");
147 event = gst_event_new_seek (1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
148 GST_SEEK_TYPE_SET, 2 * GST_SECOND, GST_SEEK_TYPE_SET, 5 * GST_SECOND);
149 fail_unless (gst_element_send_event (avisubtitle, event) == TRUE,
150 "seeking should be working now");
151 fail_unless (g_list_length (buffers) == 2,
152 "After seeking we need another buffer in the buffers");
153 newBuffer = GST_BUFFER (buffers->data);
154 buffers = g_list_remove (buffers, newBuffer);
155 fail_unless (g_list_length (buffers) == 1, "Buffers list needs to be empty");
156 fail_unless (GST_BUFFER_SIZE (newBuffer) == dst_size,
157 "size of the new buffer is wrong ( %d != %d)",
158 GST_BUFFER_SIZE (newBuffer), dst_size);
159 fail_unless (memcmp (GST_BUFFER_DATA (newBuffer), dst_data, dst_size) == 0,
160 "data of the buffer is not correct");
161 gst_buffer_unref (newBuffer);
162 /* free the buffer from seeking */
163 gst_buffer_unref (GST_BUFFER (buffers->data));
164 buffers = g_list_remove (buffers, buffers->data);
165 fail_unless (gst_element_set_state (avisubtitle,
166 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
167 cleanup_avisubtitle (avisubtitle);
171 GST_START_TEST (test_avisubtitle_negative)
173 guint8 wrong_magic[] =
174 { 0x47, 0x41, 0x41, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177 guint8 wrong_fixed_word_2[] = {
178 0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x01, 0x10,
179 0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
180 0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68
182 guint8 wrong_length_after_name[] = {
183 0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
184 0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
185 0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68
187 guint8 wrong_fixed_word_4[] = {
188 0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
189 0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
190 0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
191 0x00, 0x00, 0x00, 0x04, 0x01, 0x8e, 0x00, 0x00,
192 0x00, 0xef, 0xbb, 0xbf, 0x31, 0x0d, 0x0a, 0x30
194 guint8 wrong_total_length[] = {
195 0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
196 0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
197 0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
198 0x00, 0x00, 0x00, 0x04, 0x00, 0x8e, 0x00, 0x00,
199 0x00, 0xef, 0xbb, 0xbf, 0x31, 0x0d, 0x0a, 0x30
201 /* size of the buffer must be larger than 11 */
202 check_wrong_buffer (avisub_utf_8_with_bom, 11);
203 /* buffer must start with 'GAB2\0' */
204 check_wrong_buffer (wrong_magic, 14);
205 /* next word must be 2 */
206 check_wrong_buffer (wrong_fixed_word_2, 24);
207 /* length must be larger than the length of the name + 17 */
208 check_wrong_buffer (wrong_length_after_name, 24);
209 /* next word must be 4 */
210 check_wrong_buffer (wrong_fixed_word_4, 36);
211 /* check wrong total length */
212 check_wrong_buffer (wrong_total_length, 36);
217 GST_START_TEST (test_avisubtitle_positive)
219 guint8 avisub_utf_8_without_bom[] = {
220 0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
221 0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
222 0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
223 0x00, 0x00, 0x00, 0x04, 0x00, 0x8b, 0x00, 0x00,
224 0x00, 0x31, 0x0d, 0x0a, 0x30,
225 0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x2c,
226 0x31, 0x30, 0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20,
227 0x30, 0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32,
228 0x2c, 0x30, 0x30, 0x30, 0x0d, 0x0a, 0x3c, 0x62,
229 0x3e, 0x41, 0x6e, 0x20, 0x55, 0x54, 0x46, 0x38,
230 0x20, 0x53, 0x75, 0x62, 0x74, 0x69, 0x74, 0x6c,
231 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x42,
232 0x4f, 0x4d, 0x3c, 0x2f, 0x62, 0x3e, 0x0d, 0x0a,
233 0x0d, 0x0a, 0x32, 0x0d, 0x0a, 0x30, 0x30, 0x3a,
234 0x30, 0x30, 0x3a, 0x30, 0x32, 0x2c, 0x31, 0x30,
235 0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20, 0x30, 0x30,
236 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x34, 0x2c, 0x30,
237 0x30, 0x30, 0x0d, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
238 0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x6e, 0x6f,
239 0x6e, 0x41, 0x53, 0x43, 0x49, 0x49, 0x20, 0x2d,
240 0x20, 0xc2, 0xb5, 0xc3, 0xb6, 0xc3, 0xa4, 0xc3,
241 0xbc, 0xc3, 0x9f, 0x0d, 0x0a, 0x0d, 0x0a
243 check_correct_buffer (avisub_utf_8_with_bom, 175, avisub_utf_8_with_bom + 36,
245 check_correct_buffer (avisub_utf_8_without_bom, 172,
246 avisub_utf_8_without_bom + 33, 139);
252 avisubtitle_suite (void)
254 Suite *s = suite_create ("avisubtitle");
255 TCase *tc_chain = tcase_create ("general");
257 suite_add_tcase (s, tc_chain);
258 tcase_add_test (tc_chain, test_avisubtitle_negative);
259 tcase_add_test (tc_chain, test_avisubtitle_positive);
264 GST_CHECK_MAIN (avisubtitle);