| 1 |
// |
| 2 |
// Copyright 2018 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include <functional> |
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
#include <thread> |
| 23 |
|
| 24 |
#include <grpc/support/sync.h> |
| 25 |
|
| 26 |
namespace grpc_core { |
| 27 |
namespace testing { |
| 28 |
|
| 29 |
// This class is used to simulate a variety of network conditions in |
| 30 |
// unit tests. |
| 31 |
// |
| 32 |
// Note that resulting server only listens on the IPv6 loopback |
| 33 |
// address, "[::1]". This is expected to be OK as all known gRPC unit test |
| 34 |
// environments have this address available. |
| 35 |
// |
| 36 |
// As examples, this can be used to (but is not limited to) exercise |
| 37 |
// the following cases: |
| 38 |
// |
| 39 |
// 1) DNS resolver's UDP requests experience packet loss: |
| 40 |
// |
| 41 |
// testing::FakeUdpAndTcpServer fake_dns_server( |
| 42 |
// testing::FakeUdpAndTcpServer::AcceptMode:: |
| 43 |
// kWaitForClientToSendFirstBytes, |
| 44 |
// testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer); |
| 45 |
// auto server_uri = absl::StrFormat("dns:///[::]:%d/localhost:1234", |
| 46 |
// fake_dns_server.port()); |
| 47 |
// |
| 48 |
// 2) Server gets stuck while setting up a security handshake and client's |
| 49 |
// security handshake times out (requires using secure channels): |
| 50 |
// |
| 51 |
// testing::FakeUdpAndTcpServer fake_server( |
| 52 |
// testing::FakeUdpAndTcpServer::AcceptMode:: |
| 53 |
// kWaitForClientToSendFirstBytes, |
| 54 |
// testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer); |
| 55 |
// auto server_uri = absl::StrFormat("[::1]:%d", fake_server.port()); |
| 56 |
// |
| 57 |
// 3) Client connections are immediately closed after sending the first bytes |
| 58 |
// to an insecure server: |
| 59 |
// |
| 60 |
// testing::FakeUdpAndTcpServer fake_server( |
| 61 |
// testing::FakeUdpAndTcpServer::AcceptMode:: |
| 62 |
// kEagerlySendSettings, |
| 63 |
// testing::FakeUdpAndTcpServer::CloseSocketUponReceivingBytesFromPeer); |
| 64 |
// auto server_uri = absl::StrFormat("[::1]:%d", fake_server.port()); |
| 65 |
// |
| 66 |
class FakeUdpAndTcpServer { |
| 67 |
public: |
| 68 |
enum class ProcessReadResult { |
| 69 |
kContinueReading = 0, |
| 70 |
kCloseSocket, |
| 71 |
}; |
| 72 |
|
| 73 |
enum class AcceptMode { |
| 74 |
kWaitForClientToSendFirstBytes, // useful for emulating ALTS based |
| 75 |
// grpc servers |
| 76 |
kEagerlySendSettings, // useful for emulating insecure grpc servers (e.g. |
| 77 |
// ALTS handshake servers) |
| 78 |
}; |
| 79 |
|
| 80 |
explicit FakeUdpAndTcpServer( |
| 81 |
AcceptMode accept_mode, |
| 82 |
std::function<ProcessReadResult(int, int, int)> process_read_cb); |
| 83 |
|
| 84 |
~FakeUdpAndTcpServer(); |
| 85 |
|
| 86 |
const char* address() { return address_.c_str(); } |
| 87 |
|
| 88 |
int port() { return port_; }; |
| 89 |
|
| 90 |
static ProcessReadResult CloseSocketUponReceivingBytesFromPeer( |
| 91 |
int bytes_received_size, int read_error, int s); |
| 92 |
|
| 93 |
static ProcessReadResult CloseSocketUponCloseFromPeer(int bytes_received_size, |
| 94 |
int read_error, int s); |
| 95 |
|
| 96 |
void ReadFromUdpSocket(); |
| 97 |
|
| 98 |
// Run a loop that periodically, every 10 ms: |
| 99 |
// 1) Checks if there are any new TCP connections to accept. |
| 100 |
// 2) Checks if any data has arrived yet on established connections, |
| 101 |
// and reads from them if so, processing the sockets as configured. |
| 102 |
void RunServerLoop(); |
| 103 |
|
| 104 |
private: |
| 105 |
class FakeUdpAndTcpServerPeer { |
| 106 |
public: |
| 107 |
explicit FakeUdpAndTcpServerPeer(int fd); |
| 108 |
|
| 109 |
~FakeUdpAndTcpServerPeer(); |
| 110 |
|
| 111 |
void MaybeContinueSendingSettings(); |
| 112 |
|
| 113 |
int fd() { return fd_; } |
| 114 |
|
| 115 |
private: |
| 116 |
int fd_; |
| 117 |
int total_bytes_sent_ = 0; |
| 118 |
}; |
| 119 |
|
| 120 |
int accept_socket_; |
| 121 |
int udp_socket_; |
| 122 |
int port_; |
| 123 |
gpr_event stop_ev_; |
| 124 |
std::string address_; |
| 125 |
std::unique_ptr<std::thread> run_server_loop_thd_; |
| 126 |
const AcceptMode accept_mode_; |
| 127 |
std::function<ProcessReadResult(int, int, int)> process_read_cb_; |
| 128 |
}; |
| 129 |
|
| 130 |
} // namespace testing |
| 131 |
} // namespace grpc_core |