data-input-stream.c 14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* GLib testing framework examples and tests
 * Copyright (C) 2008 Red Hat, Inc.
 * Authors: Tomas Bzatek <tbzatek@redhat.com>
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 */
22

23 24 25 26 27 28 29 30
#include <glib/glib.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINES 	0xFFF
#define MAX_BYTES	0x10000	

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
static void
test_basic (void)
{
  GInputStream *stream;
  GInputStream *base_stream;
  gint val;

  base_stream = g_memory_input_stream_new ();
  stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));

  g_object_get (stream, "byte-order", &val, NULL);
  g_assert_cmpint (val, ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
  g_object_set (stream, "byte-order", G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN, NULL);
  g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);

  g_object_get (stream, "newline-type", &val, NULL);
  g_assert_cmpint (val, ==, G_DATA_STREAM_NEWLINE_TYPE_LF);
  g_object_set (stream, "newline-type", G_DATA_STREAM_NEWLINE_TYPE_CR_LF, NULL);
  g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);

  g_object_unref (stream);
  g_object_unref (base_stream);
}

55 56 57 58 59 60
static void
test_seek_to_start (GInputStream *stream)
{
  GError *error = NULL;
  gboolean res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
  g_assert_cmpint (res, ==, TRUE);
61
  g_assert_no_error (error);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
}

static void
test_read_lines (GDataStreamNewlineType newline_type)
{
  GInputStream *stream;
  GInputStream *base_stream;
  GError *error = NULL;
  char *data;
  int line;
  const char* lines[MAX_LINES];
  const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
  
  /*  prepare data */
  int i;
  for (i = 0; i < MAX_LINES; i++)
    lines[i] = "some_text";
	
  base_stream = g_memory_input_stream_new ();
  g_assert (base_stream != NULL);
  stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
  g_assert(stream != NULL);
	
  /*  Byte order testing */
  g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
  g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
  g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
  g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
  
  /*  Line ends testing */
  g_data_input_stream_set_newline_type (G_DATA_INPUT_STREAM (stream), newline_type);
  g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, newline_type);
	

  /*  Add sample data */
  for (i = 0; i < MAX_LINES; i++) 
    g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
99
				    g_strconcat (lines[i], endl[newline_type], NULL), -1, g_free);
100 101 102 103 104 105 106 107 108 109 110 111 112 113

  /*  Seek to the start */
  test_seek_to_start (base_stream);
	
  /*  Test read line */
  error = NULL;
  data = (char*)1;
  line = 0;
  while (data)
    {
      gsize length = -1;
      data = g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
      if (data)
	{
114
	  g_assert_cmpstr (data, ==, lines[line]);
115
          g_free (data);
116
	  g_assert_no_error (error);
117 118
	  line++;
	}
119 120
      if (error)
        g_error_free (error);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    }
  g_assert_cmpint (line, ==, MAX_LINES);
  
  
  g_object_unref (base_stream);
  g_object_unref (stream);
}

static void
test_read_lines_LF (void)
{
  test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
}

static void
test_read_lines_CR (void)
{
  test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
}

static void
test_read_lines_CR_LF (void)
{
  test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
}

147 148 149 150 151
static void
test_read_lines_any (void)
{
  test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_ANY);
}
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static void
test_read_lines_LF_valid_utf8 (void)
{
  GInputStream *stream;
  GInputStream *base_stream;
  GError *error = NULL;
  char *line;
  guint n_lines = 0;
	
  base_stream = g_memory_input_stream_new ();
  stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
	
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
				  "foo\nthis is valid UTF-8 ☺!\nbar\n", -1, NULL);

  /*  Test read line */
  error = NULL;
  while (TRUE)
    {
      gsize length = -1;
      line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
      g_assert_no_error (error);
      if (line == NULL)
	break;
      n_lines++;
      g_free (line);
    }
  g_assert_cmpint (n_lines, ==, 3);
  
  g_object_unref (base_stream);
  g_object_unref (stream);
}

static void
test_read_lines_LF_invalid_utf8 (void)
{
  GInputStream *stream;
  GInputStream *base_stream;
  GError *error = NULL;
  char *line;
  guint n_lines = 0;
	
  base_stream = g_memory_input_stream_new ();
  stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
	
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
				  "foo\nthis is not valid UTF-8 \xE5 =(\nbar\n", -1, NULL);

  /*  Test read line */
  error = NULL;
  while (TRUE)
    {
      gsize length = -1;
      line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
      if (n_lines == 0)
	g_assert_no_error (error);
      else
	{
	  g_assert (error != NULL);
	  g_clear_error (&error);
	  g_free (line);
	  break;
	}
      n_lines++;
      g_free (line);
    }
  g_assert_cmpint (n_lines, ==, 1);
  
  g_object_unref (base_stream);
  g_object_unref (stream);
}

225 226 227 228 229 230 231 232
static void
test_read_until (void)
{
  GInputStream *stream;
  GInputStream *base_stream;
  GError *error = NULL;
  char *data;
  int line;
233
  int i;
234
  
235
#define REPEATS			10   /* number of rounds */
236
#define DATA_STRING		" part1 # part2 $ part3 % part4 ^"
237
#define DATA_PART_LEN		7    /* number of characters between separators */
238
#define DATA_SEP		"#$%^"
239 240
#define DATA_SEP_LEN            4
  const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  
  base_stream = g_memory_input_stream_new ();
  stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
  
  for (i = 0; i < REPEATS; i++)
    g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
  
  /*  Test stop characters */
  error = NULL;
  data = (char*)1;
  line = 0;
  while (data)
    {
      gsize length = -1;
      data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
      if (data)
	{
	  g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
259
          g_free (data);
260
	  g_assert_no_error (error);
261 262 263
	  line++;
	}
    }
264
  g_assert_no_error (error);
265
  g_assert_cmpint (line, ==, DATA_PARTS_NUM);
266

267 268 269 270
  g_object_unref (base_stream);
  g_object_unref (stream);
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
static void
test_read_upto (void)
{
  GInputStream *stream;
  GInputStream *base_stream;
  GError *error = NULL;
  char *data;
  int line;
  int i;
  guchar stop_char;

#undef REPEATS
#undef DATA_STRING
#undef DATA_PART_LEN
#undef DATA_SEP
#undef DATA_SEP_LEN
#define REPEATS			10   /* number of rounds */
#define DATA_STRING		" part1 # part2 $ part3 \0 part4 ^"
#define DATA_PART_LEN		7    /* number of characters between separators */
#define DATA_SEP		"#$\0^"
#define DATA_SEP_LEN            4
  const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;

  base_stream = g_memory_input_stream_new ();
  stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));

  for (i = 0; i < REPEATS; i++)
    g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, 32, NULL);

  /*  Test stop characters */
  error = NULL;
  data = (char*)1;
  line = 0;
  while (data)
    {
      gsize length = -1;
      data = g_data_input_stream_read_upto (G_DATA_INPUT_STREAM (stream), DATA_SEP, DATA_SEP_LEN, &length, NULL, &error);
      if (data)
        {
          g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
          g_assert_no_error (error);
          line++;

          stop_char = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
          g_assert (memchr (DATA_SEP, stop_char, DATA_SEP_LEN) != NULL);
          g_assert_no_error (error);
        }
318
      g_free (data);
319 320 321 322 323 324 325
    }
  g_assert_no_error (error);
  g_assert_cmpint (line, ==, DATA_PARTS_NUM);

  g_object_unref (base_stream);
  g_object_unref (stream);
}
326 327 328 329 330 331 332 333 334 335
enum TestDataType {
  TEST_DATA_BYTE = 0,
  TEST_DATA_INT16,
  TEST_DATA_UINT16,
  TEST_DATA_INT32,
  TEST_DATA_UINT32,
  TEST_DATA_INT64,
  TEST_DATA_UINT64
};

336 337 338 339 340 341 342 343
#define TEST_DATA_RETYPE_BUFF(a, t, v)	\
	 (a == TEST_DATA_BYTE	? (t) *(guchar*)v : \
	 (a == TEST_DATA_INT16	? (t) *(gint16*)v :	 \
	 (a == TEST_DATA_UINT16	? (t) *(guint16*)v : \
	 (a == TEST_DATA_INT32	? (t) *(gint32*)v :	 \
	 (a == TEST_DATA_UINT32	? (t) *(guint32*)v : \
	 (a == TEST_DATA_INT64	? (t) *(gint64*)v :	 \
	 (t) *(guint64*)v )))))) 
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378


static void
test_data_array (GInputStream *stream, GInputStream *base_stream,
		 gpointer buffer, int len,
		 enum TestDataType data_type, GDataStreamByteOrder byte_order)
{
  GError *error = NULL;
  int pos = 0;
  int data_size = 1;
  gint64 data;
  GDataStreamByteOrder native;
  gboolean swap;
  
  /*  Seek to start */
  test_seek_to_start (base_stream);

  /*  Set correct data size */
  switch (data_type)
    {
    case TEST_DATA_BYTE:
      data_size = 1;
      break;
    case TEST_DATA_INT16:
    case TEST_DATA_UINT16:
      data_size = 2;
      break;
    case TEST_DATA_INT32:
    case TEST_DATA_UINT32:
      data_size = 4;
      break;
    case TEST_DATA_INT64:
    case TEST_DATA_UINT64:
      data_size = 8;
      break; 
379 380 381
    default:
      g_assert_not_reached ();
      break;
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    }

  /*  Set flag to swap bytes if needed */
  native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
  swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);

  data = 1;
  while (data != 0)
    {
      switch (data_type)
	{
	case TEST_DATA_BYTE:
	  data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
	  break;
	case TEST_DATA_INT16:
	  data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
	  if (swap)
	    data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
	  break;
	case TEST_DATA_UINT16:
	  data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
	  if (swap)
	    data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
	  break;
	case TEST_DATA_INT32:
	  data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
	  if (swap)
	    data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
	  break;
	case TEST_DATA_UINT32:
	  data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
	  if (swap)
	    data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
	  break;
	case TEST_DATA_INT64:
	  data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
	  if (swap)
	    data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
	  break;
	case TEST_DATA_UINT64:
	  data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
	  if (swap)
	    data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
	  break;
426 427 428
        default:
          g_assert_not_reached ();
          break;
429
	}
430
      if (!error)
431
	g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
432 433 434 435
      
      pos += data_size;
    }
  if (pos < len + 1)
436
    g_assert_no_error (error);
437 438
  if (error)
    g_error_free (error);
439 440 441 442 443 444 445 446
  g_assert_cmpint (pos - data_size, ==, len);
}

static void
test_read_int (void)
{
  GInputStream *stream;
  GInputStream *base_stream;
447
  GRand *randomizer;
448 449 450
  int i;
  gpointer buffer;
  
451
  randomizer = g_rand_new ();
452 453 454 455 456 457 458
  buffer = g_malloc0 (MAX_BYTES);
  
  /*  Fill in some random data */
  for (i = 0; i < MAX_BYTES; i++)
    {
      guchar x = 0;
      while (! x)
459
	x = (guchar)g_rand_int (randomizer);
460
      *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x; 
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    }

  base_stream = g_memory_input_stream_new ();
  stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
  
  
  for (i = 0; i < 3; i++)
    {
      int j;
      g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
      
      for (j = 0; j <= TEST_DATA_UINT64; j++)
	test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
    }

  g_object_unref (base_stream);
  g_object_unref (stream);
479
  g_rand_free (randomizer);
480 481 482 483 484 485 486 487 488 489 490
  g_free (buffer);
}


int
main (int   argc,
      char *argv[])
{
  g_type_init ();
  g_test_init (&argc, &argv, NULL);

491
  g_test_add_func ("/data-input-stream/basic", test_basic);
492
  g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
493 494
  g_test_add_func ("/data-input-stream/read-lines-LF-valid-utf8", test_read_lines_LF_valid_utf8);
  g_test_add_func ("/data-input-stream/read-lines-LF-invalid-utf8", test_read_lines_LF_invalid_utf8);
495 496
  g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
  g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
497
  g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any);
498
  g_test_add_func ("/data-input-stream/read-until", test_read_until);
499
  g_test_add_func ("/data-input-stream/read-upto", test_read_upto);
500 501 502 503
  g_test_add_func ("/data-input-stream/read-int", test_read_int);

  return g_test_run();
}