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