• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "src/core/ext/transport/chttp2/transport/hpack_table.h"
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/string_util.h>
28 
29 #include "src/core/lib/gpr/string.h"
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "test/core/util/test_config.h"
32 
33 #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
34 
assert_str(const grpc_chttp2_hptbl * tbl,grpc_slice mdstr,const char * str)35 static void assert_str(const grpc_chttp2_hptbl* tbl, grpc_slice mdstr,
36                        const char* str) {
37   GPR_ASSERT(grpc_slice_str_cmp(mdstr, str) == 0);
38 }
39 
assert_index(const grpc_chttp2_hptbl * tbl,uint32_t idx,const char * key,const char * value)40 static void assert_index(const grpc_chttp2_hptbl* tbl, uint32_t idx,
41                          const char* key, const char* value) {
42   grpc_mdelem md = grpc_chttp2_hptbl_lookup(tbl, idx);
43   assert_str(tbl, GRPC_MDKEY(md), key);
44   assert_str(tbl, GRPC_MDVALUE(md), value);
45 }
46 
test_static_lookup(void)47 static void test_static_lookup(void) {
48   grpc_core::ExecCtx exec_ctx;
49   grpc_chttp2_hptbl tbl;
50 
51   grpc_chttp2_hptbl_init(&tbl);
52 
53   LOG_TEST("test_static_lookup");
54   assert_index(&tbl, 1, ":authority", "");
55   assert_index(&tbl, 2, ":method", "GET");
56   assert_index(&tbl, 3, ":method", "POST");
57   assert_index(&tbl, 4, ":path", "/");
58   assert_index(&tbl, 5, ":path", "/index.html");
59   assert_index(&tbl, 6, ":scheme", "http");
60   assert_index(&tbl, 7, ":scheme", "https");
61   assert_index(&tbl, 8, ":status", "200");
62   assert_index(&tbl, 9, ":status", "204");
63   assert_index(&tbl, 10, ":status", "206");
64   assert_index(&tbl, 11, ":status", "304");
65   assert_index(&tbl, 12, ":status", "400");
66   assert_index(&tbl, 13, ":status", "404");
67   assert_index(&tbl, 14, ":status", "500");
68   assert_index(&tbl, 15, "accept-charset", "");
69   assert_index(&tbl, 16, "accept-encoding", "gzip, deflate");
70   assert_index(&tbl, 17, "accept-language", "");
71   assert_index(&tbl, 18, "accept-ranges", "");
72   assert_index(&tbl, 19, "accept", "");
73   assert_index(&tbl, 20, "access-control-allow-origin", "");
74   assert_index(&tbl, 21, "age", "");
75   assert_index(&tbl, 22, "allow", "");
76   assert_index(&tbl, 23, "authorization", "");
77   assert_index(&tbl, 24, "cache-control", "");
78   assert_index(&tbl, 25, "content-disposition", "");
79   assert_index(&tbl, 26, "content-encoding", "");
80   assert_index(&tbl, 27, "content-language", "");
81   assert_index(&tbl, 28, "content-length", "");
82   assert_index(&tbl, 29, "content-location", "");
83   assert_index(&tbl, 30, "content-range", "");
84   assert_index(&tbl, 31, "content-type", "");
85   assert_index(&tbl, 32, "cookie", "");
86   assert_index(&tbl, 33, "date", "");
87   assert_index(&tbl, 34, "etag", "");
88   assert_index(&tbl, 35, "expect", "");
89   assert_index(&tbl, 36, "expires", "");
90   assert_index(&tbl, 37, "from", "");
91   assert_index(&tbl, 38, "host", "");
92   assert_index(&tbl, 39, "if-match", "");
93   assert_index(&tbl, 40, "if-modified-since", "");
94   assert_index(&tbl, 41, "if-none-match", "");
95   assert_index(&tbl, 42, "if-range", "");
96   assert_index(&tbl, 43, "if-unmodified-since", "");
97   assert_index(&tbl, 44, "last-modified", "");
98   assert_index(&tbl, 45, "link", "");
99   assert_index(&tbl, 46, "location", "");
100   assert_index(&tbl, 47, "max-forwards", "");
101   assert_index(&tbl, 48, "proxy-authenticate", "");
102   assert_index(&tbl, 49, "proxy-authorization", "");
103   assert_index(&tbl, 50, "range", "");
104   assert_index(&tbl, 51, "referer", "");
105   assert_index(&tbl, 52, "refresh", "");
106   assert_index(&tbl, 53, "retry-after", "");
107   assert_index(&tbl, 54, "server", "");
108   assert_index(&tbl, 55, "set-cookie", "");
109   assert_index(&tbl, 56, "strict-transport-security", "");
110   assert_index(&tbl, 57, "transfer-encoding", "");
111   assert_index(&tbl, 58, "user-agent", "");
112   assert_index(&tbl, 59, "vary", "");
113   assert_index(&tbl, 60, "via", "");
114   assert_index(&tbl, 61, "www-authenticate", "");
115 
116   grpc_chttp2_hptbl_destroy(&tbl);
117 }
118 
test_many_additions(void)119 static void test_many_additions(void) {
120   grpc_chttp2_hptbl tbl;
121   int i;
122   char* key;
123   char* value;
124 
125   LOG_TEST("test_many_additions");
126 
127   grpc_core::ExecCtx exec_ctx;
128   grpc_chttp2_hptbl_init(&tbl);
129 
130   for (i = 0; i < 100000; i++) {
131     grpc_mdelem elem;
132     gpr_asprintf(&key, "K:%d", i);
133     gpr_asprintf(&value, "VALUE:%d", i);
134     elem = grpc_mdelem_from_slices(grpc_slice_from_copied_string(key),
135                                    grpc_slice_from_copied_string(value));
136     GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem) == GRPC_ERROR_NONE);
137     GRPC_MDELEM_UNREF(elem);
138     assert_index(&tbl, 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value);
139     gpr_free(key);
140     gpr_free(value);
141     if (i) {
142       gpr_asprintf(&key, "K:%d", i - 1);
143       gpr_asprintf(&value, "VALUE:%d", i - 1);
144       assert_index(&tbl, 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value);
145       gpr_free(key);
146       gpr_free(value);
147     }
148   }
149 
150   grpc_chttp2_hptbl_destroy(&tbl);
151 }
152 
find_simple(grpc_chttp2_hptbl * tbl,const char * key,const char * value)153 static grpc_chttp2_hptbl_find_result find_simple(grpc_chttp2_hptbl* tbl,
154                                                  const char* key,
155                                                  const char* value) {
156   grpc_core::ExecCtx exec_ctx;
157   grpc_mdelem md = grpc_mdelem_from_slices(
158       grpc_slice_from_copied_string(key), grpc_slice_from_copied_string(value));
159   grpc_chttp2_hptbl_find_result r = grpc_chttp2_hptbl_find(tbl, md);
160   GRPC_MDELEM_UNREF(md);
161 
162   return r;
163 }
164 
test_find(void)165 static void test_find(void) {
166   grpc_core::ExecCtx exec_ctx;
167   grpc_chttp2_hptbl tbl;
168   uint32_t i;
169   char buffer[32];
170   grpc_mdelem elem;
171   grpc_chttp2_hptbl_find_result r;
172 
173   LOG_TEST("test_find");
174 
175   grpc_chttp2_hptbl_init(&tbl);
176   elem = grpc_mdelem_from_slices(grpc_slice_from_static_string("abc"),
177                                  grpc_slice_from_static_string("xyz"));
178   GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem) == GRPC_ERROR_NONE);
179   GRPC_MDELEM_UNREF(elem);
180   elem = grpc_mdelem_from_slices(grpc_slice_from_static_string("abc"),
181                                  grpc_slice_from_static_string("123"));
182   GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem) == GRPC_ERROR_NONE);
183   GRPC_MDELEM_UNREF(elem);
184   elem = grpc_mdelem_from_slices(grpc_slice_from_static_string("x"),
185                                  grpc_slice_from_static_string("1"));
186   GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem) == GRPC_ERROR_NONE);
187   GRPC_MDELEM_UNREF(elem);
188 
189   r = find_simple(&tbl, "abc", "123");
190   GPR_ASSERT(r.index == 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
191   GPR_ASSERT(r.has_value == 1);
192 
193   r = find_simple(&tbl, "abc", "xyz");
194   GPR_ASSERT(r.index == 3 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
195   GPR_ASSERT(r.has_value == 1);
196 
197   r = find_simple(&tbl, "x", "1");
198   GPR_ASSERT(r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
199   GPR_ASSERT(r.has_value == 1);
200 
201   r = find_simple(&tbl, "x", "2");
202   GPR_ASSERT(r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
203   GPR_ASSERT(r.has_value == 0);
204 
205   r = find_simple(&tbl, "vary", "some-vary-arg");
206   GPR_ASSERT(r.index == 59);
207   GPR_ASSERT(r.has_value == 0);
208 
209   r = find_simple(&tbl, "accept-encoding", "gzip, deflate");
210   GPR_ASSERT(r.index == 16);
211   GPR_ASSERT(r.has_value == 1);
212 
213   r = find_simple(&tbl, "accept-encoding", "gzip");
214   GPR_ASSERT(r.index == 16);
215   GPR_ASSERT(r.has_value == 0);
216 
217   r = find_simple(&tbl, ":method", "GET");
218   GPR_ASSERT(r.index == 2);
219   GPR_ASSERT(r.has_value == 1);
220 
221   r = find_simple(&tbl, ":method", "POST");
222   GPR_ASSERT(r.index == 3);
223   GPR_ASSERT(r.has_value == 1);
224 
225   r = find_simple(&tbl, ":method", "PUT");
226   GPR_ASSERT(r.index == 2 || r.index == 3);
227   GPR_ASSERT(r.has_value == 0);
228 
229   r = find_simple(&tbl, "this-does-not-exist", "");
230   GPR_ASSERT(r.index == 0);
231   GPR_ASSERT(r.has_value == 0);
232 
233   /* overflow the string buffer, check find still works */
234   for (i = 0; i < 10000; i++) {
235     int64_ttoa(i, buffer);
236     elem = grpc_mdelem_from_slices(grpc_slice_from_static_string("test"),
237                                    grpc_slice_from_copied_string(buffer));
238     GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem) == GRPC_ERROR_NONE);
239     GRPC_MDELEM_UNREF(elem);
240   }
241 
242   r = find_simple(&tbl, "abc", "123");
243   GPR_ASSERT(r.index == 0);
244   GPR_ASSERT(r.has_value == 0);
245 
246   r = find_simple(&tbl, "test", "9999");
247   GPR_ASSERT(r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
248   GPR_ASSERT(r.has_value == 1);
249 
250   r = find_simple(&tbl, "test", "9998");
251   GPR_ASSERT(r.index == 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
252   GPR_ASSERT(r.has_value == 1);
253 
254   for (i = 0; i < tbl.num_ents; i++) {
255     uint32_t expect = 9999 - i;
256     int64_ttoa(expect, buffer);
257 
258     r = find_simple(&tbl, "test", buffer);
259     GPR_ASSERT(r.index == i + 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
260     GPR_ASSERT(r.has_value == 1);
261   }
262 
263   r = find_simple(&tbl, "test", "10000");
264   GPR_ASSERT(r.index != 0);
265   GPR_ASSERT(r.has_value == 0);
266 
267   grpc_chttp2_hptbl_destroy(&tbl);
268 }
269 
main(int argc,char ** argv)270 int main(int argc, char** argv) {
271   grpc_test_init(argc, argv);
272   grpc_init();
273   test_static_lookup();
274   test_many_additions();
275   test_find();
276   grpc_shutdown();
277   return 0;
278 }
279