• 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 <CUnit/CUnit.h>
31 
32 #include <nghttp2/nghttp2.h>
33 
34 #include "base64.h"
35 
36 namespace nghttp2 {
37 
test_base64_encode(void)38 void test_base64_encode(void) {
39   {
40     std::string in = "\xff";
41     auto out = base64::encode(std::begin(in), std::end(in));
42     CU_ASSERT("/w==" == out);
43   }
44   {
45     std::string in = "\xff\xfe";
46     auto out = base64::encode(std::begin(in), std::end(in));
47     CU_ASSERT("//4=" == out);
48   }
49   {
50     std::string in = "\xff\xfe\xfd";
51     auto out = base64::encode(std::begin(in), std::end(in));
52     CU_ASSERT("//79" == out);
53   }
54   {
55     std::string in = "\xff\xfe\xfd\xfc";
56     auto out = base64::encode(std::begin(in), std::end(in));
57     CU_ASSERT("//79/A==" == out);
58   }
59 }
60 
test_base64_decode(void)61 void test_base64_decode(void) {
62   BlockAllocator balloc(4096, 4096);
63   {
64     std::string in = "/w==";
65     auto out = base64::decode(std::begin(in), std::end(in));
66     CU_ASSERT("\xff" == out);
67     CU_ASSERT("\xff" == base64::decode(balloc, std::begin(in), std::end(in)));
68   }
69   {
70     std::string in = "//4=";
71     auto out = base64::decode(std::begin(in), std::end(in));
72     CU_ASSERT("\xff\xfe" == out);
73     CU_ASSERT("\xff\xfe" ==
74               base64::decode(balloc, std::begin(in), std::end(in)));
75   }
76   {
77     std::string in = "//79";
78     auto out = base64::decode(std::begin(in), std::end(in));
79     CU_ASSERT("\xff\xfe\xfd" == out);
80     CU_ASSERT("\xff\xfe\xfd" ==
81               base64::decode(balloc, std::begin(in), std::end(in)));
82   }
83   {
84     std::string in = "//79/A==";
85     auto out = base64::decode(std::begin(in), std::end(in));
86     CU_ASSERT("\xff\xfe\xfd\xfc" == out);
87     CU_ASSERT("\xff\xfe\xfd\xfc" ==
88               base64::decode(balloc, std::begin(in), std::end(in)));
89   }
90   {
91     // we check the number of valid input must be multiples of 4
92     std::string in = "//79=";
93     auto out = base64::decode(std::begin(in), std::end(in));
94     CU_ASSERT("" == out);
95     CU_ASSERT("" == base64::decode(balloc, std::begin(in), std::end(in)));
96   }
97   {
98     // ending invalid character at the boundary of multiples of 4 is
99     // bad
100     std::string in = "bmdodHRw\n";
101     auto out = base64::decode(std::begin(in), std::end(in));
102     CU_ASSERT("" == out);
103     CU_ASSERT("" == base64::decode(balloc, std::begin(in), std::end(in)));
104   }
105   {
106     // after seeing '=', subsequent input must be also '='.
107     std::string in = "//79/A=A";
108     auto out = base64::decode(std::begin(in), std::end(in));
109     CU_ASSERT("" == out);
110     CU_ASSERT("" == base64::decode(balloc, std::begin(in), std::end(in)));
111   }
112   {
113     // additional '=' at the end is bad
114     std::string in = "//79/A======";
115     auto out = base64::decode(std::begin(in), std::end(in));
116     CU_ASSERT("" == out);
117     CU_ASSERT("" == base64::decode(balloc, std::begin(in), std::end(in)));
118   }
119 }
120 
121 } // namespace nghttp2
122