| 1 |
/* GLib testing framework examples and tests |
| 2 |
* Authors: Jesse van den Kieboom <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LicenseRef-old-glib-tests |
| 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 <gio/gio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#define DATA_TO_WRITE "Hello world\n" |
| 30 |
|
| 31 |
typedef struct |
| 32 |
{ |
| 33 |
GOutputStream *conv_stream; |
| 34 |
GOutputStream *data_stream; |
| 35 |
gchar *expected_output; |
| 36 |
gsize expected_size; |
| 37 |
GMainLoop *main_loop; |
| 38 |
} SetupData; |
| 39 |
|
| 40 |
static void |
| 41 |
create_streams (SetupData *data) |
| 42 |
{ |
| 43 |
GConverter *converter; |
| 44 |
|
| 45 |
converter = G_CONVERTER (g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, -1)); |
| 46 |
|
| 47 |
data->data_stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free); |
| 48 |
data->conv_stream = g_converter_output_stream_new (data->data_stream, |
| 49 |
converter); |
| 50 |
|
| 51 |
g_object_unref (converter); |
| 52 |
} |
| 53 |
|
| 54 |
static void |
| 55 |
destroy_streams (SetupData *data) |
| 56 |
{ |
| 57 |
g_object_unref (data->data_stream); |
| 58 |
g_object_unref (data->conv_stream); |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
write_data_to_stream (SetupData *data) |
| 63 |
{ |
| 64 |
gsize bytes_written; |
| 65 |
GError *error = NULL; |
| 66 |
|
| 67 |
/* just write the data synchronously */ |
| 68 |
g_output_stream_write_all (data->conv_stream, |
| 69 |
DATA_TO_WRITE, |
| 70 |
sizeof (DATA_TO_WRITE), |
| 71 |
&bytes_written, |
| 72 |
NULL, |
| 73 |
&error); |
| 74 |
|
| 75 |
g_assert_no_error (error); |
| 76 |
g_assert_cmpint (sizeof (DATA_TO_WRITE), ==, bytes_written); |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
setup_data (SetupData *data, |
| 81 |
gconstpointer user_data) |
| 82 |
{ |
| 83 |
data->main_loop = g_main_loop_new (NULL, FALSE); |
| 84 |
create_streams (data); |
| 85 |
} |
| 86 |
|
| 87 |
static void |
| 88 |
teardown_data (SetupData *data, |
| 89 |
gconstpointer user_data) |
| 90 |
{ |
| 91 |
/* cleanup */ |
| 92 |
g_main_loop_unref (data->main_loop); |
| 93 |
|
| 94 |
destroy_streams (data); |
| 95 |
|
| 96 |
g_free (data->expected_output); |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
compare_output (SetupData *data) |
| 101 |
{ |
| 102 |
gsize size; |
| 103 |
gpointer written; |
| 104 |
|
| 105 |
written = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->data_stream)); |
| 106 |
size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream)); |
| 107 |
|
| 108 |
g_assert_cmpmem (written, size, data->expected_output, data->expected_size); |
| 109 |
} |
| 110 |
|
| 111 |
static void |
| 112 |
async_close_ready (GOutputStream *stream, |
| 113 |
GAsyncResult *result, |
| 114 |
SetupData *data) |
| 115 |
{ |
| 116 |
GError *error = NULL; |
| 117 |
|
| 118 |
/* finish the close */ |
| 119 |
g_output_stream_close_finish (stream, result, &error); |
| 120 |
|
| 121 |
g_assert_no_error (error); |
| 122 |
|
| 123 |
/* compare the output with the desired output */ |
| 124 |
compare_output (data); |
| 125 |
|
| 126 |
g_main_loop_quit (data->main_loop); |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
prepare_data (SetupData *data, |
| 131 |
gboolean manual_flush) |
| 132 |
{ |
| 133 |
GError *error = NULL; |
| 134 |
gpointer written; |
| 135 |
|
| 136 |
write_data_to_stream (data); |
| 137 |
|
| 138 |
if (manual_flush) |
| 139 |
{ |
| 140 |
g_output_stream_flush (data->conv_stream, NULL, &error); |
| 141 |
g_assert_no_error (error); |
| 142 |
} |
| 143 |
|
| 144 |
g_output_stream_close (data->conv_stream, NULL, &error); |
| 145 |
|
| 146 |
g_assert_no_error (error); |
| 147 |
|
| 148 |
written = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->data_stream)); |
| 149 |
|
| 150 |
data->expected_size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream)); |
| 151 |
|
| 152 |
g_assert_cmpuint (data->expected_size, >, 0); |
| 153 |
|
| 154 |
data->expected_output = g_memdup2 (written, data->expected_size); |
| 155 |
|
| 156 |
/* then recreate the streams and prepare them for the asynchronous close */ |
| 157 |
destroy_streams (data); |
| 158 |
create_streams (data); |
| 159 |
|
| 160 |
write_data_to_stream (data); |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
test_without_flush (SetupData *data, |
| 165 |
gconstpointer user_data) |
| 166 |
{ |
| 167 |
prepare_data (data, FALSE); |
| 168 |
|
| 169 |
g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=617937"); |
| 170 |
|
| 171 |
/* just close asynchronously */ |
| 172 |
g_output_stream_close_async (data->conv_stream, |
| 173 |
G_PRIORITY_DEFAULT, |
| 174 |
NULL, |
| 175 |
(GAsyncReadyCallback)async_close_ready, |
| 176 |
data); |
| 177 |
|
| 178 |
g_main_loop_run (data->main_loop); |
| 179 |
} |
| 180 |
|
| 181 |
static void |
| 182 |
test_with_flush (SetupData *data, gconstpointer user_data) |
| 183 |
{ |
| 184 |
GError *error = NULL; |
| 185 |
|
| 186 |
g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=617937"); |
| 187 |
|
| 188 |
prepare_data (data, TRUE); |
| 189 |
|
| 190 |
g_output_stream_flush (data->conv_stream, NULL, &error); |
| 191 |
|
| 192 |
g_assert_no_error (error); |
| 193 |
|
| 194 |
/* then close asynchronously */ |
| 195 |
g_output_stream_close_async (data->conv_stream, |
| 196 |
G_PRIORITY_DEFAULT, |
| 197 |
NULL, |
| 198 |
(GAsyncReadyCallback)async_close_ready, |
| 199 |
data); |
| 200 |
|
| 201 |
g_main_loop_run (data->main_loop); |
| 202 |
} |
| 203 |
|
| 204 |
static void |
| 205 |
async_flush_ready (GOutputStream *stream, |
| 206 |
GAsyncResult *result, |
| 207 |
SetupData *data) |
| 208 |
{ |
| 209 |
GError *error = NULL; |
| 210 |
|
| 211 |
g_output_stream_flush_finish (stream, result, &error); |
| 212 |
|
| 213 |
g_assert_no_error (error); |
| 214 |
|
| 215 |
/* then close async after the flush */ |
| 216 |
g_output_stream_close_async (data->conv_stream, |
| 217 |
G_PRIORITY_DEFAULT, |
| 218 |
NULL, |
| 219 |
(GAsyncReadyCallback)async_close_ready, |
| 220 |
data); |
| 221 |
} |
| 222 |
|
| 223 |
static void |
| 224 |
test_with_async_flush (SetupData *data, |
| 225 |
gconstpointer user_data) |
| 226 |
{ |
| 227 |
g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=617937"); |
| 228 |
|
| 229 |
prepare_data (data, TRUE); |
| 230 |
|
| 231 |
/* first flush async */ |
| 232 |
g_output_stream_flush_async (data->conv_stream, |
| 233 |
G_PRIORITY_DEFAULT, |
| 234 |
NULL, |
| 235 |
(GAsyncReadyCallback)async_flush_ready, |
| 236 |
data); |
| 237 |
|
| 238 |
g_main_loop_run (data->main_loop); |
| 239 |
} |
| 240 |
|
| 241 |
int |
| 242 |
main (int argc, |
| 243 |
char *argv[]) |
| 244 |
{ |
| 245 |
SetupData *data; |
| 246 |
|
| 247 |
g_test_init (&argc, &argv, NULL); |
| 248 |
|
| 249 |
data = g_slice_new (SetupData); |
| 250 |
|
| 251 |
/* test closing asynchronously without flushing manually */ |
| 252 |
g_test_add ("/close-async/without-flush", |
| 253 |
SetupData, |
| 254 |
data, |
| 255 |
setup_data, |
| 256 |
test_without_flush, |
| 257 |
teardown_data); |
| 258 |
|
| 259 |
/* test closing asynchronously with a synchronous manually flush */ |
| 260 |
g_test_add ("/close-async/with-flush", |
| 261 |
SetupData, |
| 262 |
data, |
| 263 |
setup_data, |
| 264 |
test_with_flush, |
| 265 |
teardown_data); |
| 266 |
|
| 267 |
/* test closing asynchronously with an asynchronous manually flush */ |
| 268 |
g_test_add ("/close-async/with-async-flush", |
| 269 |
SetupData, |
| 270 |
data, |
| 271 |
setup_data, |
| 272 |
test_with_async_flush, |
| 273 |
teardown_data); |
| 274 |
|
| 275 |
g_slice_free (SetupData, data); |
| 276 |
|
| 277 |
return g_test_run(); |
| 278 |
} |