blob: e21655ed5558f123b5f58e7ec383c9641506756a [file] [log] [blame]
Ed Tanousf9273472017-02-28 16:05:13 -08001#include "base64.hpp"
Ed Tanousf9273472017-02-28 16:05:13 -08002#include "big_list_of_naughty_strings.hpp"
Ed Tanous99923322017-03-03 14:21:24 -08003#include "gtest/gtest.h"
Ed Tanousf9273472017-02-28 16:05:13 -08004
5// Tests that Base64 basic strings work
Ed Tanous99923322017-03-03 14:21:24 -08006TEST(Base64, EncodeBasicString) {
7 std::string output;
8 EXPECT_TRUE(base64::base64_encode("Foo", output));
Ed Tanousf9273472017-02-28 16:05:13 -08009}
10
11// Tests the test vectors available in the base64 spec
Ed Tanous99923322017-03-03 14:21:24 -080012TEST(Base64, EncodeRFC4648) {
13 std::string output;
14 EXPECT_TRUE(base64::base64_encode("", output));
15 EXPECT_EQ(output, "");
16 EXPECT_TRUE(base64::base64_encode("f", output));
17 EXPECT_EQ(output, "Zg==");
18 EXPECT_TRUE(base64::base64_encode("fo", output));
19 EXPECT_EQ(output, "Zm8=");
20 EXPECT_TRUE(base64::base64_encode("foo", output));
21 EXPECT_EQ(output, "Zm9v");
22 EXPECT_TRUE(base64::base64_encode("foob", output));
23 EXPECT_EQ(output, "Zm9vYg==");
24 EXPECT_TRUE(base64::base64_encode("fooba", output));
25 EXPECT_EQ(output, "Zm9vYmE=");
26 EXPECT_TRUE(base64::base64_encode("foobar", output));
27 EXPECT_EQ(output, "Zm9vYmFy");
Ed Tanousf9273472017-02-28 16:05:13 -080028}
29
30// Tests the test vectors available in the base64 spec
Ed Tanous99923322017-03-03 14:21:24 -080031TEST(Base64, DecodeRFC4648) {
32 std::string output;
33 EXPECT_TRUE(base64::base64_decode("", output));
34 EXPECT_EQ(output, "");
35 EXPECT_TRUE(base64::base64_decode("Zg==", output));
36 EXPECT_EQ(output, "f");
37 EXPECT_TRUE(base64::base64_decode("Zm8=", output));
38 EXPECT_EQ(output, "fo");
39 EXPECT_TRUE(base64::base64_decode("Zm9v", output));
40 EXPECT_EQ(output, "foo");
41 EXPECT_TRUE(base64::base64_decode("Zm9vYg==", output));
42 EXPECT_EQ(output, "foob");
43 EXPECT_TRUE(base64::base64_decode("Zm9vYmE=", output));
44 EXPECT_EQ(output, "fooba");
45 EXPECT_TRUE(base64::base64_decode("Zm9vYmFy", output));
46 EXPECT_EQ(output, "foobar");
Ed Tanousf9273472017-02-28 16:05:13 -080047}
48
49// Tests using pathalogical cases for all escapings
Ed Tanous1ccd57c2017-03-21 13:15:58 -070050TEST(Base64, NaugtyStringsEncodeDecode) {
Ed Tanous99923322017-03-03 14:21:24 -080051 std::string base64_string;
52 std::string decoded_string;
53 for (auto& str : naughty_strings) {
54 EXPECT_TRUE(base64::base64_encode(str, base64_string));
55 EXPECT_TRUE(base64::base64_decode(base64_string, decoded_string));
56 EXPECT_EQ(str, decoded_string);
57 }
Ed Tanousf9273472017-02-28 16:05:13 -080058}
Ed Tanous1ccd57c2017-03-21 13:15:58 -070059
60// Tests using pathalogical cases for all escapings
61TEST(Base64, NaugtyStringsPathological) {
62 std::string base64_string;
63 std::string decoded_string;
64 for (auto& str : naughty_strings) {
65 base64::base64_decode(str, base64_string);
66 }
67}