• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   GMainLoop *main_loop;
31   const gchar *data1;
32   const gchar *data2;
33   GIOStream *iostream1;
34   GIOStream *iostream2;
35 } TestCopyChunksData;
36 
37 static void
test_copy_chunks_splice_cb(GObject * source_object,GAsyncResult * res,gpointer user_data)38 test_copy_chunks_splice_cb (GObject *source_object,
39     GAsyncResult *res,
40     gpointer user_data)
41 {
42   TestCopyChunksData *data = user_data;
43   GMemoryOutputStream *ostream;
44   gchar *received_data;
45   GError *error = NULL;
46 
47   g_io_stream_splice_finish (res, &error);
48   g_assert_no_error (error);
49 
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);
53 
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);
57 
58   g_assert (g_io_stream_is_closed (data->iostream1));
59   g_assert (g_io_stream_is_closed (data->iostream2));
60 
61   g_main_loop_quit (data->main_loop);
62 }
63 
64 static void
test_copy_chunks(void)65 test_copy_chunks (void)
66 {
67   TestCopyChunksData data;
68   GInputStream *istream;
69   GOutputStream *ostream;
70 
71   data.main_loop = g_main_loop_new (NULL, FALSE);
72   data.data1 = "abcdefghijklmnopqrstuvwxyz";
73   data.data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
74 
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);
80 
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);
86 
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);
91 
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);
96 
97   g_main_loop_run (data.main_loop);
98   g_main_loop_unref (data.main_loop);
99 }
100 
101 static void
close_async_done(GObject * source,GAsyncResult * result,gpointer user_data)102 close_async_done (GObject *source,
103                   GAsyncResult *result,
104                   gpointer user_data)
105 {
106   gboolean *done = user_data;
107 
108   *done = TRUE;
109 }
110 
111 static void
test_close_file(void)112 test_close_file (void)
113 {
114 #ifdef G_OS_UNIX
115   GFileIOStream *fios;
116   gboolean done;
117   GIOStream *io;
118   GFile *file;
119 
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);
124 
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);
128 
129   g_io_stream_close_async (io, 0, NULL, close_async_done, &done);
130   g_object_unref (io);
131 
132   done = FALSE;
133   while (!done)
134     g_main_context_iteration (NULL, TRUE);
135 #endif
136 }
137 
138 static void
test_close_memory(void)139 test_close_memory (void)
140 {
141   GInputStream *in;
142   GOutputStream *out;
143   gboolean done;
144   GIOStream *io;
145 
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);
151 
152   g_io_stream_close_async (io, 0, NULL, close_async_done, &done);
153   g_object_unref (io);
154 
155   done = FALSE;
156   while (!done)
157     g_main_context_iteration (NULL, TRUE);
158 }
159 
160 int
main(int argc,char * argv[])161 main (int   argc,
162       char *argv[])
163 {
164   g_test_init (&argc, &argv, NULL);
165 
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);
169 
170   return g_test_run();
171 }
172