]> git.sesse.net Git - casparcg/blob - unit-test/base64_test.cpp
* Created BUILDING.txt for describing build process.
[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
28 #include <boost/assign.hpp>
29
30 #include <common/base64.h>
31 #include <common/except.h>
32
33 namespace {
34         static const char DICTIONARY[] =
35         {
36                 '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',
37                 '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',
38                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
39                 '/', '+'
40         };
41
42         const std::set<char>& get_dictionary()
43         {
44                 static const std::set<char> dictionary(
45                                 DICTIONARY, DICTIONARY + sizeof(DICTIONARY));
46
47                 return dictionary;
48         }
49 }
50
51 namespace caspar {
52
53 TEST(Base64Test, InvalidInputLength)
54 {
55         // 0 and 4 characters should be ok
56         EXPECT_THROW(from_base64("1"), caspar_exception);
57         EXPECT_THROW(from_base64("12"), caspar_exception);
58         EXPECT_THROW(from_base64("123"), caspar_exception);
59         EXPECT_THROW(from_base64("1234\n567"), caspar_exception);
60         EXPECT_THROW(from_base64("12345"), caspar_exception);
61 }
62
63 TEST(Base64Test, InvalidInputCharacters)
64 {
65         static std::string PREFIX("AAA");
66
67         for (int i = 1; i < 256; ++i)
68         {
69                 if (get_dictionary().find(static_cast<char>(i)) == get_dictionary().end()
70                                 && !std::isspace(i, std::locale())
71                                 && i != '=')
72                 {
73                         auto invalid = PREFIX + static_cast<char>(i);
74
75                         EXPECT_THROW(from_base64(invalid), std::exception);
76                 }
77         }
78 }
79
80 TEST(Base64Test, WithoutPadding)
81 {
82         EXPECT_EQ(std::vector<uint8_t>(), from_base64(""));
83         EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0)(0),
84                         from_base64("AAAA"));
85         EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0)(0)(0)(0)(0),
86                         from_base64("AAAAAAAA"));
87         EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255)(255),
88                         from_base64("////"));
89         EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0)(0)(255)(255)(255),
90                         from_base64("AAAA////"));
91 }
92
93 TEST(Base64Test, WithPadding)
94 {
95         EXPECT_EQ(boost::assign::list_of<uint8_t>(0),
96                         from_base64("AA=="));
97         EXPECT_EQ(boost::assign::list_of<uint8_t>(0)(0),
98                         from_base64("AAA="));
99         EXPECT_EQ(boost::assign::list_of<uint8_t>(255),
100                         from_base64("//=="));
101         EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255),
102                         from_base64("//8="));
103         EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255),
104                         from_base64("//9="));
105         EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255),
106                         from_base64("//+="));
107         EXPECT_EQ(boost::assign::list_of<uint8_t>(255)(255),
108                         from_base64("///="));
109 }
110
111 TEST(Base64Test, ToBase64)
112 {
113         static const char ALL_ZERO = 0;
114         static const char ALL_ONE = static_cast<char>(static_cast<unsigned char>(255));
115         std::vector<char> data;
116
117         EXPECT_EQ("", to_base64(data.data(), data.size()));
118
119         data.push_back(ALL_ZERO);
120         EXPECT_EQ("AA==", to_base64(data.data(), data.size()));
121
122         data.push_back(ALL_ONE);
123         EXPECT_EQ("AP8=", to_base64(data.data(), data.size()));
124
125         data.push_back(ALL_ZERO);
126         EXPECT_EQ("AP8A", to_base64(data.data(), data.size()));
127
128         data.push_back(ALL_ONE);
129         EXPECT_EQ("AP8A/w==", to_base64(data.data(), data.size()));
130
131         data.push_back(ALL_ZERO);
132         EXPECT_EQ("AP8A/wA=", to_base64(data.data(), data.size()));
133 }
134
135 }