• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 /* This test does a sanity check that grpc_socket_mutator's
33  * are used by servers. It's meant to protect code and end-to-end
34  * tests that rely on this functionality but which live outside
35  * of the grpc github repo. */
36 
37 namespace grpc {
38 namespace {
39 
40 bool mock_socket_mutator_mutate_fd(int, grpc_socket_mutator*);
41 int mock_socket_mutator_compare(grpc_socket_mutator*, grpc_socket_mutator*);
42 void mock_socket_mutator_destroy(grpc_socket_mutator*);
43 
44 const grpc_socket_mutator_vtable mock_socket_mutator_vtable = {
45     mock_socket_mutator_mutate_fd,
46     mock_socket_mutator_compare,
47     mock_socket_mutator_destroy,
48 };
49 
50 class MockSocketMutator : public grpc_socket_mutator {
51  public:
MockSocketMutator()52   MockSocketMutator() : mutate_fd_call_count_(0) {
53     grpc_socket_mutator_init(this, &mock_socket_mutator_vtable);
54   }
55   int mutate_fd_call_count_;
56 };
57 
mock_socket_mutator_mutate_fd(int fd,grpc_socket_mutator * m)58 bool mock_socket_mutator_mutate_fd(int fd, grpc_socket_mutator* m) {
59   MockSocketMutator* s = reinterpret_cast<MockSocketMutator*>(m);
60   s->mutate_fd_call_count_++;
61   return true;
62 }
63 
mock_socket_mutator_compare(grpc_socket_mutator * a,grpc_socket_mutator * b)64 int mock_socket_mutator_compare(grpc_socket_mutator* a,
65                                 grpc_socket_mutator* b) {
66   return (uintptr_t)a - (uintptr_t)b;
67 }
68 
mock_socket_mutator_destroy(grpc_socket_mutator * m)69 void mock_socket_mutator_destroy(grpc_socket_mutator* m) {
70   MockSocketMutator* s = reinterpret_cast<MockSocketMutator*>(m);
71   delete s;
72 }
73 
74 class MockSocketMutatorServerBuilderOption : public grpc::ServerBuilderOption {
75  public:
MockSocketMutatorServerBuilderOption(MockSocketMutator * mock_socket_mutator)76   MockSocketMutatorServerBuilderOption(MockSocketMutator* mock_socket_mutator)
77       : mock_socket_mutator_(mock_socket_mutator) {}
78 
UpdateArguments(ChannelArguments * args)79   void UpdateArguments(ChannelArguments* args) override {
80     args->SetSocketMutator(mock_socket_mutator_);
81   }
82 
UpdatePlugins(std::vector<std::unique_ptr<ServerBuilderPlugin>> *)83   void UpdatePlugins(
84       std::vector<std::unique_ptr<ServerBuilderPlugin>>*) override{};
85 
86   MockSocketMutator* mock_socket_mutator_;
87 };
88 
TEST(ServerBuilderWithSocketMutatorTest,CreateServerWithSocketMutator)89 TEST(ServerBuilderWithSocketMutatorTest, CreateServerWithSocketMutator) {
90   auto address = "localhost:" + std::to_string(grpc_pick_unused_port_or_die());
91   auto mock_socket_mutator = new MockSocketMutator();
92   std::unique_ptr<grpc::ServerBuilderOption> mock_socket_mutator_builder_option(
93       new MockSocketMutatorServerBuilderOption(mock_socket_mutator));
94   testing::EchoTestService::Service echo_service;
95   EXPECT_EQ(mock_socket_mutator->mutate_fd_call_count_, 0);
96   ServerBuilder builder;
97   builder.RegisterService(&echo_service);
98   builder.AddListeningPort(address, InsecureServerCredentials());
99   builder.SetOption(std::move(mock_socket_mutator_builder_option));
100   std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
101   EXPECT_NE(server, nullptr);
102   // Only assert that the socket mutator was used.
103   EXPECT_GE(mock_socket_mutator->mutate_fd_call_count_, 1);
104   server->Shutdown();
105 }
106 
107 }  // namespace
108 }  // namespace grpc
109 
main(int argc,char ** argv)110 int main(int argc, char** argv) {
111   ::testing::InitGoogleTest(&argc, argv);
112   grpc_init();
113   int ret = RUN_ALL_TESTS();
114   grpc_shutdown();
115   return ret;
116 }
117