QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "quic/tools/quic_client.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6 | |
| 7 | #include <dirent.h> |
| 8 | #include <sys/types.h> |
| 9 | |
| 10 | #include <memory> |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 11 | #include <utility> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | |
vasilvv | 89fe24d | 2020-10-26 14:55:28 -0700 | [diff] [blame] | 13 | #include "absl/strings/match.h" |
vasilvv | 6c9e9c3 | 2020-10-08 08:16:57 -0700 | [diff] [blame] | 14 | #include "absl/strings/string_view.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 15 | #include "quic/platform/api/quic_epoll.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 16 | #include "quic/platform/api/quic_test.h" |
| 17 | #include "quic/platform/api/quic_test_loopback.h" |
| 18 | #include "quic/test_tools/crypto_test_utils.h" |
| 19 | #include "quic/test_tools/quic_client_peer.h" |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 20 | #include "common/quiche_text_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | |
| 22 | namespace quic { |
| 23 | namespace test { |
| 24 | namespace { |
| 25 | |
| 26 | const char* kPathToFds = "/proc/self/fd"; |
| 27 | |
wub | b5f7586 | 2022-03-02 08:35:58 -0800 | [diff] [blame] | 28 | // Return the value of a symbolic link in |path|, if |path| is not found, return |
| 29 | // an empty string. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 30 | std::string ReadLink(const std::string& path) { |
| 31 | std::string result(PATH_MAX, '\0'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 32 | ssize_t result_size = readlink(path.c_str(), &result[0], result.size()); |
wub | b5f7586 | 2022-03-02 08:35:58 -0800 | [diff] [blame] | 33 | if (result_size < 0 && errno == ENOENT) { |
| 34 | return ""; |
| 35 | } |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 36 | QUICHE_CHECK(result_size > 0 && |
wub | b5f7586 | 2022-03-02 08:35:58 -0800 | [diff] [blame] | 37 | static_cast<size_t>(result_size) < result.size()) |
| 38 | << "result_size:" << result_size << ", errno:" << errno |
| 39 | << ", path:" << path; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | result.resize(result_size); |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | // Counts the number of open sockets for the current process. |
| 45 | size_t NumOpenSocketFDs() { |
| 46 | size_t socket_count = 0; |
| 47 | dirent* file; |
| 48 | std::unique_ptr<DIR, int (*)(DIR*)> fd_directory(opendir(kPathToFds), |
| 49 | closedir); |
| 50 | while ((file = readdir(fd_directory.get())) != nullptr) { |
vasilvv | 6c9e9c3 | 2020-10-08 08:16:57 -0700 | [diff] [blame] | 51 | absl::string_view name(file->d_name); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 52 | if (name == "." || name == "..") { |
| 53 | continue; |
| 54 | } |
| 55 | |
vasilvv | 1ea0b54 | 2020-12-03 15:21:00 -0800 | [diff] [blame] | 56 | std::string fd_path = ReadLink(absl::StrCat(kPathToFds, "/", name)); |
vasilvv | 89fe24d | 2020-10-26 14:55:28 -0700 | [diff] [blame] | 57 | if (absl::StartsWith(fd_path, "socket:")) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 58 | socket_count++; |
| 59 | } |
| 60 | } |
| 61 | return socket_count; |
| 62 | } |
| 63 | |
zhongyi | ef8d1e9 | 2019-08-26 12:47:21 -0700 | [diff] [blame] | 64 | class QuicClientTest : public QuicTest { |
| 65 | public: |
| 66 | QuicClientTest() { |
| 67 | // Creates and destroys a single client first which may open persistent |
| 68 | // sockets when initializing platform dependencies like certificate |
| 69 | // verifier. Future creation of addtional clients will deterministically |
| 70 | // open one socket per client. |
| 71 | CreateAndInitializeQuicClient(); |
| 72 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 73 | |
zhongyi | ef8d1e9 | 2019-08-26 12:47:21 -0700 | [diff] [blame] | 74 | // Creates a new QuicClient and Initializes it on an unused port. |
| 75 | // Caller is responsible for deletion. |
| 76 | std::unique_ptr<QuicClient> CreateAndInitializeQuicClient() { |
rch | 2ba3b68 | 2022-03-21 06:55:34 -0700 | [diff] [blame] | 77 | QuicSocketAddress server_address(QuicSocketAddress(TestLoopback(), 0)); |
zhongyi | ef8d1e9 | 2019-08-26 12:47:21 -0700 | [diff] [blame] | 78 | QuicServerId server_id("hostname", server_address.port(), false); |
| 79 | ParsedQuicVersionVector versions = AllSupportedVersions(); |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 80 | auto client = std::make_unique<QuicClient>( |
zhongyi | ef8d1e9 | 2019-08-26 12:47:21 -0700 | [diff] [blame] | 81 | server_address, server_id, versions, &epoll_server_, |
| 82 | crypto_test_utils::ProofVerifierForTesting()); |
| 83 | EXPECT_TRUE(client->Initialize()); |
| 84 | return client; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | QuicEpollServer epoll_server_; |
| 89 | }; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | |
| 91 | TEST_F(QuicClientTest, DoNotLeakSocketFDs) { |
| 92 | // Make sure that the QuicClient doesn't leak socket FDs. Doing so could cause |
| 93 | // port exhaustion in long running processes which repeatedly create clients. |
| 94 | |
zhongyi | ef8d1e9 | 2019-08-26 12:47:21 -0700 | [diff] [blame] | 95 | // Record the initial number of FDs. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 96 | size_t number_of_open_fds = NumOpenSocketFDs(); |
| 97 | |
| 98 | // Create a number of clients, initialize them, and verify this has resulted |
| 99 | // in additional FDs being opened. |
| 100 | const int kNumClients = 50; |
| 101 | for (int i = 0; i < kNumClients; ++i) { |
zhongyi | ef8d1e9 | 2019-08-26 12:47:21 -0700 | [diff] [blame] | 102 | EXPECT_EQ(number_of_open_fds, NumOpenSocketFDs()); |
| 103 | std::unique_ptr<QuicClient> client(CreateAndInitializeQuicClient()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 104 | // Initializing the client will create a new FD. |
zhongyi | ef8d1e9 | 2019-08-26 12:47:21 -0700 | [diff] [blame] | 105 | EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // The FDs created by the QuicClients should now be closed. |
| 109 | EXPECT_EQ(number_of_open_fds, NumOpenSocketFDs()); |
| 110 | } |
| 111 | |
| 112 | TEST_F(QuicClientTest, CreateAndCleanUpUDPSockets) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 113 | size_t number_of_open_fds = NumOpenSocketFDs(); |
| 114 | |
zhongyi | ef8d1e9 | 2019-08-26 12:47:21 -0700 | [diff] [blame] | 115 | std::unique_ptr<QuicClient> client(CreateAndInitializeQuicClient()); |
| 116 | // Creating and initializing a client will result in one socket being opened. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); |
zhongyi | ef8d1e9 | 2019-08-26 12:47:21 -0700 | [diff] [blame] | 118 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 119 | // Create more UDP sockets. |
| 120 | EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); |
| 121 | EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); |
| 122 | EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); |
| 123 | EXPECT_EQ(number_of_open_fds + 3, NumOpenSocketFDs()); |
| 124 | |
| 125 | // Clean up UDP sockets. |
| 126 | QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); |
| 127 | EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); |
| 128 | QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); |
| 129 | EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); |
| 130 | } |
| 131 | |
| 132 | } // namespace |
| 133 | } // namespace test |
| 134 | } // namespace quic |