Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / test / core / bad_client / tests / unknown_frame.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>
20
21 #include <gtest/gtest.h>
22
23 #include <grpc/support/string_util.h>
24
25 #include "src/core/lib/surface/server.h"
26 #include "test/core/bad_client/bad_client.h"
27
28 static void verifier(grpc_server* server, grpc_completion_queue* cq,
29                      void* /*registered_method*/) {
30   while (server->core_server->HasOpenConnections()) {
31     GPR_ASSERT(grpc_completion_queue_next(
32                    cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
33                    .type == GRPC_QUEUE_TIMEOUT);
34   }
35 }
36
37 #define APPEND_BUFFER(string, to_append) \
38   ((string).append((to_append), sizeof(to_append) - 1))
39
40 namespace {
41 TEST(UnknownFrameType, Test) {
42   /* test that all invalid/unknown frame types are handled */
43   for (int i = 10; i <= 255; i++) {
44     std::string unknown_frame_string;
45     APPEND_BUFFER(unknown_frame_string, "\x00\x00\x00");
46     char frame_type = static_cast<char>(i);
47     unknown_frame_string.append(&frame_type, 1);
48     APPEND_BUFFER(unknown_frame_string, "\x00\x00\x00\x00\x01");
49     grpc_bad_client_arg args[2];
50     args[0] = connection_preface_arg;
51     args[1].client_validator = nullptr;
52     args[1].client_payload = unknown_frame_string.c_str();
53     args[1].client_payload_length = unknown_frame_string.size();
54     grpc_run_bad_client_test(verifier, args, 2, GRPC_BAD_CLIENT_DISCONNECT);
55   }
56 }
57 }  // namespace
58
59 int main(int argc, char** argv) {
60   grpc_init();
61   grpc::testing::TestEnvironment env(argc, argv);
62   ::testing::InitGoogleTest(&argc, argv);
63   int retval = RUN_ALL_TESTS();
64   grpc_shutdown();
65   return retval;
66 }