blob: 348497603427ea4ce5d4e8f665f1a99961ec2f41 [file] [log] [blame]
Ed Tanousf9273472017-02-28 16:05:13 -08001#include "base64.hpp"
2#include "gtest/gtest.h"
3#include "big_list_of_naughty_strings.hpp"
4
5// Tests that Base64 basic strings work
6TEST(Base64, EncodeBasicString)
7{
8 std::string output;
9 EXPECT_TRUE(base64::base64_encode("Foo", output));
10}
11
12// Tests the test vectors available in the base64 spec
13TEST(Base64, EncodeRFC4648)
14{
15 std::string output;
16 EXPECT_TRUE(base64::base64_encode("", output));
17 EXPECT_EQ(output, "");
18 EXPECT_TRUE(base64::base64_encode("f", output));
19 EXPECT_EQ(output, "Zg==");
20 EXPECT_TRUE(base64::base64_encode("fo", output));
21 EXPECT_EQ(output, "Zm8=");
22 EXPECT_TRUE(base64::base64_encode("foo", output));
23 EXPECT_EQ(output, "Zm9v");
24 EXPECT_TRUE(base64::base64_encode("foob", output));
25 EXPECT_EQ(output, "Zm9vYg==");
26 EXPECT_TRUE(base64::base64_encode("fooba", output));
27 EXPECT_EQ(output, "Zm9vYmE=");
28 EXPECT_TRUE(base64::base64_encode("foobar", output));
29 EXPECT_EQ(output, "Zm9vYmFy");
30}
31
32// Tests the test vectors available in the base64 spec
33TEST(Base64, DecodeRFC4648)
34{
35 std::string output;
36 EXPECT_TRUE(base64::base64_decode("", output));
37 EXPECT_EQ(output, "");
38 EXPECT_TRUE(base64::base64_decode("Zg==", output));
39 EXPECT_EQ(output, "f");
40 EXPECT_TRUE(base64::base64_decode("Zm8=", output));
41 EXPECT_EQ(output, "fo");
42 EXPECT_TRUE(base64::base64_decode("Zm9v", output));
43 EXPECT_EQ(output, "foo");
44 EXPECT_TRUE(base64::base64_decode("Zm9vYg==", output));
45 EXPECT_EQ(output, "foob");
46 EXPECT_TRUE(base64::base64_decode("Zm9vYmE=", output));
47 EXPECT_EQ(output, "fooba");
48 EXPECT_TRUE(base64::base64_decode("Zm9vYmFy", output));
49 EXPECT_EQ(output, "foobar");
50}
51
52// Tests using pathalogical cases for all escapings
53TEST(Base64, NaugtyStrings){
54 std::string base64_string;
55 std::string decoded_string;
56 for (auto& str: naughty_strings){
57 EXPECT_TRUE(base64::base64_encode(str, base64_string));
58 EXPECT_TRUE(base64::base64_decode(base64_string, decoded_string));
59 EXPECT_EQ(str, decoded_string);
60 }
61}
62