root/maint/gnulib/tests/test-base32.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Self tests for base32.
   2    Copyright (C) 2004, 2008-2021 Free Software Foundation, Inc.
   3    Based on the tests for base64 written by Simon Josefsson.
   4    Adapted for base32 by Gijs van Tulder.
   5 
   6    This program is free software: you can redistribute it and/or modify
   7    it under the terms of the GNU General Public License as published by
   8    the Free Software Foundation; either version 3 of the License, or
   9    (at your option) any later version.
  10 
  11    This program is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14    GNU General Public License for more details.
  15 
  16    You should have received a copy of the GNU General Public License
  17    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  18 
  19 #include <config.h>
  20 
  21 #include "base32.h"
  22 
  23 #include <stddef.h>
  24 #include <stdbool.h>
  25 #include <stdlib.h>
  26 #include <string.h>
  27 #include <stdint.h>
  28 
  29 #include "macros.h"
  30 
  31 int
  32 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  33 {
  34   const char *in = "abcdefghijklmnop";
  35   const char *b32in = "MFRGGZDFMZTWQ2LKNNWG23TPOA======";
  36   char out[255];
  37   idx_t len;
  38   bool ok;
  39   char *p;
  40 
  41   memset (out, 0x42, sizeof (out));
  42   base32_encode (in, 0, out, 0);
  43   ASSERT (out[0] == '\x42');
  44 
  45   memset (out, 0x42, sizeof (out));
  46   base32_encode (in, 1, out, 10);
  47   ASSERT (memcmp (out, "ME======", 1) == 0);
  48 
  49   memset (out, 0x42, sizeof (out));
  50   base32_encode (in, 1, out, 2);
  51   ASSERT (memcmp (out, "ME======", 2) == 0);
  52 
  53   memset (out, 0x42, sizeof (out));
  54   base32_encode (in, 1, out, 3);
  55   ASSERT (memcmp (out, "ME======", 3) == 0);
  56 
  57   memset (out, 0x42, sizeof (out));
  58   base32_encode (in, 1, out, 4);
  59   ASSERT (memcmp (out, "ME======", 4) == 0);
  60 
  61   memset (out, 0x42, sizeof (out));
  62   base32_encode (in, 1, out, 8);
  63   ASSERT (memcmp (out, "ME======", 8) == 0);
  64 
  65   memset (out, 0x42, sizeof (out));
  66   base32_encode (in, 2, out, 8);
  67   ASSERT (memcmp (out, "MFRA====", 8) == 0);
  68 
  69   memset (out, 0x42, sizeof (out));
  70   base32_encode (in, 3, out, 8);
  71   ASSERT (memcmp (out, "MFRGG===", 8) == 0);
  72 
  73   memset (out, 0x42, sizeof (out));
  74   base32_encode (in, 4, out, 8);
  75   ASSERT (memcmp (out, "MFRGGZA=", 8) == 0);
  76 
  77   memset (out, 0x42, sizeof (out));
  78   base32_encode (in, 5, out, 8);
  79   ASSERT (memcmp (out, "MFRGGZDF", 8) == 0);
  80 
  81   memset (out, 0x42, sizeof (out));
  82   base32_encode (in, 6, out, 16);
  83   ASSERT (memcmp (out, "MFRGGZDFMY======", 16) == 0);
  84 
  85   memset (out, 0x42, sizeof (out));
  86   base32_encode (in, 6, out, 100);
  87   ASSERT (memcmp (out, "MFRGGZDFMY======", 16) == 0);
  88 
  89   /* Decode. */
  90 
  91   memset (out, 0x42, sizeof (out));
  92   len = 0;
  93   ok = base32_decode (b32in, 8, out, &len);
  94   ASSERT (ok);
  95   ASSERT (len == 0);
  96 
  97   memset (out, 0x42, sizeof (out));
  98   len = 1;
  99   ok = base32_decode (b32in, 8, out, &len);
 100   ASSERT (ok);
 101   ASSERT (len == 1);
 102   ASSERT (memcmp (out, "abcdefghijklmnop", 1) == 0);
 103 
 104   memset (out, 0x42, sizeof (out));
 105   len = 2;
 106   ok = base32_decode (b32in, 8, out, &len);
 107   ASSERT (ok);
 108   ASSERT (len == 2);
 109   ASSERT (memcmp (out, "abcdefghijklmnop", 2) == 0);
 110 
 111   memset (out, 0x42, sizeof (out));
 112   len = 3;
 113   ok = base32_decode (b32in, 8, out, &len);
 114   ASSERT (ok);
 115   ASSERT (len == 3);
 116   ASSERT (memcmp (out, "abcdefghijklmnop", 3) == 0);
 117 
 118   memset (out, 0x42, sizeof (out));
 119   len = 4;
 120   ok = base32_decode (b32in, 8, out, &len);
 121   ASSERT (ok);
 122   ASSERT (len == 4);
 123   ASSERT (memcmp (out, "abcdefghijklmnop", 4) == 0);
 124 
 125   memset (out, 0x42, sizeof (out));
 126   len = 5;
 127   ok = base32_decode (b32in, 8, out, &len);
 128   ASSERT (ok);
 129   ASSERT (len == 5);
 130   ASSERT (memcmp (out, "abcdefghijklmnop", 5) == 0);
 131 
 132   memset (out, 0x42, sizeof (out));
 133   len = 6;
 134   ok = base32_decode (b32in, 8, out, &len);
 135   ASSERT (ok);
 136   ASSERT (len == 5);
 137   ASSERT (memcmp (out, "abcdefghijklmnop", 5) == 0);
 138 
 139   memset (out, 0x42, sizeof (out));
 140   len = 100;
 141   ok = base32_decode (b32in, strlen (b32in), out, &len);
 142   ASSERT (ok);
 143   ASSERT (len == 16);
 144   ASSERT (memcmp (out, "abcdefghijklmnop", 16) == 0);
 145 
 146   /* Allocating encode */
 147 
 148   len = base32_encode_alloc (in, strlen (in), &p);
 149   ASSERT (len == 32);
 150   ASSERT (strcmp (p, "MFRGGZDFMZTWQ2LKNNWG23TPOA======") == 0);
 151   free (p);
 152 
 153   len = base32_encode_alloc (in, IDX_MAX - 5, &p);
 154   ASSERT (len == 0);
 155 
 156   /* Decode context function */
 157   {
 158     struct base32_decode_context ctx;
 159 
 160     base32_decode_ctx_init (&ctx);
 161 
 162     len = sizeof (out);
 163     ok = base32_decode_ctx (&ctx, b32in, strlen (b32in), out, &len);
 164     ASSERT (ok);
 165     ASSERT (len == 16);
 166     ASSERT (memcmp (out, "abcdefghijklmnop", len) == 0);
 167   }
 168 
 169   /* Allocating decode context function */
 170 
 171   ok = base32_decode_alloc_ctx (NULL, b32in, strlen (b32in), &p, &len);
 172   ASSERT (ok);
 173   ASSERT (len == 16);
 174   ASSERT (memcmp (out, "abcdefghijklmnop", len) == 0);
 175   free (p);
 176 
 177   {
 178     struct base32_decode_context ctx;
 179     const char *newlineb32 = "MFRG\nGZDFMZTWQ2LKNNW\nG23TPOA======";
 180 
 181     base32_decode_ctx_init (&ctx);
 182 
 183     ok = base32_decode_alloc_ctx (&ctx, newlineb32, strlen (newlineb32), &p, &len);
 184     ASSERT (ok);
 185     ASSERT (len == strlen (in));
 186     ASSERT (memcmp (p, in, len) == 0);
 187     free (p);
 188   }
 189 
 190   {
 191     struct base32_decode_context ctx;
 192     base32_decode_ctx_init (&ctx);
 193 
 194     ok = base32_decode_alloc_ctx (&ctx, "MFRGGZDFM\nZTWQ2LK", 17, &p, &len);
 195     ASSERT (ok);
 196     ASSERT (len == 10);
 197     ASSERT (memcmp (p, "abcdefghij", len) == 0);
 198     free (p);
 199 
 200     base32_decode_ctx_init (&ctx);
 201 
 202     ok = base32_decode_alloc_ctx (&ctx, "MF\n", 3, &p, &len);
 203     ASSERT (ok);
 204     ASSERT (len == 0);
 205     free (p);
 206 
 207     ok = base32_decode_alloc_ctx (&ctx, "RGGZDFMZ", 8, &p, &len);
 208     ASSERT (ok);
 209     ASSERT (len == 5);
 210     ASSERT (memcmp (p, "abcde", len) == 0);
 211     free (p);
 212 
 213     ok = base32_decode_alloc_ctx (&ctx, "TWQ2LK", 6, &p, &len);
 214     ASSERT (ok);
 215     ASSERT (len == 5);
 216     ASSERT (memcmp (p, "fghij", len) == 0);
 217     free (p);
 218 
 219     ok = base32_decode_alloc_ctx (&ctx, "", 0, &p, &len);
 220     ASSERT (ok);
 221     free (p);
 222   }
 223 
 224   {
 225     struct base32_decode_context ctx;
 226     const char *newlineb32 = "\n\n\n\n\n";
 227 
 228     base32_decode_ctx_init (&ctx);
 229 
 230     ok = base32_decode_alloc_ctx (&ctx, newlineb32, strlen (newlineb32), &p, &len);
 231     ASSERT (ok);
 232     ASSERT (len == 0);
 233     free (p);
 234   }
 235 
 236   ok = base32_decode_alloc_ctx (NULL, " ! ", 3, &p, &len);
 237   ASSERT (!ok);
 238 
 239   ok = base32_decode_alloc_ctx (NULL, "ABC\nDEF", 7, &p, &len);
 240   ASSERT (!ok);
 241 
 242   ok = base32_decode_alloc_ctx (NULL, "AA", 2, &p, &len);
 243   ASSERT (!ok);
 244 
 245   ok = base32_decode_alloc_ctx (NULL, "AA=", 3, &p, &len);
 246   ASSERT (!ok);
 247 
 248   ok = base32_decode_alloc_ctx (NULL, "AABBAAxx", 8, &p, &len);
 249   ASSERT (!ok);
 250 
 251   ok = base32_decode_alloc_ctx (NULL, "AABBAA=X", 8, &p, &len);
 252   ASSERT (!ok);
 253 
 254   ok = base32_decode_alloc_ctx (NULL, "AABBAA=X", 8, &p, &len);
 255   ASSERT (!ok);
 256 
 257   ok = base32_decode_alloc_ctx (NULL, "AABBAA=A", 8, &p, &len);
 258   ASSERT (!ok);
 259 
 260   return 0;
 261 }

/* [previous][next][first][last][top][bottom][index][help] */