Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | a99687b | 2016-01-27 13:59:08 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/transport/chttp2/hpack_table.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | #include <stdio.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 37 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
yang-g | d88e1d8 | 2015-12-02 13:23:33 -0800 | [diff] [blame] | 39 | #include <grpc/grpc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 42 | #include <grpc/support/string_util.h> |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 43 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 44 | #include "src/core/lib/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include "test/core/util/test_config.h" |
| 46 | |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 47 | #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
| 49 | static 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 Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 54 | static void assert_index(const grpc_chttp2_hptbl *tbl, uint32_t idx, |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 55 | const char *key, const char *value) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | 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 Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 61 | static void test_static_lookup(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | grpc_chttp2_hptbl tbl; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 64 | grpc_chttp2_hptbl_init(&tbl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 66 | LOG_TEST("test_static_lookup"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 132 | static void test_many_additions(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 133 | grpc_chttp2_hptbl tbl; |
| 134 | int i; |
Craig Tiller | 68cc1af | 2015-01-23 15:10:53 -0800 | [diff] [blame] | 135 | char *key; |
| 136 | char *value; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 137 | |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 138 | LOG_TEST("test_many_additions"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 139 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 140 | grpc_chttp2_hptbl_init(&tbl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | |
| 142 | for (i = 0; i < 1000000; i++) { |
Craig Tiller | 82e249b | 2015-11-11 01:14:39 +0000 | [diff] [blame] | 143 | grpc_mdelem *elem; |
Craig Tiller | 68cc1af | 2015-01-23 15:10:53 -0800 | [diff] [blame] | 144 | gpr_asprintf(&key, "K:%d", i); |
| 145 | gpr_asprintf(&value, "VALUE:%d", i); |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 146 | elem = grpc_mdelem_from_strings(key, value); |
Craig Tiller | 82e249b | 2015-11-11 01:14:39 +0000 | [diff] [blame] | 147 | GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem)); |
| 148 | GRPC_MDELEM_UNREF(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | assert_index(&tbl, 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value); |
Craig Tiller | 68cc1af | 2015-01-23 15:10:53 -0800 | [diff] [blame] | 150 | gpr_free(key); |
| 151 | gpr_free(value); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 152 | if (i) { |
Craig Tiller | 68cc1af | 2015-01-23 15:10:53 -0800 | [diff] [blame] | 153 | gpr_asprintf(&key, "K:%d", i - 1); |
| 154 | gpr_asprintf(&value, "VALUE:%d", i - 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | assert_index(&tbl, 2 + GRPC_CHTTP2_LAST_STATIC_ENTRY, key, value); |
Craig Tiller | 68cc1af | 2015-01-23 15:10:53 -0800 | [diff] [blame] | 156 | gpr_free(key); |
| 157 | gpr_free(value); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | grpc_chttp2_hptbl_destroy(&tbl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static grpc_chttp2_hptbl_find_result find_simple(grpc_chttp2_hptbl *tbl, |
| 165 | const char *key, |
| 166 | const char *value) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 167 | grpc_mdelem *md = grpc_mdelem_from_strings(key, value); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 168 | grpc_chttp2_hptbl_find_result r = grpc_chttp2_hptbl_find(tbl, md); |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 169 | GRPC_MDELEM_UNREF(md); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | return r; |
| 171 | } |
| 172 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 173 | static void test_find(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 174 | grpc_chttp2_hptbl tbl; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 175 | uint32_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 176 | char buffer[32]; |
Craig Tiller | 82e249b | 2015-11-11 01:14:39 +0000 | [diff] [blame] | 177 | grpc_mdelem *elem; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | grpc_chttp2_hptbl_find_result r; |
| 179 | |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 180 | LOG_TEST("test_find"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 182 | grpc_chttp2_hptbl_init(&tbl); |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 183 | elem = grpc_mdelem_from_strings("abc", "xyz"); |
Craig Tiller | 82e249b | 2015-11-11 01:14:39 +0000 | [diff] [blame] | 184 | GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem)); |
| 185 | GRPC_MDELEM_UNREF(elem); |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 186 | elem = grpc_mdelem_from_strings("abc", "123"); |
Craig Tiller | 82e249b | 2015-11-11 01:14:39 +0000 | [diff] [blame] | 187 | GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem)); |
| 188 | GRPC_MDELEM_UNREF(elem); |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 189 | elem = grpc_mdelem_from_strings("x", "1"); |
Craig Tiller | 82e249b | 2015-11-11 01:14:39 +0000 | [diff] [blame] | 190 | GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem)); |
| 191 | GRPC_MDELEM_UNREF(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 192 | |
| 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 Tattermusch | a99687b | 2016-01-27 13:59:08 -0800 | [diff] [blame] | 239 | int64_ttoa(i, buffer); |
Craig Tiller | 0927c70 | 2015-11-20 12:25:54 -0800 | [diff] [blame] | 240 | elem = grpc_mdelem_from_strings("test", buffer); |
Craig Tiller | 82e249b | 2015-11-11 01:14:39 +0000 | [diff] [blame] | 241 | GPR_ASSERT(grpc_chttp2_hptbl_add(&tbl, elem)); |
| 242 | GRPC_MDELEM_UNREF(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 243 | } |
| 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 Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 258 | uint32_t expect = 9999 - i; |
Jan Tattermusch | a99687b | 2016-01-27 13:59:08 -0800 | [diff] [blame] | 259 | int64_ttoa(expect, buffer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 260 | |
| 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | int main(int argc, char **argv) { |
| 274 | grpc_test_init(argc, argv); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 275 | grpc_init(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 276 | test_static_lookup(); |
| 277 | test_many_additions(); |
| 278 | test_find(); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 279 | grpc_shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 280 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 281 | } |