1 /* Copyright (c) 2003, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 // First include (the generated) my_config.h, to get correct platform defines.
24 #include "my_config.h"
25 #include <gtest/gtest.h>
26 
27 #include <my_global.h>
28 #include <my_sys.h>
29 #include <base64.h>
30 #include <string.h>
31 
32 namespace mysys_base64_unittest {
33 
34 const int BASE64_LOOP_COUNT= 500;
35 
TEST(Mysys,Base64)36 TEST(Mysys, Base64)
37 {
38   int i, cmp;
39   size_t j, k, l, dst_len, needed_length;
40   MY_INIT("base64-t");
41 
42   for (i= 0; i < BASE64_LOOP_COUNT; i++)
43   {
44     /* Create source data */
45     const size_t src_len= rand() % 1000 + 1;
46 
47     char * src= (char *) malloc(src_len);
48     char * s= src;
49     char * str;
50     char * dst;
51 
52     for (j= 0; j<src_len; j++)
53     {
54       char c= rand();
55       *s++= c;
56     }
57 
58     /* Encode */
59     needed_length= base64_needed_encoded_length(src_len);
60     str= (char *) malloc(needed_length);
61     for (k= 0; k < needed_length; k++)
62       str[k]= (char)0xff; /* Fill memory to check correct NUL termination */
63     EXPECT_EQ(0, base64_encode(src, src_len, str))
64       << "base64_encode: size " << i;
65     EXPECT_EQ(needed_length, strlen(str) + 1)
66       << "base64_needed_encoded_length: " << i;
67 
68     /* Decode */
69     dst= (char *) malloc(base64_needed_decoded_length(strlen(str)));
70     dst_len= base64_decode(str, strlen(str), dst, NULL, 0);
71     EXPECT_EQ(dst_len, src_len) << "Comparing lengths";
72 
73     cmp= memcmp(src, dst, src_len);
74     EXPECT_EQ(0, cmp) << "Comparing encode-decode result";
75     if (cmp != 0)
76     {
77       char buf[80];
78       ADD_FAILURE() <<
79         "       --------- src ---------   --------- dst ---------";
80       for (k= 0; k<src_len; k+=8)
81       {
82         sprintf(buf, "%.4x   ", (uint) k);
83         for (l=0; l<8 && k+l<src_len; l++)
84         {
85           unsigned char c= src[k+l];
86           sprintf(buf, "%.2x ", (unsigned)c);
87         }
88 
89         sprintf(buf, "  ");
90 
91         for (l=0; l<8 && k+l<dst_len; l++)
92         {
93           unsigned char c= dst[k+l];
94           sprintf(buf, "%.2x ", (unsigned)c);
95         }
96         ADD_FAILURE() << buf;
97       }
98       ADD_FAILURE() << "src length: " << src_len
99                     << "dst length: " << dst_len;
100     }
101     free(str);
102     free(src);
103     free(dst);
104   }
105 }
106 
107 }
108