2 * Copyright © 2009 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * See the included COPYING file for more information.
11 * Author: Ryan Lortie <desrt@desrt.ca>
15 #include <glib/glib.h>
18 /* GFilterInputStream and GFilterOutputStream are abstract, so define
19 * minimal subclasses for testing. (This used to use
20 * GBufferedInputStream and GBufferedOutputStream, but those have
21 * their own test program, and they override some methods, meaning the
22 * core filter stream functionality wasn't getting fully tested.)
25 GType
test_filter_input_stream_get_type (void);
26 GType
test_filter_output_stream_get_type (void);
28 #define TEST_TYPE_FILTER_INPUT_STREAM (test_filter_input_stream_get_type ())
29 #define TEST_FILTER_INPUT_STREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TEST_TYPE_FILTER_INPUT_STREAM, TestFilterInputStream))
30 #define TEST_TYPE_FILTER_OUTPUT_STREAM (test_filter_output_stream_get_type ())
31 #define TEST_FILTER_OUTPUT_STREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TEST_TYPE_FILTER_OUTPUT_STREAM, TestFilterOutputStream))
33 typedef GFilterInputStream TestFilterInputStream
;
34 typedef GFilterInputStreamClass TestFilterInputStreamClass
;
35 typedef GFilterOutputStream TestFilterOutputStream
;
36 typedef GFilterOutputStreamClass TestFilterOutputStreamClass
;
38 G_DEFINE_TYPE (TestFilterInputStream
, test_filter_input_stream
, G_TYPE_FILTER_INPUT_STREAM
)
39 G_DEFINE_TYPE (TestFilterOutputStream
, test_filter_output_stream
, G_TYPE_FILTER_OUTPUT_STREAM
)
42 test_filter_input_stream_init (TestFilterInputStream
*stream
)
47 test_filter_input_stream_class_init (TestFilterInputStreamClass
*klass
)
52 test_filter_output_stream_init (TestFilterOutputStream
*stream
)
57 test_filter_output_stream_class_init (TestFilterOutputStreamClass
*klass
)
64 test_input_filter (void)
66 GInputStream
*base
, *f1
, *f2
, *s
;
71 g_test_bug ("568394");
72 base
= g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL
);
73 f1
= g_object_new (TEST_TYPE_FILTER_INPUT_STREAM
,
75 "close-base-stream", FALSE
,
77 f2
= g_object_new (TEST_TYPE_FILTER_INPUT_STREAM
,
81 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f1
)) == base
);
82 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f2
)) == base
);
84 g_assert (!g_input_stream_is_closed (base
));
85 g_assert (!g_input_stream_is_closed (f1
));
86 g_assert (!g_input_stream_is_closed (f2
));
89 "close-base-stream", &close_base
,
92 g_assert (!close_base
);
98 g_assert (!g_input_stream_is_closed (base
));
99 g_assert (!g_input_stream_is_closed (f2
));
101 g_input_stream_skip (f2
, 3, NULL
, &error
);
102 g_assert_no_error (error
);
104 memset (buf
, 0, 1024);
105 g_input_stream_read_all (f2
, buf
, 1024, NULL
, NULL
, &error
);
106 g_assert_no_error (error
);
107 g_assert_cmpstr (buf
, ==, "defghijk");
111 g_assert (g_input_stream_is_closed (base
));
113 g_object_unref (base
);
117 test_output_filter (void)
119 GOutputStream
*base
, *f1
, *f2
;
121 base
= g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
122 f1
= g_object_new (TEST_TYPE_FILTER_OUTPUT_STREAM
,
124 "close-base-stream", FALSE
,
126 f2
= g_object_new (TEST_TYPE_FILTER_OUTPUT_STREAM
,
130 g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f1
)) == base
);
131 g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f2
)) == base
);
133 g_assert (!g_output_stream_is_closed (base
));
134 g_assert (!g_output_stream_is_closed (f1
));
135 g_assert (!g_output_stream_is_closed (f2
));
139 g_assert (!g_output_stream_is_closed (base
));
140 g_assert (!g_output_stream_is_closed (f2
));
144 g_assert (g_output_stream_is_closed (base
));
146 g_object_unref (base
);
149 gpointer expected_obj
;
150 gpointer expected_data
;
151 gboolean callback_happened
;
155 return_result_cb (GObject
*object
,
156 GAsyncResult
*result
,
159 GAsyncResult
**ret
= user_data
;
161 *ret
= g_object_ref (result
);
162 g_main_loop_quit (loop
);
166 in_cb (GObject
*object
,
167 GAsyncResult
*result
,
170 GError
*error
= NULL
;
172 g_assert (object
== expected_obj
);
173 g_assert (user_data
== expected_data
);
174 g_assert (callback_happened
== FALSE
);
176 g_input_stream_close_finish (expected_obj
, result
, &error
);
177 g_assert (error
== NULL
);
179 callback_happened
= TRUE
;
180 g_main_loop_quit (loop
);
184 test_input_async (void)
186 GInputStream
*base
, *f1
, *f2
;
188 GAsyncResult
*result
= NULL
;
189 GError
*error
= NULL
;
191 loop
= g_main_loop_new (NULL
, FALSE
);
193 base
= g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyz", -1, NULL
);
194 f1
= g_object_new (TEST_TYPE_FILTER_INPUT_STREAM
,
196 "close-base-stream", FALSE
,
198 f2
= g_object_new (TEST_TYPE_FILTER_INPUT_STREAM
,
202 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f1
)) == base
);
203 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f2
)) == base
);
206 memset (buf
, 0, sizeof (buf
));
207 g_input_stream_read_async (f1
, buf
, 10, G_PRIORITY_DEFAULT
,
208 NULL
, return_result_cb
, &result
);
209 g_main_loop_run (loop
);
210 g_assert_cmpint (g_input_stream_read_finish (f1
, result
, &error
), ==, 10);
211 g_assert_cmpstr (buf
, ==, "abcdefghij");
212 g_assert_no_error (error
);
213 g_clear_object (&result
);
215 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (base
)), ==, 10);
217 g_input_stream_skip_async (f2
, 10, G_PRIORITY_DEFAULT
,
218 NULL
, return_result_cb
, &result
);
219 g_main_loop_run (loop
);
220 g_assert_cmpint (g_input_stream_skip_finish (f2
, result
, &error
), ==, 10);
221 g_assert_no_error (error
);
222 g_clear_object (&result
);
224 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (base
)), ==, 20);
226 memset (buf
, 0, sizeof (buf
));
227 g_input_stream_read_async (f1
, buf
, 10, G_PRIORITY_DEFAULT
,
228 NULL
, return_result_cb
, &result
);
229 g_main_loop_run (loop
);
230 g_assert_cmpint (g_input_stream_read_finish (f1
, result
, &error
), ==, 6);
231 g_assert_cmpstr (buf
, ==, "uvwxyz");
232 g_assert_no_error (error
);
233 g_clear_object (&result
);
235 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (base
)), ==, 26);
238 g_assert (!g_input_stream_is_closed (base
));
239 g_assert (!g_input_stream_is_closed (f1
));
240 g_assert (!g_input_stream_is_closed (f2
));
243 expected_data
= g_malloc (20);
244 callback_happened
= FALSE
;
245 g_input_stream_close_async (f1
, 0, NULL
, in_cb
, expected_data
);
247 g_assert (callback_happened
== FALSE
);
248 g_main_loop_run (loop
);
249 g_assert (callback_happened
== TRUE
);
251 g_assert (!g_input_stream_is_closed (base
));
252 g_assert (!g_input_stream_is_closed (f2
));
253 g_free (expected_data
);
255 g_assert (!g_input_stream_is_closed (base
));
256 g_assert (!g_input_stream_is_closed (f2
));
259 expected_data
= g_malloc (20);
260 callback_happened
= FALSE
;
261 g_input_stream_close_async (f2
, 0, NULL
, in_cb
, expected_data
);
263 g_assert (callback_happened
== FALSE
);
264 g_main_loop_run (loop
);
265 g_assert (callback_happened
== TRUE
);
267 g_assert (g_input_stream_is_closed (base
));
268 g_assert (g_input_stream_is_closed (f2
));
269 g_free (expected_data
);
272 g_assert (g_input_stream_is_closed (base
));
273 g_object_unref (base
);
274 g_main_loop_unref (loop
);
278 out_cb (GObject
*object
,
279 GAsyncResult
*result
,
282 GError
*error
= NULL
;
284 g_assert (object
== expected_obj
);
285 g_assert (user_data
== expected_data
);
286 g_assert (callback_happened
== FALSE
);
288 g_output_stream_close_finish (expected_obj
, result
, &error
);
289 g_assert (error
== NULL
);
291 callback_happened
= TRUE
;
292 g_main_loop_quit (loop
);
296 test_output_async (void)
298 GOutputStream
*base
, *f1
, *f2
;
299 GAsyncResult
*result
= NULL
;
300 GError
*error
= NULL
;
302 loop
= g_main_loop_new (NULL
, FALSE
);
304 base
= g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
305 f1
= g_object_new (TEST_TYPE_FILTER_OUTPUT_STREAM
,
307 "close-base-stream", FALSE
,
309 f2
= g_object_new (TEST_TYPE_FILTER_OUTPUT_STREAM
,
313 g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f1
)) == base
);
314 g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f2
)) == base
);
317 g_output_stream_write_async (f1
, "abcdefghijklm", 13, G_PRIORITY_DEFAULT
,
318 NULL
, return_result_cb
, &result
);
319 g_main_loop_run (loop
);
320 g_assert_cmpint (g_output_stream_write_finish (f1
, result
, &error
), ==, 13);
321 g_assert_no_error (error
);
322 g_clear_object (&result
);
324 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (base
)), ==, 13);
326 g_output_stream_write_async (f2
, "nopqrstuvwxyz", 13, G_PRIORITY_DEFAULT
,
327 NULL
, return_result_cb
, &result
);
328 g_main_loop_run (loop
);
329 g_assert_cmpint (g_output_stream_write_finish (f2
, result
, &error
), ==, 13);
330 g_assert_no_error (error
);
331 g_clear_object (&result
);
333 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (base
)), ==, 26);
335 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base
)), ==, 26);
336 g_output_stream_write (base
, "\0", 1, NULL
, &error
);
337 g_assert_no_error (error
);
338 g_assert_cmpstr (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (base
)), ==, "abcdefghijklmnopqrstuvwxyz");
341 g_assert (!g_output_stream_is_closed (base
));
342 g_assert (!g_output_stream_is_closed (f1
));
343 g_assert (!g_output_stream_is_closed (f2
));
346 expected_data
= g_malloc (20);
347 callback_happened
= FALSE
;
348 g_output_stream_close_async (f1
, 0, NULL
, out_cb
, expected_data
);
350 g_assert (callback_happened
== FALSE
);
351 g_main_loop_run (loop
);
352 g_assert (callback_happened
== TRUE
);
354 g_assert (!g_output_stream_is_closed (base
));
355 g_assert (!g_output_stream_is_closed (f2
));
356 g_free (expected_data
);
358 g_assert (!g_output_stream_is_closed (base
));
359 g_assert (!g_output_stream_is_closed (f2
));
362 expected_data
= g_malloc (20);
363 callback_happened
= FALSE
;
364 g_output_stream_close_async (f2
, 0, NULL
, out_cb
, expected_data
);
366 g_assert (callback_happened
== FALSE
);
367 g_main_loop_run (loop
);
368 g_assert (callback_happened
== TRUE
);
370 g_assert (g_output_stream_is_closed (base
));
371 g_assert (g_output_stream_is_closed (f2
));
372 g_free (expected_data
);
375 g_assert (g_output_stream_is_closed (base
));
376 g_object_unref (base
);
377 g_main_loop_unref (loop
);
381 main (int argc
, char **argv
)
383 g_test_init (&argc
, &argv
, NULL
);
384 g_test_bug_base ("http://bugzilla.gnome.org/");
386 g_test_add_func ("/filter-stream/input", test_input_filter
);
387 g_test_add_func ("/filter-stream/output", test_output_filter
);
388 g_test_add_func ("/filter-stream/async-input", test_input_async
);
389 g_test_add_func ("/filter-stream/async-output", test_output_async
);