| 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 <memory> |
| 20 |
|
| 21 |
#include <gtest/gtest.h> |
| 22 |
|
| 23 |
#include <grpc/grpc.h> |
| 24 |
#include <grpcpp/server.h> |
| 25 |
#include <grpcpp/server_builder.h> |
| 26 |
#include <grpcpp/support/config.h> |
| 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 |
nullptr, |
| 50 |
}; |
| 51 |
|
| 52 |
class MockSocketMutator : public grpc_socket_mutator { |
| 53 |
public: |
| 54 |
MockSocketMutator() : mutate_fd_call_count_(0) { |
| 55 |
grpc_socket_mutator_init(this, &mock_socket_mutator_vtable); |
| 56 |
} |
| 57 |
int mutate_fd_call_count_; |
| 58 |
}; |
| 59 |
|
| 60 |
bool mock_socket_mutator_mutate_fd(int /*fd*/, grpc_socket_mutator* m) { |
| 61 |
MockSocketMutator* s = reinterpret_cast<MockSocketMutator*>(m); |
| 62 |
s->mutate_fd_call_count_++; |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
int mock_socket_mutator_compare(grpc_socket_mutator* a, |
| 67 |
grpc_socket_mutator* b) { |
| 68 |
return reinterpret_cast<uintptr_t>(a) - reinterpret_cast<uintptr_t>(b); |
| 69 |
} |
| 70 |
|
| 71 |
void mock_socket_mutator_destroy(grpc_socket_mutator* m) { |
| 72 |
MockSocketMutator* s = reinterpret_cast<MockSocketMutator*>(m); |
| 73 |
delete s; |
| 74 |
} |
| 75 |
|
| 76 |
class MockSocketMutatorServerBuilderOption : public grpc::ServerBuilderOption { |
| 77 |
public: |
| 78 |
explicit MockSocketMutatorServerBuilderOption( |
| 79 |
MockSocketMutator* mock_socket_mutator) |
| 80 |
: mock_socket_mutator_(mock_socket_mutator) {} |
| 81 |
|
| 82 |
void UpdateArguments(ChannelArguments* args) override { |
| 83 |
args->SetSocketMutator(mock_socket_mutator_); |
| 84 |
} |
| 85 |
|
| 86 |
void UpdatePlugins( |
| 87 |
std::vector<std::unique_ptr<ServerBuilderPlugin>>*) override{}; |
| 88 |
|
| 89 |
MockSocketMutator* mock_socket_mutator_; |
| 90 |
}; |
| 91 |
|
| 92 |
class ServerBuilderWithSocketMutatorTest : public ::testing::Test { |
| 93 |
protected: |
| 94 |
static void SetUpTestCase() { grpc_init(); } |
| 95 |
|
| 96 |
static void TearDownTestCase() { grpc_shutdown(); } |
| 97 |
}; |
| 98 |
|
| 99 |
TEST_F(ServerBuilderWithSocketMutatorTest, CreateServerWithSocketMutator) { |
| 100 |
auto address = "localhost:" + std::to_string(grpc_pick_unused_port_or_die()); |
| 101 |
auto mock_socket_mutator = new MockSocketMutator(); |
| 102 |
std::unique_ptr<grpc::ServerBuilderOption> mock_socket_mutator_builder_option( |
| 103 |
new MockSocketMutatorServerBuilderOption(mock_socket_mutator)); |
| 104 |
testing::EchoTestService::Service echo_service; |
| 105 |
EXPECT_EQ(mock_socket_mutator->mutate_fd_call_count_, 0); |
| 106 |
ServerBuilder builder; |
| 107 |
builder.RegisterService(&echo_service); |
| 108 |
builder.AddListeningPort(address, InsecureServerCredentials()); |
| 109 |
builder.SetOption(std::move(mock_socket_mutator_builder_option)); |
| 110 |
std::unique_ptr<grpc::Server> server(builder.BuildAndStart()); |
| 111 |
EXPECT_NE(server, nullptr); |
| 112 |
// Only assert that the socket mutator was used. |
| 113 |
EXPECT_GE(mock_socket_mutator->mutate_fd_call_count_, 1); |
| 114 |
server->Shutdown(); |
| 115 |
} |
| 116 |
|
| 117 |
} // namespace |
| 118 |
} // namespace grpc |
| 119 |
|
| 120 |
int main(int argc, char** argv) { |
| 121 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 122 |
::testing::InitGoogleTest(&argc, argv); |
| 123 |
int ret = RUN_ALL_TESTS(); |
| 124 |
return ret; |
| 125 |
} |