glib/tests/base64-test.c
changeset 18 47c74d1534e1
child 34 5fae379060a7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/glib/tests/base64-test.c	Fri Apr 16 16:46:38 2010 +0300
@@ -0,0 +1,167 @@
+/*
+* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
+* All rights reserved.
+* This component and the accompanying materials are made available
+* under the terms of "Eclipse Public License v1.0"
+* which accompanies this distribution, and is available
+* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* Initial Contributors:
+* Nokia Corporation - initial contribution.
+*
+* Contributors:
+*
+* Description: 
+*
+*/
+
+#include "config.h"
+
+#include <glib.h>
+#include <string.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <stdlib.h>
+#ifdef __SYMBIAN32__
+#include "mrt2_glib2_test.h"
+#endif /*__SYMBIAN32__*/
+
+#define DATA_SIZE 1024
+#define BLOCK_SIZE 32
+#define NUM_BLOCKS 32
+static guchar data[DATA_SIZE];
+
+static void
+test_incremental (gboolean line_break, 
+		  gint     length)
+{
+  char *p;
+  gsize len, decoded_len, max, input_len, block_size;
+  int state, save;
+  guint decoder_save;
+  char *text;
+  guchar *data2;
+
+  data2 = g_malloc (length);
+  text = g_malloc (length * 4);
+
+  len = 0;
+  state = 0;
+  save = 0;
+  input_len = 0;
+  while (input_len < length)
+    {
+      block_size = MIN (BLOCK_SIZE, length - input_len);
+      len += g_base64_encode_step (data + input_len, block_size,
+				   line_break, text + len, &state, &save);
+      input_len += block_size;
+    }
+  len += g_base64_encode_close (line_break, text + len, &state, &save);
+
+  if (line_break)
+    max = length * 4 / 3 + length * 4 / (3 * 72) + 7;
+  else
+    max = length * 4 / 3 + 6;
+  if (len > max)
+    {
+      g_print ("Too long encoded length: got %d, expected max %d\n",
+	       len, max);
+      exit (1);
+    }
+
+  decoded_len = 0;
+  state = 0;
+  decoder_save = 0;
+  p = text;
+  while (len > 0)
+    {
+      int chunk_len = MIN (BLOCK_SIZE, len);
+      decoded_len += g_base64_decode_step (p, 
+					   chunk_len, 
+					   data2 + decoded_len,
+					   &state, &decoder_save);
+      p += chunk_len;
+      len -= chunk_len;
+    }
+ 
+  if (decoded_len != length)
+    {
+      g_print ("Wrong decoded length: got %d, expected %d\n",
+	       decoded_len, length);
+      exit (1);
+    }
+
+  if (memcmp (data, data2, length) != 0)
+    {
+      g_print ("Wrong decoded base64 data\n");
+      exit (1);
+    }
+
+  g_free (text);
+  g_free (data2);
+}
+
+static void
+test_full (gint length)
+{
+  char *text;
+  guchar *data2;
+  gsize len;
+
+  text = g_base64_encode (data, length);
+  data2 = g_base64_decode (text, &len);
+  g_free (text);
+
+  if (len != length)
+    {
+      g_print ("Wrong decoded length: got %d, expected %d\n",
+	       len, length);
+      exit (1);
+    }
+
+  if (memcmp (data, data2, length) != 0)
+    {
+      g_print ("Wrong decoded base64 data\n");
+      exit (1);
+    }
+
+  g_free (data2);
+}
+
+int
+main (int argc, char *argv[])
+{
+  int i;
+
+  #ifdef __SYMBIAN32__
+  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);
+  g_set_print_handler(mrtPrintHandler);
+  #endif /*__SYMBIAN32__*/
+
+  for (i = 0; i < DATA_SIZE; i++)
+    data[i] = (guchar)i;
+
+  test_full (DATA_SIZE);
+  test_full (1);
+  test_full (2);
+  test_full (3);
+
+  test_incremental (FALSE, DATA_SIZE);
+  test_incremental (TRUE, DATA_SIZE);
+
+  test_incremental (FALSE, DATA_SIZE - 1);
+  test_incremental (TRUE, DATA_SIZE - 1);
+
+  test_incremental (FALSE, DATA_SIZE - 2);
+  test_incremental (TRUE, DATA_SIZE - 2);
+
+  test_incremental (FALSE, 1);
+  test_incremental (FALSE, 2);
+  test_incremental (FALSE, 3);
+  #ifdef __SYMBIAN32__
+  testResultXml("base64-test");
+  #endif /* EMULATOR */
+
+  return 0;
+}