• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/grpc.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
27 #include "src/core/lib/gpr/string.h"
28 #include "src/core/lib/iomgr/exec_ctx.h"
29 #include "src/core/lib/slice/slice_internal.h"
30 #include "src/core/lib/slice/slice_string_helpers.h"
31 
32 static int all_ok = 1;
33 
expect_slice_eq(grpc_slice expected,grpc_slice slice,const char * debug,int line)34 static void expect_slice_eq(grpc_slice expected, grpc_slice slice,
35                             const char* debug, int line) {
36   if (!grpc_slice_eq(slice, expected)) {
37     char* hs = grpc_dump_slice(slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
38     char* he = grpc_dump_slice(expected, GPR_DUMP_HEX | GPR_DUMP_ASCII);
39     gpr_log(GPR_ERROR, "FAILED:%d: %s\ngot:  %s\nwant: %s", line, debug, hs,
40             he);
41     gpr_free(hs);
42     gpr_free(he);
43     all_ok = 0;
44   }
45   grpc_slice_unref_internal(expected);
46   grpc_slice_unref_internal(slice);
47 }
48 
base64_encode(const char * s)49 static grpc_slice base64_encode(const char* s) {
50   grpc_slice ss = grpc_slice_from_copied_string(s);
51   grpc_slice out = grpc_chttp2_base64_encode(ss);
52   grpc_slice_unref_internal(ss);
53   return out;
54 }
55 
base64_decode(const char * s)56 static grpc_slice base64_decode(const char* s) {
57   grpc_slice ss = grpc_slice_from_copied_string(s);
58   grpc_slice out = grpc_chttp2_base64_decode(ss);
59   grpc_slice_unref_internal(ss);
60   return out;
61 }
62 
base64_decode_with_length(const char * s,size_t output_length)63 static grpc_slice base64_decode_with_length(const char* s,
64                                             size_t output_length) {
65   grpc_slice ss = grpc_slice_from_copied_string(s);
66   grpc_slice out = grpc_chttp2_base64_decode_with_length(ss, output_length);
67   grpc_slice_unref_internal(ss);
68   return out;
69 }
70 
base64_infer_length(const char * s)71 static size_t base64_infer_length(const char* s) {
72   grpc_slice ss = grpc_slice_from_copied_string(s);
73   size_t out = grpc_chttp2_base64_infer_length_after_decode(ss);
74   grpc_slice_unref_internal(ss);
75   return out;
76 }
77 
78 #define EXPECT_DECODED_LENGTH(s, expected) \
79   GPR_ASSERT((expected) == base64_infer_length((s)));
80 
81 #define EXPECT_SLICE_EQ(expected, slice)                                    \
82   expect_slice_eq(                                                          \
83       grpc_slice_from_copied_buffer(expected, sizeof(expected) - 1), slice, \
84       #slice, __LINE__);
85 
86 #define ENCODE_AND_DECODE(s) \
87   EXPECT_SLICE_EQ(           \
88       s, grpc_chttp2_base64_decode_with_length(base64_encode(s), strlen(s)));
89 
main(int argc,char ** argv)90 int main(int argc, char** argv) {
91   grpc_init();
92   {
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 charactors 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 charactors 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   grpc_shutdown();
167   return all_ok ? 0 : 1;
168 }
169