bin_decoder_test.cc
Go to the documentation of this file.
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 
20 
21 #include <string.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 
33 
34 static int all_ok = 1;
35 
37  const char* debug, int line) {
38  if (!grpc_slice_eq(slice, expected)) {
40  char* he = grpc_dump_slice(expected, GPR_DUMP_HEX | GPR_DUMP_ASCII);
41  gpr_log(GPR_ERROR, "FAILED:%d: %s\ngot: %s\nwant: %s", line, debug, hs,
42  he);
43  gpr_free(hs);
44  gpr_free(he);
45  all_ok = 0;
46  }
47  grpc_slice_unref_internal(expected);
49 }
50 
51 static grpc_slice base64_encode(const char* s) {
55  return out;
56 }
57 
58 static grpc_slice base64_decode(const char* s) {
62  return out;
63 }
64 
66  size_t output_length) {
70  return out;
71 }
72 
73 static size_t base64_infer_length(const char* s) {
77  return out;
78 }
79 
80 #define EXPECT_DECODED_LENGTH(s, expected) \
81  GPR_ASSERT((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 
92 int main(int argc, char** argv) {
94  grpc_init();
95  {
97 
98  /* ENCODE_AND_DECODE tests grpc_chttp2_base64_decode_with_length(), which
99  takes encoded base64 strings without pad chars, but output length is
100  required. */
101  /* Base64 test vectors from RFC 4648 */
102  ENCODE_AND_DECODE("");
103  ENCODE_AND_DECODE("f");
104  ENCODE_AND_DECODE("foo");
105  ENCODE_AND_DECODE("fo");
106  ENCODE_AND_DECODE("foob");
107  ENCODE_AND_DECODE("fooba");
108  ENCODE_AND_DECODE("foobar");
109 
110  ENCODE_AND_DECODE("\xc0\xc1\xc2\xc3\xc4\xc5");
111 
112  /* Base64 test vectors from RFC 4648, with pad chars */
113  /* BASE64("") = "" */
115  /* BASE64("f") = "Zg==" */
116  EXPECT_SLICE_EQ("f", base64_decode("Zg=="));
117  /* BASE64("fo") = "Zm8=" */
118  EXPECT_SLICE_EQ("fo", base64_decode("Zm8="));
119  /* BASE64("foo") = "Zm9v" */
120  EXPECT_SLICE_EQ("foo", base64_decode("Zm9v"));
121  /* BASE64("foob") = "Zm9vYg==" */
122  EXPECT_SLICE_EQ("foob", base64_decode("Zm9vYg=="));
123  /* BASE64("fooba") = "Zm9vYmE=" */
124  EXPECT_SLICE_EQ("fooba", base64_decode("Zm9vYmE="));
125  /* BASE64("foobar") = "Zm9vYmFy" */
126  EXPECT_SLICE_EQ("foobar", base64_decode("Zm9vYmFy"));
127 
128  EXPECT_SLICE_EQ("\xc0\xc1\xc2\xc3\xc4\xc5", base64_decode("wMHCw8TF"));
129 
130  // Test illegal input length in grpc_chttp2_base64_decode
131  EXPECT_SLICE_EQ("", base64_decode("a"));
132  EXPECT_SLICE_EQ("", base64_decode("ab"));
133  EXPECT_SLICE_EQ("", base64_decode("abc"));
134 
135  // Test illegal charactors in grpc_chttp2_base64_decode
136  EXPECT_SLICE_EQ("", base64_decode("Zm:v"));
137  EXPECT_SLICE_EQ("", base64_decode("Zm=v"));
138 
139  // Test output_length longer than max possible output length in
140  // grpc_chttp2_base64_decode_with_length
144 
145  // Test illegal charactors in grpc_chttp2_base64_decode_with_length
148 
149  EXPECT_DECODED_LENGTH("", 0);
150  EXPECT_DECODED_LENGTH("ab", 1);
151  EXPECT_DECODED_LENGTH("abc", 2);
152  EXPECT_DECODED_LENGTH("abcd", 3);
153  EXPECT_DECODED_LENGTH("abcdef", 4);
154  EXPECT_DECODED_LENGTH("abcdefg", 5);
155  EXPECT_DECODED_LENGTH("abcdefgh", 6);
156 
157  EXPECT_DECODED_LENGTH("ab==", 1);
158  EXPECT_DECODED_LENGTH("abc=", 2);
159  EXPECT_DECODED_LENGTH("abcd", 3);
160  EXPECT_DECODED_LENGTH("abcdef==", 4);
161  EXPECT_DECODED_LENGTH("abcdefg=", 5);
162  EXPECT_DECODED_LENGTH("abcdefgh", 6);
163 
164  EXPECT_DECODED_LENGTH("a", 0);
165  EXPECT_DECODED_LENGTH("a===", 0);
166  EXPECT_DECODED_LENGTH("abcde", 0);
167  EXPECT_DECODED_LENGTH("abcde===", 0);
168  }
169  grpc_shutdown();
170  return all_ok ? 0 : 1;
171 }
grpc_chttp2_base64_infer_length_after_decode
size_t grpc_chttp2_base64_infer_length_after_decode(const grpc_slice &slice)
Definition: bin_decoder.cc:85
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
log.h
generate.env
env
Definition: generate.py:37
grpc_dump_slice
char * grpc_dump_slice(const grpc_slice &s, uint32_t flags)
Definition: slice_string_helpers.cc:25
grpc_slice_from_copied_string
GPRAPI grpc_slice grpc_slice_from_copied_string(const char *source)
Definition: slice/slice.cc:177
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
GPR_DUMP_HEX
#define GPR_DUMP_HEX
Definition: string.h:34
GPR_DUMP_ASCII
#define GPR_DUMP_ASCII
Definition: string.h:35
base64_decode
static grpc_slice base64_decode(const char *s)
Definition: bin_decoder_test.cc:58
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
base64_decode_with_length
static grpc_slice base64_decode_with_length(const char *s, size_t output_length)
Definition: bin_decoder_test.cc:65
main
int main(int argc, char **argv)
Definition: bin_decoder_test.cc:92
grpc.h
base64_infer_length
static size_t base64_infer_length(const char *s)
Definition: bin_decoder_test.cc:73
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_chttp2_base64_decode
grpc_slice grpc_chttp2_base64_decode(const grpc_slice &input)
Definition: bin_decoder.cc:163
slice_internal.h
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_core::ExecCtx
Definition: exec_ctx.h:97
test_config.h
base64_encode
static grpc_slice base64_encode(const char *s)
Definition: bin_decoder_test.cc:51
grpc_chttp2_base64_encode
grpc_slice grpc_chttp2_base64_encode(const grpc_slice &input)
Definition: bin_encoder.cc:52
ENCODE_AND_DECODE
#define ENCODE_AND_DECODE(s)
Definition: bin_decoder_test.cc:88
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
expect_slice_eq
static void expect_slice_eq(grpc_slice expected, grpc_slice slice, const char *debug, int line)
Definition: bin_decoder_test.cc:36
alloc.h
grpc_chttp2_base64_decode_with_length
grpc_slice grpc_chttp2_base64_decode_with_length(const grpc_slice &input, size_t output_length)
Definition: bin_decoder.cc:207
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
regen-readme.line
line
Definition: regen-readme.py:30
exec_ctx.h
EXPECT_DECODED_LENGTH
#define EXPECT_DECODED_LENGTH(s, expected)
Definition: bin_decoder_test.cc:80
_gevent_test_main.debug
def debug(sig, frame)
Definition: _gevent_test_main.py:57
EXPECT_SLICE_EQ
#define EXPECT_SLICE_EQ(expected, slice)
Definition: bin_decoder_test.cc:83
all_ok
static int all_ok
Definition: bin_decoder_test.cc:34
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
bin_decoder.h
grpc_slice_eq
GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b)
Definition: slice/slice.cc:387
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
bin_encoder.h
slice_string_helpers.h
grpc_slice_unref_internal
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:39


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:37