2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
24 #include <gtest/gtest.h>
29 #include <boost/assign.hpp>
31 #include <common/base64.h>
32 #include <common/except.h>
35 const std::set<char>& get_dictionary()
37 static const std::set<char> dictionary{
38 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
39 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
40 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
50 TEST(Base64Test, InvalidInputLength)
52 // 0 and 4 characters should be ok
53 EXPECT_THROW(from_base64("1"), caspar_exception);
54 EXPECT_THROW(from_base64("12"), caspar_exception);
55 EXPECT_THROW(from_base64("123"), caspar_exception);
56 EXPECT_THROW(from_base64("1234\n567"), caspar_exception);
57 EXPECT_THROW(from_base64("12345"), caspar_exception);
60 TEST(Base64Test, InvalidInputCharacters)
62 static std::string PREFIX("AAA");
64 for (int i = 1; i < 256; ++i)
66 if (get_dictionary().find(static_cast<char>(i)) == get_dictionary().end()
70 auto invalid = PREFIX + static_cast<char>(i);
72 EXPECT_THROW(from_base64(invalid), std::exception);
77 TEST(Base64Test, WithoutPadding)
79 EXPECT_EQ(std::vector<uint8_t>(), from_base64(""));
80 EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0)(0),
82 EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0)(0)(0)(0)(0),
83 from_base64("AAAAAAAA"));
84 EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255)(255),
86 EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0)(0)(255)(255)(255),
87 from_base64("AAAA////"));
90 TEST(Base64Test, WithPadding)
92 EXPECT_EQ(boost::assign::list_of<uint8_t>(0),
94 EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0),
96 EXPECT_EQ(boost::assign::list_of<uint8_t>(255),
98 EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255),
100 EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255),
101 from_base64("//9="));
102 EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255),
103 from_base64("//+="));
104 EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255),
105 from_base64("///="));
108 TEST(Base64Test, ToBase64)
110 static const char ALL_ZERO = 0;
111 static const char ALL_ONE = static_cast<char>(static_cast<unsigned char>(255));
112 std::vector<char> data;
114 EXPECT_EQ("", to_base64(data.data(), data.size()));
116 data.push_back(ALL_ZERO);
117 EXPECT_EQ("AA==", to_base64(data.data(), data.size()));
119 data.push_back(ALL_ONE);
120 EXPECT_EQ("AP8=", to_base64(data.data(), data.size()));
122 data.push_back(ALL_ZERO);
123 EXPECT_EQ("AP8A", to_base64(data.data(), data.size()));
125 data.push_back(ALL_ONE);
126 EXPECT_EQ("AP8A/w==", to_base64(data.data(), data.size()));
128 data.push_back(ALL_ZERO);
129 EXPECT_EQ("AP8A/wA=", to_base64(data.data(), data.size()));