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