Bug 547481 – g_data_input_stream_read_line behaves not as stated in the
[platform/upstream/glib.git] / gio / tests / data-input-stream.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2008 Red Hat, Inc.
3  * Authors: Tomas Bzatek <tbzatek@redhat.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 #define MAX_LINES       0xFFF
29 #define MAX_BYTES       0x10000 
30
31 static void
32 test_seek_to_start (GInputStream *stream)
33 {
34   GError *error = NULL;
35   gboolean res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
36   g_assert_cmpint (res, ==, TRUE);
37   g_assert_no_error (error);
38 }
39
40 static void
41 test_read_lines (GDataStreamNewlineType newline_type)
42 {
43   GInputStream *stream;
44   GInputStream *base_stream;
45   GError *error = NULL;
46   char *data;
47   int line;
48   const char* lines[MAX_LINES];
49   const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
50   
51   /*  prepare data */
52   int i;
53   for (i = 0; i < MAX_LINES; i++)
54     lines[i] = "some_text";
55         
56   base_stream = g_memory_input_stream_new ();
57   g_assert (base_stream != NULL);
58   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
59   g_assert(stream != NULL);
60         
61   /*  Byte order testing */
62   g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
63   g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
64   g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
65   g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
66   
67   /*  Line ends testing */
68   g_data_input_stream_set_newline_type (G_DATA_INPUT_STREAM (stream), newline_type);
69   g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, newline_type);
70         
71
72   /*  Add sample data */
73   for (i = 0; i < MAX_LINES; i++) 
74     g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
75                                     g_strconcat (lines[i], endl[newline_type], NULL), -1, NULL);
76
77   /*  Seek to the start */
78   test_seek_to_start (base_stream);
79         
80   /*  Test read line */
81   error = NULL;
82   data = (char*)1;
83   line = 0;
84   while (data)
85     {
86       gsize length = -1;
87       data = g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
88       if (data)
89         {
90           char *expected;
91
92           expected = g_strconcat (lines[line], endl[newline_type], NULL);
93           g_assert_cmpstr (data, ==, expected);
94           g_assert_no_error (error);
95           g_free (expected);  
96           line++;
97         }
98     }
99   g_assert_cmpint (line, ==, MAX_LINES);
100   
101   
102   g_object_unref (base_stream);
103   g_object_unref (stream);
104 }
105
106 static void
107 test_read_lines_LF (void)
108 {
109   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
110 }
111
112 static void
113 test_read_lines_CR (void)
114 {
115   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
116 }
117
118 static void
119 test_read_lines_CR_LF (void)
120 {
121   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
122 }
123
124
125 static void
126 test_read_until (void)
127 {
128   GInputStream *stream;
129   GInputStream *base_stream;
130   GError *error = NULL;
131   char *data;
132   int line;
133   int i;
134   
135 #define REPEATS                 10   /* number of rounds */
136 #define DATA_STRING             " part1 # part2 $ part3 % part4 ^"
137 #define DATA_PART_LEN           7    /* number of characters between separators */
138 #define DATA_SEP                "#$%^"
139   const int DATA_PARTS_NUM = strlen (DATA_SEP) * REPEATS;
140   
141   base_stream = g_memory_input_stream_new ();
142   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
143   
144   for (i = 0; i < REPEATS; i++)
145     g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
146   
147   /*  Test stop characters */
148   error = NULL;
149   data = (char*)1;
150   line = 0;
151   while (data)
152     {
153       gsize length = -1;
154       data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
155       if (data)
156         {
157           g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
158           g_assert_no_error (error);
159           line++;
160         }
161     }
162   g_assert_no_error (error);
163   g_assert_cmpint (line, ==, DATA_PARTS_NUM);
164         
165         
166   g_object_unref (base_stream);
167   g_object_unref (stream);
168 }
169
170 enum TestDataType {
171   TEST_DATA_BYTE = 0,
172   TEST_DATA_INT16,
173   TEST_DATA_UINT16,
174   TEST_DATA_INT32,
175   TEST_DATA_UINT32,
176   TEST_DATA_INT64,
177   TEST_DATA_UINT64
178 };
179
180 #define TEST_DATA_RETYPE_BUFF(a, v)     \
181          (a == TEST_DATA_BYTE   ? *(guchar*)v : \
182          (a == TEST_DATA_INT16  ? *(gint16*)v :  \
183          (a == TEST_DATA_UINT16 ? *(guint16*)v : \
184          (a == TEST_DATA_INT32  ? *(gint32*)v :  \
185          (a == TEST_DATA_UINT32 ? *(guint32*)v : \
186          (a == TEST_DATA_INT64  ? *(gint64*)v :  \
187          *(guint64*)v )))))) 
188
189
190 static void
191 test_data_array (GInputStream *stream, GInputStream *base_stream,
192                  gpointer buffer, int len,
193                  enum TestDataType data_type, GDataStreamByteOrder byte_order)
194 {
195   GError *error = NULL;
196   int pos = 0;
197   int data_size = 1;
198   gint64 data;
199   GDataStreamByteOrder native;
200   gboolean swap;
201   
202   /*  Seek to start */
203   test_seek_to_start (base_stream);
204
205   /*  Set correct data size */
206   switch (data_type)
207     {
208     case TEST_DATA_BYTE:
209       data_size = 1;
210       break;
211     case TEST_DATA_INT16:
212     case TEST_DATA_UINT16:
213       data_size = 2;
214       break;
215     case TEST_DATA_INT32:
216     case TEST_DATA_UINT32:
217       data_size = 4;
218       break;
219     case TEST_DATA_INT64:
220     case TEST_DATA_UINT64:
221       data_size = 8;
222       break; 
223     }
224
225   /*  Set flag to swap bytes if needed */
226   native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
227   swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
228
229   data = 1;
230   while (data != 0)
231     {
232       switch (data_type)
233         {
234         case TEST_DATA_BYTE:
235           data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
236           break;
237         case TEST_DATA_INT16:
238           data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
239           if (swap)
240             data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
241           break;
242         case TEST_DATA_UINT16:
243           data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
244           if (swap)
245             data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
246           break;
247         case TEST_DATA_INT32:
248           data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
249           if (swap)
250             data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
251           break;
252         case TEST_DATA_UINT32:
253           data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
254           if (swap)
255             data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
256           break;
257         case TEST_DATA_INT64:
258           data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
259           if (swap)
260             data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
261           break;
262         case TEST_DATA_UINT64:
263           data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
264           if (swap)
265             data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
266           break;
267         }
268       if ((data) && (! error))  
269         g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, ((guchar*)buffer + pos)));
270       
271       pos += data_size;
272     }
273   if (pos < len + 1)
274     g_assert_no_error (error);
275   g_assert_cmpint (pos - data_size, ==, len);
276 }
277
278 static void
279 test_read_int (void)
280 {
281   GInputStream *stream;
282   GInputStream *base_stream;
283   GRand *rand;
284   int i;
285   gpointer buffer;
286   
287   rand = g_rand_new ();
288   buffer = g_malloc0 (MAX_BYTES);
289   
290   /*  Fill in some random data */
291   for (i = 0; i < MAX_BYTES; i++)
292     {
293       guchar x = 0;
294       while (! x)
295         x = (guchar)g_rand_int (rand);
296       *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x; 
297     }
298
299   base_stream = g_memory_input_stream_new ();
300   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
301   g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
302   
303   
304   for (i = 0; i < 3; i++)
305     {
306       int j;
307       g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
308       
309       for (j = 0; j <= TEST_DATA_UINT64; j++)
310         test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
311     }
312
313   g_object_unref (base_stream);
314   g_object_unref (stream);
315   g_rand_free (rand);
316   g_free (buffer);
317 }
318
319
320 int
321 main (int   argc,
322       char *argv[])
323 {
324   g_type_init ();
325   g_test_init (&argc, &argv, NULL);
326
327   g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
328   g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
329   g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
330   g_test_add_func ("/data-input-stream/read-until", test_read_until);
331   g_test_add_func ("/data-input-stream/read-int", test_read_int);
332
333   return g_test_run();
334 }