Install all test data
[platform/upstream/glib.git] / gio / tests / async-splice-output-stream.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2010-2012 Collabora Ltd.
3  * Authors: Xavier Claessens <xclaesse@gmail.com>
4  *          Mike Ruprecht <mike.ruprecht@collabora.co.uk>
5  *
6  * This work is provided "as is"; redistribution and modification
7  * in whole or in part, in any medium, physical or electronic is
8  * permitted without restriction.
9  *
10  * This work is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * In no event shall the authors or contributors be liable for any
15  * direct, indirect, incidental, special, exemplary, or consequential
16  * damages (including, but not limited to, procurement of substitute
17  * goods or services; loss of use, data, or profits; or business
18  * interruption) however caused and on any theory of liability, whether
19  * in contract, strict liability, or tort (including negligence or
20  * otherwise) arising in any way out of the use of this software, even
21  * if advised of the possibility of such damage.
22  */
23
24 #include <glib/glib.h>
25 #include <glib/gstdio.h>
26 #include <gio/gio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 typedef enum
31 {
32   TEST_THREADED_NONE    = 0,
33   TEST_THREADED_ISTREAM = 1,
34   TEST_THREADED_OSTREAM = 2,
35   TEST_THREADED_BOTH    = TEST_THREADED_ISTREAM | TEST_THREADED_OSTREAM,
36 } TestThreadedFlags;
37
38 typedef struct
39 {
40   GMainLoop *main_loop;
41   const gchar *data;
42   GInputStream *istream;
43   GOutputStream *ostream;
44   TestThreadedFlags flags;
45   gchar *input_path;
46   gchar *output_path;
47 } TestCopyChunksData;
48
49 static void
50 test_copy_chunks_splice_cb (GObject      *source,
51                             GAsyncResult *res,
52                             gpointer      user_data)
53 {
54   TestCopyChunksData *data = user_data;
55   gchar *received_data;
56   GError *error = NULL;
57   gssize bytes_spliced;
58
59   bytes_spliced = g_output_stream_splice_finish (G_OUTPUT_STREAM (source),
60                                                  res, &error);
61   g_assert_no_error (error);
62   g_assert_cmpint (bytes_spliced, ==, strlen (data->data));
63
64   if (data->flags & TEST_THREADED_OSTREAM)
65     {
66       gsize length = 0;
67
68       g_file_get_contents (data->output_path, &received_data,
69                            &length, &error);
70       g_assert_no_error (error);
71       g_assert_cmpstr (received_data, ==, data->data);
72       g_free (received_data);
73     }
74   else
75     {
76       received_data = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->ostream));
77       g_assert_cmpstr (received_data, ==, data->data);
78     }
79
80   g_assert (g_input_stream_is_closed (data->istream));
81   g_assert (g_output_stream_is_closed (data->ostream));
82
83   if (data->flags & TEST_THREADED_ISTREAM)
84     {
85       g_unlink (data->input_path);
86       g_free (data->input_path);
87     }
88
89   if (data->flags & TEST_THREADED_OSTREAM)
90     {
91       g_unlink (data->output_path);
92       g_free (data->output_path);
93     }
94
95   g_main_loop_quit (data->main_loop);
96 }
97
98 static void
99 test_copy_chunks_start (TestThreadedFlags flags)
100 {
101   TestCopyChunksData data;
102   GError *error = NULL;
103
104   data.main_loop = g_main_loop_new (NULL, FALSE);
105   data.data = "abcdefghijklmnopqrstuvwxyz";
106   data.flags = flags;
107
108   if (data.flags & TEST_THREADED_ISTREAM)
109     {
110       GFile *file;
111       GFileIOStream *stream;
112
113       file = g_file_new_tmp ("test-inputXXXXXX", &stream, &error);
114       g_assert_no_error (error);
115       g_object_unref (stream);
116       data.input_path = g_file_get_path (file);
117       g_file_set_contents (data.input_path,
118                            data.data, strlen (data.data),
119                            &error);
120       g_assert_no_error (error);
121       data.istream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
122       g_assert_no_error (error);
123       g_object_unref (file);
124     }
125   else
126     {
127       data.istream = g_memory_input_stream_new_from_data (data.data, -1, NULL);
128     }
129
130   if (data.flags & TEST_THREADED_OSTREAM)
131     {
132       GFile *file;
133       GFileIOStream *stream;
134
135       file = g_file_new_tmp ("test-outputXXXXXX", &stream, &error);
136       g_assert_no_error (error);
137       g_object_unref (stream);
138       data.output_path = g_file_get_path (file);
139       data.ostream = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE,
140                                                       G_FILE_CREATE_NONE,
141                                                       NULL, &error));
142       g_assert_no_error (error);
143       g_object_unref (file);
144     }
145   else
146     {
147       data.ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
148     }
149
150   g_output_stream_splice_async (data.ostream, data.istream,
151                                 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
152                                 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
153                                 G_PRIORITY_DEFAULT, NULL,
154                                 test_copy_chunks_splice_cb, &data);
155
156   /* We do not hold a ref in data struct, this is to make sure the operation
157    * keeps the iostream objects alive until it finishes
158    */
159   g_object_unref (data.istream);
160   g_object_unref (data.ostream);
161
162   g_main_loop_run (data.main_loop);
163   g_main_loop_unref (data.main_loop);
164 }
165
166 static void
167 test_copy_chunks (void)
168 {
169   test_copy_chunks_start (TEST_THREADED_NONE);
170 }
171
172 static void
173 test_copy_chunks_threaded_input (void)
174 {
175   test_copy_chunks_start (TEST_THREADED_ISTREAM);
176 }
177
178 static void
179 test_copy_chunks_threaded_output (void)
180 {
181   test_copy_chunks_start (TEST_THREADED_OSTREAM);
182 }
183
184 static void
185 test_copy_chunks_threaded (void)
186 {
187   test_copy_chunks_start (TEST_THREADED_BOTH);
188 }
189
190 int
191 main (int   argc,
192       char *argv[])
193 {
194   g_test_init (&argc, &argv, NULL);
195
196   g_test_add_func ("/async-splice/copy-chunks", test_copy_chunks);
197   g_test_add_func ("/async-splice/copy-chunks-threaded-input",
198                    test_copy_chunks_threaded_input);
199   g_test_add_func ("/async-splice/copy-chunks-threaded-output",
200                    test_copy_chunks_threaded_output);
201   g_test_add_func ("/async-splice/copy-chunks-threaded",
202                    test_copy_chunks_threaded);
203
204   return g_test_run();
205 }