forked from Psiphon-Inc/psicash-lib-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64_test.cpp
90 lines (71 loc) · 1.81 KB
/
base64_test.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
#include "gtest/gtest.h"
#include "base64.hpp"
using namespace std;
using namespace base64;
TEST(TestBase64, Encode)
{
// Test vectors: https://tools.ietf.org/html/rfc4648#page-12
auto b64 = B64Encode("");
ASSERT_EQ(b64, "");
b64 = B64Encode("f");
ASSERT_EQ(b64, "Zg==");
b64 = B64Encode("fo");
ASSERT_EQ(b64, "Zm8=");
b64 = B64Encode("foo");
ASSERT_EQ(b64, "Zm9v");
b64 = B64Encode("foob");
ASSERT_EQ(b64, "Zm9vYg==");
b64 = B64Encode("fooba");
ASSERT_EQ(b64, "Zm9vYmE=");
b64 = B64Encode("foobar");
ASSERT_EQ(b64, "Zm9vYmFy");
// The vector overload
vector<BYTE> v;
b64 = B64Encode(v);
ASSERT_EQ(b64, "");
BYTE b[] = "f";
v = vector<BYTE>(b, b+1);
b64 = B64Encode(v);
ASSERT_EQ(b64, "Zg==");
}
TEST(TestBase64, Decode)
{
vector<BYTE> v, want;
string s;
s = "";
want = vector<BYTE>(s.c_str(), s.c_str()+s.size());
v = B64Decode("");
ASSERT_EQ(v, want);
s = "fo";
want = vector<BYTE>(s.c_str(), s.c_str()+s.size());
v = B64Decode("Zm8=");
ASSERT_EQ(v, want);
s = "foo";
want = vector<BYTE>(s.c_str(), s.c_str()+s.size());
v = B64Decode("Zm9v");
ASSERT_EQ(v, want);
s = "foob";
want = vector<BYTE>(s.c_str(), s.c_str()+s.size());
v = B64Decode("Zm9vYg==");
ASSERT_EQ(v, want);
s = "fooba";
want = vector<BYTE>(s.c_str(), s.c_str()+s.size());
v = B64Decode("Zm9vYmE=");
ASSERT_EQ(v, want);
s = "foobar";
want = vector<BYTE>(s.c_str(), s.c_str()+s.size());
v = B64Decode("Zm9vYmFy");
ASSERT_EQ(v, want);
// Not padded
s = "foob";
want = vector<BYTE>(s.c_str(), s.c_str()+s.size());
v = B64Decode("Zm9vYg");
ASSERT_EQ(v, want);
}
TEST(TestBase64, TrimPadding)
{
ASSERT_EQ(TrimPadding("abc"), "abc");
ASSERT_EQ(TrimPadding("abc="), "abc");
ASSERT_EQ(TrimPadding("abc=="), "abc");
ASSERT_EQ(TrimPadding("abc==="), "abc");
}