Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | b7f3f6e | 2016-03-25 17:13:09 -0700 | [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_parser.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
| 36 | #include <stdarg.h> |
| 37 | |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 38 | #include <grpc/grpc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
| 41 | #include <grpc/support/slice.h> |
| 42 | #include "test/core/util/parse_hexstring.h" |
| 43 | #include "test/core/util/slice_splitter.h" |
| 44 | #include "test/core/util/test_config.h" |
| 45 | |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 46 | typedef struct { va_list args; } test_checker; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
| 48 | static void onhdr(void *ud, grpc_mdelem *md) { |
| 49 | const char *ekey, *evalue; |
| 50 | test_checker *chk = ud; |
| 51 | ekey = va_arg(chk->args, char *); |
| 52 | GPR_ASSERT(ekey); |
| 53 | evalue = va_arg(chk->args, char *); |
| 54 | GPR_ASSERT(evalue); |
| 55 | GPR_ASSERT(gpr_slice_str_cmp(md->key->slice, ekey) == 0); |
| 56 | GPR_ASSERT(gpr_slice_str_cmp(md->value->slice, evalue) == 0); |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 57 | GRPC_MDELEM_UNREF(md); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void test_vector(grpc_chttp2_hpack_parser *parser, |
| 61 | grpc_slice_split_mode mode, const char *hexstring, |
| 62 | ... /* char *key, char *value */) { |
| 63 | gpr_slice input = parse_hexstring(hexstring); |
| 64 | gpr_slice *slices; |
| 65 | size_t nslices; |
| 66 | size_t i; |
| 67 | test_checker chk; |
| 68 | |
| 69 | va_start(chk.args, hexstring); |
| 70 | |
| 71 | parser->on_header = onhdr; |
| 72 | parser->on_header_user_data = &chk; |
| 73 | |
| 74 | grpc_split_slices(mode, &input, 1, &slices, &nslices); |
| 75 | gpr_slice_unref(input); |
| 76 | |
| 77 | for (i = 0; i < nslices; i++) { |
| 78 | GPR_ASSERT(grpc_chttp2_hpack_parser_parse( |
| 79 | parser, GPR_SLICE_START_PTR(slices[i]), GPR_SLICE_END_PTR(slices[i]))); |
| 80 | } |
| 81 | |
| 82 | for (i = 0; i < nslices; i++) { |
| 83 | gpr_slice_unref(slices[i]); |
| 84 | } |
| 85 | gpr_free(slices); |
| 86 | |
| 87 | GPR_ASSERT(NULL == va_arg(chk.args, char *)); |
| 88 | |
| 89 | va_end(chk.args); |
| 90 | } |
| 91 | |
| 92 | static void test_vectors(grpc_slice_split_mode mode) { |
| 93 | grpc_chttp2_hpack_parser parser; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 94 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 95 | grpc_chttp2_hpack_parser_init(&parser); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 96 | /* D.2.1 */ |
| 97 | test_vector(&parser, mode, |
| 98 | "400a 6375 7374 6f6d 2d6b 6579 0d63 7573" |
| 99 | "746f 6d2d 6865 6164 6572", |
| 100 | "custom-key", "custom-header", NULL); |
| 101 | /* D.2.2 */ |
| 102 | test_vector(&parser, mode, "040c 2f73 616d 706c 652f 7061 7468", ":path", |
| 103 | "/sample/path", NULL); |
| 104 | /* D.2.3 */ |
| 105 | test_vector(&parser, mode, |
| 106 | "1008 7061 7373 776f 7264 0673 6563 7265" |
| 107 | "74", |
| 108 | "password", "secret", NULL); |
| 109 | /* D.2.4 */ |
| 110 | test_vector(&parser, mode, "82", ":method", "GET", NULL); |
| 111 | grpc_chttp2_hpack_parser_destroy(&parser); |
| 112 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 113 | grpc_chttp2_hpack_parser_init(&parser); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | /* D.3.1 */ |
| 115 | test_vector(&parser, mode, |
| 116 | "8286 8441 0f77 7777 2e65 7861 6d70 6c65" |
| 117 | "2e63 6f6d", |
| 118 | ":method", "GET", ":scheme", "http", ":path", "/", ":authority", |
| 119 | "www.example.com", NULL); |
| 120 | /* D.3.2 */ |
| 121 | test_vector(&parser, mode, "8286 84be 5808 6e6f 2d63 6163 6865", ":method", |
| 122 | "GET", ":scheme", "http", ":path", "/", ":authority", |
| 123 | "www.example.com", "cache-control", "no-cache", NULL); |
| 124 | /* D.3.3 */ |
| 125 | test_vector(&parser, mode, |
| 126 | "8287 85bf 400a 6375 7374 6f6d 2d6b 6579" |
| 127 | "0c63 7573 746f 6d2d 7661 6c75 65", |
| 128 | ":method", "GET", ":scheme", "https", ":path", "/index.html", |
| 129 | ":authority", "www.example.com", "custom-key", "custom-value", |
| 130 | NULL); |
| 131 | grpc_chttp2_hpack_parser_destroy(&parser); |
| 132 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 133 | grpc_chttp2_hpack_parser_init(&parser); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 134 | /* D.4.1 */ |
| 135 | test_vector(&parser, mode, |
| 136 | "8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4" |
| 137 | "ff", |
| 138 | ":method", "GET", ":scheme", "http", ":path", "/", ":authority", |
| 139 | "www.example.com", NULL); |
| 140 | /* D.4.2 */ |
| 141 | test_vector(&parser, mode, "8286 84be 5886 a8eb 1064 9cbf", ":method", "GET", |
| 142 | ":scheme", "http", ":path", "/", ":authority", "www.example.com", |
| 143 | "cache-control", "no-cache", NULL); |
| 144 | /* D.4.3 */ |
| 145 | test_vector(&parser, mode, |
| 146 | "8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925" |
| 147 | "a849 e95b b8e8 b4bf", |
| 148 | ":method", "GET", ":scheme", "https", ":path", "/index.html", |
| 149 | ":authority", "www.example.com", "custom-key", "custom-value", |
| 150 | NULL); |
| 151 | grpc_chttp2_hpack_parser_destroy(&parser); |
| 152 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 153 | grpc_chttp2_hpack_parser_init(&parser); |
Craig Tiller | 82e249b | 2015-11-11 01:14:39 +0000 | [diff] [blame] | 154 | grpc_chttp2_hptbl_set_max_bytes(&parser.table, 256); |
| 155 | grpc_chttp2_hptbl_set_current_table_size(&parser.table, 256); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | /* D.5.1 */ |
| 157 | test_vector(&parser, mode, |
| 158 | "4803 3330 3258 0770 7269 7661 7465 611d" |
| 159 | "4d6f 6e2c 2032 3120 4f63 7420 3230 3133" |
| 160 | "2032 303a 3133 3a32 3120 474d 546e 1768" |
| 161 | "7474 7073 3a2f 2f77 7777 2e65 7861 6d70" |
| 162 | "6c65 2e63 6f6d", |
| 163 | ":status", "302", "cache-control", "private", "date", |
| 164 | "Mon, 21 Oct 2013 20:13:21 GMT", "location", |
| 165 | "https://www.example.com", NULL); |
| 166 | /* D.5.2 */ |
| 167 | test_vector(&parser, mode, "4803 3330 37c1 c0bf", ":status", "307", |
| 168 | "cache-control", "private", "date", |
| 169 | "Mon, 21 Oct 2013 20:13:21 GMT", "location", |
| 170 | "https://www.example.com", NULL); |
| 171 | /* D.5.3 */ |
| 172 | test_vector(&parser, mode, |
| 173 | "88c1 611d 4d6f 6e2c 2032 3120 4f63 7420" |
| 174 | "3230 3133 2032 303a 3133 3a32 3220 474d" |
| 175 | "54c0 5a04 677a 6970 7738 666f 6f3d 4153" |
| 176 | "444a 4b48 514b 425a 584f 5157 454f 5049" |
| 177 | "5541 5851 5745 4f49 553b 206d 6178 2d61" |
| 178 | "6765 3d33 3630 303b 2076 6572 7369 6f6e" |
| 179 | "3d31", |
| 180 | ":status", "200", "cache-control", "private", "date", |
| 181 | "Mon, 21 Oct 2013 20:13:22 GMT", "location", |
| 182 | "https://www.example.com", "content-encoding", "gzip", |
| 183 | "set-cookie", |
| 184 | "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", NULL); |
| 185 | grpc_chttp2_hpack_parser_destroy(&parser); |
| 186 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 187 | grpc_chttp2_hpack_parser_init(&parser); |
Craig Tiller | 82e249b | 2015-11-11 01:14:39 +0000 | [diff] [blame] | 188 | grpc_chttp2_hptbl_set_max_bytes(&parser.table, 256); |
| 189 | grpc_chttp2_hptbl_set_current_table_size(&parser.table, 256); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 190 | /* D.6.1 */ |
| 191 | test_vector(&parser, mode, |
| 192 | "4882 6402 5885 aec3 771a 4b61 96d0 7abe" |
| 193 | "9410 54d4 44a8 2005 9504 0b81 66e0 82a6" |
| 194 | "2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8" |
| 195 | "e9ae 82ae 43d3", |
| 196 | ":status", "302", "cache-control", "private", "date", |
| 197 | "Mon, 21 Oct 2013 20:13:21 GMT", "location", |
| 198 | "https://www.example.com", NULL); |
| 199 | /* D.6.2 */ |
| 200 | test_vector(&parser, mode, "4883 640e ffc1 c0bf", ":status", "307", |
| 201 | "cache-control", "private", "date", |
| 202 | "Mon, 21 Oct 2013 20:13:21 GMT", "location", |
| 203 | "https://www.example.com", NULL); |
| 204 | /* D.6.3 */ |
| 205 | test_vector(&parser, mode, |
| 206 | "88c1 6196 d07a be94 1054 d444 a820 0595" |
| 207 | "040b 8166 e084 a62d 1bff c05a 839b d9ab" |
| 208 | "77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b" |
| 209 | "3960 d5af 2708 7f36 72c1 ab27 0fb5 291f" |
| 210 | "9587 3160 65c0 03ed 4ee5 b106 3d50 07", |
| 211 | ":status", "200", "cache-control", "private", "date", |
| 212 | "Mon, 21 Oct 2013 20:13:22 GMT", "location", |
| 213 | "https://www.example.com", "content-encoding", "gzip", |
| 214 | "set-cookie", |
| 215 | "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", NULL); |
| 216 | grpc_chttp2_hpack_parser_destroy(&parser); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | int main(int argc, char **argv) { |
| 220 | grpc_test_init(argc, argv); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 221 | grpc_init(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 222 | test_vectors(GRPC_SLICE_SPLIT_MERGE_ALL); |
| 223 | test_vectors(GRPC_SLICE_SPLIT_ONE_BYTE); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 224 | grpc_shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 225 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 226 | } |