xref: /aosp_15_r20/external/grpc-grpc/test/core/bad_client/tests/unknown_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 <string>
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 
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 
39 #define APPEND_BUFFER(string, to_append) \
40   ((string).append((to_append), sizeof(to_append) - 1))
41 
42 namespace {
TEST(UnknownFrameType,Test)43 TEST(UnknownFrameType, Test) {
44   // test that all invalid/unknown frame types are handled
45   for (int i = 10; i <= 255; i++) {
46     std::string unknown_frame_string;
47     APPEND_BUFFER(unknown_frame_string, "\x00\x00\x00");
48     char frame_type = static_cast<char>(i);
49     unknown_frame_string.append(&frame_type, 1);
50     APPEND_BUFFER(unknown_frame_string, "\x00\x00\x00\x00\x01");
51     grpc_bad_client_arg args[2];
52     args[0] = connection_preface_arg;
53     args[1].client_validator = nullptr;
54     args[1].client_payload = unknown_frame_string.c_str();
55     args[1].client_payload_length = unknown_frame_string.size();
56     grpc_run_bad_client_test(verifier, args, 2, GRPC_BAD_CLIENT_DISCONNECT);
57   }
58 }
59 }  // namespace
60 
main(int argc,char ** argv)61 int main(int argc, char** argv) {
62   grpc::testing::TestEnvironment env(&argc, argv);
63   ::testing::InitGoogleTest(&argc, argv);
64   grpc_init();
65   int retval = RUN_ALL_TESTS();
66   grpc_shutdown();
67   return retval;
68 }
69