os/ossrv/glib/tests/base64-test.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.
sl@0
     3
*/
sl@0
     4
#include "config.h"
sl@0
     5
sl@0
     6
#include <glib.h>
sl@0
     7
#include <string.h>
sl@0
     8
#ifdef HAVE_UNISTD_H
sl@0
     9
#include <unistd.h>
sl@0
    10
#endif
sl@0
    11
#include <stdlib.h>
sl@0
    12
#ifdef __SYMBIAN32__
sl@0
    13
#include "mrt2_glib2_test.h"
sl@0
    14
#endif /*__SYMBIAN32__*/
sl@0
    15
sl@0
    16
#define DATA_SIZE 1024
sl@0
    17
#define BLOCK_SIZE 32
sl@0
    18
#define NUM_BLOCKS 32
sl@0
    19
static guchar data[DATA_SIZE];
sl@0
    20
sl@0
    21
static void
sl@0
    22
test_incremental (gboolean line_break, 
sl@0
    23
		  gint     length)
sl@0
    24
{
sl@0
    25
  char *p;
sl@0
    26
  gsize len, decoded_len, max, input_len, block_size;
sl@0
    27
  int state, save;
sl@0
    28
  guint decoder_save;
sl@0
    29
  char *text;
sl@0
    30
  guchar *data2;
sl@0
    31
sl@0
    32
  data2 = g_malloc (length);
sl@0
    33
  text = g_malloc (length * 4);
sl@0
    34
sl@0
    35
  len = 0;
sl@0
    36
  state = 0;
sl@0
    37
  save = 0;
sl@0
    38
  input_len = 0;
sl@0
    39
  while (input_len < length)
sl@0
    40
    {
sl@0
    41
      block_size = MIN (BLOCK_SIZE, length - input_len);
sl@0
    42
      len += g_base64_encode_step (data + input_len, block_size,
sl@0
    43
				   line_break, text + len, &state, &save);
sl@0
    44
      input_len += block_size;
sl@0
    45
    }
sl@0
    46
  len += g_base64_encode_close (line_break, text + len, &state, &save);
sl@0
    47
sl@0
    48
  if (line_break)
sl@0
    49
    max = length * 4 / 3 + length * 4 / (3 * 72) + 7;
sl@0
    50
  else
sl@0
    51
    max = length * 4 / 3 + 6;
sl@0
    52
  if (len > max)
sl@0
    53
    {
sl@0
    54
      g_print ("Too long encoded length: got %d, expected max %d\n",
sl@0
    55
	       len, max);
sl@0
    56
      exit (1);
sl@0
    57
    }
sl@0
    58
sl@0
    59
  decoded_len = 0;
sl@0
    60
  state = 0;
sl@0
    61
  decoder_save = 0;
sl@0
    62
  p = text;
sl@0
    63
  while (len > 0)
sl@0
    64
    {
sl@0
    65
      int chunk_len = MIN (BLOCK_SIZE, len);
sl@0
    66
      decoded_len += g_base64_decode_step (p, 
sl@0
    67
					   chunk_len, 
sl@0
    68
					   data2 + decoded_len,
sl@0
    69
					   &state, &decoder_save);
sl@0
    70
      p += chunk_len;
sl@0
    71
      len -= chunk_len;
sl@0
    72
    }
sl@0
    73
 
sl@0
    74
  if (decoded_len != length)
sl@0
    75
    {
sl@0
    76
      g_print ("Wrong decoded length: got %d, expected %d\n",
sl@0
    77
	       decoded_len, length);
sl@0
    78
      exit (1);
sl@0
    79
    }
sl@0
    80
sl@0
    81
  if (memcmp (data, data2, length) != 0)
sl@0
    82
    {
sl@0
    83
      g_print ("Wrong decoded base64 data\n");
sl@0
    84
      exit (1);
sl@0
    85
    }
sl@0
    86
sl@0
    87
  g_free (text);
sl@0
    88
  g_free (data2);
sl@0
    89
}
sl@0
    90
sl@0
    91
static void
sl@0
    92
test_full (gint length)
sl@0
    93
{
sl@0
    94
  char *text;
sl@0
    95
  guchar *data2;
sl@0
    96
  gsize len;
sl@0
    97
sl@0
    98
  text = g_base64_encode (data, length);
sl@0
    99
  data2 = g_base64_decode (text, &len);
sl@0
   100
  g_free (text);
sl@0
   101
sl@0
   102
  if (len != length)
sl@0
   103
    {
sl@0
   104
      g_print ("Wrong decoded length: got %d, expected %d\n",
sl@0
   105
	       len, length);
sl@0
   106
      exit (1);
sl@0
   107
    }
sl@0
   108
sl@0
   109
  if (memcmp (data, data2, length) != 0)
sl@0
   110
    {
sl@0
   111
      g_print ("Wrong decoded base64 data\n");
sl@0
   112
      exit (1);
sl@0
   113
    }
sl@0
   114
sl@0
   115
  g_free (data2);
sl@0
   116
}
sl@0
   117
sl@0
   118
int
sl@0
   119
main (int argc, char *argv[])
sl@0
   120
{
sl@0
   121
  int i;
sl@0
   122
sl@0
   123
  #ifdef __SYMBIAN32__
sl@0
   124
  g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
sl@0
   125
  g_set_print_handler(mrtPrintHandler);
sl@0
   126
  #endif /*__SYMBIAN32__*/
sl@0
   127
sl@0
   128
  for (i = 0; i < DATA_SIZE; i++)
sl@0
   129
    data[i] = (guchar)i;
sl@0
   130
sl@0
   131
  test_full (DATA_SIZE);
sl@0
   132
  test_full (1);
sl@0
   133
  test_full (2);
sl@0
   134
  test_full (3);
sl@0
   135
sl@0
   136
  test_incremental (FALSE, DATA_SIZE);
sl@0
   137
  test_incremental (TRUE, DATA_SIZE);
sl@0
   138
sl@0
   139
  test_incremental (FALSE, DATA_SIZE - 1);
sl@0
   140
  test_incremental (TRUE, DATA_SIZE - 1);
sl@0
   141
sl@0
   142
  test_incremental (FALSE, DATA_SIZE - 2);
sl@0
   143
  test_incremental (TRUE, DATA_SIZE - 2);
sl@0
   144
sl@0
   145
  test_incremental (FALSE, 1);
sl@0
   146
  test_incremental (FALSE, 2);
sl@0
   147
  test_incremental (FALSE, 3);
sl@0
   148
  #ifdef __SYMBIAN32__
sl@0
   149
  testResultXml("base64-test");
sl@0
   150
  #endif /* EMULATOR */
sl@0
   151
sl@0
   152
  return 0;
sl@0
   153
}