Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: third_party/grpc/test/core/httpcli/parser_test.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015, Google Inc.
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
34 #include "src/core/httpcli/parser.h"
35
36 #include <stdarg.h>
37 #include <string.h>
38
39 #include <grpc/support/alloc.h>
40 #include <grpc/support/log.h>
41 #include <grpc/support/string_util.h>
42 #include <grpc/support/useful.h>
43 #include "test/core/util/slice_splitter.h"
44 #include "test/core/util/test_config.h"
45
46 static void test_succeeds(grpc_slice_split_mode split_mode, char *response,
47 int expect_status, char *expect_body, ...) {
48 grpc_httpcli_parser parser;
49 gpr_slice input_slice = gpr_slice_from_copied_string(response);
50 size_t num_slices;
51 size_t i;
52 gpr_slice *slices;
53 va_list args;
54
55 grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
56 gpr_slice_unref(input_slice);
57
58 grpc_httpcli_parser_init(&parser);
59
60 for (i = 0; i < num_slices; i++) {
61 GPR_ASSERT(grpc_httpcli_parser_parse(&parser, slices[i]));
62 gpr_slice_unref(slices[i]);
63 }
64 GPR_ASSERT(grpc_httpcli_parser_eof(&parser));
65
66 GPR_ASSERT(expect_status == parser.r.status);
67 if (expect_body != NULL) {
68 GPR_ASSERT(strlen(expect_body) == parser.r.body_length);
69 GPR_ASSERT(0 == memcmp(expect_body, parser.r.body, parser.r.body_length));
70 } else {
71 GPR_ASSERT(parser.r.body_length == 0);
72 }
73
74 va_start(args, expect_body);
75 i = 0;
76 for (;;) {
77 char *expect_key;
78 char *expect_value;
79 expect_key = va_arg(args, char *);
80 if (!expect_key) break;
81 GPR_ASSERT(i < parser.r.hdr_count);
82 expect_value = va_arg(args, char *);
83 GPR_ASSERT(expect_value);
84 GPR_ASSERT(0 == strcmp(expect_key, parser.r.hdrs[i].key));
85 GPR_ASSERT(0 == strcmp(expect_value, parser.r.hdrs[i].value));
86 i++;
87 }
88 va_end(args);
89 GPR_ASSERT(i == parser.r.hdr_count);
90
91 grpc_httpcli_parser_destroy(&parser);
92 gpr_free(slices);
93 }
94
95 static void test_fails(grpc_slice_split_mode split_mode, char *response) {
96 grpc_httpcli_parser parser;
97 gpr_slice input_slice = gpr_slice_from_copied_string(response);
98 size_t num_slices;
99 size_t i;
100 gpr_slice *slices;
101 int done = 0;
102
103 grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
104 gpr_slice_unref(input_slice);
105
106 grpc_httpcli_parser_init(&parser);
107
108 for (i = 0; i < num_slices; i++) {
109 if (!done && !grpc_httpcli_parser_parse(&parser, slices[i])) {
110 done = 1;
111 }
112 gpr_slice_unref(slices[i]);
113 }
114 if (!done && !grpc_httpcli_parser_eof(&parser)) {
115 done = 1;
116 }
117 GPR_ASSERT(done);
118
119 grpc_httpcli_parser_destroy(&parser);
120 gpr_free(slices);
121 }
122
123 int main(int argc, char **argv) {
124 size_t i;
125 const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY,
126 GRPC_SLICE_SPLIT_ONE_BYTE};
127 char *tmp1, *tmp2;
128
129 grpc_test_init(argc, argv);
130
131 for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) {
132 test_succeeds(split_modes[i],
133 "HTTP/1.0 200 OK\r\n"
134 "xyz: abc\r\n"
135 "\r\n"
136 "hello world!",
137 200, "hello world!", "xyz", "abc", NULL);
138 test_succeeds(split_modes[i],
139 "HTTP/1.0 404 Not Found\r\n"
140 "\r\n",
141 404, NULL, NULL);
142 test_succeeds(split_modes[i],
143 "HTTP/1.1 200 OK\r\n"
144 "xyz: abc\r\n"
145 "\r\n"
146 "hello world!",
147 200, "hello world!", "xyz", "abc", NULL);
148 test_fails(split_modes[i], "HTTP/1.0\r\n");
149 test_fails(split_modes[i], "HTTP/1.2\r\n");
150 test_fails(split_modes[i], "HTTP/1.0 000 XYX\r\n");
151 test_fails(split_modes[i], "HTTP/1.0 200 OK\n");
152 test_fails(split_modes[i], "HTTP/1.0 200 OK\r\n");
153 test_fails(split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n");
154 test_fails(split_modes[i],
155 "HTTP/1.0 200 OK\r\n"
156 "xyz: abc\r\n"
157 " def\r\n"
158 "\r\n"
159 "hello world!");
160
161 tmp1 = gpr_malloc(2 * GRPC_HTTPCLI_MAX_HEADER_LENGTH);
162 memset(tmp1, 'a', 2 * GRPC_HTTPCLI_MAX_HEADER_LENGTH - 1);
163 tmp1[2 * GRPC_HTTPCLI_MAX_HEADER_LENGTH - 1] = 0;
164 gpr_asprintf(&tmp2, "HTTP/1.0 200 OK\r\nxyz: %s\r\n\r\n", tmp1);
165 test_fails(split_modes[i], tmp2);
166 gpr_free(tmp1);
167 gpr_free(tmp2);
168 }
169
170 return 0;
171 }
OLDNEW
« no previous file with comments | « third_party/grpc/test/core/httpcli/httpscli_test.c ('k') | third_party/grpc/test/core/httpcli/test_server.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698