os/ossrv/glib/tests/testingbase64.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* GLib testing framework examples and tests
     2  * Copyright (C) 2008 Asbjoern Pettersen
     3  * Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.
     4  * This work is provided "as is"; redistribution and modification
     5  * in whole or in part, in any medium, physical or electronic is
     6  * permitted without restriction.
     7  *
     8  * This work is distributed in the hope that it will be useful,
     9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    11  *
    12  * In no event shall the authors or contributors be liable for any
    13  * direct, indirect, incidental, special, exemplary, or consequential
    14  * damages (including, but not limited to, procurement of substitute
    15  * goods or services; loss of use, data, or profits; or business
    16  * interruption) however caused and on any theory of liability, whether
    17  * in contract, strict liability, or tort (including negligence or
    18  * otherwise) arising in any way out of the use of this software, even
    19  * if advised of the possibility of such damage.
    20  */
    21 
    22 #include <glib.h>
    23 #include <stdlib.h>
    24 #include <glib.h>
    25 #include <string.h>
    26 #ifdef HAVE_UNISTD_H
    27 #include <unistd.h>
    28 #endif
    29 #ifdef __SYMBIAN32__
    30 #include "mrt2_glib2_test.h"
    31 #endif /*__SYMBIAN32__*/
    32 #define DATA_SIZE 1024
    33 
    34 struct MyRawData
    35 {
    36   gint length;   /* of data */
    37   guchar data[DATA_SIZE];
    38 };
    39 
    40 /* 100 pre-encoded string from data[] buffer. Data length from 1..100
    41  */
    42 static const char *ok_100_encode_strs[] = {
    43   "AA==",
    44   "AAE=",
    45   "AAEC",
    46   "AAECAw==",
    47   "AAECAwQ=",
    48   "AAECAwQF",
    49   "AAECAwQFBg==",
    50   "AAECAwQFBgc=",
    51   "AAECAwQFBgcI",
    52   "AAECAwQFBgcICQ==",
    53   "AAECAwQFBgcICQo=",
    54   "AAECAwQFBgcICQoL",
    55   "AAECAwQFBgcICQoLDA==",
    56   "AAECAwQFBgcICQoLDA0=",
    57   "AAECAwQFBgcICQoLDA0O",
    58   "AAECAwQFBgcICQoLDA0ODw==",
    59   "AAECAwQFBgcICQoLDA0ODxA=",
    60   "AAECAwQFBgcICQoLDA0ODxAR",
    61   "AAECAwQFBgcICQoLDA0ODxAREg==",
    62   "AAECAwQFBgcICQoLDA0ODxAREhM=",
    63   "AAECAwQFBgcICQoLDA0ODxAREhMU",
    64   "AAECAwQFBgcICQoLDA0ODxAREhMUFQ==",
    65   "AAECAwQFBgcICQoLDA0ODxAREhMUFRY=",
    66   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYX",
    67   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGA==",
    68   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBk=",
    69   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBka",
    70   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGw==",
    71   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxw=",
    72   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwd",
    73   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHg==",
    74   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=",
    75   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8g",
    76   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gIQ==",
    77   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISI=",
    78   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj",
    79   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJA==",
    80   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCU=",
    81   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUm",
    82   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJw==",
    83   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJyg=",
    84   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygp",
    85   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKg==",
    86   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKis=",
    87   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKiss",
    88   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLQ==",
    89   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4=",
    90   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v",
    91   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMA==",
    92   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDE=",
    93   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEy",
    94   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMw==",
    95   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ=",
    96   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1",
    97   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Ng==",
    98   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc=",
    99   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4",
   100   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OQ==",
   101   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo=",
   102   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7",
   103   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PA==",
   104   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0=",
   105   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+",
   106   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==",
   107   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0A=",
   108   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BB",
   109   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQg==",
   110   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkM=",
   111   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNE",
   112   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERQ==",
   113   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUY=",
   114   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH",
   115   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSA==",
   116   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSEk=",
   117   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElK",
   118   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKSw==",
   119   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0w=",
   120   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xN",
   121   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTg==",
   122   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk8=",
   123   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9Q",
   124   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUQ==",
   125   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVI=",
   126   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJT",
   127   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVA==",
   128   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFU=",
   129   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVW",
   130   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWVw==",
   131   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1g=",
   132   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZ",
   133   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==",
   134   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWls=",
   135   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltc",
   136   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXQ==",
   137   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV4=",
   138   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5f",
   139   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYA==",
   140   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGE=",
   141   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFi",
   142   "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiYw==",
   143   NULL
   144 };
   145 
   146 static void
   147 generate_databuffer_for_base64 (struct MyRawData *p)
   148 {
   149   int i;
   150   for (i = 0; i < DATA_SIZE; i++)
   151     p->data[i] = i;
   152 }
   153 
   154 static void
   155 test_base64_encode (void)
   156 {
   157   int i;
   158   gint length = 1;
   159   char *text;
   160   struct MyRawData myraw;
   161 
   162   generate_databuffer_for_base64 (&myraw);
   163 
   164   for (i = 0; ok_100_encode_strs[i]; i++)
   165     {
   166       length = i + 1;
   167       text = g_base64_encode (myraw.data, length);
   168       g_assert_cmpstr (text, ==, ok_100_encode_strs[i]);
   169       /* printf ("\"%s\",\n",text); */
   170       g_free (text);
   171     }
   172 }
   173 
   174 
   175 static void
   176 decode_and_compare (const gchar            *datap,
   177                     const struct MyRawData *p)
   178 {
   179   guchar *data2;
   180   gsize len;
   181   int memcmp_decode;
   182 
   183   data2 = g_base64_decode (datap, &len);
   184   g_assert_cmpint (len, ==, p->length);
   185   /* g_print ("length: got %d, expected %d\n",len, length); */
   186   memcmp_decode = memcmp (p->data, data2, p->length);
   187   g_assert_cmpint (memcmp_decode, ==, 0);
   188   g_free (data2);
   189 }
   190 
   191 static void
   192 test_base64_decode (void)
   193 {
   194   int i;
   195   struct MyRawData myraw;
   196 
   197   generate_databuffer_for_base64 (&myraw);
   198 
   199   for (i = 0; ok_100_encode_strs[i]; i++)
   200     {
   201       myraw.length = i + 1;
   202       decode_and_compare (ok_100_encode_strs[i], &myraw);
   203     }
   204 }
   205 
   206 static void
   207 test_base64_encode_decode (void)
   208 {
   209   int i;
   210   char *text;
   211   struct MyRawData myraw;
   212 
   213   generate_databuffer_for_base64 (&myraw);
   214 
   215   for (i = 0; i < DATA_SIZE; i++)
   216     {
   217       myraw.length = i + 1;
   218       text = g_base64_encode (myraw.data, myraw.length);
   219 
   220       decode_and_compare (text, &myraw);
   221 
   222       g_free (text);
   223     }
   224 }
   225 
   226 
   227 int
   228 main (int   argc,
   229       char *argv[])
   230 {
   231   int retval;
   232     #ifdef __SYMBIAN32__
   233   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);
   234   g_set_print_handler(mrtPrintHandler);
   235   #endif /*__SYMBIAN32__*/
   236   g_test_init (&argc, &argv, NULL);
   237 
   238   g_test_add_func ("/misc/base64/encode", test_base64_encode);
   239   g_test_add_func ("/misc/base64/decode", test_base64_decode);
   240   g_test_add_func ("/misc/base64/encode_decode", test_base64_encode_decode);
   241 
   242   retval = g_test_run();
   243   #if __SYMBIAN32__
   244   testResultXml("testingbase64");
   245   #endif /* EMULATOR */
   246   return retval;
   247 }