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