xref: /aosp_15_r20/external/grpc-grpc/test/core/bad_client/tests/initial_settings_frame.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 "absl/strings/str_cat.h"
20 
21 #include <grpc/grpc.h>
22 #include <grpc/slice.h>
23 #include <grpc/support/log.h>
24 
25 #include "src/core/lib/experiments/experiments.h"
26 #include "src/core/lib/surface/server.h"
27 #include "test/core/bad_client/bad_client.h"
28 #include "test/core/end2end/cq_verifier.h"
29 #include "test/core/util/test_config.h"
30 
31 #define PFX_STR "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
32 #define ONE_SETTING_HDR "\x00\x00\x06\x04\x00\x00\x00\x00\x00"
33 #define ZERO_SETTING_HDR "\x00\x00\x00\x04\x00\x00\x00\x00\x00"
34 #define SETTING_ACK "\x00\x00\x00\x04\x01\x00\x00\x00\x00"
35 
36 #define RST_STREAM_1 "\x00\x00\x04\x03\x00\x00\x00\x00\x01\x00\x00\x00\x00"
37 #define RST_STREAM_3 "\x00\x00\x04\x03\x00\x00\x00\x00\x03\x00\x00\x00\x00"
38 
39 #define FOOBAR_0                                                           \
40   "\x00\x00\xca\x01\x04\x00\x00\x00\x01" /* headers: generated from        \
41                                             simple_request.headers in this \
42                                             directory */                   \
43   "\x10\x05:path\x09/foo/bar0"                                             \
44   "\x10\x07:scheme\x04http"                                                \
45   "\x10\x07:method\x04POST"                                                \
46   "\x10\x0a:authority\x09localhost"                                        \
47   "\x10\x0c"                                                               \
48   "content-type\x10"                                                       \
49   "application/grpc"                                                       \
50   "\x10\x14grpc-accept-encoding\x15"                                       \
51   "deflate,identity,gzip"                                                  \
52   "\x10\x02te\x08trailers"                                                 \
53   "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
54 
55 #define FOOBAR_1                                                           \
56   "\x00\x00\xca\x01\x04\x00\x00\x00\x05" /* headers: generated from        \
57                                             simple_request.headers in this \
58                                             directory */                   \
59   "\x10\x05:path\x09/foo/bar1"                                             \
60   "\x10\x07:scheme\x04http"                                                \
61   "\x10\x07:method\x04POST"                                                \
62   "\x10\x0a:authority\x09localhost"                                        \
63   "\x10\x0c"                                                               \
64   "content-type\x10"                                                       \
65   "application/grpc"                                                       \
66   "\x10\x14grpc-accept-encoding\x15"                                       \
67   "deflate,identity,gzip"                                                  \
68   "\x10\x02te\x08trailers"                                                 \
69   "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
70 
71 #define FOOBAR_2                                                           \
72   "\x00\x00\xca\x01\x04\x00\x00\x00\x03" /* headers: generated from        \
73                                             simple_request.headers in this \
74                                             directory */                   \
75   "\x10\x05:path\x09/foo/bar2"                                             \
76   "\x10\x07:scheme\x04http"                                                \
77   "\x10\x07:method\x04POST"                                                \
78   "\x10\x0a:authority\x09localhost"                                        \
79   "\x10\x0c"                                                               \
80   "content-type\x10"                                                       \
81   "application/grpc"                                                       \
82   "\x10\x14grpc-accept-encoding\x15"                                       \
83   "deflate,identity,gzip"                                                  \
84   "\x10\x02te\x08trailers"                                                 \
85   "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
86 
verifier(grpc_server * server,grpc_completion_queue * cq,void *)87 static void verifier(grpc_server* server, grpc_completion_queue* cq,
88                      void* /*registered_method*/) {
89   while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
90     GPR_ASSERT(grpc_completion_queue_next(
91                    cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
92                    .type == GRPC_QUEUE_TIMEOUT);
93   }
94 }
95 
single_request_verifier(grpc_server * server,grpc_completion_queue * cq,void *)96 static void single_request_verifier(grpc_server* server,
97                                     grpc_completion_queue* cq,
98                                     void* /*registered_method*/) {
99   grpc_call_error error;
100   grpc_call* s;
101   grpc_call_details call_details;
102   grpc_core::CqVerifier cqv(cq);
103   grpc_metadata_array request_metadata_recv;
104 
105   for (int i = 0; i < 2; i++) {
106     grpc_call_details_init(&call_details);
107     grpc_metadata_array_init(&request_metadata_recv);
108 
109     error = grpc_server_request_call(server, &s, &call_details,
110                                      &request_metadata_recv, cq, cq,
111                                      grpc_core::CqVerifier::tag(101));
112     GPR_ASSERT(GRPC_CALL_OK == error);
113     cqv.Expect(grpc_core::CqVerifier::tag(101), true);
114     cqv.Verify();
115 
116     GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.host, "localhost"));
117     GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method,
118                                        absl::StrCat("/foo/bar", i).c_str()));
119 
120     grpc_metadata_array_destroy(&request_metadata_recv);
121     grpc_call_details_destroy(&call_details);
122     grpc_call_unref(s);
123   }
124 }
125 
main(int argc,char ** argv)126 int main(int argc, char** argv) {
127   grpc::testing::TestEnvironment env(&argc, argv);
128   grpc_init();
129 
130   // various partial prefixes
131   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00",
132                            GRPC_BAD_CLIENT_DISCONNECT);
133   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00",
134                            GRPC_BAD_CLIENT_DISCONNECT);
135   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00",
136                            GRPC_BAD_CLIENT_DISCONNECT);
137   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x06",
138                            GRPC_BAD_CLIENT_DISCONNECT);
139   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x06",
140                            GRPC_BAD_CLIENT_DISCONNECT);
141   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x06",
142                            GRPC_BAD_CLIENT_DISCONNECT);
143   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x04",
144                            GRPC_BAD_CLIENT_DISCONNECT);
145   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x04\x00",
146                            GRPC_BAD_CLIENT_DISCONNECT);
147   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x04\x01",
148                            GRPC_BAD_CLIENT_DISCONNECT);
149   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x04\xff",
150                            GRPC_BAD_CLIENT_DISCONNECT);
151   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
152                            PFX_STR "\x00\x00\x00\x04\x00\x00",
153                            GRPC_BAD_CLIENT_DISCONNECT);
154   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
155                            PFX_STR "\x00\x00\x00\x04\x00\x00\x00",
156                            GRPC_BAD_CLIENT_DISCONNECT);
157   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
158                            PFX_STR "\x00\x00\x00\x04\x00\x00\x00\x00",
159                            GRPC_BAD_CLIENT_DISCONNECT);
160   // must not send frames with stream id != 0
161   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
162                            PFX_STR "\x00\x00\x00\x04\x00\x00\x00\x00\x01", 0);
163   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
164                            PFX_STR "\x00\x00\x00\x04\x00\x40\x00\x00\x00", 0);
165   // settings frame must be a multiple of six bytes long
166   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
167                            PFX_STR "\x00\x00\x01\x04\x00\x00\x00\x00\x00", 0);
168   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
169                            PFX_STR "\x00\x00\x02\x04\x00\x00\x00\x00\x00", 0);
170   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
171                            PFX_STR "\x00\x00\x03\x04\x00\x00\x00\x00\x00", 0);
172   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
173                            PFX_STR "\x00\x00\x04\x04\x00\x00\x00\x00\x00", 0);
174   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
175                            PFX_STR "\x00\x00\x05\x04\x00\x00\x00\x00\x00", 0);
176   // some settings values are illegal
177   // max frame size = 0
178   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
179                            PFX_STR ONE_SETTING_HDR "\x00\x05\x00\x00\x00\x00",
180                            GRPC_BAD_CLIENT_DISCONNECT);
181   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
182                            PFX_STR ONE_SETTING_HDR "\x00\x06\xff\xff\xff\xff",
183                            GRPC_BAD_CLIENT_DISCONNECT);
184   // update intiial window size
185   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
186                            PFX_STR ONE_SETTING_HDR "\x00\x04\x00\x01\x00\x00",
187                            GRPC_BAD_CLIENT_DISCONNECT);
188   // ack with data
189   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
190                            PFX_STR
191                            "\x00\x00\x00\x04\x00\x00\x00\x00\x00"
192                            "\x00\x00\x01\x04\x01\x00\x00\x00\x00",
193                            0);
194   // settings frame with invalid flags
195   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
196                            PFX_STR "\x00\x00\x00\x04\x10\x00\x00\x00\x00", 0);
197   // unknown settings should be ignored
198   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
199                            PFX_STR ONE_SETTING_HDR "\x00\x99\x00\x00\x00\x00",
200                            GRPC_BAD_CLIENT_DISCONNECT);
201 
202   // too many requests before the settings ack is sent should be cancelled
203   GRPC_RUN_BAD_CLIENT_TEST(single_request_verifier, nullptr,
204                            PFX_STR ZERO_SETTING_HDR FOOBAR_0 FOOBAR_2
205                                SETTING_ACK RST_STREAM_1 RST_STREAM_3 FOOBAR_1,
206                            GRPC_BAD_CLIENT_MAX_CONCURRENT_REQUESTS_OF_ONE);
207 
208   grpc_shutdown();
209   return 0;
210 }
211