1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/ext/transport/chttp2/transport/bin_decoder.h"
20
21 #include <grpc/support/alloc.h>
22 #include <string.h>
23
24 #include <memory>
25
26 #include "absl/log/log.h"
27 #include "gtest/gtest.h"
28 #include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 #include "src/core/lib/slice/slice_string_helpers.h"
31 #include "src/core/util/string.h"
32 #include "test/core/test_util/test_config.h"
33
34 static int all_ok = 1;
35
expect_slice_eq(grpc_slice expected,grpc_slice slice,const char * debug,int line)36 static void expect_slice_eq(grpc_slice expected, grpc_slice slice,
37 const char* debug, int line) {
38 if (!grpc_slice_eq(slice, expected)) {
39 char* hs = grpc_dump_slice(slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
40 char* he = grpc_dump_slice(expected, GPR_DUMP_HEX | GPR_DUMP_ASCII);
41 LOG(ERROR) << "FAILED:" << line << ": " << debug << "\ngot: " << hs
42 << "\nwant: " << he;
43 gpr_free(hs);
44 gpr_free(he);
45 all_ok = 0;
46 }
47 grpc_slice_unref(expected);
48 grpc_slice_unref(slice);
49 }
50
base64_encode(const char * s)51 static grpc_slice base64_encode(const char* s) {
52 grpc_slice ss = grpc_slice_from_copied_string(s);
53 grpc_slice out = grpc_chttp2_base64_encode(ss);
54 grpc_slice_unref(ss);
55 return out;
56 }
57
base64_decode(const char * s)58 static grpc_slice base64_decode(const char* s) {
59 grpc_slice ss = grpc_slice_from_copied_string(s);
60 grpc_slice out = grpc_chttp2_base64_decode(ss);
61 grpc_slice_unref(ss);
62 return out;
63 }
64
base64_decode_with_length(const char * s,size_t output_length)65 static grpc_slice base64_decode_with_length(const char* s,
66 size_t output_length) {
67 grpc_slice ss = grpc_slice_from_copied_string(s);
68 grpc_slice out = grpc_chttp2_base64_decode_with_length(ss, output_length);
69 grpc_slice_unref(ss);
70 return out;
71 }
72
base64_infer_length(const char * s)73 static size_t base64_infer_length(const char* s) {
74 grpc_slice ss = grpc_slice_from_copied_string(s);
75 size_t out = grpc_chttp2_base64_infer_length_after_decode(ss);
76 grpc_slice_unref(ss);
77 return out;
78 }
79
80 #define EXPECT_DECODED_LENGTH(s, expected) \
81 ASSERT_EQ((expected), base64_infer_length((s)));
82
83 #define EXPECT_SLICE_EQ(expected, slice) \
84 expect_slice_eq( \
85 grpc_slice_from_copied_buffer(expected, sizeof(expected) - 1), slice, \
86 #slice, __LINE__);
87
88 #define ENCODE_AND_DECODE(s) \
89 EXPECT_SLICE_EQ( \
90 s, grpc_chttp2_base64_decode_with_length(base64_encode(s), strlen(s)));
91
TEST(BinDecoderTest,MainTest)92 TEST(BinDecoderTest, MainTest) {
93 grpc_core::ExecCtx exec_ctx;
94
95 // ENCODE_AND_DECODE tests grpc_chttp2_base64_decode_with_length(), which
96 // takes encoded base64 strings without pad chars, but output length is
97 // required.
98 // Base64 test vectors from RFC 4648
99 ENCODE_AND_DECODE("");
100 ENCODE_AND_DECODE("f");
101 ENCODE_AND_DECODE("foo");
102 ENCODE_AND_DECODE("fo");
103 ENCODE_AND_DECODE("foob");
104 ENCODE_AND_DECODE("fooba");
105 ENCODE_AND_DECODE("foobar");
106
107 ENCODE_AND_DECODE("\xc0\xc1\xc2\xc3\xc4\xc5");
108
109 // Base64 test vectors from RFC 4648, with pad chars
110 // BASE64("") = ""
111 EXPECT_SLICE_EQ("", base64_decode(""));
112 // BASE64("f") = "Zg=="
113 EXPECT_SLICE_EQ("f", base64_decode("Zg=="));
114 // BASE64("fo") = "Zm8="
115 EXPECT_SLICE_EQ("fo", base64_decode("Zm8="));
116 // BASE64("foo") = "Zm9v"
117 EXPECT_SLICE_EQ("foo", base64_decode("Zm9v"));
118 // BASE64("foob") = "Zm9vYg=="
119 EXPECT_SLICE_EQ("foob", base64_decode("Zm9vYg=="));
120 // BASE64("fooba") = "Zm9vYmE="
121 EXPECT_SLICE_EQ("fooba", base64_decode("Zm9vYmE="));
122 // BASE64("foobar") = "Zm9vYmFy"
123 EXPECT_SLICE_EQ("foobar", base64_decode("Zm9vYmFy"));
124
125 EXPECT_SLICE_EQ("\xc0\xc1\xc2\xc3\xc4\xc5", base64_decode("wMHCw8TF"));
126
127 // Test illegal input length in grpc_chttp2_base64_decode
128 EXPECT_SLICE_EQ("", base64_decode("a"));
129 EXPECT_SLICE_EQ("", base64_decode("ab"));
130 EXPECT_SLICE_EQ("", base64_decode("abc"));
131
132 // Test illegal characters in grpc_chttp2_base64_decode
133 EXPECT_SLICE_EQ("", base64_decode("Zm:v"));
134 EXPECT_SLICE_EQ("", base64_decode("Zm=v"));
135
136 // Test output_length longer than max possible output length in
137 // grpc_chttp2_base64_decode_with_length
138 EXPECT_SLICE_EQ("", base64_decode_with_length("Zg", 2));
139 EXPECT_SLICE_EQ("", base64_decode_with_length("Zm8", 3));
140 EXPECT_SLICE_EQ("", base64_decode_with_length("Zm9v", 4));
141
142 // Test illegal characters in grpc_chttp2_base64_decode_with_length
143 EXPECT_SLICE_EQ("", base64_decode_with_length("Zm:v", 3));
144 EXPECT_SLICE_EQ("", base64_decode_with_length("Zm=v", 3));
145
146 EXPECT_DECODED_LENGTH("", 0);
147 EXPECT_DECODED_LENGTH("ab", 1);
148 EXPECT_DECODED_LENGTH("abc", 2);
149 EXPECT_DECODED_LENGTH("abcd", 3);
150 EXPECT_DECODED_LENGTH("abcdef", 4);
151 EXPECT_DECODED_LENGTH("abcdefg", 5);
152 EXPECT_DECODED_LENGTH("abcdefgh", 6);
153
154 EXPECT_DECODED_LENGTH("ab==", 1);
155 EXPECT_DECODED_LENGTH("abc=", 2);
156 EXPECT_DECODED_LENGTH("abcd", 3);
157 EXPECT_DECODED_LENGTH("abcdef==", 4);
158 EXPECT_DECODED_LENGTH("abcdefg=", 5);
159 EXPECT_DECODED_LENGTH("abcdefgh", 6);
160
161 EXPECT_DECODED_LENGTH("a", 0);
162 EXPECT_DECODED_LENGTH("a===", 0);
163 EXPECT_DECODED_LENGTH("abcde", 0);
164 EXPECT_DECODED_LENGTH("abcde===", 0);
165 }
166
main(int argc,char ** argv)167 int main(int argc, char** argv) {
168 grpc::testing::TestEnvironment env(&argc, argv);
169 ::testing::InitGoogleTest(&argc, argv);
170 grpc::testing::TestGrpcScope grpc_scope;
171 return RUN_ALL_TESTS();
172 }
173