17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include <my_global.h>
25 namespace mysys_base64_unittest {
27 const int BASE64_LOOP_COUNT= 500;
32 size_t j, k, l, dst_len, needed_length;
35 for (i= 0; i < BASE64_LOOP_COUNT; i++)
38 const size_t src_len= rand() % 1000 + 1;
40 char * src= (
char *) malloc(src_len);
45 for (j= 0; j<src_len; j++)
52 needed_length= base64_needed_encoded_length(src_len);
53 str= (
char *) malloc(needed_length);
54 for (k= 0; k < needed_length; k++)
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;
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";
66 cmp= memcmp(src, dst, src_len);
67 EXPECT_EQ(0, cmp) <<
"Comparing encode-decode result";
72 " --------- src --------- --------- dst ---------";
73 for (k= 0; k<src_len; k+=8)
75 sprintf(buf,
"%.4x ", (uint) k);
76 for (l=0; l<8 && k+l<src_len; l++)
78 unsigned char c= src[k+l];
79 sprintf(buf,
"%.2x ", (
unsigned)c);
84 for (l=0; l<8 && k+l<dst_len; l++)
86 unsigned char c= dst[k+l];
87 sprintf(buf,
"%.2x ", (
unsigned)c);
91 ADD_FAILURE() <<
"src length: " << src_len
92 <<
"dst length: " << dst_len;