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