• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2016 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "base64_test.h"
26 
27 #include <cstring>
28 #include <iostream>
29 
30 #include "munitxx.h"
31 
32 #include <nghttp2/nghttp2.h>
33 
34 #include "base64.h"
35 
36 using namespace std::literals;
37 
38 namespace nghttp2 {
39 
40 namespace {
41 const MunitTest tests[]{
42   munit_void_test(test_base64_encode),
43   munit_void_test(test_base64_decode),
44   munit_test_end(),
45 };
46 } // namespace
47 
48 const MunitSuite base64_suite{
49   "/base64", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE,
50 };
51 
test_base64_encode(void)52 void test_base64_encode(void) {
53   {
54     std::string in = "\xff";
55     auto out = base64::encode(std::begin(in), std::end(in));
56     assert_stdstring_equal("/w==", out);
57   }
58   {
59     std::string in = "\xff\xfe";
60     auto out = base64::encode(std::begin(in), std::end(in));
61     assert_stdstring_equal("//4=", out);
62   }
63   {
64     std::string in = "\xff\xfe\xfd";
65     auto out = base64::encode(std::begin(in), std::end(in));
66     assert_stdstring_equal("//79", out);
67   }
68   {
69     std::string in = "\xff\xfe\xfd\xfc";
70     auto out = base64::encode(std::begin(in), std::end(in));
71     assert_stdstring_equal("//79/A==", out);
72   }
73 }
74 
test_base64_decode(void)75 void test_base64_decode(void) {
76   BlockAllocator balloc(4096, 4096);
77   {
78     auto in = "/w=="sv;
79     auto out = base64::decode(std::begin(in), std::end(in));
80     assert_stdsv_equal("\xff"sv, out);
81     assert_stdsv_equal("\xff"sv, StringRef{base64::decode(
82                                    balloc, std::begin(in), std::end(in))});
83   }
84   {
85     auto in = "//4="sv;
86     auto out = base64::decode(std::begin(in), std::end(in));
87     assert_stdsv_equal("\xff\xfe"sv, out);
88     assert_stdsv_equal("\xff\xfe"sv, StringRef{base64::decode(
89                                        balloc, std::begin(in), std::end(in))});
90   }
91   {
92     auto in = "//79"sv;
93     auto out = base64::decode(std::begin(in), std::end(in));
94     assert_stdsv_equal("\xff\xfe\xfd"sv, out);
95     assert_stdsv_equal(
96       "\xff\xfe\xfd"sv,
97       StringRef{base64::decode(balloc, std::begin(in), std::end(in))});
98   }
99   {
100     auto in = "//79/A=="sv;
101     auto out = base64::decode(std::begin(in), std::end(in));
102     assert_stdsv_equal("\xff\xfe\xfd\xfc"sv, out);
103     assert_stdsv_equal(
104       "\xff\xfe\xfd\xfc"sv,
105       StringRef{base64::decode(balloc, std::begin(in), std::end(in))});
106   }
107   {
108     // we check the number of valid input must be multiples of 4
109     auto in = "//79="sv;
110     auto out = base64::decode(std::begin(in), std::end(in));
111     assert_stdsv_equal(""sv, out);
112     assert_stdsv_equal(
113       ""sv, StringRef{base64::decode(balloc, std::begin(in), std::end(in))});
114   }
115   {
116     // ending invalid character at the boundary of multiples of 4 is
117     // bad
118     auto in = "bmdodHRw\n"sv;
119     auto out = base64::decode(std::begin(in), std::end(in));
120     assert_stdsv_equal("", out);
121     assert_stdsv_equal(
122       ""sv, StringRef{base64::decode(balloc, std::begin(in), std::end(in))});
123   }
124   {
125     // after seeing '=', subsequent input must be also '='.
126     auto in = "//79/A=A"sv;
127     auto out = base64::decode(std::begin(in), std::end(in));
128     assert_stdsv_equal(""sv, out);
129     assert_stdsv_equal(
130       ""sv, StringRef{base64::decode(balloc, std::begin(in), std::end(in))});
131   }
132   {
133     // additional '=' at the end is bad
134     auto in = "//79/A======"sv;
135     auto out = base64::decode(std::begin(in), std::end(in));
136     assert_stdsv_equal(""sv, out);
137     assert_stdsv_equal(
138       ""sv, StringRef{base64::decode(balloc, std::begin(in), std::end(in))});
139   }
140 }
141 
142 } // namespace nghttp2
143