File: base64.cpp

package info (click to toggle)
cpprest 2.10.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,916 kB
  • sloc: cpp: 71,086; sh: 275; makefile: 170; javascript: 147
file content (260 lines) | stat: -rw-r--r-- 10,048 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * base64.cpp
 *
 * Tests for base64-related utility functions and classes.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

using namespace utility;

namespace tests
{
namespace functional
{
namespace utils_tests
{
SUITE(base64)
{
    // Note: base64 works by encoding any 3 bytes as a four-byte string. Each triple is encoded independently of
    // previous and subsequent triples. If, for a given set of input bytes, the number is not an even multiple of 3,
    // the remaining 1 or two bytes are encoded and padded using '=' characters at the end. The encoding format is
    // defined by IETF RFC 4648. Such padding is only allowed at the end of a encoded string, which makes it impossible
    // to generally concatenate encoded strings and wind up with a string that is a valid base64 encoding.
    //
    // Since each triple of bytes is independent of others, we don't have to test particularly large sets if input data,
    // validating that the algorithm can process at least two triples should be sufficient.
    //
    TEST(rfc_4648_tests_encode)
    {
        // These tests are what base64 RFC 4648 proposes.
        {
            std::vector<unsigned char> str1;
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("")), utility::conversions::to_base64(str1));
        }
        {
            std::vector<unsigned char> str1;
            str1.push_back('f');
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("Zg==")), utility::conversions::to_base64(str1));
        }
        {
            std::vector<unsigned char> str1;
            str1.push_back('f');
            str1.push_back('o');
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("Zm8=")), utility::conversions::to_base64(str1));
        }
        {
            std::vector<unsigned char> str1;
            str1.push_back('f');
            str1.push_back('o');
            str1.push_back('o');
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("Zm9v")), utility::conversions::to_base64(str1));
        }
        {
            std::vector<unsigned char> str1;
            str1.push_back('f');
            str1.push_back('o');
            str1.push_back('o');
            str1.push_back('b');
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("Zm9vYg==")), utility::conversions::to_base64(str1));
        }
        {
            std::vector<unsigned char> str1;
            str1.push_back('f');
            str1.push_back('o');
            str1.push_back('o');
            str1.push_back('b');
            str1.push_back('a');
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("Zm9vYmE=")), utility::conversions::to_base64(str1));
        }
        {
            std::vector<unsigned char> str1;
            str1.push_back('f');
            str1.push_back('o');
            str1.push_back('o');
            str1.push_back('b');
            str1.push_back('a');
            str1.push_back('r');
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("Zm9vYmFy")), utility::conversions::to_base64(str1));
        }
    }

    TEST(rfc_4648_tests_decode)
    {
        // These tests are what base64 RFC 4648 proposes.
        {
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR(""));
            VERIFY_ARE_EQUAL(0u, str1.size());
        }
        {
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("Zg=="));
            VERIFY_ARE_EQUAL(1u, str1.size());
            VERIFY_ARE_EQUAL('f', str1[0]);
        }
        {
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("Zm8="));
            VERIFY_ARE_EQUAL(2u, str1.size());
            VERIFY_ARE_EQUAL('f', str1[0]);
            VERIFY_ARE_EQUAL('o', str1[1]);
        }
        {
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("Zm9v"));
            VERIFY_ARE_EQUAL(3u, str1.size());
            VERIFY_ARE_EQUAL('f', str1[0]);
            VERIFY_ARE_EQUAL('o', str1[1]);
            VERIFY_ARE_EQUAL('o', str1[2]);
        }
        {
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("Zm9vYg=="));
            VERIFY_ARE_EQUAL(4u, str1.size());
            VERIFY_ARE_EQUAL('f', str1[0]);
            VERIFY_ARE_EQUAL('o', str1[1]);
            VERIFY_ARE_EQUAL('o', str1[2]);
            VERIFY_ARE_EQUAL('b', str1[3]);
        }
        {
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("Zm9vYmE="));
            VERIFY_ARE_EQUAL(5u, str1.size());
            VERIFY_ARE_EQUAL('f', str1[0]);
            VERIFY_ARE_EQUAL('o', str1[1]);
            VERIFY_ARE_EQUAL('o', str1[2]);
            VERIFY_ARE_EQUAL('b', str1[3]);
            VERIFY_ARE_EQUAL('a', str1[4]);
        }
        {
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("Zm9vYmFy"));
            VERIFY_ARE_EQUAL(6u, str1.size());
            VERIFY_ARE_EQUAL('f', str1[0]);
            VERIFY_ARE_EQUAL('o', str1[1]);
            VERIFY_ARE_EQUAL('o', str1[2]);
            VERIFY_ARE_EQUAL('b', str1[3]);
            VERIFY_ARE_EQUAL('a', str1[4]);
            VERIFY_ARE_EQUAL('r', str1[5]);
        }
    }

    TEST(additional_encode)
    {
        {
            // Check '/' encoding
            std::vector<unsigned char> str1;
            str1.push_back(254);
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("/g==")), utility::conversions::to_base64(str1));
        }
        {
            // Check '+' encoding
            std::vector<unsigned char> str1;
            str1.push_back(250);
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("+g==")), utility::conversions::to_base64(str1));
        }
        {
            std::vector<unsigned char> str1;
            str1.push_back('f');
            str1.push_back('o');
            str1.push_back(239);
            str1.push_back('b');
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("Zm/vYg==")), utility::conversions::to_base64(str1));
        }
        {
            std::vector<unsigned char> str1;
            str1.push_back('g');
            str1.push_back(239);
            str1.push_back('o');
            str1.push_back('b');
            VERIFY_ARE_EQUAL(string_t(_XPLATSTR("Z+9vYg==")), utility::conversions::to_base64(str1));
        }
    }

    TEST(additional_decode)
    {
        // Tests beyond what the RFC recommends.
        {
            // Check '/' decoding
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("/g=="));
            VERIFY_ARE_EQUAL(1u, str1.size());
            VERIFY_ARE_EQUAL(254u, str1[0]);
        }
        {
            // Check '+' decoding
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("+g=="));
            VERIFY_ARE_EQUAL(1u, str1.size());
            VERIFY_ARE_EQUAL(250u, str1[0]);
        }
        {
            // Check '/' decoding
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("Zm/vYg=="));
            VERIFY_ARE_EQUAL(4u, str1.size());
            VERIFY_ARE_EQUAL('f', str1[0]);
            VERIFY_ARE_EQUAL('o', str1[1]);
            VERIFY_ARE_EQUAL(239, str1[2]);
            VERIFY_ARE_EQUAL('b', str1[3]);
        }
        {
            // Check '+' decoding
            std::vector<unsigned char> str1 = utility::conversions::from_base64(_XPLATSTR("Z+9vYg=="));
            VERIFY_ARE_EQUAL(4u, str1.size());
            VERIFY_ARE_EQUAL('g', str1[0]);
            VERIFY_ARE_EQUAL(239, str1[1]);
            VERIFY_ARE_EQUAL('o', str1[2]);
            VERIFY_ARE_EQUAL('b', str1[3]);
        }
        {
            // Check the whole base64 alphabet
            std::vector<unsigned char> str1 = utility::conversions::from_base64(
                _XPLATSTR("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));
            VERIFY_ARE_EQUAL(48u, str1.size());
        }
    }

    TEST(bad_decode)
    {
        // These tests are for input that should be disallowed by a very strict decoder, but
        // the available APIs on Windows accept them, as does glib, which is used on Linux.

        // Invalid character before padding, unused ones.
        VERIFY_THROWS(utility::conversions::from_base64(_XPLATSTR("/q==")), std::runtime_error);
        VERIFY_THROWS(utility::conversions::from_base64(_XPLATSTR("Zm9vYmD=")), std::runtime_error);

        // CRLF in the middle.
        VERIFY_THROWS(utility::conversions::from_base64(
                          _XPLATSTR("ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\nabcdefghijklmnopqrstuvwxyz\r\n0123456789+/")),
                      std::runtime_error);

        // Not the right length.
        VERIFY_THROWS(utility::conversions::from_base64(_XPLATSTR("/q")), std::runtime_error);
        // Characters not in the alphabet
        VERIFY_THROWS(utility::conversions::from_base64(_XPLATSTR("$%#@")), std::runtime_error);
        // Too much padding at the end.
        VERIFY_THROWS(utility::conversions::from_base64(_XPLATSTR("/q=========")), std::runtime_error);
        // Valid strings, concatenated
        VERIFY_THROWS(utility::conversions::from_base64(_XPLATSTR("Z+9vYg==Z+9vYg==")), std::runtime_error);
    }

    TEST(large_string)
    {
        const size_t size = 64 * 1024;

        std::vector<unsigned char> data(size);
        for (auto i = 0u; i < size; ++i)
        {
            data[i] = (unsigned char)(rand() & 0xFF);
        }

        auto string = utility::conversions::to_base64(data);
        auto data2 = utility::conversions::from_base64(string);

        VERIFY_ARE_EQUAL(data, data2);
    }

} // SUITE(base64)

} // namespace utils_tests
} // namespace functional
} // namespace tests