1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tests/base64-test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,153 @@
1.4 +/*
1.5 +* Portions copyright (c) 2009 Nokia Corporation. All rights reserved.
1.6 +*/
1.7 +#include "config.h"
1.8 +
1.9 +#include <glib.h>
1.10 +#include <string.h>
1.11 +#ifdef HAVE_UNISTD_H
1.12 +#include <unistd.h>
1.13 +#endif
1.14 +#include <stdlib.h>
1.15 +#ifdef __SYMBIAN32__
1.16 +#include "mrt2_glib2_test.h"
1.17 +#endif /*__SYMBIAN32__*/
1.18 +
1.19 +#define DATA_SIZE 1024
1.20 +#define BLOCK_SIZE 32
1.21 +#define NUM_BLOCKS 32
1.22 +static guchar data[DATA_SIZE];
1.23 +
1.24 +static void
1.25 +test_incremental (gboolean line_break,
1.26 + gint length)
1.27 +{
1.28 + char *p;
1.29 + gsize len, decoded_len, max, input_len, block_size;
1.30 + int state, save;
1.31 + guint decoder_save;
1.32 + char *text;
1.33 + guchar *data2;
1.34 +
1.35 + data2 = g_malloc (length);
1.36 + text = g_malloc (length * 4);
1.37 +
1.38 + len = 0;
1.39 + state = 0;
1.40 + save = 0;
1.41 + input_len = 0;
1.42 + while (input_len < length)
1.43 + {
1.44 + block_size = MIN (BLOCK_SIZE, length - input_len);
1.45 + len += g_base64_encode_step (data + input_len, block_size,
1.46 + line_break, text + len, &state, &save);
1.47 + input_len += block_size;
1.48 + }
1.49 + len += g_base64_encode_close (line_break, text + len, &state, &save);
1.50 +
1.51 + if (line_break)
1.52 + max = length * 4 / 3 + length * 4 / (3 * 72) + 7;
1.53 + else
1.54 + max = length * 4 / 3 + 6;
1.55 + if (len > max)
1.56 + {
1.57 + g_print ("Too long encoded length: got %d, expected max %d\n",
1.58 + len, max);
1.59 + exit (1);
1.60 + }
1.61 +
1.62 + decoded_len = 0;
1.63 + state = 0;
1.64 + decoder_save = 0;
1.65 + p = text;
1.66 + while (len > 0)
1.67 + {
1.68 + int chunk_len = MIN (BLOCK_SIZE, len);
1.69 + decoded_len += g_base64_decode_step (p,
1.70 + chunk_len,
1.71 + data2 + decoded_len,
1.72 + &state, &decoder_save);
1.73 + p += chunk_len;
1.74 + len -= chunk_len;
1.75 + }
1.76 +
1.77 + if (decoded_len != length)
1.78 + {
1.79 + g_print ("Wrong decoded length: got %d, expected %d\n",
1.80 + decoded_len, length);
1.81 + exit (1);
1.82 + }
1.83 +
1.84 + if (memcmp (data, data2, length) != 0)
1.85 + {
1.86 + g_print ("Wrong decoded base64 data\n");
1.87 + exit (1);
1.88 + }
1.89 +
1.90 + g_free (text);
1.91 + g_free (data2);
1.92 +}
1.93 +
1.94 +static void
1.95 +test_full (gint length)
1.96 +{
1.97 + char *text;
1.98 + guchar *data2;
1.99 + gsize len;
1.100 +
1.101 + text = g_base64_encode (data, length);
1.102 + data2 = g_base64_decode (text, &len);
1.103 + g_free (text);
1.104 +
1.105 + if (len != length)
1.106 + {
1.107 + g_print ("Wrong decoded length: got %d, expected %d\n",
1.108 + len, length);
1.109 + exit (1);
1.110 + }
1.111 +
1.112 + if (memcmp (data, data2, length) != 0)
1.113 + {
1.114 + g_print ("Wrong decoded base64 data\n");
1.115 + exit (1);
1.116 + }
1.117 +
1.118 + g_free (data2);
1.119 +}
1.120 +
1.121 +int
1.122 +main (int argc, char *argv[])
1.123 +{
1.124 + int i;
1.125 +
1.126 + #ifdef __SYMBIAN32__
1.127 + 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);
1.128 + g_set_print_handler(mrtPrintHandler);
1.129 + #endif /*__SYMBIAN32__*/
1.130 +
1.131 + for (i = 0; i < DATA_SIZE; i++)
1.132 + data[i] = (guchar)i;
1.133 +
1.134 + test_full (DATA_SIZE);
1.135 + test_full (1);
1.136 + test_full (2);
1.137 + test_full (3);
1.138 +
1.139 + test_incremental (FALSE, DATA_SIZE);
1.140 + test_incremental (TRUE, DATA_SIZE);
1.141 +
1.142 + test_incremental (FALSE, DATA_SIZE - 1);
1.143 + test_incremental (TRUE, DATA_SIZE - 1);
1.144 +
1.145 + test_incremental (FALSE, DATA_SIZE - 2);
1.146 + test_incremental (TRUE, DATA_SIZE - 2);
1.147 +
1.148 + test_incremental (FALSE, 1);
1.149 + test_incremental (FALSE, 2);
1.150 + test_incremental (FALSE, 3);
1.151 + #ifdef __SYMBIAN32__
1.152 + testResultXml("base64-test");
1.153 + #endif /* EMULATOR */
1.154 +
1.155 + return 0;
1.156 +}