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 <string.h>
22
23 #include <memory>
24
25 #include "gtest/gtest.h"
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29
30 #include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
31 #include "src/core/lib/gpr/string.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "src/core/lib/slice/slice_string_helpers.h"
34 #include "test/core/util/test_config.h"
35
36 static int all_ok = 1;
37
expect_slice_eq(grpc_slice expected,grpc_slice slice,const char * debug,int line)38 static void expect_slice_eq(grpc_slice expected, grpc_slice slice,
39 const char* debug, int line) {
40 if (!grpc_slice_eq(slice, expected)) {
41 char* hs = grpc_dump_slice(slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
42 char* he = grpc_dump_slice(expected, GPR_DUMP_HEX | GPR_DUMP_ASCII);
43 gpr_log(GPR_ERROR, "FAILED:%d: %s\ngot: %s\nwant: %s", line, debug, hs,
44 he);
45 gpr_free(hs);
46 gpr_free(he);
47 all_ok = 0;
48 }
49 grpc_slice_unref(expected);
50 grpc_slice_unref(slice);
51 }
52
base64_encode(const char * s)53 static grpc_slice base64_encode(const char* s) {
54 grpc_slice ss = grpc_slice_from_copied_string(s);
55 grpc_slice out = grpc_chttp2_base64_encode(ss);
56 grpc_slice_unref(ss);
57 return out;
58 }
59
base64_decode(const char * s)60 static grpc_slice base64_decode(const char* s) {
61 grpc_slice ss = grpc_slice_from_copied_string(s);
62 grpc_slice out = grpc_chttp2_base64_decode(ss);
63 grpc_slice_unref(ss);
64 return out;
65 }
66
base64_decode_with_length(const char * s,size_t output_length)67 static grpc_slice base64_decode_with_length(const char* s,
68 size_t output_length) {
69 grpc_slice ss = grpc_slice_from_copied_string(s);
70 grpc_slice out = grpc_chttp2_base64_decode_with_length(ss, output_length);
71 grpc_slice_unref(ss);
72 return out;
73 }
74
base64_infer_length(const char * s)75 static size_t base64_infer_length(const char* s) {
76 grpc_slice ss = grpc_slice_from_copied_string(s);
77 size_t out = grpc_chttp2_base64_infer_length_after_decode(ss);
78 grpc_slice_unref(ss);
79 return out;
80 }
81
82 #define EXPECT_DECODED_LENGTH(s, expected) \
83 ASSERT_EQ((expected), base64_infer_length((s)));
84
85 #define EXPECT_SLICE_EQ(expected, slice) \
86 expect_slice_eq( \
87 grpc_slice_from_copied_buffer(expected, sizeof(expected) - 1), slice, \
88 #slice, __LINE__);
89
90 #define ENCODE_AND_DECODE(s) \
91 EXPECT_SLICE_EQ( \
92 s, grpc_chttp2_base64_decode_with_length(base64_encode(s), strlen(s)));
93
TEST(BinDecoderTest,MainTest)94 TEST(BinDecoderTest, MainTest) {
95 grpc_core::ExecCtx exec_ctx;
96
97 // ENCODE_AND_DECODE tests grpc_chttp2_base64_decode_with_length(), which
98 // takes encoded base64 strings without pad chars, but output length is
99 // required.
100 // Base64 test vectors from RFC 4648
101 ENCODE_AND_DECODE("");
102 ENCODE_AND_DECODE("f");
103 ENCODE_AND_DECODE("foo");
104 ENCODE_AND_DECODE("fo");
105 ENCODE_AND_DECODE("foob");
106 ENCODE_AND_DECODE("fooba");
107 ENCODE_AND_DECODE("foobar");
108
109 ENCODE_AND_DECODE("\xc0\xc1\xc2\xc3\xc4\xc5");
110
111 // Base64 test vectors from RFC 4648, with pad chars
112 // BASE64("") = ""
113 EXPECT_SLICE_EQ("", base64_decode(""));
114 // BASE64("f") = "Zg=="
115 EXPECT_SLICE_EQ("f", base64_decode("Zg=="));
116 // BASE64("fo") = "Zm8="
117 EXPECT_SLICE_EQ("fo", base64_decode("Zm8="));
118 // BASE64("foo") = "Zm9v"
119 EXPECT_SLICE_EQ("foo", base64_decode("Zm9v"));
120 // BASE64("foob") = "Zm9vYg=="
121 EXPECT_SLICE_EQ("foob", base64_decode("Zm9vYg=="));
122 // BASE64("fooba") = "Zm9vYmE="
123 EXPECT_SLICE_EQ("fooba", base64_decode("Zm9vYmE="));
124 // BASE64("foobar") = "Zm9vYmFy"
125 EXPECT_SLICE_EQ("foobar", base64_decode("Zm9vYmFy"));
126
127 EXPECT_SLICE_EQ("\xc0\xc1\xc2\xc3\xc4\xc5", base64_decode("wMHCw8TF"));
128
129 // Test illegal input length in grpc_chttp2_base64_decode
130 EXPECT_SLICE_EQ("", base64_decode("a"));
131 EXPECT_SLICE_EQ("", base64_decode("ab"));
132 EXPECT_SLICE_EQ("", base64_decode("abc"));
133
134 // Test illegal charactors in grpc_chttp2_base64_decode
135 EXPECT_SLICE_EQ("", base64_decode("Zm:v"));
136 EXPECT_SLICE_EQ("", base64_decode("Zm=v"));
137
138 // Test output_length longer than max possible output length in
139 // grpc_chttp2_base64_decode_with_length
140 EXPECT_SLICE_EQ("", base64_decode_with_length("Zg", 2));
141 EXPECT_SLICE_EQ("", base64_decode_with_length("Zm8", 3));
142 EXPECT_SLICE_EQ("", base64_decode_with_length("Zm9v", 4));
143
144 // Test illegal charactors in grpc_chttp2_base64_decode_with_length
145 EXPECT_SLICE_EQ("", base64_decode_with_length("Zm:v", 3));
146 EXPECT_SLICE_EQ("", base64_decode_with_length("Zm=v", 3));
147
148 EXPECT_DECODED_LENGTH("", 0);
149 EXPECT_DECODED_LENGTH("ab", 1);
150 EXPECT_DECODED_LENGTH("abc", 2);
151 EXPECT_DECODED_LENGTH("abcd", 3);
152 EXPECT_DECODED_LENGTH("abcdef", 4);
153 EXPECT_DECODED_LENGTH("abcdefg", 5);
154 EXPECT_DECODED_LENGTH("abcdefgh", 6);
155
156 EXPECT_DECODED_LENGTH("ab==", 1);
157 EXPECT_DECODED_LENGTH("abc=", 2);
158 EXPECT_DECODED_LENGTH("abcd", 3);
159 EXPECT_DECODED_LENGTH("abcdef==", 4);
160 EXPECT_DECODED_LENGTH("abcdefg=", 5);
161 EXPECT_DECODED_LENGTH("abcdefgh", 6);
162
163 EXPECT_DECODED_LENGTH("a", 0);
164 EXPECT_DECODED_LENGTH("a===", 0);
165 EXPECT_DECODED_LENGTH("abcde", 0);
166 EXPECT_DECODED_LENGTH("abcde===", 0);
167 }
168
main(int argc,char ** argv)169 int main(int argc, char** argv) {
170 grpc::testing::TestEnvironment env(&argc, argv);
171 ::testing::InitGoogleTest(&argc, argv);
172 grpc::testing::TestGrpcScope grpc_scope;
173 return RUN_ALL_TESTS();
174 }
175