glib/tests/base64-test.c
changeset 18 47c74d1534e1
child 34 5fae379060a7
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "config.h"
       
    19 
       
    20 #include <glib.h>
       
    21 #include <string.h>
       
    22 #ifdef HAVE_UNISTD_H
       
    23 #include <unistd.h>
       
    24 #endif
       
    25 #include <stdlib.h>
       
    26 #ifdef __SYMBIAN32__
       
    27 #include "mrt2_glib2_test.h"
       
    28 #endif /*__SYMBIAN32__*/
       
    29 
       
    30 #define DATA_SIZE 1024
       
    31 #define BLOCK_SIZE 32
       
    32 #define NUM_BLOCKS 32
       
    33 static guchar data[DATA_SIZE];
       
    34 
       
    35 static void
       
    36 test_incremental (gboolean line_break, 
       
    37 		  gint     length)
       
    38 {
       
    39   char *p;
       
    40   gsize len, decoded_len, max, input_len, block_size;
       
    41   int state, save;
       
    42   guint decoder_save;
       
    43   char *text;
       
    44   guchar *data2;
       
    45 
       
    46   data2 = g_malloc (length);
       
    47   text = g_malloc (length * 4);
       
    48 
       
    49   len = 0;
       
    50   state = 0;
       
    51   save = 0;
       
    52   input_len = 0;
       
    53   while (input_len < length)
       
    54     {
       
    55       block_size = MIN (BLOCK_SIZE, length - input_len);
       
    56       len += g_base64_encode_step (data + input_len, block_size,
       
    57 				   line_break, text + len, &state, &save);
       
    58       input_len += block_size;
       
    59     }
       
    60   len += g_base64_encode_close (line_break, text + len, &state, &save);
       
    61 
       
    62   if (line_break)
       
    63     max = length * 4 / 3 + length * 4 / (3 * 72) + 7;
       
    64   else
       
    65     max = length * 4 / 3 + 6;
       
    66   if (len > max)
       
    67     {
       
    68       g_print ("Too long encoded length: got %d, expected max %d\n",
       
    69 	       len, max);
       
    70       exit (1);
       
    71     }
       
    72 
       
    73   decoded_len = 0;
       
    74   state = 0;
       
    75   decoder_save = 0;
       
    76   p = text;
       
    77   while (len > 0)
       
    78     {
       
    79       int chunk_len = MIN (BLOCK_SIZE, len);
       
    80       decoded_len += g_base64_decode_step (p, 
       
    81 					   chunk_len, 
       
    82 					   data2 + decoded_len,
       
    83 					   &state, &decoder_save);
       
    84       p += chunk_len;
       
    85       len -= chunk_len;
       
    86     }
       
    87  
       
    88   if (decoded_len != length)
       
    89     {
       
    90       g_print ("Wrong decoded length: got %d, expected %d\n",
       
    91 	       decoded_len, length);
       
    92       exit (1);
       
    93     }
       
    94 
       
    95   if (memcmp (data, data2, length) != 0)
       
    96     {
       
    97       g_print ("Wrong decoded base64 data\n");
       
    98       exit (1);
       
    99     }
       
   100 
       
   101   g_free (text);
       
   102   g_free (data2);
       
   103 }
       
   104 
       
   105 static void
       
   106 test_full (gint length)
       
   107 {
       
   108   char *text;
       
   109   guchar *data2;
       
   110   gsize len;
       
   111 
       
   112   text = g_base64_encode (data, length);
       
   113   data2 = g_base64_decode (text, &len);
       
   114   g_free (text);
       
   115 
       
   116   if (len != length)
       
   117     {
       
   118       g_print ("Wrong decoded length: got %d, expected %d\n",
       
   119 	       len, length);
       
   120       exit (1);
       
   121     }
       
   122 
       
   123   if (memcmp (data, data2, length) != 0)
       
   124     {
       
   125       g_print ("Wrong decoded base64 data\n");
       
   126       exit (1);
       
   127     }
       
   128 
       
   129   g_free (data2);
       
   130 }
       
   131 
       
   132 int
       
   133 main (int argc, char *argv[])
       
   134 {
       
   135   int i;
       
   136 
       
   137   #ifdef __SYMBIAN32__
       
   138   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);
       
   139   g_set_print_handler(mrtPrintHandler);
       
   140   #endif /*__SYMBIAN32__*/
       
   141 
       
   142   for (i = 0; i < DATA_SIZE; i++)
       
   143     data[i] = (guchar)i;
       
   144 
       
   145   test_full (DATA_SIZE);
       
   146   test_full (1);
       
   147   test_full (2);
       
   148   test_full (3);
       
   149 
       
   150   test_incremental (FALSE, DATA_SIZE);
       
   151   test_incremental (TRUE, DATA_SIZE);
       
   152 
       
   153   test_incremental (FALSE, DATA_SIZE - 1);
       
   154   test_incremental (TRUE, DATA_SIZE - 1);
       
   155 
       
   156   test_incremental (FALSE, DATA_SIZE - 2);
       
   157   test_incremental (TRUE, DATA_SIZE - 2);
       
   158 
       
   159   test_incremental (FALSE, 1);
       
   160   test_incremental (FALSE, 2);
       
   161   test_incremental (FALSE, 3);
       
   162   #ifdef __SYMBIAN32__
       
   163   testResultXml("base64-test");
       
   164   #endif /* EMULATOR */
       
   165 
       
   166   return 0;
       
   167 }