MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mysys_base64-t.cc
1 /* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include <my_global.h>
21 #include <my_sys.h>
22 #include <base64.h>
23 #include <string.h>
24 
25 namespace mysys_base64_unittest {
26 
27 const int BASE64_LOOP_COUNT= 500;
28 
29 TEST(Mysys, Base64)
30 {
31  int i, cmp;
32  size_t j, k, l, dst_len, needed_length;
33  MY_INIT("base64-t");
34 
35  for (i= 0; i < BASE64_LOOP_COUNT; i++)
36  {
37  /* Create source data */
38  const size_t src_len= rand() % 1000 + 1;
39 
40  char * src= (char *) malloc(src_len);
41  char * s= src;
42  char * str;
43  char * dst;
44 
45  for (j= 0; j<src_len; j++)
46  {
47  char c= rand();
48  *s++= c;
49  }
50 
51  /* Encode */
52  needed_length= base64_needed_encoded_length(src_len);
53  str= (char *) malloc(needed_length);
54  for (k= 0; k < needed_length; k++)
55  str[k]= 0xff; /* Fill memory to check correct NUL termination */
56  EXPECT_EQ(0, base64_encode(src, src_len, str))
57  << "base64_encode: size " << i;
58  EXPECT_EQ(needed_length, strlen(str) + 1)
59  << "base64_needed_encoded_length: " << i;
60 
61  /* Decode */
62  dst= (char *) malloc(base64_needed_decoded_length(strlen(str)));
63  dst_len= base64_decode(str, strlen(str), dst, NULL, 0);
64  EXPECT_EQ(dst_len, src_len) << "Comparing lengths";
65 
66  cmp= memcmp(src, dst, src_len);
67  EXPECT_EQ(0, cmp) << "Comparing encode-decode result";
68  if (cmp != 0)
69  {
70  char buf[80];
71  ADD_FAILURE() <<
72  " --------- src --------- --------- dst ---------";
73  for (k= 0; k<src_len; k+=8)
74  {
75  sprintf(buf, "%.4x ", (uint) k);
76  for (l=0; l<8 && k+l<src_len; l++)
77  {
78  unsigned char c= src[k+l];
79  sprintf(buf, "%.2x ", (unsigned)c);
80  }
81 
82  sprintf(buf, " ");
83 
84  for (l=0; l<8 && k+l<dst_len; l++)
85  {
86  unsigned char c= dst[k+l];
87  sprintf(buf, "%.2x ", (unsigned)c);
88  }
89  ADD_FAILURE() << buf;
90  }
91  ADD_FAILURE() << "src length: " << src_len
92  << "dst length: " << dst_len;
93  }
94  free(str);
95  free(src);
96  free(dst);
97  }
98 }
99 
100 }