summaryrefslogtreecommitdiff
path: root/src/base64_test.cpp
blob: 2758dfa80f8623b045cf09b4d768753784e9977e (plain)
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
#include "base64.hpp"
#include "big_list_of_naughty_strings.hpp"
#include "gtest/gtest.h"

// Tests that Base64 basic strings work
TEST(Base64, EncodeBasicString) {
  std::string output;
  EXPECT_TRUE(base64::base64_encode("Foo", output));
}

// Tests the test vectors available in the base64 spec
TEST(Base64, EncodeRFC4648) {
  std::string output;
  EXPECT_TRUE(base64::base64_encode("", output));
  EXPECT_EQ(output, "");
  EXPECT_TRUE(base64::base64_encode("f", output));
  EXPECT_EQ(output, "Zg==");
  EXPECT_TRUE(base64::base64_encode("fo", output));
  EXPECT_EQ(output, "Zm8=");
  EXPECT_TRUE(base64::base64_encode("foo", output));
  EXPECT_EQ(output, "Zm9v");
  EXPECT_TRUE(base64::base64_encode("foob", output));
  EXPECT_EQ(output, "Zm9vYg==");
  EXPECT_TRUE(base64::base64_encode("fooba", output));
  EXPECT_EQ(output, "Zm9vYmE=");
  EXPECT_TRUE(base64::base64_encode("foobar", output));
  EXPECT_EQ(output, "Zm9vYmFy");
}

// Tests the test vectors available in the base64 spec
TEST(Base64, DecodeRFC4648) {
  std::string output;
  EXPECT_TRUE(base64::base64_decode("", output));
  EXPECT_EQ(output, "");
  EXPECT_TRUE(base64::base64_decode("Zg==", output));
  EXPECT_EQ(output, "f");
  EXPECT_TRUE(base64::base64_decode("Zm8=", output));
  EXPECT_EQ(output, "fo");
  EXPECT_TRUE(base64::base64_decode("Zm9v", output));
  EXPECT_EQ(output, "foo");
  EXPECT_TRUE(base64::base64_decode("Zm9vYg==", output));
  EXPECT_EQ(output, "foob");
  EXPECT_TRUE(base64::base64_decode("Zm9vYmE=", output));
  EXPECT_EQ(output, "fooba");
  EXPECT_TRUE(base64::base64_decode("Zm9vYmFy", output));
  EXPECT_EQ(output, "foobar");
}

// Tests using pathalogical cases for all escapings
TEST(Base64, NaugtyStrings) {
  std::string base64_string;
  std::string decoded_string;
  for (auto& str : naughty_strings) {
    EXPECT_TRUE(base64::base64_encode(str, base64_string));
    EXPECT_TRUE(base64::base64_decode(base64_string, decoded_string));
    EXPECT_EQ(str, decoded_string);
  }
}