1 /*
2 *
3 * Copyright 2017 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 <grpcpp/impl/codegen/config.h>
20 #include <gtest/gtest.h>
21
22 #include <grpcpp/server.h>
23 #include <grpcpp/server_builder.h>
24
25 #include <grpc/grpc.h>
26 #include <memory>
27
28 #include "src/core/lib/iomgr/socket_mutator.h"
29 #include "src/proto/grpc/testing/echo.grpc.pb.h"
30 #include "test/core/util/port.h"
31 #include "test/core/util/test_config.h"
32
33 /* This test does a sanity check that grpc_socket_mutator's
34 * are used by servers. It's meant to protect code and end-to-end
35 * tests that rely on this functionality but which live outside
36 * of the grpc github repo. */
37
38 namespace grpc {
39 namespace {
40
41 bool mock_socket_mutator_mutate_fd(int, grpc_socket_mutator*);
42 int mock_socket_mutator_compare(grpc_socket_mutator*, grpc_socket_mutator*);
43 void mock_socket_mutator_destroy(grpc_socket_mutator*);
44
45 const grpc_socket_mutator_vtable mock_socket_mutator_vtable = {
46 mock_socket_mutator_mutate_fd,
47 mock_socket_mutator_compare,
48 mock_socket_mutator_destroy,
49 };
50
51 class MockSocketMutator : public grpc_socket_mutator {
52 public:
MockSocketMutator()53 MockSocketMutator() : mutate_fd_call_count_(0) {
54 grpc_socket_mutator_init(this, &mock_socket_mutator_vtable);
55 }
56 int mutate_fd_call_count_;
57 };
58
mock_socket_mutator_mutate_fd(int,grpc_socket_mutator * m)59 bool mock_socket_mutator_mutate_fd(int /*fd*/, grpc_socket_mutator* m) {
60 MockSocketMutator* s = reinterpret_cast<MockSocketMutator*>(m);
61 s->mutate_fd_call_count_++;
62 return true;
63 }
64
mock_socket_mutator_compare(grpc_socket_mutator * a,grpc_socket_mutator * b)65 int mock_socket_mutator_compare(grpc_socket_mutator* a,
66 grpc_socket_mutator* b) {
67 return (uintptr_t)a - (uintptr_t)b;
68 }
69
mock_socket_mutator_destroy(grpc_socket_mutator * m)70 void mock_socket_mutator_destroy(grpc_socket_mutator* m) {
71 MockSocketMutator* s = reinterpret_cast<MockSocketMutator*>(m);
72 delete s;
73 }
74
75 class MockSocketMutatorServerBuilderOption : public grpc::ServerBuilderOption {
76 public:
MockSocketMutatorServerBuilderOption(MockSocketMutator * mock_socket_mutator)77 MockSocketMutatorServerBuilderOption(MockSocketMutator* mock_socket_mutator)
78 : mock_socket_mutator_(mock_socket_mutator) {}
79
UpdateArguments(ChannelArguments * args)80 void UpdateArguments(ChannelArguments* args) override {
81 args->SetSocketMutator(mock_socket_mutator_);
82 }
83
UpdatePlugins(std::vector<std::unique_ptr<ServerBuilderPlugin>> *)84 void UpdatePlugins(
85 std::vector<std::unique_ptr<ServerBuilderPlugin>>*) override{};
86
87 MockSocketMutator* mock_socket_mutator_;
88 };
89
90 class ServerBuilderWithSocketMutatorTest : public ::testing::Test {
91 protected:
SetUpTestCase()92 static void SetUpTestCase() { grpc_init(); }
93
TearDownTestCase()94 static void TearDownTestCase() { grpc_shutdown(); }
95 };
96
TEST_F(ServerBuilderWithSocketMutatorTest,CreateServerWithSocketMutator)97 TEST_F(ServerBuilderWithSocketMutatorTest, CreateServerWithSocketMutator) {
98 auto address = "localhost:" + std::to_string(grpc_pick_unused_port_or_die());
99 auto mock_socket_mutator = new MockSocketMutator();
100 std::unique_ptr<grpc::ServerBuilderOption> mock_socket_mutator_builder_option(
101 new MockSocketMutatorServerBuilderOption(mock_socket_mutator));
102 testing::EchoTestService::Service echo_service;
103 EXPECT_EQ(mock_socket_mutator->mutate_fd_call_count_, 0);
104 ServerBuilder builder;
105 builder.RegisterService(&echo_service);
106 builder.AddListeningPort(address, InsecureServerCredentials());
107 builder.SetOption(std::move(mock_socket_mutator_builder_option));
108 std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
109 EXPECT_NE(server, nullptr);
110 // Only assert that the socket mutator was used.
111 EXPECT_GE(mock_socket_mutator->mutate_fd_call_count_, 1);
112 server->Shutdown();
113 }
114
115 } // namespace
116 } // namespace grpc
117
main(int argc,char ** argv)118 int main(int argc, char** argv) {
119 grpc::testing::TestEnvironment env(argc, argv);
120 ::testing::InitGoogleTest(&argc, argv);
121 int ret = RUN_ALL_TESTS();
122 return ret;
123 }
124