1 | /* GLib testing framework examples and tests |
2 | * Copyright (C) 2010 Collabora Ltd. |
3 | * Authors: Xavier Claessens <xclaesse@gmail.com> |
4 | * |
5 | * This work is provided "as is"; redistribution and modification |
6 | * in whole or in part, in any medium, physical or electronic is |
7 | * permitted without restriction. |
8 | * |
9 | * This work is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
12 | * |
13 | * In no event shall the authors or contributors be liable for any |
14 | * direct, indirect, incidental, special, exemplary, or consequential |
15 | * damages (including, but not limited to, procurement of substitute |
16 | * goods or services; loss of use, data, or profits; or business |
17 | * interruption) however caused and on any theory of liability, whether |
18 | * in contract, strict liability, or tort (including negligence or |
19 | * otherwise) arising in any way out of the use of this software, even |
20 | * if advised of the possibility of such damage. |
21 | */ |
22 | |
23 | #include <glib/glib.h> |
24 | #include <gio/gio.h> |
25 | #include <stdlib.h> |
26 | #include <string.h> |
27 | |
28 | typedef struct |
29 | { |
30 | GMainLoop *main_loop; |
31 | const gchar *data1; |
32 | const gchar *data2; |
33 | GIOStream *iostream1; |
34 | GIOStream *iostream2; |
35 | } TestCopyChunksData; |
36 | |
37 | static void |
38 | test_copy_chunks_splice_cb (GObject *source_object, |
39 | GAsyncResult *res, |
40 | gpointer user_data) |
41 | { |
42 | TestCopyChunksData *data = user_data; |
43 | GMemoryOutputStream *ostream; |
44 | gchar *received_data; |
45 | GError *error = NULL; |
46 | |
47 | g_io_stream_splice_finish (result: res, error: &error); |
48 | g_assert_no_error (error); |
49 | |
50 | ostream = G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data->iostream1)); |
51 | received_data = g_memory_output_stream_get_data (ostream); |
52 | g_assert_cmpstr (received_data, ==, data->data2); |
53 | |
54 | ostream = G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data->iostream2)); |
55 | received_data = g_memory_output_stream_get_data (ostream); |
56 | g_assert_cmpstr (received_data, ==, data->data1); |
57 | |
58 | g_assert (g_io_stream_is_closed (data->iostream1)); |
59 | g_assert (g_io_stream_is_closed (data->iostream2)); |
60 | |
61 | g_main_loop_quit (loop: data->main_loop); |
62 | } |
63 | |
64 | static void |
65 | test_copy_chunks (void) |
66 | { |
67 | TestCopyChunksData data; |
68 | GInputStream *istream; |
69 | GOutputStream *ostream; |
70 | |
71 | data.main_loop = g_main_loop_new (NULL, FALSE); |
72 | data.data1 = "abcdefghijklmnopqrstuvwxyz" ; |
73 | data.data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; |
74 | |
75 | istream = g_memory_input_stream_new_from_data (data: data.data1, len: -1, NULL); |
76 | ostream = g_memory_output_stream_new (NULL, size: 0, realloc_function: g_realloc, destroy_function: g_free); |
77 | data.iostream1 = g_simple_io_stream_new (input_stream: istream, output_stream: ostream); |
78 | g_object_unref (object: istream); |
79 | g_object_unref (object: ostream); |
80 | |
81 | istream = g_memory_input_stream_new_from_data (data: data.data2, len: -1, NULL); |
82 | ostream = g_memory_output_stream_new (NULL, size: 0, realloc_function: g_realloc, destroy_function: g_free); |
83 | data.iostream2 = g_simple_io_stream_new (input_stream: istream, output_stream: ostream); |
84 | g_object_unref (object: istream); |
85 | g_object_unref (object: ostream); |
86 | |
87 | g_io_stream_splice_async (stream1: data.iostream1, stream2: data.iostream2, |
88 | flags: G_IO_STREAM_SPLICE_CLOSE_STREAM1 | G_IO_STREAM_SPLICE_CLOSE_STREAM2 | |
89 | G_IO_STREAM_SPLICE_WAIT_FOR_BOTH, G_PRIORITY_DEFAULT, |
90 | NULL, callback: test_copy_chunks_splice_cb, user_data: &data); |
91 | |
92 | /* We do not hold a ref in data struct, this is to make sure the operation |
93 | * keeps the iostream objects alive until it finishes */ |
94 | g_object_unref (object: data.iostream1); |
95 | g_object_unref (object: data.iostream2); |
96 | |
97 | g_main_loop_run (loop: data.main_loop); |
98 | g_main_loop_unref (loop: data.main_loop); |
99 | } |
100 | |
101 | static void |
102 | close_async_done (GObject *source, |
103 | GAsyncResult *result, |
104 | gpointer user_data) |
105 | { |
106 | gboolean *done = user_data; |
107 | |
108 | *done = TRUE; |
109 | } |
110 | |
111 | static void |
112 | test_close_file (void) |
113 | { |
114 | #ifdef G_OS_UNIX |
115 | GFileIOStream *fios; |
116 | gboolean done; |
117 | GIOStream *io; |
118 | GFile *file; |
119 | |
120 | file = g_file_new_for_path (path: "/dev/null" ); |
121 | fios = g_file_open_readwrite (file, NULL, NULL); |
122 | g_object_unref (object: file); |
123 | g_assert (fios); |
124 | |
125 | io = g_simple_io_stream_new (input_stream: g_io_stream_get_input_stream (G_IO_STREAM (fios)), |
126 | output_stream: g_io_stream_get_output_stream (G_IO_STREAM (fios))); |
127 | g_object_unref (object: fios); |
128 | |
129 | g_io_stream_close_async (stream: io, io_priority: 0, NULL, callback: close_async_done, user_data: &done); |
130 | g_object_unref (object: io); |
131 | |
132 | done = FALSE; |
133 | while (!done) |
134 | g_main_context_iteration (NULL, TRUE); |
135 | #endif |
136 | } |
137 | |
138 | static void |
139 | test_close_memory (void) |
140 | { |
141 | GInputStream *in; |
142 | GOutputStream *out; |
143 | gboolean done; |
144 | GIOStream *io; |
145 | |
146 | in = g_memory_input_stream_new (); |
147 | out = g_memory_output_stream_new_resizable (); |
148 | io = g_simple_io_stream_new (input_stream: in, output_stream: out); |
149 | g_object_unref (object: out); |
150 | g_object_unref (object: in); |
151 | |
152 | g_io_stream_close_async (stream: io, io_priority: 0, NULL, callback: close_async_done, user_data: &done); |
153 | g_object_unref (object: io); |
154 | |
155 | done = FALSE; |
156 | while (!done) |
157 | g_main_context_iteration (NULL, TRUE); |
158 | } |
159 | |
160 | int |
161 | main (int argc, |
162 | char *argv[]) |
163 | { |
164 | g_test_init (argc: &argc, argv: &argv, NULL); |
165 | |
166 | g_test_add_func (testpath: "/io-stream/copy-chunks" , test_func: test_copy_chunks); |
167 | g_test_add_func (testpath: "/io-stream/close/async/memory" , test_func: test_close_memory); |
168 | g_test_add_func (testpath: "/io-stream/close/async/file" , test_func: test_close_file); |
169 | |
170 | return g_test_run(); |
171 | } |
172 | |