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 | |
| 5 | #include "net/third_party/quiche/src/quic/tools/quic_client.h" |
| 6 | |
| 7 | #include <dirent.h> |
| 8 | #include <sys/types.h> |
| 9 | |
| 10 | #include <memory> |
| 11 | |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_epoll.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_port_utils.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_test_loopback.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 18 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
| 19 | #include "net/third_party/quiche/src/quic/test_tools/quic_client_peer.h" |
| 20 | |
| 21 | namespace quic { |
| 22 | namespace test { |
| 23 | namespace { |
| 24 | |
| 25 | const char* kPathToFds = "/proc/self/fd"; |
| 26 | |
| 27 | QuicString ReadLink(const QuicString& path) { |
| 28 | QuicString result(PATH_MAX, '\0'); |
| 29 | ssize_t result_size = readlink(path.c_str(), &result[0], result.size()); |
| 30 | CHECK(result_size > 0 && static_cast<size_t>(result_size) < result.size()); |
| 31 | result.resize(result_size); |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | // Counts the number of open sockets for the current process. |
| 36 | size_t NumOpenSocketFDs() { |
| 37 | size_t socket_count = 0; |
| 38 | dirent* file; |
| 39 | std::unique_ptr<DIR, int (*)(DIR*)> fd_directory(opendir(kPathToFds), |
| 40 | closedir); |
| 41 | while ((file = readdir(fd_directory.get())) != nullptr) { |
| 42 | QuicStringPiece name(file->d_name); |
| 43 | if (name == "." || name == "..") { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | QuicString fd_path = ReadLink(QuicStrCat(kPathToFds, "/", name)); |
| 48 | if (QuicTextUtils::StartsWith(fd_path, "socket:")) { |
| 49 | socket_count++; |
| 50 | } |
| 51 | } |
| 52 | return socket_count; |
| 53 | } |
| 54 | |
| 55 | // Creates a new QuicClient and Initializes it. Caller is responsible for |
| 56 | // deletion. |
| 57 | std::unique_ptr<QuicClient> CreateAndInitializeQuicClient(QuicEpollServer* eps, |
| 58 | uint16_t port) { |
| 59 | QuicSocketAddress server_address(QuicSocketAddress(TestLoopback(), port)); |
| 60 | QuicServerId server_id("hostname", server_address.port(), false); |
| 61 | ParsedQuicVersionVector versions = AllSupportedVersions(); |
| 62 | auto client = |
| 63 | QuicMakeUnique<QuicClient>(server_address, server_id, versions, eps, |
| 64 | crypto_test_utils::ProofVerifierForTesting()); |
| 65 | EXPECT_TRUE(client->Initialize()); |
| 66 | return client; |
| 67 | } |
| 68 | |
| 69 | class QuicClientTest : public QuicTest {}; |
| 70 | |
| 71 | TEST_F(QuicClientTest, DoNotLeakSocketFDs) { |
| 72 | // Make sure that the QuicClient doesn't leak socket FDs. Doing so could cause |
| 73 | // port exhaustion in long running processes which repeatedly create clients. |
| 74 | |
| 75 | // Record initial number of FDs, after creating EpollServer and creating and |
| 76 | // destroying a single client (the latter is needed since initializing |
| 77 | // platform dependencies like certificate verifier may open a persistent |
| 78 | // socket). |
| 79 | QuicEpollServer eps; |
| 80 | CreateAndInitializeQuicClient(&eps, QuicPickUnusedPortOrDie()); |
| 81 | size_t number_of_open_fds = NumOpenSocketFDs(); |
| 82 | |
| 83 | // Create a number of clients, initialize them, and verify this has resulted |
| 84 | // in additional FDs being opened. |
| 85 | const int kNumClients = 50; |
| 86 | for (int i = 0; i < kNumClients; ++i) { |
| 87 | std::unique_ptr<QuicClient> client( |
| 88 | CreateAndInitializeQuicClient(&eps, QuicPickUnusedPortOrDie())); |
| 89 | |
| 90 | // Initializing the client will create a new FD. |
| 91 | EXPECT_LT(number_of_open_fds, NumOpenSocketFDs()); |
| 92 | } |
| 93 | |
| 94 | // The FDs created by the QuicClients should now be closed. |
| 95 | EXPECT_EQ(number_of_open_fds, NumOpenSocketFDs()); |
| 96 | } |
| 97 | |
| 98 | TEST_F(QuicClientTest, CreateAndCleanUpUDPSockets) { |
| 99 | QuicEpollServer eps; |
| 100 | size_t number_of_open_fds = NumOpenSocketFDs(); |
| 101 | |
| 102 | std::unique_ptr<QuicClient> client( |
| 103 | CreateAndInitializeQuicClient(&eps, QuicPickUnusedPortOrDie())); |
| 104 | EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); |
| 105 | // Create more UDP sockets. |
| 106 | EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); |
| 107 | EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); |
| 108 | EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); |
| 109 | EXPECT_EQ(number_of_open_fds + 3, NumOpenSocketFDs()); |
| 110 | |
| 111 | // Clean up UDP sockets. |
| 112 | QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); |
| 113 | EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); |
| 114 | QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); |
| 115 | EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); |
| 116 | } |
| 117 | |
| 118 | } // namespace |
| 119 | } // namespace test |
| 120 | } // namespace quic |