~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to unittest/mysys/base64-t.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU General Public License as
 
5
   published by the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful, but
 
8
   WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
10
   General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA */
 
15
 
 
16
#include <my_global.h>
 
17
#include <my_sys.h>
 
18
#include <base64.h>
 
19
#include <tap.h>
 
20
#include <string.h>
 
21
 
 
22
#define BASE64_LOOP_COUNT 500
 
23
#define BASE64_ROWS 4                           /* Number of ok(..) */
 
24
 
 
25
int
 
26
main(void)
 
27
{
 
28
  int i, cmp;
 
29
  size_t j, k, l, dst_len, needed_length;
 
30
  MY_INIT("base64-t");
 
31
 
 
32
  plan(BASE64_LOOP_COUNT * BASE64_ROWS);
 
33
 
 
34
  for (i= 0; i < BASE64_LOOP_COUNT; i++)
 
35
  {
 
36
    /* Create source data */
 
37
    const size_t src_len= rand() % 1000 + 1;
 
38
 
 
39
    char * src= (char *) malloc(src_len);
 
40
    char * s= src;
 
41
    char * str;
 
42
    char * dst;
 
43
 
 
44
    for (j= 0; j<src_len; j++)
 
45
    {
 
46
      char c= rand();
 
47
      *s++= c;
 
48
    }
 
49
 
 
50
    /* Encode */
 
51
    needed_length= base64_needed_encoded_length(src_len);
 
52
    str= (char *) malloc(needed_length);
 
53
    for (k= 0; k < needed_length; k++)
 
54
      str[k]= 0xff; /* Fill memory to check correct NUL termination */
 
55
    ok(base64_encode(src, src_len, str) == 0,
 
56
       "base64_encode: size %d", i);
 
57
    ok(needed_length == strlen(str) + 1,
 
58
       "base64_needed_encoded_length: size %d", i);
 
59
 
 
60
    /* Decode */
 
61
    dst= (char *) malloc(base64_needed_decoded_length(strlen(str)));
 
62
    dst_len= base64_decode(str, strlen(str), dst, NULL);
 
63
    ok(dst_len == src_len, "Comparing lengths");
 
64
 
 
65
    cmp= memcmp(src, dst, src_len);
 
66
    ok(cmp == 0, "Comparing encode-decode result");
 
67
    if (cmp != 0)
 
68
    {
 
69
      char buf[80];
 
70
      diag("       --------- src ---------   --------- dst ---------");
 
71
      for (k= 0; k<src_len; k+=8)
 
72
      {
 
73
        sprintf(buf, "%.4x   ", (uint) k);
 
74
        for (l=0; l<8 && k+l<src_len; l++)
 
75
        {
 
76
          unsigned char c= src[k+l];
 
77
          sprintf(buf, "%.2x ", (unsigned)c);
 
78
        }
 
79
 
 
80
        sprintf(buf, "  ");
 
81
 
 
82
        for (l=0; l<8 && k+l<dst_len; l++)
 
83
        {
 
84
          unsigned char c= dst[k+l];
 
85
          sprintf(buf, "%.2x ", (unsigned)c);
 
86
        }
 
87
        diag(buf);
 
88
      }
 
89
      diag("src length: %.8x, dst length: %.8x\n",
 
90
           (uint) src_len, (uint) dst_len);
 
91
    }
 
92
  }
 
93
  return exit_status();
 
94
}