bin_encoder_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 /* This is here for grpc_is_binary_header
24  * TODO(murgatroid99): Remove this
25  */
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 
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(expected);
49 }
50 
51 static grpc_slice B64(const char* s) {
54  grpc_slice_unref(ss);
55  return out;
56 }
57 
58 static grpc_slice HUFF(const char* s) {
61  grpc_slice_unref(ss);
62  return out;
63 }
64 
65 #define EXPECT_SLICE_EQ(expected, slice) \
66  expect_slice_eq( \
67  grpc_slice_from_copied_buffer(expected, sizeof(expected) - 1), slice, \
68  #slice, __LINE__);
69 
70 static void expect_combined_equiv(const char* s, size_t len, int line) {
75  if (!grpc_slice_eq(expect, got)) {
77  char* e = grpc_dump_slice(expect, GPR_DUMP_HEX | GPR_DUMP_ASCII);
79  gpr_log(GPR_ERROR, "FAILED:%d:\ntest: %s\ngot: %s\nwant: %s", line, t, g,
80  e);
81  gpr_free(t);
82  gpr_free(e);
83  gpr_free(g);
84  all_ok = 0;
85  }
87  grpc_slice_unref(base64);
88  grpc_slice_unref(expect);
89  grpc_slice_unref(got);
90 }
91 
92 #define EXPECT_COMBINED_EQUIV(x) \
93  expect_combined_equiv(x, sizeof(x) - 1, __LINE__)
94 
95 static void expect_binary_header(const char* hdr, int binary) {
97  gpr_log(GPR_ERROR, "FAILED: expected header '%s' to be %s", hdr,
98  binary ? "binary" : "not binary");
99  all_ok = 0;
100  }
101 }
102 
103 int main(int argc, char** argv) {
104  grpc::testing::TestEnvironment env(&argc, argv);
105  grpc_init();
106 
107  /* Base64 test vectors from RFC 4648, with padding removed */
108  /* BASE64("") = "" */
109  EXPECT_SLICE_EQ("", B64(""));
110  /* BASE64("f") = "Zg" */
111  EXPECT_SLICE_EQ("Zg", B64("f"));
112  /* BASE64("fo") = "Zm8" */
113  EXPECT_SLICE_EQ("Zm8", B64("fo"));
114  /* BASE64("foo") = "Zm9v" */
115  EXPECT_SLICE_EQ("Zm9v", B64("foo"));
116  /* BASE64("foob") = "Zm9vYg" */
117  EXPECT_SLICE_EQ("Zm9vYg", B64("foob"));
118  /* BASE64("fooba") = "Zm9vYmE" */
119  EXPECT_SLICE_EQ("Zm9vYmE", B64("fooba"));
120  /* BASE64("foobar") = "Zm9vYmFy" */
121  EXPECT_SLICE_EQ("Zm9vYmFy", B64("foobar"));
122 
123  EXPECT_SLICE_EQ("wMHCw8TF", B64("\xc0\xc1\xc2\xc3\xc4\xc5"));
124 
125  /* Huffman encoding tests */
126  EXPECT_SLICE_EQ("\xf1\xe3\xc2\xe5\xf2\x3a\x6b\xa0\xab\x90\xf4\xff",
127  HUFF("www.example.com"));
128  EXPECT_SLICE_EQ("\xa8\xeb\x10\x64\x9c\xbf", HUFF("no-cache"));
129  EXPECT_SLICE_EQ("\x25\xa8\x49\xe9\x5b\xa9\x7d\x7f", HUFF("custom-key"));
130  EXPECT_SLICE_EQ("\x25\xa8\x49\xe9\x5b\xb8\xe8\xb4\xbf", HUFF("custom-value"));
131  EXPECT_SLICE_EQ("\xae\xc3\x77\x1a\x4b", HUFF("private"));
133  "\xd0\x7a\xbe\x94\x10\x54\xd4\x44\xa8\x20\x05\x95\x04\x0b\x81\x66\xe0\x82"
134  "\xa6\x2d\x1b\xff",
135  HUFF("Mon, 21 Oct 2013 20:13:21 GMT"));
137  "\x9d\x29\xad\x17\x18\x63\xc7\x8f\x0b\x97\xc8\xe9\xae\x82\xae\x43\xd3",
138  HUFF("https://www.example.com"));
139 
140  /* Various test vectors for combined encoding */
143  EXPECT_COMBINED_EQUIV("fo");
144  EXPECT_COMBINED_EQUIV("foo");
145  EXPECT_COMBINED_EQUIV("foob");
146  EXPECT_COMBINED_EQUIV("fooba");
147  EXPECT_COMBINED_EQUIV("foobar");
148  EXPECT_COMBINED_EQUIV("www.example.com");
149  EXPECT_COMBINED_EQUIV("no-cache");
150  EXPECT_COMBINED_EQUIV("custom-key");
151  EXPECT_COMBINED_EQUIV("custom-value");
152  EXPECT_COMBINED_EQUIV("private");
153  EXPECT_COMBINED_EQUIV("Mon, 21 Oct 2013 20:13:21 GMT");
154  EXPECT_COMBINED_EQUIV("https://www.example.com");
156  "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
157  "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
158  "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
159  "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
160  "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
161  "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
162  "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
163  "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
164  "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
165  "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
166  "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
167  "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
168  "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
169  "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
170  "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
171  "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff");
172 
173  expect_binary_header("foo-bin", 1);
174  expect_binary_header("foo-bar", 0);
175  expect_binary_header("-bin", 0);
176 
177  grpc_shutdown();
178  return all_ok ? 0 : 1;
179 }
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
B64
static grpc_slice B64(const char *s)
Definition: bin_encoder_test.cc:51
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
log.h
grpc_chttp2_base64_encode_and_huffman_compress
grpc_slice grpc_chttp2_base64_encode_and_huffman_compress(const grpc_slice &input)
Definition: bin_encoder.cc:169
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
EXPECT_COMBINED_EQUIV
#define EXPECT_COMBINED_EQUIV(x)
Definition: bin_encoder_test.cc:92
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
expect_slice_eq
static void expect_slice_eq(grpc_slice expected, grpc_slice slice, const char *debug, int line)
Definition: bin_encoder_test.cc:36
HUFF
static grpc_slice HUFF(const char *s)
Definition: bin_encoder_test.cc:58
GPR_DUMP_HEX
#define GPR_DUMP_HEX
Definition: string.h:34
expect_binary_header
static void expect_binary_header(const char *hdr, int binary)
Definition: bin_encoder_test.cc:95
GPR_DUMP_ASCII
#define GPR_DUMP_ASCII
Definition: string.h:35
expect_combined_equiv
static void expect_combined_equiv(const char *s, size_t len, int line)
Definition: bin_encoder_test.cc:70
all_ok
static int all_ok
Definition: bin_encoder_test.cc:34
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
grpc.h
grpc_slice_from_static_string
GPRAPI grpc_slice grpc_slice_from_static_string(const char *source)
Definition: slice/slice.cc:89
EXPECT_SLICE_EQ
#define EXPECT_SLICE_EQ(expected, slice)
Definition: bin_encoder_test.cc:65
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
g
struct @717 g
test_config.h
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Definition: slice/slice.cc:170
grpc_chttp2_base64_encode
grpc_slice grpc_chttp2_base64_encode(const grpc_slice &input)
Definition: bin_encoder.cc:52
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_is_binary_header
GRPCAPI int grpc_is_binary_header(grpc_slice slice)
Definition: validate_metadata.cc:131
regen-readme.line
line
Definition: regen-readme.py:30
_gevent_test_main.debug
def debug(sig, frame)
Definition: _gevent_test_main.py:57
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
grpc_chttp2_huffman_compress
grpc_slice grpc_chttp2_huffman_compress(const grpc_slice &input)
Definition: bin_encoder.cc:96
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
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
main
int main(int argc, char **argv)
Definition: bin_encoder_test.cc:103


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