Debian Codegraph @

unittest/gunit/mysys_base64-t.cc

raw symbols SCIP
1 /* Copyright (c) 2003, 2026, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25 #include <string.h>
26
27 #include "base64.h"
28 #include "my_sys.h"
29
30 namespace mysys_base64_unittest {
31
32 const int BASE64_LOOP_COUNT = 500;
33
34 TEST(Mysys, Base64) {
35 int i, cmp;
36 size_t j, k, l, dst_len, needed_length;
37 MY_INIT("base64-t");
38
39 for (i = 0; i < BASE64_LOOP_COUNT; i++) {
40 /* Create source data */
41 const size_t src_len = rand() % 1000 + 1;
42
43 char *src = (char *)malloc(src_len);
44 char *s = src;
45 char *str;
46 char *dst;
47
48 for (j = 0; j < src_len; j++) {
49 char c = rand();
50 *s++ = c;
51 }
52
53 /* Encode */
54 needed_length = base64_needed_encoded_length(src_len);
55 str = (char *)malloc(needed_length);
56 for (k = 0; k < needed_length; k++)
57 str[k] = (char)0xff; /* Fill memory to check correct NUL termination */
58 EXPECT_EQ(0, base64_encode(src, src_len, str))
59 << "base64_encode: size " << i;
60 EXPECT_EQ(needed_length, strlen(str) + 1)
61 << "base64_needed_encoded_length: " << i;
62
63 /* Decode */
64 dst = (char *)malloc(base64_needed_decoded_length(strlen(str)));
65 dst_len = base64_decode(str, strlen(str), dst, nullptr, 0);
66 EXPECT_EQ(dst_len, src_len) << "Comparing lengths";
67
68 cmp = memcmp(src, dst, src_len);
69 EXPECT_EQ(0, cmp) << "Comparing encode-decode result";
70 if (cmp != 0) {
71 char buf[80];
72 ADD_FAILURE()
73 << " --------- src --------- --------- dst ---------";
74 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++) {
77 unsigned char c = src[k + l];
78 sprintf(buf, "%.2x ", (unsigned)c);
79 }
80
81 sprintf(buf, " ");
82
83 for (l = 0; l < 8 && k + l < dst_len; l++) {
84 unsigned char c = dst[k + l];
85 sprintf(buf, "%.2x ", (unsigned)c);
86 }
87 ADD_FAILURE() << buf;
88 }
89 ADD_FAILURE() << "src length: " << src_len << "dst length: " << dst_len;
90 }
91 free(str);
92 free(src);
93 free(dst);
94 }
95 }
96
97 } // namespace mysys_base64_unittest