Add some more cases to the app-id unit tests
[glib.git] / gio / tests / io-stream.c
blob90c735790b4ea68bff8fd02d63a351607453c3c8
1 /* GLib testing framework examples and tests
2 * Copyright (C) 2010 Collabora Ltd.
3 * Authors: Xavier Claessens <xclaesse@gmail.com>
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.
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.
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.
23 #include <glib/glib.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 typedef struct
30 GMainLoop *main_loop;
31 const gchar *data1;
32 const gchar *data2;
33 GIOStream *iostream1;
34 GIOStream *iostream2;
35 } TestCopyChunksData;
37 static void
38 test_copy_chunks_splice_cb (GObject *source_object,
39 GAsyncResult *res,
40 gpointer user_data)
42 TestCopyChunksData *data = user_data;
43 GMemoryOutputStream *ostream;
44 gchar *received_data;
45 GError *error = NULL;
47 g_io_stream_splice_finish (res, &error);
48 g_assert_no_error (error);
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);
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);
58 g_assert (g_io_stream_is_closed (data->iostream1));
59 g_assert (g_io_stream_is_closed (data->iostream2));
61 g_main_loop_quit (data->main_loop);
64 static void
65 test_copy_chunks (void)
67 TestCopyChunksData data;
68 GInputStream *istream;
69 GOutputStream *ostream;
71 data.main_loop = g_main_loop_new (NULL, FALSE);
72 data.data1 = "abcdefghijklmnopqrstuvwxyz";
73 data.data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
75 istream = g_memory_input_stream_new_from_data (data.data1, -1, NULL);
76 ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
77 data.iostream1 = g_simple_io_stream_new (istream, ostream);
78 g_object_unref (istream);
79 g_object_unref (ostream);
81 istream = g_memory_input_stream_new_from_data (data.data2, -1, NULL);
82 ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
83 data.iostream2 = g_simple_io_stream_new (istream, ostream);
84 g_object_unref (istream);
85 g_object_unref (ostream);
87 g_io_stream_splice_async (data.iostream1, data.iostream2,
88 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, test_copy_chunks_splice_cb, &data);
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 (data.iostream1);
95 g_object_unref (data.iostream2);
97 g_main_loop_run (data.main_loop);
98 g_main_loop_unref (data.main_loop);
101 static void
102 close_async_done (GObject *source,
103 GAsyncResult *result,
104 gpointer user_data)
106 gboolean *done = user_data;
108 *done = TRUE;
111 static void
112 test_close_file (void)
114 #ifdef G_OS_UNIX
115 GFileIOStream *fios;
116 gboolean done;
117 GIOStream *io;
118 GFile *file;
120 file = g_file_new_for_path ("/dev/null");
121 fios = g_file_open_readwrite (file, NULL, NULL);
122 g_object_unref (file);
123 g_assert (fios);
125 io = g_simple_io_stream_new (g_io_stream_get_input_stream (G_IO_STREAM (fios)),
126 g_io_stream_get_output_stream (G_IO_STREAM (fios)));
127 g_object_unref (fios);
129 g_io_stream_close_async (io, 0, NULL, close_async_done, &done);
130 g_object_unref (io);
132 done = FALSE;
133 while (!done)
134 g_main_context_iteration (NULL, TRUE);
135 #endif
138 static void
139 test_close_memory (void)
141 GInputStream *in;
142 GOutputStream *out;
143 gboolean done;
144 GIOStream *io;
146 in = g_memory_input_stream_new ();
147 out = g_memory_output_stream_new_resizable ();
148 io = g_simple_io_stream_new (in, out);
149 g_object_unref (out);
150 g_object_unref (in);
152 g_io_stream_close_async (io, 0, NULL, close_async_done, &done);
153 g_object_unref (io);
155 done = FALSE;
156 while (!done)
157 g_main_context_iteration (NULL, TRUE);
161 main (int argc,
162 char *argv[])
164 g_test_init (&argc, &argv, NULL);
166 g_test_add_func ("/io-stream/copy-chunks", test_copy_chunks);
167 g_test_add_func ("/io-stream/close/async/memory", test_close_memory);
168 g_test_add_func ("/io-stream/close/async/file", test_close_file);
170 return g_test_run();