4 * Purple is the legal property of its developers, whose names are too
5 * numerous to list here. Please refer to the COPYRIGHT file distributed
6 * with this source distribution
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include <queuedoutputstream.h>
28 static const gsize test_bytes_data_len
= 5;
29 static const guint8 test_bytes_data
[] = "12345";
31 static const gsize test_bytes_data_len2
= 4;
32 static const guint8 test_bytes_data2
[] = "6789";
34 static const gsize test_bytes_data_len3
= 12;
35 static const guint8 test_bytes_data3
[] = "101112131415";
38 test_queued_output_stream_new(void) {
39 GOutputStream
*output
;
40 PurpleQueuedOutputStream
*queued
;
43 output
= g_memory_output_stream_new_resizable();
44 g_assert_nonnull(output
);
46 queued
= purple_queued_output_stream_new(output
);
47 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued
));
49 g_assert_true(g_output_stream_close(
50 G_OUTPUT_STREAM(queued
), NULL
, &err
));
51 g_assert_no_error(err
);
53 g_clear_object(&queued
);
54 g_clear_object(&output
);
58 test_queued_output_stream_push_bytes_async_cb(GObject
*source
,
59 GAsyncResult
*res
, gpointer user_data
)
61 PurpleQueuedOutputStream
*queued
= PURPLE_QUEUED_OUTPUT_STREAM(source
);
62 gboolean
*done
= user_data
;
65 g_assert_true(purple_queued_output_stream_push_bytes_finish(queued
,
67 g_assert_no_error(err
);
73 test_queued_output_stream_push_bytes_async(void) {
74 GMemoryOutputStream
*output
;
75 PurpleQueuedOutputStream
*queued
;
78 gboolean done
= FALSE
;
80 output
= G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
81 g_assert_nonnull(output
);
83 queued
= purple_queued_output_stream_new(G_OUTPUT_STREAM(output
));
84 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued
));
86 bytes
= g_bytes_new_static(test_bytes_data
, test_bytes_data_len
);
87 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
88 G_PRIORITY_DEFAULT
, NULL
,
89 test_queued_output_stream_push_bytes_async_cb
, &done
);
93 g_main_context_iteration(NULL
, TRUE
);
96 g_assert_cmpmem(g_memory_output_stream_get_data(output
),
97 g_memory_output_stream_get_data_size(output
),
98 test_bytes_data
, test_bytes_data_len
);
100 g_assert_true(g_output_stream_close(
101 G_OUTPUT_STREAM(queued
), NULL
, &err
));
102 g_assert_no_error(err
);
104 g_clear_object(&queued
);
105 g_clear_object(&output
);
109 test_queued_output_stream_push_bytes_async_multiple_cb(GObject
*source
,
110 GAsyncResult
*res
, gpointer user_data
)
112 PurpleQueuedOutputStream
*queued
= PURPLE_QUEUED_OUTPUT_STREAM(source
);
113 gint
*done
= user_data
;
116 g_assert_true(purple_queued_output_stream_push_bytes_finish(queued
,
118 g_assert_no_error(err
);
124 test_queued_output_stream_push_bytes_async_multiple(void) {
125 GMemoryOutputStream
*output
;
126 PurpleQueuedOutputStream
*queued
;
128 gchar
*all_test_bytes_data
;
132 output
= G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
133 g_assert_nonnull(output
);
135 queued
= purple_queued_output_stream_new(G_OUTPUT_STREAM(output
));
136 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued
));
138 bytes
= g_bytes_new_static(test_bytes_data
, test_bytes_data_len
);
139 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
140 G_PRIORITY_DEFAULT
, NULL
,
141 test_queued_output_stream_push_bytes_async_multiple_cb
,
143 g_bytes_unref(bytes
);
145 bytes
= g_bytes_new_static(test_bytes_data2
, test_bytes_data_len2
);
146 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
147 G_PRIORITY_DEFAULT
, NULL
,
148 test_queued_output_stream_push_bytes_async_multiple_cb
,
150 g_bytes_unref(bytes
);
152 bytes
= g_bytes_new_static(test_bytes_data3
, test_bytes_data_len3
);
153 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
154 G_PRIORITY_DEFAULT
, NULL
,
155 test_queued_output_stream_push_bytes_async_multiple_cb
,
157 g_bytes_unref(bytes
);
160 g_main_context_iteration(NULL
, TRUE
);
163 g_assert_cmpint(done
, ==, 0);
165 all_test_bytes_data
= g_strconcat((const gchar
*)test_bytes_data
,
166 test_bytes_data2
, test_bytes_data3
, NULL
);
168 g_assert_cmpmem(g_memory_output_stream_get_data(output
),
169 g_memory_output_stream_get_data_size(output
),
170 all_test_bytes_data
, strlen(all_test_bytes_data
));
172 g_free(all_test_bytes_data
);
174 g_assert_true(g_output_stream_close(
175 G_OUTPUT_STREAM(queued
), NULL
, &err
));
176 g_assert_no_error(err
);
178 g_clear_object(&queued
);
179 g_clear_object(&output
);
183 test_queued_output_stream_push_bytes_async_error_cb(GObject
*source
,
184 GAsyncResult
*res
, gpointer user_data
)
186 PurpleQueuedOutputStream
*queued
= PURPLE_QUEUED_OUTPUT_STREAM(source
);
187 gint
*done
= user_data
;
190 g_assert_false(purple_queued_output_stream_push_bytes_finish(queued
,
193 g_assert_error(err
, G_IO_ERROR
, G_IO_ERROR_CANCELLED
);
200 test_queued_output_stream_push_bytes_async_error(void) {
201 GMemoryOutputStream
*output
;
202 PurpleQueuedOutputStream
*queued
;
204 GCancellable
*cancellable
;
208 output
= G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
209 g_assert_nonnull(output
);
211 queued
= purple_queued_output_stream_new(G_OUTPUT_STREAM(output
));
212 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued
));
214 cancellable
= g_cancellable_new();
215 g_assert_nonnull(cancellable
);
217 g_cancellable_cancel(cancellable
);
218 g_assert_true(g_cancellable_is_cancelled(cancellable
));
220 bytes
= g_bytes_new_static(test_bytes_data
, test_bytes_data_len
);
222 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
223 G_PRIORITY_DEFAULT
, cancellable
,
224 test_queued_output_stream_push_bytes_async_error_cb
,
227 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
228 G_PRIORITY_DEFAULT
, cancellable
,
229 test_queued_output_stream_push_bytes_async_error_cb
,
232 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
233 G_PRIORITY_DEFAULT
, cancellable
,
234 test_queued_output_stream_push_bytes_async_error_cb
,
237 g_bytes_unref(bytes
);
240 g_main_context_iteration(NULL
, TRUE
);
243 g_assert_cmpint(done
, ==, 0);
245 g_assert_cmpmem(g_memory_output_stream_get_data(output
),
246 g_memory_output_stream_get_data_size(output
),
249 g_assert_true(g_output_stream_close(
250 G_OUTPUT_STREAM(queued
), NULL
, &err
));
251 g_assert_no_error(err
);
253 g_clear_object(&queued
);
254 g_clear_object(&output
);
258 test_queued_output_stream_clear_queue_on_error_cb(GObject
*source
,
259 GAsyncResult
*res
, gpointer user_data
)
261 PurpleQueuedOutputStream
*queued
= PURPLE_QUEUED_OUTPUT_STREAM(source
);
262 gint
*done
= user_data
;
265 g_assert_false(purple_queued_output_stream_push_bytes_finish(queued
,
268 g_assert_error(err
, G_IO_ERROR
, G_IO_ERROR_CANCELLED
);
271 purple_queued_output_stream_clear_queue(queued
);
277 test_queued_output_stream_clear_queue_on_error(void) {
278 GMemoryOutputStream
*output
;
279 PurpleQueuedOutputStream
*queued
;
281 GCancellable
*cancellable
;
285 output
= G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
286 g_assert_nonnull(output
);
288 queued
= purple_queued_output_stream_new(G_OUTPUT_STREAM(output
));
289 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued
));
291 cancellable
= g_cancellable_new();
292 g_assert_nonnull(cancellable
);
294 g_cancellable_cancel(cancellable
);
295 g_assert_true(g_cancellable_is_cancelled(cancellable
));
297 bytes
= g_bytes_new_static(test_bytes_data
, test_bytes_data_len
);
299 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
300 G_PRIORITY_DEFAULT
, cancellable
,
301 test_queued_output_stream_clear_queue_on_error_cb
,
304 /* Don't set cancellable on these */
305 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
306 G_PRIORITY_DEFAULT
, NULL
,
307 test_queued_output_stream_clear_queue_on_error_cb
,
310 purple_queued_output_stream_push_bytes_async(queued
, bytes
,
311 G_PRIORITY_DEFAULT
, NULL
,
312 test_queued_output_stream_clear_queue_on_error_cb
,
315 g_bytes_unref(bytes
);
318 g_main_context_iteration(NULL
, TRUE
);
321 g_assert_cmpint(done
, ==, 0);
323 g_assert_cmpmem(g_memory_output_stream_get_data(output
),
324 g_memory_output_stream_get_data_size(output
),
327 g_assert_true(g_output_stream_close(
328 G_OUTPUT_STREAM(queued
), NULL
, &err
));
329 g_assert_no_error(err
);
331 g_clear_object(&queued
);
332 g_clear_object(&output
);
335 /******************************************************************************
337 *****************************************************************************/
339 main(gint argc
, gchar
**argv
) {
340 g_test_init(&argc
, &argv
, NULL
);
342 g_test_set_nonfatal_assertions();
344 g_test_add_func("/queued-output-stream/new",
345 test_queued_output_stream_new
);
346 g_test_add_func("/queued-output-stream/push-bytes-async",
347 test_queued_output_stream_push_bytes_async
);
348 g_test_add_func("/queued-output-stream/push-bytes-async-multiple",
349 test_queued_output_stream_push_bytes_async_multiple
);
350 g_test_add_func("/queued-output-stream/push-bytes-async-error",
351 test_queued_output_stream_push_bytes_async_error
);
352 g_test_add_func("/queued-output-stream/clear-queue-on-error",
353 test_queued_output_stream_clear_queue_on_error
);