Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 1 | #include "base64.hpp" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 2 | #include "big_list_of_naughty_strings.hpp" |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 3 | #include "gtest/gtest.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 4 | |
| 5 | // Tests that Base64 basic strings work |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 6 | TEST(Base64, EncodeBasicString) { |
| 7 | std::string output; |
| 8 | EXPECT_TRUE(base64::base64_encode("Foo", output)); |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 9 | } |
| 10 | |
| 11 | // Tests the test vectors available in the base64 spec |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 12 | TEST(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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // Tests the test vectors available in the base64 spec |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 31 | TEST(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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Tests using pathalogical cases for all escapings |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 50 | TEST(Base64, NaugtyStringsEncodeDecode) { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 51 | 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 Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 58 | } |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 59 | |
| 60 | // Tests using pathalogical cases for all escapings |
| 61 | TEST(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 | } |