Line data Source code
1 : // Copyright (c) 2023-2026 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdev
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : /** \file
20 : * \brief Verify that encode()/decode() work as expected.
21 : *
22 : * This file implements tests to verify the encode() and decode() functions
23 : * used to transform an input to base64 and back.
24 : */
25 :
26 : // self
27 : //
28 : #include <snapdev/base64.h>
29 :
30 : #include "catch_main.h"
31 :
32 :
33 : // last include
34 : //
35 : #include <snapdev/poison.h>
36 :
37 :
38 :
39 2 : CATCH_TEST_CASE("base64", "[base64][string]")
40 : {
41 2 : CATCH_START_SECTION("base64: verify encoding & decoding")
42 : {
43 1001 : for(int count(0); count < 1000; ++count)
44 : {
45 : // determine buffer size
46 : //
47 1000 : std::size_t size;
48 1000 : SNAP_CATCH2_NAMESPACE::random(size);
49 1000 : size = size % 2000 + 1;
50 :
51 : // fill buffer with random data
52 : //
53 1000 : std::string input;
54 1009041 : for(std::size_t idx(0); idx < size; ++idx)
55 : {
56 1008041 : std::uint8_t b;
57 1008041 : SNAP_CATCH2_NAMESPACE::random(b);
58 1008041 : input += static_cast<char>(b);
59 : }
60 :
61 : // encode the buffer
62 : //
63 3000 : std::string output("ignore input in the output variable");
64 1000 : snapdev::base64::encode(input, output);
65 :
66 : // encoded size is 4 characters for every 3 bytes of input rounded up
67 : //
68 1000 : std::size_t const expected_size((size + 2) / 3 * 4);
69 1000 : CATCH_REQUIRE(expected_size == output.length());
70 :
71 : // compare with OS base64 command
72 : //
73 3000 : std::string const expected_output(SNAP_CATCH2_NAMESPACE::popen2("base64 --wrap=0", input));
74 : //{
75 : //std::ofstream f("input.bin");
76 : //f.write(input.data(), input.length());
77 : //}
78 : //{
79 : //std::ofstream f("output.b64");
80 : //f.write(output.data(), output.length());
81 : //}
82 : //{
83 : //std::ofstream f("out.b64");
84 : //f.write(expected_output.data(), expected_output.length());
85 : //}
86 1000 : CATCH_REQUIRE_LONG_STRING(expected_output, output);
87 :
88 3000 : std::string back("will this work?");
89 1000 : CATCH_REQUIRE(snapdev::base64::decode(output, back));
90 :
91 1000 : CATCH_REQUIRE(input.length() == back.length());
92 1000 : CATCH_REQUIRE_LONG_STRING(input, back);
93 1000 : }
94 : }
95 2 : CATCH_END_SECTION()
96 :
97 2 : CATCH_START_SECTION("base64: an empty string is valid and decodes as an empty buffer")
98 : {
99 1 : std::string empty;
100 1 : CATCH_REQUIRE(snapdev::base64::decode(std::string(), empty));
101 1 : CATCH_REQUIRE(empty.empty());
102 1 : }
103 2 : CATCH_END_SECTION()
104 2 : }
105 :
106 :
107 1 : CATCH_TEST_CASE("base64_error", "[base64][string][error]")
108 : {
109 1 : CATCH_START_SECTION("base64_error: verify invalid characters make decoder return false")
110 : {
111 256 : for(int c(0); c < 255; ++c)
112 : {
113 765 : std::string bad_base64("ABC");
114 255 : bad_base64 += static_cast<char>(c);
115 255 : bad_base64 += "XYZ";
116 255 : std::string ignore;
117 :
118 255 : if(c == '+'
119 254 : || c == '='
120 253 : || c == '/'
121 252 : || c >= '0' && c <= '9'
122 242 : || c >= 'A' && c <= 'Z'
123 216 : || c >= 'a' && c <= 'z')
124 : {
125 : // c is not a bad character, it works
126 : //
127 65 : CATCH_REQUIRE(snapdev::base64::decode(bad_base64, ignore));
128 65 : }
129 : else
130 : {
131 190 : CATCH_REQUIRE_FALSE(snapdev::base64::decode(bad_base64, ignore));
132 : }
133 255 : }
134 : }
135 1 : CATCH_END_SECTION()
136 1 : }
137 :
138 :
139 :
140 : // vim: ts=4 sw=4 et
|