Add g_io_stream_splice_async/finish()
[platform/upstream/glib.git] / gio / tests / io-stream.c
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   GIOStream parent;
31   GInputStream *input_stream;
32   GOutputStream *output_stream;
33 } GTestIOStream;
34
35 typedef struct
36 {
37   GIOStreamClass parent_class;
38 } GTestIOStreamClass;
39
40 G_DEFINE_TYPE (GTestIOStream, g_test_io_stream, G_TYPE_IO_STREAM);
41
42
43 static GInputStream *
44 get_input_stream (GIOStream *io_stream)
45 {
46   GTestIOStream *self =  (GTestIOStream *) io_stream;
47
48   return self->input_stream;
49 }
50
51 static GOutputStream *
52 get_output_stream (GIOStream *io_stream)
53 {
54   GTestIOStream *self =  (GTestIOStream *) io_stream;
55
56   return self->output_stream;
57 }
58
59 static void
60 finalize (GObject *object)
61 {
62   GTestIOStream *self = (GTestIOStream *) object;
63
64   if (self->input_stream != NULL)
65     g_object_unref (self->input_stream);
66
67   if (self->output_stream != NULL)
68     g_object_unref (self->output_stream);
69
70   G_OBJECT_CLASS (g_test_io_stream_parent_class)->finalize (object);
71 }
72
73 static void
74 g_test_io_stream_class_init (GTestIOStreamClass *klass)
75 {
76   GObjectClass *object_class = G_OBJECT_CLASS (klass);
77   GIOStreamClass *io_class = G_IO_STREAM_CLASS (klass);
78
79   object_class->finalize = finalize;
80
81   io_class->get_input_stream = get_input_stream;
82   io_class->get_output_stream = get_output_stream;
83 }
84
85 static void
86 g_test_io_stream_init (GTestIOStream *self)
87 {
88 }
89
90 static GIOStream *
91 g_test_io_stream_new (GInputStream *input, GOutputStream *output)
92 {
93   GTestIOStream *self;
94
95   self = g_object_new (g_test_io_stream_get_type (), NULL);
96   self->input_stream = g_object_ref (input);
97   self->output_stream = g_object_ref (output);
98
99   return G_IO_STREAM (self);
100 }
101
102 typedef struct
103 {
104   GMainLoop *main_loop;
105   const gchar *data1;
106   const gchar *data2;
107   GIOStream *iostream1;
108   GIOStream *iostream2;
109 } TestCopyChunksData;
110
111 static void
112 test_copy_chunks_splice_cb (GObject *source_object,
113     GAsyncResult *res,
114     gpointer user_data)
115 {
116   TestCopyChunksData *data = user_data;
117   GMemoryOutputStream *ostream;
118   gchar *received_data;
119   GError *error = NULL;
120
121   g_io_stream_splice_finish (res, &error);
122   g_assert_no_error (error);
123
124   ostream = G_MEMORY_OUTPUT_STREAM (((GTestIOStream *) data->iostream1)->output_stream);
125   received_data = g_memory_output_stream_get_data (ostream);
126   g_assert_cmpstr (received_data, ==, data->data2);
127
128   ostream = G_MEMORY_OUTPUT_STREAM (((GTestIOStream *) data->iostream2)->output_stream);
129   received_data = g_memory_output_stream_get_data (ostream);
130   g_assert_cmpstr (received_data, ==, data->data1);
131
132   g_assert (g_io_stream_is_closed (data->iostream1));
133   g_assert (g_io_stream_is_closed (data->iostream2));
134
135   g_main_loop_quit (data->main_loop);
136 }
137
138 static void
139 test_copy_chunks (void)
140 {
141   TestCopyChunksData data;
142   GInputStream *istream;
143   GOutputStream *ostream;
144
145   data.main_loop = g_main_loop_new (NULL, FALSE);
146   data.data1 = "abcdefghijklmnopqrstuvwxyz";
147   data.data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
148
149   istream = g_memory_input_stream_new_from_data (data.data1, -1, NULL);
150   ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
151   data.iostream1 = g_test_io_stream_new (istream, ostream);
152   g_object_unref (istream);
153   g_object_unref (ostream);
154
155   istream = g_memory_input_stream_new_from_data (data.data2, -1, NULL);
156   ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
157   data.iostream2 = g_test_io_stream_new (istream, ostream);
158   g_object_unref (istream);
159   g_object_unref (ostream);
160
161   g_io_stream_splice_async (data.iostream1, data.iostream2,
162       G_IO_STREAM_SPLICE_CLOSE_STREAM1 | G_IO_STREAM_SPLICE_CLOSE_STREAM2 |
163       G_IO_STREAM_SPLICE_WAIT_FOR_BOTH, G_PRIORITY_DEFAULT,
164       NULL, test_copy_chunks_splice_cb, &data);
165
166   /* We do not hold a ref in data struct, this is to make sure the operation
167    * keeps the iostream objects alive until it finishes */
168   g_object_unref (data.iostream1);
169   g_object_unref (data.iostream2);
170
171   g_main_loop_run (data.main_loop);
172   g_main_loop_unref (data.main_loop);
173 }
174
175 int
176 main (int   argc,
177       char *argv[])
178 {
179   g_type_init ();
180   g_test_init (&argc, &argv, NULL);
181
182   g_test_add_func ("/io-stream/copy-chunks", test_copy_chunks);
183
184   return g_test_run();
185 }