]> git.sesse.net Git - casparcg/blob - unit-test/base64_test.cpp
[image_scroll_producer]:
[casparcg] / unit-test / base64_test.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "stdafx.h"
23
24 #include <gtest/gtest.h>
25
26 #include <set>
27 #include <cctype>
28
29 #include <boost/assign.hpp>
30
31 #include <common/base64.h>
32 #include <common/except.h>
33
34 namespace {
35         const std::set<char>& get_dictionary()
36         {
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',
41                         '/', '+'
42                 };
43
44                 return dictionary;
45         }
46 }
47
48 namespace caspar {
49
50 TEST(Base64Test, InvalidInputLength)
51 {
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);
58 }
59
60 TEST(Base64Test, InvalidInputCharacters)
61 {
62         static std::string PREFIX("AAA");
63
64         for (int i = 1; i < 256; ++i)
65         {
66                 if (get_dictionary().find(static_cast<char>(i)) == get_dictionary().end()
67                                 && !std::isspace(i)
68                                 && i != '=')
69                 {
70                         auto invalid = PREFIX + static_cast<char>(i);
71
72                         EXPECT_THROW(from_base64(invalid), std::exception);
73                 }
74         }
75 }
76
77 TEST(Base64Test, WithoutPadding)
78 {
79         EXPECT_EQ(std::vector<uint8_t>(), from_base64(""));
80         EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0)(0),
81                         from_base64("AAAA"));
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),
85                         from_base64("////"));
86         EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0)(0)(255)(255)(255),
87                         from_base64("AAAA////"));
88 }
89
90 TEST(Base64Test, WithPadding)
91 {
92         EXPECT_EQ(boost::assign::list_of<uint8_t>(0),
93                         from_base64("AA=="));
94         EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0),
95                         from_base64("AAA="));
96         EXPECT_EQ(boost::assign::list_of<uint8_t>(255),
97                         from_base64("//=="));
98         EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255),
99                         from_base64("//8="));
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("///="));
106 }
107
108 TEST(Base64Test, ToBase64)
109 {
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;
113
114         EXPECT_EQ("", to_base64(data.data(), data.size()));
115
116         data.push_back(ALL_ZERO);
117         EXPECT_EQ("AA==", to_base64(data.data(), data.size()));
118
119         data.push_back(ALL_ONE);
120         EXPECT_EQ("AP8=", to_base64(data.data(), data.size()));
121
122         data.push_back(ALL_ZERO);
123         EXPECT_EQ("AP8A", to_base64(data.data(), data.size()));
124
125         data.push_back(ALL_ONE);
126         EXPECT_EQ("AP8A/w==", to_base64(data.data(), data.size()));
127
128         data.push_back(ALL_ZERO);
129         EXPECT_EQ("AP8A/wA=", to_base64(data.data(), data.size()));
130 }
131
132 }