blob: fbacdc3ad6a3516ed04a57b1550c7fb26227cdf6 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermuscha99687b2016-01-27 13:59:08 -08003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/transport/chttp2/hpack_table.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036#include <stdio.h>
Craig Tillerf40df232016-03-25 13:38:14 -070037#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
yang-gd88e1d82015-12-02 13:23:33 -080039#include <grpc/grpc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070042#include <grpc/support/string_util.h>
Craig Tiller0e72ede2015-11-19 07:48:53 -080043
Craig Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045#include "test/core/util/test_config.h"
46
Craig Tiller35696192015-05-24 15:00:37 -070047#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
49static void assert_str(const grpc_chttp2_hptbl *tbl, grpc_mdstr *mdstr,
50 const char *str) {
51 GPR_ASSERT(gpr_slice_str_cmp(mdstr->slice, str) == 0);
52}
53
Craig Tiller7536af02015-12-22 13:49:30 -080054static void assert_index(const grpc_chttp2_hptbl *tbl, uint32_t idx,
Craig Tiller32ca48c2015-09-10 11:47:15 -070055 const char *key, const char *value) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056 grpc_mdelem *md = grpc_chttp2_hptbl_lookup(tbl, idx);
57 assert_str(tbl, md->key, key);
58 assert_str(tbl, md->value, value);
59}
60
Craig Tiller32946d32015-01-15 11:37:30 -080061static void test_static_lookup(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062 grpc_chttp2_hptbl tbl;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063
Craig Tillerb2b42612015-11-20 12:02:17 -080064 grpc_chttp2_hptbl_init(&tbl);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065
Craig Tiller35696192015-05-24 15:00:37 -070066 LOG_TEST("test_static_lookup");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 assert_index(&tbl, 1, ":authority", "");
68 assert_index(&tbl, 2, ":method", "GET");
69 assert_index(&tbl, 3, ":method", "POST");
70 assert_index(&tbl, 4, ":path", "/");
71 assert_index(&tbl, 5, ":path", "/index.html");
72 assert_index(&tbl, 6, ":scheme", "http");
73 assert_index(&tbl, 7, ":scheme", "https");
74 assert_index(&tbl, 8, ":status", "200");
75 assert_index(&tbl, 9, ":status", "204");
76 assert_index(&tbl, 10, ":status", "206");
77 assert_index(&tbl, 11, ":status", "304");
78 assert_index(&tbl, 12, ":status", "400");
79 assert_index(&tbl, 13, ":status", "404");
80 assert_index(&tbl, 14, ":status", "500");
81 assert_index(&tbl, 15, "accept-charset", "");
82 assert_index(&tbl, 16, "accept-encoding", "gzip, deflate");
83 assert_index(&tbl, 17, "accept-language", "");
84 assert_index(&tbl, 18, "accept-ranges", "");
85 assert_index(&tbl, 19, "accept", "");
86 assert_index(&tbl, 20, "access-control-allow-origin", "");
87 assert_index(&tbl, 21, "age", "");
88 assert_index(&tbl, 22, "allow", "");
89 assert_index(&tbl, 23, "authorization", "");
90 assert_index(&tbl, 24, "cache-control", "");
91 assert_index(&tbl, 25, "content-disposition", "");
92 assert_index(&tbl, 26, "content-encoding", "");
93 assert_index(&tbl, 27, "content-language", "");
94 assert_index(&tbl, 28, "content-length", "");
95 assert_index(&tbl, 29, "content-location", "");
96 assert_index(&tbl, 30, "content-range", "");
97 assert_index(&tbl, 31, "content-type", "");
98 assert_index(&tbl, 32, "cookie", "");
99 assert_index(&tbl, 33, "date", "");
100 assert_index(&tbl, 34, "etag", "");
101 assert_index(&tbl, 35, "expect", "");
102 assert_index(&tbl, 36, "expires", "");
103 assert_index(&tbl, 37, "from", "");
104 assert_index(&tbl, 38, "host", "");
105 assert_index(&tbl, 39, "if-match", "");
106 assert_index(&tbl, 40, "if-modified-since", "");
107 assert_index(&tbl, 41, "if-none-match", "");
108 assert_index(&tbl, 42, "if-range", "");
109 assert_index(&tbl, 43, "if-unmodified-since", "");
110 assert_index(&tbl, 44, "last-modified", "");
111 assert_index(&tbl, 45, "link", "");
112 assert_index(&tbl, 46, "location", "");
113 assert_index(&tbl, 47, "max-forwards", "");
114 assert_index(&tbl, 48, "proxy-authenticate", "");
115 assert_index(&tbl, 49, "proxy-authorization", "");
116 assert_index(&tbl, 50, "range", "");
117 assert_index(&tbl, 51, "referer", "");
118 assert_index(&tbl, 52, "refresh", "");
119 assert_index(&tbl, 53, "retry-after", "");
120 assert_index(&tbl, 54, "server", "");
121 assert_index(&tbl, 55, "set-cookie", "");
122 assert_index(&tbl, 56, "strict-transport-security", "");
123 assert_index(&tbl, 57, "transfer-encoding", "");
124 assert_index(&tbl, 58, "user-agent", "");
125 assert_index(&tbl, 59, "vary", "");
126 assert_index(&tbl, 60, "via", "");
127 assert_index(&tbl, 61, "www-authenticate", "");
128
129 grpc_chttp2_hptbl_destroy(&tbl);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130}
131
Craig Tiller32946d32015-01-15 11:37:30 -0800132static void test_many_additions(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133 grpc_chttp2_hptbl tbl;
134 int i;
Craig Tiller68cc1af2015-01-23 15:10:53 -0800135 char *key;
136 char *value;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800137
Craig Tiller35696192015-05-24 15:00:37 -0700138 LOG_TEST("test_many_additions");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800139
Craig Tillerb2b42612015-11-20 12:02:17 -0800140 grpc_chttp2_hptbl_init(&tbl);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141
142 for (i = 0; i < 1000000; i++) {
Craig Tiller82e249b2015-11-11 01:14:39 +0000143 grpc_mdelem *elem;
Craig Tiller68cc1af2015-01-23 15:10:53 -0800144 gpr_asprintf(&key, "K:%d", i);
145 gpr_asprintf(&value, "VALUE:%d", i);
Craig Tiller0927c702015-11-20 12:25:54 -0800146 elem = grpc_mdelem_from_strings(key, value);
Craig Tiller82e249b2015-11-11 01:14:39 +0000147 GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem));
148 GRPC_MDELEM_UNREF(elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149 assert_index(&tbl, 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value);
Craig Tiller68cc1af2015-01-23 15:10:53 -0800150 gpr_free(key);
151 gpr_free(value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152 if (i) {
Craig Tiller68cc1af2015-01-23 15:10:53 -0800153 gpr_asprintf(&key, "K:%d", i - 1);
154 gpr_asprintf(&value, "VALUE:%d", i - 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800155 assert_index(&tbl, 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value);
Craig Tiller68cc1af2015-01-23 15:10:53 -0800156 gpr_free(key);
157 gpr_free(value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158 }
159 }
160
161 grpc_chttp2_hptbl_destroy(&tbl);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800162}
163
164static grpc_chttp2_hptbl_find_result find_simple(grpc_chttp2_hptbl *tbl,
165 const char *key,
166 const char *value) {
Craig Tillerb2b42612015-11-20 12:02:17 -0800167 grpc_mdelem *md = grpc_mdelem_from_strings(key, value);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800168 grpc_chttp2_hptbl_find_result r = grpc_chttp2_hptbl_find(tbl, md);
Craig Tiller1a65a232015-07-06 10:22:32 -0700169 GRPC_MDELEM_UNREF(md);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170 return r;
171}
172
Craig Tiller32946d32015-01-15 11:37:30 -0800173static void test_find(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174 grpc_chttp2_hptbl tbl;
Craig Tiller7536af02015-12-22 13:49:30 -0800175 uint32_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176 char buffer[32];
Craig Tiller82e249b2015-11-11 01:14:39 +0000177 grpc_mdelem *elem;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178 grpc_chttp2_hptbl_find_result r;
179
Craig Tiller35696192015-05-24 15:00:37 -0700180 LOG_TEST("test_find");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181
Craig Tillerb2b42612015-11-20 12:02:17 -0800182 grpc_chttp2_hptbl_init(&tbl);
Craig Tiller0927c702015-11-20 12:25:54 -0800183 elem = grpc_mdelem_from_strings("abc", "xyz");
Craig Tiller82e249b2015-11-11 01:14:39 +0000184 GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem));
185 GRPC_MDELEM_UNREF(elem);
Craig Tiller0927c702015-11-20 12:25:54 -0800186 elem = grpc_mdelem_from_strings("abc", "123");
Craig Tiller82e249b2015-11-11 01:14:39 +0000187 GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem));
188 GRPC_MDELEM_UNREF(elem);
Craig Tiller0927c702015-11-20 12:25:54 -0800189 elem = grpc_mdelem_from_strings("x", "1");
Craig Tiller82e249b2015-11-11 01:14:39 +0000190 GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem));
191 GRPC_MDELEM_UNREF(elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192
193 r = find_simple(&tbl, "abc", "123");
194 GPR_ASSERT(r.index == 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
195 GPR_ASSERT(r.has_value == 1);
196
197 r = find_simple(&tbl, "abc", "xyz");
198 GPR_ASSERT(r.index == 3 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
199 GPR_ASSERT(r.has_value == 1);
200
201 r = find_simple(&tbl, "x", "1");
202 GPR_ASSERT(r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
203 GPR_ASSERT(r.has_value == 1);
204
205 r = find_simple(&tbl, "x", "2");
206 GPR_ASSERT(r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
207 GPR_ASSERT(r.has_value == 0);
208
209 r = find_simple(&tbl, "vary", "some-vary-arg");
210 GPR_ASSERT(r.index == 59);
211 GPR_ASSERT(r.has_value == 0);
212
213 r = find_simple(&tbl, "accept-encoding", "gzip, deflate");
214 GPR_ASSERT(r.index == 16);
215 GPR_ASSERT(r.has_value == 1);
216
217 r = find_simple(&tbl, "accept-encoding", "gzip");
218 GPR_ASSERT(r.index == 16);
219 GPR_ASSERT(r.has_value == 0);
220
221 r = find_simple(&tbl, ":method", "GET");
222 GPR_ASSERT(r.index == 2);
223 GPR_ASSERT(r.has_value == 1);
224
225 r = find_simple(&tbl, ":method", "POST");
226 GPR_ASSERT(r.index == 3);
227 GPR_ASSERT(r.has_value == 1);
228
229 r = find_simple(&tbl, ":method", "PUT");
230 GPR_ASSERT(r.index == 2 || r.index == 3);
231 GPR_ASSERT(r.has_value == 0);
232
233 r = find_simple(&tbl, "this-does-not-exist", "");
234 GPR_ASSERT(r.index == 0);
235 GPR_ASSERT(r.has_value == 0);
236
237 /* overflow the string buffer, check find still works */
238 for (i = 0; i < 10000; i++) {
Jan Tattermuscha99687b2016-01-27 13:59:08 -0800239 int64_ttoa(i, buffer);
Craig Tiller0927c702015-11-20 12:25:54 -0800240 elem = grpc_mdelem_from_strings("test", buffer);
Craig Tiller82e249b2015-11-11 01:14:39 +0000241 GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem));
242 GRPC_MDELEM_UNREF(elem);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800243 }
244
245 r = find_simple(&tbl, "abc", "123");
246 GPR_ASSERT(r.index == 0);
247 GPR_ASSERT(r.has_value == 0);
248
249 r = find_simple(&tbl, "test", "9999");
250 GPR_ASSERT(r.index == 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
251 GPR_ASSERT(r.has_value == 1);
252
253 r = find_simple(&tbl, "test", "9998");
254 GPR_ASSERT(r.index == 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
255 GPR_ASSERT(r.has_value == 1);
256
257 for (i = 0; i < tbl.num_ents; i++) {
Craig Tiller7536af02015-12-22 13:49:30 -0800258 uint32_t expect = 9999 - i;
Jan Tattermuscha99687b2016-01-27 13:59:08 -0800259 int64_ttoa(expect, buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800260
261 r = find_simple(&tbl, "test", buffer);
262 GPR_ASSERT(r.index == i + 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY);
263 GPR_ASSERT(r.has_value == 1);
264 }
265
266 r = find_simple(&tbl, "test", "10000");
267 GPR_ASSERT(r.index != 0);
268 GPR_ASSERT(r.has_value == 0);
269
270 grpc_chttp2_hptbl_destroy(&tbl);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800271}
272
273int main(int argc, char **argv) {
274 grpc_test_init(argc, argv);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800275 grpc_init();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800276 test_static_lookup();
277 test_many_additions();
278 test_find();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800279 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800280 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800281}