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