/
Base64Tests.cpp
96 lines (76 loc) · 2.48 KB
/
Base64Tests.cpp
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
/*
* Base64Tests.cpp
*
* Copyright (C) 2020 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#include <tests/TestThat.hpp>
#include <shared_core/Error.hpp>
#include <core/Base64.hpp>
#include <core/StringUtils.hpp>
namespace rstudio {
namespace core {
namespace base64 {
test_context("Base64 Encoding")
{
std::string encoded;
test_that("Various small strings encode correctly")
{
encode("a", &encoded);
expect_true(encoded == "YQ==");
encode("ab", &encoded);
expect_true(encoded == "YWI=");
encode("abc", &encoded);
expect_true(encoded == "YWJj");
encode("abcd", &encoded);
expect_true(encoded == "YWJjZA==");
encode("abcde", &encoded);
expect_true(encoded == "YWJjZGU=");
encode("abcdef", &encoded);
expect_true(encoded == "YWJjZGVm");
}
std::string decoded;
test_that("Various small strings decode correctly")
{
decode("YQ==", &decoded);
expect_true(decoded == "a");
decode("YWI=", &decoded);
expect_true(decoded == "ab");
decode("YWJj", &decoded);
expect_true(decoded == "abc");
decode("YWJjZA==", &decoded);
expect_true(decoded == "abcd");
decode("YWJjZGU=", &decoded);
expect_true(decoded == "abcde");
decode("YWJjZGVm", &decoded);
expect_true(decoded == "abcdef");
}
test_that("Contents are preserved in encode / decode process")
{
Error error;
::srand(1);
for (std::size_t i = 0; i < 100; ++i)
{
std::string random =
string_utils::makeRandomByteString(::rand() % 1024);
std::string encoded;
error = encode(random, &encoded);
expect_true(!error);
std::string decoded;
error = decode(encoded, &decoded);
expect_true(!error);
expect_true(random == decoded);
}
}
}
} // end namespace base64
} // end namespace core
} // end namespace rstudio