xref: /aosp_15_r20/external/grpc-grpc/test/core/bad_client/tests/badreq.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <grpc/grpc.h>
20 #include <grpc/support/log.h>
21 
22 #include "src/core/lib/surface/server.h"
23 #include "test/core/bad_client/bad_client.h"
24 #include "test/core/util/test_config.h"
25 
26 #define PFX_STR                      \
27   "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
28   "\x00\x00\x00\x04\x00\x00\x00\x00\x00"  // settings frame
29 
verifier(grpc_server * server,grpc_completion_queue * cq,void *)30 static void verifier(grpc_server* server, grpc_completion_queue* cq,
31                      void* /*registered_method*/) {
32   while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
33     GPR_ASSERT(grpc_completion_queue_next(
34                    cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
35                    .type == GRPC_QUEUE_TIMEOUT);
36   }
37 }
38 
main(int argc,char ** argv)39 int main(int argc, char** argv) {
40   grpc::testing::TestEnvironment env(&argc, argv);
41   grpc_init();
42 
43   // invalid content type
44   GRPC_RUN_BAD_CLIENT_TEST(
45       verifier, nullptr,
46       PFX_STR
47       "\x00\x00\xc2\x01\x04\x00\x00\x00\x01"
48       "\x10\x05:path\x08/foo/bar"
49       "\x10\x07:scheme\x04http"
50       "\x10\x07:method\x04POST"
51       "\x10\x0a:authority\x09localhost"
52       "\x10\x0c"
53       "content-type\x09text/html"
54       "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip"
55       "\x10\x02te\x08trailers"
56       "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)",
57       GRPC_BAD_CLIENT_DISCONNECT);
58 
59   // invalid te
60   GRPC_RUN_BAD_CLIENT_TEST(
61       verifier, nullptr,
62       PFX_STR
63       "\x00\x00\xcb\x01\x04\x00\x00\x00\x01"
64       "\x10\x05:path\x08/foo/bar"
65       "\x10\x07:scheme\x04http"
66       "\x10\x07:method\x04POST"
67       "\x10\x0a:authority\x09localhost"
68       "\x10\x0c"
69       "content-type\x10"
70       "application/grpc"
71       "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip"
72       "\x10\x02te\x0a"
73       "frobnicate"
74       "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)",
75       GRPC_BAD_CLIENT_DISCONNECT);
76 
77   // two path headers
78   GRPC_RUN_BAD_CLIENT_TEST(
79       verifier, nullptr,
80       PFX_STR
81       "\x00\x00\xd9\x01\x04\x00\x00\x00\x01"
82       "\x10\x05:path\x08/foo/bar"
83       "\x10\x05:path\x08/foo/bah"
84       "\x10\x07:scheme\x04http"
85       "\x10\x07:method\x04POST"
86       "\x10\x0a:authority\x09localhost"
87       "\x10\x0c"
88       "content-type\x10"
89       "application/grpc"
90       "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip"
91       "\x10\x02te\x08trailers"
92       "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)",
93       GRPC_BAD_CLIENT_DISCONNECT);
94 
95   // bad accept-encoding algorithm
96   GRPC_RUN_BAD_CLIENT_TEST(
97       verifier, nullptr,
98       PFX_STR
99       "\x00\x00\xd2\x01\x04\x00\x00\x00\x01"
100       "\x10\x05:path\x08/foo/bar"
101       "\x10\x07:scheme\x04http"
102       "\x10\x07:method\x04POST"
103       "\x10\x0a:authority\x09localhost"
104       "\x10\x0c"
105       "content-type\x10"
106       "application/grpc"
107       "\x10\x14grpc-accept-encoding\x1enobody-knows-the-trouble-i-see"
108       "\x10\x02te\x08trailers"
109       "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)",
110       GRPC_BAD_CLIENT_DISCONNECT);
111 
112   // bad grpc-encoding algorithm
113   GRPC_RUN_BAD_CLIENT_TEST(
114       verifier, nullptr,
115       PFX_STR
116       "\x00\x00\xf5\x01\x04\x00\x00\x00\x01"
117       "\x10\x05:path\x08/foo/bar"
118       "\x10\x07:scheme\x04http"
119       "\x10\x07:method\x04POST"
120       "\x10\x0a:authority\x09localhost"
121       "\x10\x0c"
122       "content-type\x10"
123       "application/grpc"
124       "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip"
125       "\x10\x0dgrpc-encoding\x1cyou-dont-know-how-to-do-this"
126       "\x10\x02te\x08trailers"
127       "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)",
128       GRPC_BAD_CLIENT_DISCONNECT);
129 
130   grpc_shutdown();
131   return 0;
132 }
133