1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Tomas Bzatek <tbzatek@redhat.com>
4 *
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
6 *
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
10 *
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 *
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
23 */
24
25 #include <glib/glib.h>
26 #include <gio/gio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #define MAX_LINES 0xFFF
31 #define MAX_BYTES 0x10000
32
33 static void
34 test_basic (void)
35 {
36 GInputStream *stream;
37 GInputStream *base_stream;
38 gint val;
39
40 base_stream = g_memory_input_stream_new ();
41 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
42
43 g_object_get (stream, "byte-order", &val, NULL);
44 g_assert_cmpint (val, ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
45 g_object_set (stream, "byte-order", G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN, NULL);
46 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
47
48 g_object_get (stream, "newline-type", &val, NULL);
49 g_assert_cmpint (val, ==, G_DATA_STREAM_NEWLINE_TYPE_LF);
50 g_object_set (stream, "newline-type", G_DATA_STREAM_NEWLINE_TYPE_CR_LF, NULL);
51 g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
52
53 g_object_unref (stream);
54 g_object_unref (base_stream);
55 }
56
57 static void
58 test_seek_to_start (GInputStream *stream)
59 {
60 GError *error = NULL;
61 gboolean res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
62 g_assert_cmpint (res, ==, TRUE);
63 g_assert_no_error (error);
64 }
65
66 static void
67 test_read_lines (GDataStreamNewlineType newline_type)
68 {
69 GInputStream *stream;
70 GInputStream *base_stream;
71 GError *error = NULL;
72 char *data;
73 int line;
74 const char* lines[MAX_LINES];
75 const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
76
77 /* prepare data */
78 int i;
79 for (i = 0; i < MAX_LINES; i++)
80 lines[i] = "some_text";
81
82 base_stream = g_memory_input_stream_new ();
83 g_assert (base_stream != NULL);
84 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
85 g_assert(stream != NULL);
86
87 /* Byte order testing */
88 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
89 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
90 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
91 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
92
93 /* Line ends testing */
94 g_data_input_stream_set_newline_type (G_DATA_INPUT_STREAM (stream), newline_type);
95 g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, newline_type);
96
97
98 /* Add sample data */
99 for (i = 0; i < MAX_LINES; i++)
100 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
101 g_strconcat (lines[i], endl[newline_type], NULL), -1, g_free);
102
103 /* Seek to the start */
104 test_seek_to_start (base_stream);
105
106 /* Test read line */
107 error = NULL;
108 data = (char*)1;
109 line = 0;
110 while (data)
111 {
112 gsize length = -1;
113 data = g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
114 if (data)
115 {
116 g_assert_cmpstr (data, ==, lines[line]);
117 g_free (data);
118 g_assert_no_error (error);
119 line++;
120 }
121 if (error)
122 g_error_free (error);
123 }
124 g_assert_cmpint (line, ==, MAX_LINES);
125
126
127 g_object_unref (base_stream);
128 g_object_unref (stream);
129 }
130
131 static void
132 test_read_lines_LF (void)
133 {
134 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
135 }
136
137 static void
138 test_read_lines_CR (void)
139 {
140 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
141 }
142
143 static void
144 test_read_lines_CR_LF (void)
145 {
146 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
147 }
148
149 static void
150 test_read_lines_any (void)
151 {
152 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_ANY);
153 }
154
155 static void
156 test_read_lines_LF_valid_utf8 (void)
157 {
158 GInputStream *stream;
159 GInputStream *base_stream;
160 GError *error = NULL;
161 char *line;
162 guint n_lines = 0;
163
164 base_stream = g_memory_input_stream_new ();
165 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
166
167 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
168 "foo\nthis is valid UTF-8 ☺!\nbar\n", -1, NULL);
169
170 /* Test read line */
171 error = NULL;
172 while (TRUE)
173 {
174 gsize length = -1;
175 line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
176 g_assert_no_error (error);
177 if (line == NULL)
178 break;
179 n_lines++;
180 g_free (line);
181 }
182 g_assert_cmpint (n_lines, ==, 3);
183
184 g_object_unref (base_stream);
185 g_object_unref (stream);
186 }
187
188 static void
189 test_read_lines_LF_invalid_utf8 (void)
190 {
191 GInputStream *stream;
192 GInputStream *base_stream;
193 GError *error = NULL;
194 char *line;
195 guint n_lines = 0;
196
197 base_stream = g_memory_input_stream_new ();
198 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
199
200 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
201 "foo\nthis is not valid UTF-8 \xE5 =(\nbar\n", -1, NULL);
202
203 /* Test read line */
204 error = NULL;
205 while (TRUE)
206 {
207 gsize length = -1;
208 line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
209 if (n_lines == 0)
210 g_assert_no_error (error);
211 else
212 {
213 g_assert (error != NULL);
214 g_clear_error (&error);
215 g_free (line);
216 break;
217 }
218 n_lines++;
219 g_free (line);
220 }
221 g_assert_cmpint (n_lines, ==, 1);
222
223 g_object_unref (base_stream);
224 g_object_unref (stream);
225 }
226
227 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
228
229 static void
230 test_read_until (void)
231 {
232 GInputStream *stream;
233 GInputStream *base_stream;
234 GError *error = NULL;
235 char *data;
236 int line;
237 int i;
238
239 #define REPEATS 10 /* number of rounds */
240 #define DATA_STRING " part1 # part2 $ part3 % part4 ^"
241 #define DATA_PART_LEN 7 /* number of characters between separators */
242 #define DATA_SEP "#$%^"
243 #define DATA_SEP_LEN 4
244 const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
245
246 base_stream = g_memory_input_stream_new ();
247 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
248
249 for (i = 0; i < REPEATS; i++)
250 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
251
252 /* Test stop characters */
253 error = NULL;
254 data = (char*)1;
255 line = 0;
256 while (data)
257 {
258 gsize length = -1;
259 data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
260 if (data)
261 {
262 g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
263 g_free (data);
264 g_assert_no_error (error);
265 line++;
266 }
267 }
268 g_assert_no_error (error);
269 g_assert_cmpint (line, ==, DATA_PARTS_NUM);
270
271 g_object_unref (base_stream);
272 g_object_unref (stream);
273 }
274
275 G_GNUC_END_IGNORE_DEPRECATIONS
276
277 static void
278 test_read_upto (void)
279 {
280 GInputStream *stream;
281 GInputStream *base_stream;
282 GError *error = NULL;
283 char *data;
284 int line;
285 int i;
286 guchar stop_char;
287
288 #undef REPEATS
289 #undef DATA_STRING
290 #undef DATA_PART_LEN
291 #undef DATA_SEP
292 #undef DATA_SEP_LEN
293 #define REPEATS 10 /* number of rounds */
294 #define DATA_STRING " part1 # part2 $ part3 \0 part4 ^"
295 #define DATA_PART_LEN 7 /* number of characters between separators */
296 #define DATA_SEP "#$\0^"
297 #define DATA_SEP_LEN 4
298 const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
299
300 base_stream = g_memory_input_stream_new ();
301 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
302
303 for (i = 0; i < REPEATS; i++)
304 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, 32, NULL);
305
306 /* Test stop characters */
307 error = NULL;
308 data = (char*)1;
309 line = 0;
310 while (data)
311 {
312 gsize length = -1;
313 data = g_data_input_stream_read_upto (G_DATA_INPUT_STREAM (stream), DATA_SEP, DATA_SEP_LEN, &length, NULL, &error);
314 if (data)
315 {
316 g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
317 g_assert_no_error (error);
318 line++;
319
320 stop_char = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
321 g_assert (memchr (DATA_SEP, stop_char, DATA_SEP_LEN) != NULL);
322 g_assert_no_error (error);
323 }
324 g_free (data);
325 }
326 g_assert_no_error (error);
327 g_assert_cmpint (line, ==, DATA_PARTS_NUM);
328
329 g_object_unref (base_stream);
330 g_object_unref (stream);
331 }
332 enum TestDataType {
333 TEST_DATA_BYTE = 0,
334 TEST_DATA_INT16,
335 TEST_DATA_UINT16,
336 TEST_DATA_INT32,
337 TEST_DATA_UINT32,
338 TEST_DATA_INT64,
339 TEST_DATA_UINT64
340 };
341
342 /* The order is reversed to avoid -Wduplicated-branches. */
343 #define TEST_DATA_RETYPE_BUFF(a, t, v) \
344 (a == TEST_DATA_UINT64 ? (t) *(guint64*)v : \
345 (a == TEST_DATA_INT64 ? (t) *(gint64*)v : \
346 (a == TEST_DATA_UINT32 ? (t) *(guint32*)v : \
347 (a == TEST_DATA_INT32 ? (t) *(gint32*)v : \
348 (a == TEST_DATA_UINT16 ? (t) *(guint16*)v : \
349 (a == TEST_DATA_INT16 ? (t) *(gint16*)v : \
350 (t) *(guchar*)v ))))))
351
352
353 static void
354 test_data_array (GInputStream *stream, GInputStream *base_stream,
355 gpointer buffer, int len,
356 enum TestDataType data_type, GDataStreamByteOrder byte_order)
357 {
358 GError *error = NULL;
359 int pos = 0;
360 int data_size = 1;
361 gint64 data;
362 GDataStreamByteOrder native;
363 gboolean swap;
364
365 /* Seek to start */
366 test_seek_to_start (base_stream);
367
368 /* Set correct data size */
369 switch (data_type)
370 {
371 case TEST_DATA_BYTE:
372 data_size = 1;
373 break;
374 case TEST_DATA_INT16:
375 case TEST_DATA_UINT16:
376 data_size = 2;
377 break;
378 case TEST_DATA_INT32:
379 case TEST_DATA_UINT32:
380 data_size = 4;
381 break;
382 case TEST_DATA_INT64:
383 case TEST_DATA_UINT64:
384 data_size = 8;
385 break;
386 default:
387 g_assert_not_reached ();
388 break;
389 }
390
391 /* Set flag to swap bytes if needed */
392 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
393 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
394
395 data = 1;
396 while (data != 0)
397 {
398 switch (data_type)
399 {
400 case TEST_DATA_BYTE:
401 data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
402 break;
403 case TEST_DATA_INT16:
404 data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
405 if (swap)
406 data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
407 break;
408 case TEST_DATA_UINT16:
409 data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
410 if (swap)
411 data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
412 break;
413 case TEST_DATA_INT32:
414 data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
415 if (swap)
416 data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
417 break;
418 case TEST_DATA_UINT32:
419 data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
420 if (swap)
421 data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
422 break;
423 case TEST_DATA_INT64:
424 data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
425 if (swap)
426 data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
427 break;
428 case TEST_DATA_UINT64:
429 data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
430 if (swap)
431 data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
432 break;
433 default:
434 g_assert_not_reached ();
435 break;
436 }
437 if (!error)
438 g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
439
440 pos += data_size;
441 }
442 if (pos < len + 1)
443 g_assert_no_error (error);
444 if (error)
445 g_error_free (error);
446 g_assert_cmpint (pos - data_size, ==, len);
447 }
448
449 static void
450 test_read_int (void)
451 {
452 GInputStream *stream;
453 GInputStream *base_stream;
454 GRand *randomizer;
455 int i;
456 gpointer buffer;
457
458 randomizer = g_rand_new ();
459 buffer = g_malloc0 (MAX_BYTES);
460
461 /* Fill in some random data */
462 for (i = 0; i < MAX_BYTES; i++)
463 {
464 guchar x = 0;
465 while (! x)
466 x = (guchar)g_rand_int (randomizer);
467 *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x;
468 }
469
470 base_stream = g_memory_input_stream_new ();
471 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
472 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
473
474
475 for (i = 0; i < 3; i++)
476 {
477 int j;
478 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
479
480 for (j = 0; j <= TEST_DATA_UINT64; j++)
481 test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
482 }
483
484 g_object_unref (base_stream);
485 g_object_unref (stream);
486 g_rand_free (randomizer);
487 g_free (buffer);
488 }
489
490
491 int
492 main (int argc,
493 char *argv[])
494 {
495 g_test_init (&argc, &argv, NULL);
496
497 g_test_add_func ("/data-input-stream/basic", test_basic);
498 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
499 g_test_add_func ("/data-input-stream/read-lines-LF-valid-utf8", test_read_lines_LF_valid_utf8);
500 g_test_add_func ("/data-input-stream/read-lines-LF-invalid-utf8", test_read_lines_LF_invalid_utf8);
501 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
502 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
503 g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any);
504 g_test_add_func ("/data-input-stream/read-until", test_read_until);
505 g_test_add_func ("/data-input-stream/read-upto", test_read_upto);
506 g_test_add_func ("/data-input-stream/read-int", test_read_int);
507
508 return g_test_run();
509 }