Debian Codegraph @

test/core/bad_client/tests/simple_request.cc

raw
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 <stdint.h>
20
21 #include <grpc/grpc.h>
22 #include <grpc/slice.h>
23 #include <grpc/support/log.h>
24
25 #include "src/core/lib/surface/server.h"
26 #include "test/core/bad_client/bad_client.h"
27 #include "test/core/end2end/cq_verifier.h"
28 #include "test/core/util/test_config.h"
29
30 #define PFX_STR \
31 "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
32 "\x00\x00\x00\x04\x00\x00\x00\x00\x00" /* settings frame */ \
33 "\x00\x00\xc9\x01\x04\x00\x00\x00\x01" /* headers: generated from \
34 simple_request.headers in this \
35 directory */ \
36 "\x10\x05:path\x08/foo/bar" \
37 "\x10\x07:scheme\x04http" \
38 "\x10\x07:method\x04POST" \
39 "\x10\x0a:authority\x09localhost" \
40 "\x10\x0c" \
41 "content-type\x10" \
42 "application/grpc" \
43 "\x10\x14grpc-accept-encoding\x15" \
44 "deflate,identity,gzip" \
45 "\x10\x02te\x08trailers" \
46 "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
47
48 #define PFX_STR_UNUSUAL \
49 "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
50 "\x00\x00\x00\x04\x00\x00\x00\x00\x00" /* settings frame */ \
51 "\x00\x00\xf4\x01\x04\x00\x00\x00\x01" /* headers: generated from \
52 simple_request_unusual.headers \
53 in this directory */ \
54 "\x10\x05:path\x08/foo/bar" \
55 "\x10\x07:scheme\x04http" \
56 "\x10\x07:method\x04POST" \
57 "\x10\x04host\x09localhost" \
58 "\x10\x0c" \
59 "content-type\x1e" \
60 "application/grpc+this-is-valid" \
61 "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip" \
62 "\x10\x02te\x08trailers" \
63 "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" \
64 "\x10\x0cgrpc-timeout\x03" \
65 "10S" \
66 "\x10\x0cgrpc-timeout\x02" \
67 "5S"
68
69 #define PFX_STR_UNUSUAL2 \
70 "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
71 "\x00\x00\x00\x04\x00\x00\x00\x00\x00" /* settings frame */ \
72 "\x00\x00\xf4\x01\x04\x00\x00\x00\x01" /* headers: generated from \
73 simple_request_unusual2.headers \
74 in this directory */ \
75 "\x10\x05:path\x08/foo/bar" \
76 "\x10\x07:scheme\x04http" \
77 "\x10\x07:method\x04POST" \
78 "\x10\x04host\x09localhost" \
79 "\x10\x0c" \
80 "content-type\x1e" \
81 "application/grpc;this-is-valid" \
82 "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip" \
83 "\x10\x02te\x08trailers" \
84 "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" \
85 "\x10\x0cgrpc-timeout\x03" \
86 "10S" \
87 "\x10\x0cgrpc-timeout\x02" \
88 "5S"
89
90 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
91
92 static void verifier(grpc_server* server, grpc_completion_queue* cq,
93 void* /*registered_method*/) {
94 grpc_call_error error;
95 grpc_call* s;
96 grpc_call_details call_details;
97 grpc_core::CqVerifier cqv(cq);
98 grpc_metadata_array request_metadata_recv;
99
100 grpc_call_details_init(&call_details);
101 grpc_metadata_array_init(&request_metadata_recv);
102
103 error = grpc_server_request_call(server, &s, &call_details,
104 &request_metadata_recv, cq, cq, tag(101));
105 GPR_ASSERT(GRPC_CALL_OK == error);
106 cqv.Expect(tag(101), true);
107 cqv.Verify();
108
109 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.host, "localhost"));
110 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo/bar"));
111
112 grpc_metadata_array_destroy(&request_metadata_recv);
113 grpc_call_details_destroy(&call_details);
114 grpc_call_unref(s);
115 }
116
117 static void failure_verifier(grpc_server* server, grpc_completion_queue* cq,
118 void* /*registered_method*/) {
119 while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
120 GPR_ASSERT(grpc_completion_queue_next(
121 cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
122 .type == GRPC_QUEUE_TIMEOUT);
123 }
124 }
125
126 int main(int argc, char** argv) {
127 grpc::testing::TestEnvironment env(&argc, argv);
128 grpc_init();
129
130 /* basic request: check that things are working */
131 GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR, 0);
132 GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR_UNUSUAL, 0);
133 GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR_UNUSUAL2, 0);
134
135 /* push an illegal data frame */
136 GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
137 PFX_STR
138 "\x00\x00\x05\x00\x00\x00\x00\x00\x01"
139 "\x34\x00\x00\x00\x00",
140 0);
141
142 /* push a data frame with bad flags */
143 GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
144 PFX_STR "\x00\x00\x00\x00\x02\x00\x00\x00\x01", 0);
145 /* push a window update with a bad length */
146 GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
147 PFX_STR "\x00\x00\x01\x08\x00\x00\x00\x00\x01", 0);
148 /* push a window update with bad flags */
149 GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
150 PFX_STR "\x00\x00\x00\x08\x10\x00\x00\x00\x01", 0);
151 /* push a window update with bad data (0 is not legal window size increment)
152 */
153 GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
154 PFX_STR
155 "\x00\x00\x04\x08\x00\x00\x00\x00\x01"
156 "\x00\x00\x00\x00",
157 0);
158 /* push a short goaway */
159 GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
160 PFX_STR "\x00\x00\x04\x07\x00\x00\x00\x00\x00", 0);
161 /* disconnect before sending goaway */
162 GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
163 PFX_STR "\x00\x01\x12\x07\x00\x00\x00\x00\x00",
164 GRPC_BAD_CLIENT_DISCONNECT);
165 /* push a rst_stream with a bad length */
166 GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
167 PFX_STR "\x00\x00\x01\x03\x00\x00\x00\x00\x01", 0);
168 /* push a rst_stream with bad flags */
169 GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
170 PFX_STR "\x00\x00\x00\x03\x10\x00\x00\x00\x01", 0);
171
172 grpc_shutdown();
173 return 0;
174 }