-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64Tests.cpp
More file actions
79 lines (68 loc) 路 2.66 KB
/
Copy pathBase64Tests.cpp
File metadata and controls
79 lines (68 loc) 路 2.66 KB
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
/// @file
/// @brief Unit tests for the base64 helper (Internal/utils/Base64.h) used by settings share codes.
///
/// Covers the RFC 4648 test vectors, encode/decode round-trips over binary data (all byte values),
/// the three padding lengths, and the documented decode behaviour of stopping at the first
/// non-base64 character.
#include <string>
#include <gtest/gtest.h>
// Tests' IncludePath doesn't cover Internal/utils, so reach the header by relative path.
#include "../Internal/utils/Base64.h"
TEST(Base64Test, EncodesKnownVectors)
{
// RFC 4648 搂10 examples; exercise all three padding cases.
EXPECT_EQ(Base64::Encode(""), "");
EXPECT_EQ(Base64::Encode("f"), "Zg==");
EXPECT_EQ(Base64::Encode("fo"), "Zm8=");
EXPECT_EQ(Base64::Encode("foo"), "Zm9v");
EXPECT_EQ(Base64::Encode("foob"), "Zm9vYg==");
EXPECT_EQ(Base64::Encode("fooba"), "Zm9vYmE=");
EXPECT_EQ(Base64::Encode("foobar"), "Zm9vYmFy");
EXPECT_EQ(Base64::Encode("M"), "TQ==");
EXPECT_EQ(Base64::Encode("Ma"), "TWE=");
EXPECT_EQ(Base64::Encode("Man"), "TWFu");
}
TEST(Base64Test, DecodesKnownVectors)
{
EXPECT_EQ(Base64::Decode(""), "");
EXPECT_EQ(Base64::Decode("Zg=="), "f");
EXPECT_EQ(Base64::Decode("Zm8="), "fo");
EXPECT_EQ(Base64::Decode("Zm9v"), "foo");
EXPECT_EQ(Base64::Decode("Zm9vYg=="), "foob");
EXPECT_EQ(Base64::Decode("Zm9vYmE="), "fooba");
EXPECT_EQ(Base64::Decode("Zm9vYmFy"), "foobar");
}
TEST(Base64Test, OutputLengthIsPaddedToMultipleOfFour)
{
// One, two and three trailing bytes each still yield a length divisible by four.
EXPECT_EQ(Base64::Encode("M").size() % 4, 0u);
EXPECT_EQ(Base64::Encode("Ma").size() % 4, 0u);
EXPECT_EQ(Base64::Encode("Man").size() % 4, 0u);
}
TEST(Base64Test, RoundTripsAllByteValues)
{
// A payload containing every byte 0x00..0xFF must survive encode -> decode unchanged.
std::string raw;
raw.reserve(256);
for (int b = 0; b < 256; ++b)
raw.push_back(static_cast<char>(b));
EXPECT_EQ(Base64::Decode(Base64::Encode(raw)), raw);
}
TEST(Base64Test, RoundTripsEveryPayloadLengthShift)
{
// Cover all three "bits" phases of the encoder across successive lengths.
std::string raw;
for (int i = 0; i < 20; ++i)
{
raw.push_back(static_cast<char>('a' + i));
EXPECT_EQ(Base64::Decode(Base64::Encode(raw)), raw);
}
}
TEST(Base64Test, DecodeStopsAtFirstNonBase64Char)
{
// Documented behaviour: decode halts at the first character outside the alphabet (padding
// included), so trailing junk after a complete group is ignored.
EXPECT_EQ(Base64::Decode("Zm9v Zm9v"), "foo"); // space ends decoding after the first group
EXPECT_EQ(Base64::Decode("Zm9v!!!!"), "foo");
EXPECT_EQ(Base64::Decode("Zm9v========"), "foo"); // padding is a non-alphabet char, so it stops
}