Branch data Line data Source code
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 : 1 : test_basic (void)
35 : : {
36 : : GInputStream *stream;
37 : : GInputStream *base_stream;
38 : : gint val;
39 : :
40 : 1 : base_stream = g_memory_input_stream_new ();
41 : 1 : stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
42 : :
43 : 1 : g_object_get (stream, "byte-order", &val, NULL);
44 : 1 : g_assert_cmpint (val, ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
45 : 1 : g_object_set (stream, "byte-order", G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN, NULL);
46 : 1 : g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
47 : :
48 : 1 : g_object_get (stream, "newline-type", &val, NULL);
49 : 1 : g_assert_cmpint (val, ==, G_DATA_STREAM_NEWLINE_TYPE_LF);
50 : 1 : g_object_set (stream, "newline-type", G_DATA_STREAM_NEWLINE_TYPE_CR_LF, NULL);
51 : 1 : g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
52 : :
53 : 1 : g_object_unref (stream);
54 : 1 : g_object_unref (base_stream);
55 : 1 : }
56 : :
57 : : static void
58 : 25 : test_seek_to_start (GInputStream *stream)
59 : : {
60 : 25 : GError *error = NULL;
61 : 25 : gboolean res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
62 : 25 : g_assert_cmpint (res, ==, TRUE);
63 : 25 : g_assert_no_error (error);
64 : 25 : }
65 : :
66 : : static void
67 : 4 : test_read_lines (GDataStreamNewlineType newline_type)
68 : : {
69 : : GInputStream *stream;
70 : : GInputStream *base_stream;
71 : 4 : GError *error = NULL;
72 : : char *data;
73 : : int line;
74 : : const char* lines[MAX_LINES];
75 : 4 : const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
76 : :
77 : : /* prepare data */
78 : : int i;
79 [ + + ]: 16384 : for (i = 0; i < MAX_LINES; i++)
80 : 16380 : lines[i] = "some_text";
81 : :
82 : 4 : base_stream = g_memory_input_stream_new ();
83 : 4 : g_assert (base_stream != NULL);
84 : 4 : stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
85 : 4 : g_assert(stream != NULL);
86 : :
87 : : /* Byte order testing */
88 : 4 : g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
89 : 4 : g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
90 : 4 : g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
91 : 4 : 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 : 4 : g_data_input_stream_set_newline_type (G_DATA_INPUT_STREAM (stream), newline_type);
95 : 4 : g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, newline_type);
96 : :
97 : :
98 : : /* Add sample data */
99 [ + + ]: 16384 : for (i = 0; i < MAX_LINES; i++)
100 : 16380 : g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
101 : 16380 : g_strconcat (lines[i], endl[newline_type], NULL), -1, g_free);
102 : :
103 : : /* Seek to the start */
104 : 4 : test_seek_to_start (base_stream);
105 : :
106 : : /* Test read line */
107 : 4 : error = NULL;
108 : 4 : data = (char*)1;
109 : 4 : line = 0;
110 [ + + ]: 16388 : while (data)
111 : : {
112 : 16384 : gsize length = -1;
113 : 16384 : data = g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
114 [ + + ]: 16384 : if (data)
115 : : {
116 : 16380 : g_assert_cmpstr (data, ==, lines[line]);
117 : 16380 : g_free (data);
118 : 16380 : g_assert_no_error (error);
119 : 16380 : line++;
120 : : }
121 [ - + ]: 16384 : if (error)
122 : 0 : g_error_free (error);
123 : : }
124 : 4 : g_assert_cmpint (line, ==, MAX_LINES);
125 : :
126 : :
127 : 4 : g_object_unref (base_stream);
128 : 4 : g_object_unref (stream);
129 : 4 : }
130 : :
131 : : static void
132 : 1 : test_read_lines_LF (void)
133 : : {
134 : 1 : test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
135 : 1 : }
136 : :
137 : : static void
138 : 1 : test_read_lines_CR (void)
139 : : {
140 : 1 : test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
141 : 1 : }
142 : :
143 : : static void
144 : 1 : test_read_lines_CR_LF (void)
145 : : {
146 : 1 : test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
147 : 1 : }
148 : :
149 : : static void
150 : 1 : test_read_lines_any (void)
151 : : {
152 : 1 : test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_ANY);
153 : 1 : }
154 : :
155 : : static void
156 : 1 : test_read_lines_LF_valid_utf8 (void)
157 : : {
158 : : GInputStream *stream;
159 : : GInputStream *base_stream;
160 : 1 : GError *error = NULL;
161 : : char *line;
162 : 1 : guint n_lines = 0;
163 : :
164 : 1 : base_stream = g_memory_input_stream_new ();
165 : 1 : stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
166 : :
167 : 1 : 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 : 1 : error = NULL;
172 : : while (TRUE)
173 : 3 : {
174 : 4 : gsize length = -1;
175 : 4 : line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
176 : 4 : g_assert_no_error (error);
177 [ + + ]: 4 : if (line == NULL)
178 : 1 : break;
179 : 3 : n_lines++;
180 : 3 : g_free (line);
181 : : }
182 : 1 : g_assert_cmpint (n_lines, ==, 3);
183 : :
184 : 1 : g_object_unref (base_stream);
185 : 1 : g_object_unref (stream);
186 : 1 : }
187 : :
188 : : static void
189 : 1 : test_read_lines_LF_invalid_utf8 (void)
190 : : {
191 : : GInputStream *stream;
192 : : GInputStream *base_stream;
193 : 1 : GError *error = NULL;
194 : : char *line;
195 : 1 : guint n_lines = 0;
196 : :
197 : 1 : base_stream = g_memory_input_stream_new ();
198 : 1 : stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
199 : :
200 : 1 : 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 : 1 : error = NULL;
205 : : while (TRUE)
206 : 1 : {
207 : 2 : gsize length = -1;
208 : 2 : line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
209 [ + + ]: 2 : if (n_lines == 0)
210 : 1 : g_assert_no_error (error);
211 : : else
212 : : {
213 : 1 : g_assert (error != NULL);
214 : 1 : g_clear_error (&error);
215 : 1 : g_free (line);
216 : 1 : break;
217 : : }
218 : 1 : n_lines++;
219 : 1 : g_free (line);
220 : : }
221 : 1 : g_assert_cmpint (n_lines, ==, 1);
222 : :
223 : 1 : g_object_unref (base_stream);
224 : 1 : g_object_unref (stream);
225 : 1 : }
226 : :
227 : : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
228 : :
229 : : static void
230 : 1 : test_read_until (void)
231 : : {
232 : : GInputStream *stream;
233 : : GInputStream *base_stream;
234 : 1 : 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 : 1 : const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
245 : :
246 : 1 : base_stream = g_memory_input_stream_new ();
247 : 1 : stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
248 : :
249 [ + + ]: 11 : for (i = 0; i < REPEATS; i++)
250 : 10 : g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
251 : :
252 : : /* Test stop characters */
253 : 1 : error = NULL;
254 : 1 : data = (char*)1;
255 : 1 : line = 0;
256 [ + + ]: 42 : while (data)
257 : : {
258 : 41 : gsize length = -1;
259 : 41 : data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
260 [ + + ]: 41 : if (data)
261 : : {
262 : 40 : g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
263 : 40 : g_free (data);
264 : 40 : g_assert_no_error (error);
265 : 40 : line++;
266 : : }
267 : : }
268 : 1 : g_assert_no_error (error);
269 : 1 : g_assert_cmpint (line, ==, DATA_PARTS_NUM);
270 : :
271 : 1 : g_object_unref (base_stream);
272 : 1 : g_object_unref (stream);
273 : 1 : }
274 : :
275 : : G_GNUC_END_IGNORE_DEPRECATIONS
276 : :
277 : : static void
278 : 1 : test_read_upto (void)
279 : : {
280 : : GInputStream *stream;
281 : : GInputStream *base_stream;
282 : 1 : 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 : 1 : const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
299 : :
300 : 1 : base_stream = g_memory_input_stream_new ();
301 : 1 : stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
302 : :
303 [ + + ]: 11 : for (i = 0; i < REPEATS; i++)
304 : 10 : g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, 32, NULL);
305 : :
306 : : /* Test stop characters */
307 : 1 : error = NULL;
308 : 1 : data = (char*)1;
309 : 1 : line = 0;
310 [ + + ]: 42 : while (data)
311 : : {
312 : 41 : gsize length = -1;
313 : 41 : data = g_data_input_stream_read_upto (G_DATA_INPUT_STREAM (stream), DATA_SEP, DATA_SEP_LEN, &length, NULL, &error);
314 [ + + ]: 41 : if (data)
315 : : {
316 : 40 : g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
317 : 40 : g_assert_no_error (error);
318 : 40 : line++;
319 : :
320 : 40 : stop_char = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
321 : 40 : g_assert (memchr (DATA_SEP, stop_char, DATA_SEP_LEN) != NULL);
322 : 40 : g_assert_no_error (error);
323 : : }
324 : 41 : g_free (data);
325 : : }
326 : 1 : g_assert_no_error (error);
327 : 1 : g_assert_cmpint (line, ==, DATA_PARTS_NUM);
328 : :
329 : 1 : g_object_unref (base_stream);
330 : 1 : g_object_unref (stream);
331 : 1 : }
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 : 21 : test_data_array (GInputStream *stream, GInputStream *base_stream,
355 : : gpointer buffer, int len,
356 : : enum TestDataType data_type, GDataStreamByteOrder byte_order)
357 : : {
358 : 21 : GError *error = NULL;
359 : 21 : int pos = 0;
360 : 21 : int data_size = 1;
361 : : gint64 data;
362 : : GDataStreamByteOrder native;
363 : : gboolean swap;
364 : :
365 : : /* Seek to start */
366 : 21 : test_seek_to_start (base_stream);
367 : :
368 : : /* Set correct data size */
369 [ + + + + : 21 : switch (data_type)
- ]
370 : : {
371 : 3 : case TEST_DATA_BYTE:
372 : 3 : data_size = 1;
373 : 3 : break;
374 : 6 : case TEST_DATA_INT16:
375 : : case TEST_DATA_UINT16:
376 : 6 : data_size = 2;
377 : 6 : break;
378 : 6 : case TEST_DATA_INT32:
379 : : case TEST_DATA_UINT32:
380 : 6 : data_size = 4;
381 : 6 : break;
382 : 6 : case TEST_DATA_INT64:
383 : : case TEST_DATA_UINT64:
384 : 6 : data_size = 8;
385 : 6 : break;
386 : 0 : default:
387 : : g_assert_not_reached ();
388 : : break;
389 : : }
390 : :
391 : : /* Set flag to swap bytes if needed */
392 : 21 : native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
393 [ + + + + ]: 21 : swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
394 : :
395 : 21 : data = 1;
396 [ + + ]: 540714 : while (data != 0)
397 : : {
398 [ + + + + : 540693 : switch (data_type)
+ + + - ]
399 : : {
400 : 196611 : case TEST_DATA_BYTE:
401 : 196611 : data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
402 : 196611 : break;
403 : 98307 : case TEST_DATA_INT16:
404 : 98307 : data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
405 [ + + ]: 98307 : if (swap)
406 : 32769 : data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
407 : 98307 : break;
408 : 98307 : case TEST_DATA_UINT16:
409 : 98307 : data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
410 [ + + ]: 98307 : if (swap)
411 : 32769 : data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
412 : 98307 : break;
413 : 49155 : case TEST_DATA_INT32:
414 : 49155 : data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
415 [ + + ]: 49155 : if (swap)
416 : 16385 : data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
417 : 49155 : break;
418 : 49155 : case TEST_DATA_UINT32:
419 : 49155 : data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
420 [ + + ]: 49155 : if (swap)
421 : 16385 : data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
422 : 49155 : break;
423 : 24579 : case TEST_DATA_INT64:
424 : 24579 : data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
425 [ + + ]: 24579 : if (swap)
426 : 8193 : data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
427 : 24579 : break;
428 : 24579 : case TEST_DATA_UINT64:
429 : 24579 : data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
430 [ + + ]: 24579 : if (swap)
431 : 8193 : data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
432 : 24579 : break;
433 : 0 : default:
434 : : g_assert_not_reached ();
435 : : break;
436 : : }
437 [ + + ]: 540693 : if (!error)
438 : 540672 : g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
439 : :
440 : 540693 : pos += data_size;
441 : : }
442 [ - + ]: 21 : if (pos < len + 1)
443 : 0 : g_assert_no_error (error);
444 [ + - ]: 21 : if (error)
445 : 21 : g_error_free (error);
446 : 21 : g_assert_cmpint (pos - data_size, ==, len);
447 : 21 : }
448 : :
449 : : static void
450 : 1 : test_read_int (void)
451 : : {
452 : : GInputStream *stream;
453 : : GInputStream *base_stream;
454 : : GRand *randomizer;
455 : : int i;
456 : : gpointer buffer;
457 : :
458 : 1 : randomizer = g_rand_new ();
459 : 1 : buffer = g_malloc0 (MAX_BYTES);
460 : :
461 : : /* Fill in some random data */
462 [ + + ]: 65537 : for (i = 0; i < MAX_BYTES; i++)
463 : : {
464 : 65536 : guchar x = 0;
465 [ + + ]: 131354 : while (! x)
466 : 65818 : x = (guchar)g_rand_int (randomizer);
467 : 65536 : *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x;
468 : : }
469 : :
470 : 1 : base_stream = g_memory_input_stream_new ();
471 : 1 : stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
472 : 1 : g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
473 : :
474 : :
475 [ + + ]: 4 : for (i = 0; i < 3; i++)
476 : : {
477 : : int j;
478 : 3 : g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
479 : :
480 [ + + ]: 24 : for (j = 0; j <= TEST_DATA_UINT64; j++)
481 : 21 : test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
482 : : }
483 : :
484 : 1 : g_object_unref (base_stream);
485 : 1 : g_object_unref (stream);
486 : 1 : g_rand_free (randomizer);
487 : 1 : g_free (buffer);
488 : 1 : }
489 : :
490 : :
491 : : int
492 : 1 : main (int argc,
493 : : char *argv[])
494 : : {
495 : 1 : g_test_init (&argc, &argv, NULL);
496 : :
497 : 1 : g_test_add_func ("/data-input-stream/basic", test_basic);
498 : 1 : g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
499 : 1 : g_test_add_func ("/data-input-stream/read-lines-LF-valid-utf8", test_read_lines_LF_valid_utf8);
500 : 1 : g_test_add_func ("/data-input-stream/read-lines-LF-invalid-utf8", test_read_lines_LF_invalid_utf8);
501 : 1 : g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
502 : 1 : g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
503 : 1 : g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any);
504 : 1 : g_test_add_func ("/data-input-stream/read-until", test_read_until);
505 : 1 : g_test_add_func ("/data-input-stream/read-upto", test_read_upto);
506 : 1 : g_test_add_func ("/data-input-stream/read-int", test_read_int);
507 : :
508 : 1 : return g_test_run();
509 : : }
|