xref: /aosp_15_r20/external/grpc-grpc/test/core/bad_client/tests/bad_streaming_id.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2019 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 <memory>
20 
21 #include <gtest/gtest.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25 
26 #include "src/core/lib/surface/server.h"
27 #include "test/core/bad_client/bad_client.h"
28 #include "test/core/util/test_config.h"
29 
30 #define HEADER_FRAME_ID_1                                                  \
31   "\x00\x00\xc9\x01\x05\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 HEADER_FRAME_ID_2                                                  \
47   "\x00\x00\xc9\x01\x05\x00\x00\x00\x02" /* headers: generated from        \
48                                             simple_request.headers in this \
49                                             directory */                   \
50   "\x10\x05:path\x08/foo/bar"                                              \
51   "\x10\x07:scheme\x04http"                                                \
52   "\x10\x07:method\x04POST"                                                \
53   "\x10\x0a:authority\x09localhost"                                        \
54   "\x10\x0c"                                                               \
55   "content-type\x10"                                                       \
56   "application/grpc"                                                       \
57   "\x10\x14grpc-accept-encoding\x15"                                       \
58   "deflate,identity,gzip"                                                  \
59   "\x10\x02te\x08trailers"                                                 \
60   "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
61 
62 #define HEADER_FRAME_ID_3                                                  \
63   "\x00\x00\xc9\x01\x05\x00\x00\x00\x03" /* headers: generated from        \
64                                             simple_request.headers in this \
65                                             directory */                   \
66   "\x10\x05:path\x08/foo/bar"                                              \
67   "\x10\x07:scheme\x04http"                                                \
68   "\x10\x07:method\x04POST"                                                \
69   "\x10\x0a:authority\x09localhost"                                        \
70   "\x10\x0c"                                                               \
71   "content-type\x10"                                                       \
72   "application/grpc"                                                       \
73   "\x10\x14grpc-accept-encoding\x15"                                       \
74   "deflate,identity,gzip"                                                  \
75   "\x10\x02te\x08trailers"                                                 \
76   "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
77 
78 namespace {
79 
verifier(grpc_server * server,grpc_completion_queue * cq,void *)80 void verifier(grpc_server* server, grpc_completion_queue* cq,
81               void* /*registered_method*/) {
82   while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
83     GPR_ASSERT(grpc_completion_queue_next(
84                    cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
85                    .type == GRPC_QUEUE_TIMEOUT);
86   }
87 }
88 
TEST(BadStreamingId,RegularHeader)89 TEST(BadStreamingId, RegularHeader) {
90   grpc_bad_client_arg args[2];
91   args[0] = connection_preface_arg;
92   args[1].client_validator = nullptr;
93   args[1].client_payload = HEADER_FRAME_ID_1;
94   args[1].client_payload_length = sizeof(HEADER_FRAME_ID_1) - 1;
95   grpc_run_bad_client_test(verifier, args, 2, GRPC_BAD_CLIENT_DISCONNECT);
96 }
97 
TEST(BadStreamingId,NonClientStreamId)98 TEST(BadStreamingId, NonClientStreamId) {
99   grpc_bad_client_arg args[2];
100   args[0] = connection_preface_arg;
101   // send a header frame with non-client stream id 2
102   args[1].client_validator = nullptr;
103   args[1].client_payload = HEADER_FRAME_ID_2;
104   args[1].client_payload_length = sizeof(HEADER_FRAME_ID_2) - 1;
105   grpc_run_bad_client_test(verifier, args, 2, GRPC_BAD_CLIENT_DISCONNECT);
106 }
107 
TEST(BadStreamingId,ClosedStreamId)108 TEST(BadStreamingId, ClosedStreamId) {
109   grpc_bad_client_arg args[4];
110   args[0] = connection_preface_arg;
111   // send a header frame with stream id 1
112   args[1].client_validator = nullptr;
113   args[1].client_payload = HEADER_FRAME_ID_1;
114   args[1].client_payload_length = sizeof(HEADER_FRAME_ID_1) - 1;
115   // send a header frame with stream id 3
116   args[2].client_validator = nullptr;
117   args[2].client_payload = HEADER_FRAME_ID_3;
118   args[2].client_payload_length = sizeof(HEADER_FRAME_ID_3) - 1;
119   // send a header frame with closed stream id 1 again
120   args[3].client_validator = nullptr;
121   args[3].client_payload = HEADER_FRAME_ID_1;
122   args[3].client_payload_length = sizeof(HEADER_FRAME_ID_1) - 1;
123   grpc_run_bad_client_test(verifier, args, 4, GRPC_BAD_CLIENT_DISCONNECT);
124 }
125 
126 }  // namespace
127 
main(int argc,char ** argv)128 int main(int argc, char** argv) {
129   grpc::testing::TestEnvironment env(&argc, argv);
130   ::testing::InitGoogleTest(&argc, argv);
131   grpc_init();
132   int retval = RUN_ALL_TESTS();
133   grpc_shutdown();
134   return retval;
135 }
136