fake_udp_and_tcp_server.h
Go to the documentation of this file.
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 
18 
19 #include <functional>
20 #include <set>
21 #include <string>
22 #include <thread>
23 
24 #include "absl/memory/memory.h"
25 
26 #include <grpc/slice.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
31 
32 namespace grpc_core {
33 namespace testing {
34 
35 // This class is used to simulate a variety of network conditions in
36 // unit tests.
37 //
38 // Note that resulting server only listens on the IPv6 loopback
39 // address, "[::1]". This is expected to be OK as all known gRPC unit test
40 // environments have this address available.
41 //
42 // As examples, this can be used to (but is not limited to) exercise
43 // the following cases:
44 //
45 // 1) DNS resolver's UDP requests experience packet loss:
46 //
47 // testing::FakeUdpAndTcpServer fake_dns_server(
48 // testing::FakeUdpAndTcpServer::AcceptMode::
49 // kWaitForClientToSendFirstBytes,
50 // testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer);
51 // auto server_uri = absl::StrFormat("dns:///[::]:%d/localhost:1234",
52 // fake_dns_server.port());
53 //
54 // 2) Server gets stuck while setting up a security handshake and client's
55 // security handshake times out (requires using secure channels):
56 //
57 // testing::FakeUdpAndTcpServer fake_server(
58 // testing::FakeUdpAndTcpServer::AcceptMode::
59 // kWaitForClientToSendFirstBytes,
60 // testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer);
61 // auto server_uri = absl::StrFormat("[::1]:%d", fake_server.port());
62 //
63 // 3) Client connections are immediately closed after sending the first bytes
64 // to an insecure server:
65 //
66 // testing::FakeUdpAndTcpServer fake_server(
67 // testing::FakeUdpAndTcpServer::AcceptMode::
68 // kEagerlySendSettings,
69 // testing::FakeUdpAndTcpServer::CloseSocketUponReceivingBytesFromPeer);
70 // auto server_uri = absl::StrFormat("[::1]:%d", fake_server.port());
71 //
73  public:
74  enum class ProcessReadResult {
75  kContinueReading = 0,
77  };
78 
79  enum class AcceptMode {
80  kWaitForClientToSendFirstBytes, // useful for emulating ALTS based
81  // grpc servers
82  kEagerlySendSettings, // useful for emulating insecure grpc servers (e.g.
83  // ALTS handshake servers)
84  };
85 
86  explicit FakeUdpAndTcpServer(
87  AcceptMode accept_mode,
88  std::function<ProcessReadResult(int, int, int)> process_read_cb);
89 
91 
92  const char* address() { return address_.c_str(); }
93 
94  int port() { return port_; };
95 
97  int bytes_received_size, int read_error, int s);
98 
99  static ProcessReadResult CloseSocketUponCloseFromPeer(int bytes_received_size,
100  int read_error, int s);
101 
102  void ReadFromUdpSocket();
103 
104  // Run a loop that periodically, every 10 ms:
105  // 1) Checks if there are any new TCP connections to accept.
106  // 2) Checks if any data has arrived yet on established connections,
107  // and reads from them if so, processing the sockets as configured.
108  void RunServerLoop();
109 
110  private:
112  public:
113  explicit FakeUdpAndTcpServerPeer(int fd);
114 
116 
118 
119  int fd() { return fd_; }
120 
121  private:
122  int fd_;
124  };
125 
128  int port_;
131  std::unique_ptr<std::thread> run_server_loop_thd_;
134 };
135 
136 } // namespace testing
137 } // namespace grpc_core
testing
Definition: aws_request_signer_test.cc:25
log.h
grpc_core::testing::FakeUdpAndTcpServer::FakeUdpAndTcpServerPeer::MaybeContinueSendingSettings
void MaybeContinueSendingSettings()
Definition: fake_udp_and_tcp_server.cc:206
grpc_core::testing::FakeUdpAndTcpServer::address_
std::string address_
Definition: fake_udp_and_tcp_server.h:130
grpc_core::testing::FakeUdpAndTcpServer::ReadFromUdpSocket
void ReadFromUdpSocket()
Definition: fake_udp_and_tcp_server.cc:232
grpc_core::testing::FakeUdpAndTcpServer::CloseSocketUponReceivingBytesFromPeer
static ProcessReadResult CloseSocketUponReceivingBytesFromPeer(int bytes_received_size, int read_error, int s)
Definition: fake_udp_and_tcp_server.cc:160
slice.h
grpc_core::testing::FakeUdpAndTcpServer::process_read_cb_
std::function< ProcessReadResult(int, int, int)> process_read_cb_
Definition: fake_udp_and_tcp_server.h:133
grpc_core
Definition: call_metric_recorder.h:31
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::testing::FakeUdpAndTcpServer::FakeUdpAndTcpServerPeer::fd
int fd()
Definition: fake_udp_and_tcp_server.h:119
time.h
grpc_core::testing::FakeUdpAndTcpServer::port
int port()
Definition: fake_udp_and_tcp_server.h:94
grpc_core::testing::FakeUdpAndTcpServer::AcceptMode::kWaitForClientToSendFirstBytes
@ kWaitForClientToSendFirstBytes
grpc_core::testing::FakeUdpAndTcpServer::FakeUdpAndTcpServerPeer::fd_
int fd_
Definition: fake_udp_and_tcp_server.h:122
string_util.h
grpc_core::testing::FakeUdpAndTcpServer::ProcessReadResult
ProcessReadResult
Definition: fake_udp_and_tcp_server.h:74
grpc_core::testing::FakeUdpAndTcpServer::accept_mode_
const AcceptMode accept_mode_
Definition: fake_udp_and_tcp_server.h:132
grpc_core::testing::FakeUdpAndTcpServer::address
const char * address()
Definition: fake_udp_and_tcp_server.h:92
grpc_core::testing::FakeUdpAndTcpServer::udp_socket_
int udp_socket_
Definition: fake_udp_and_tcp_server.h:127
grpc_core::testing::FakeUdpAndTcpServer::FakeUdpAndTcpServerPeer::~FakeUdpAndTcpServerPeer
~FakeUdpAndTcpServerPeer()
Definition: fake_udp_and_tcp_server.cc:201
grpc_core::testing::FakeUdpAndTcpServer
Definition: fake_udp_and_tcp_server.h:72
grpc_core::testing::FakeUdpAndTcpServer::accept_socket_
int accept_socket_
Definition: fake_udp_and_tcp_server.h:126
grpc_core::testing::FakeUdpAndTcpServer::run_server_loop_thd_
std::unique_ptr< std::thread > run_server_loop_thd_
Definition: fake_udp_and_tcp_server.h:131
grpc_core::testing::FakeUdpAndTcpServer::ProcessReadResult::kCloseSocket
@ kCloseSocket
grpc_core::testing::FakeUdpAndTcpServer::stop_ev_
gpr_event stop_ev_
Definition: fake_udp_and_tcp_server.h:129
grpc_core::testing::FakeUdpAndTcpServer::FakeUdpAndTcpServerPeer::FakeUdpAndTcpServerPeer
FakeUdpAndTcpServerPeer(int fd)
Definition: fake_udp_and_tcp_server.cc:198
grpc_core::testing::FakeUdpAndTcpServer::AcceptMode
AcceptMode
Definition: fake_udp_and_tcp_server.h:79
grpc_core::testing::FakeUdpAndTcpServer::FakeUdpAndTcpServerPeer::total_bytes_sent_
int total_bytes_sent_
Definition: fake_udp_and_tcp_server.h:123
grpc_core::testing::FakeUdpAndTcpServer::port_
int port_
Definition: fake_udp_and_tcp_server.h:128
gpr_event
Definition: impl/codegen/sync_generic.h:31
grpc_core::testing::FakeUdpAndTcpServer::AcceptMode::kEagerlySendSettings
@ kEagerlySendSettings
grpc_core::testing::FakeUdpAndTcpServer::ProcessReadResult::kContinueReading
@ kContinueReading
grpc_core::testing::FakeUdpAndTcpServer::FakeUdpAndTcpServer
FakeUdpAndTcpServer(AcceptMode accept_mode, std::function< ProcessReadResult(int, int, int)> process_read_cb)
Definition: fake_udp_and_tcp_server.cc:58
alloc.h
grpc_core::testing::FakeUdpAndTcpServer::~FakeUdpAndTcpServer
~FakeUdpAndTcpServer()
Definition: fake_udp_and_tcp_server.cc:146
grpc_core::testing::FakeUdpAndTcpServer::RunServerLoop
void RunServerLoop()
Definition: fake_udp_and_tcp_server.cc:237
grpc_core::testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer
static ProcessReadResult CloseSocketUponCloseFromPeer(int bytes_received_size, int read_error, int s)
Definition: fake_udp_and_tcp_server.cc:179
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_core::testing::FakeUdpAndTcpServer::FakeUdpAndTcpServerPeer
Definition: fake_udp_and_tcp_server.h:111
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:22