| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/tools/quic/quic_client.h" | 5 #include "net/tools/quic/quic_client.h" |
| 6 | 6 |
| 7 #include <dirent.h> | 7 #include <dirent.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/files/file_enumerator.h" | 12 #include "base/files/file_enumerator.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "net/quic/platform/api/quic_test.h" |
| 14 #include "net/quic/platform/api/quic_text_utils.h" | 15 #include "net/quic/platform/api/quic_text_utils.h" |
| 15 #include "net/quic/test_tools/crypto_test_utils.h" | 16 #include "net/quic/test_tools/crypto_test_utils.h" |
| 16 #include "net/quic/test_tools/quic_test_utils.h" | 17 #include "net/quic/test_tools/quic_test_utils.h" |
| 17 #include "net/tools/epoll_server/epoll_server.h" | 18 #include "net/tools/epoll_server/epoll_server.h" |
| 18 #include "net/tools/quic/test_tools/quic_client_peer.h" | 19 #include "net/tools/quic/test_tools/quic_client_peer.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 namespace test { | 23 namespace test { |
| 23 namespace { | 24 namespace { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 53 QuicServerId server_id("hostname", server_address.port(), | 54 QuicServerId server_id("hostname", server_address.port(), |
| 54 PRIVACY_MODE_DISABLED); | 55 PRIVACY_MODE_DISABLED); |
| 55 QuicVersionVector versions = AllSupportedVersions(); | 56 QuicVersionVector versions = AllSupportedVersions(); |
| 56 QuicClient* client = | 57 QuicClient* client = |
| 57 new QuicClient(server_address, server_id, versions, eps, | 58 new QuicClient(server_address, server_id, versions, eps, |
| 58 crypto_test_utils::ProofVerifierForTesting()); | 59 crypto_test_utils::ProofVerifierForTesting()); |
| 59 EXPECT_TRUE(client->Initialize()); | 60 EXPECT_TRUE(client->Initialize()); |
| 60 return client; | 61 return client; |
| 61 } | 62 } |
| 62 | 63 |
| 63 TEST(QuicClientTest, DoNotLeakSocketFDs) { | 64 class QuicClientTest : public QuicTest {}; |
| 65 |
| 66 TEST_F(QuicClientTest, DoNotLeakSocketFDs) { |
| 64 // Make sure that the QuicClient doesn't leak socket FDs. Doing so could cause | 67 // Make sure that the QuicClient doesn't leak socket FDs. Doing so could cause |
| 65 // port exhaustion in long running processes which repeatedly create clients. | 68 // port exhaustion in long running processes which repeatedly create clients. |
| 66 | 69 |
| 67 // Create a ProofVerifier before counting the number of open FDs to work | 70 // Create a ProofVerifier before counting the number of open FDs to work |
| 68 // around some ASAN weirdness. | 71 // around some ASAN weirdness. |
| 69 crypto_test_utils::ProofVerifierForTesting().reset(); | 72 crypto_test_utils::ProofVerifierForTesting().reset(); |
| 70 | 73 |
| 71 // Record initial number of FDs, after creation of EpollServer. | 74 // Record initial number of FDs, after creation of EpollServer. |
| 72 EpollServer eps; | 75 EpollServer eps; |
| 73 size_t number_of_open_fds = NumOpenSocketFDs(); | 76 size_t number_of_open_fds = NumOpenSocketFDs(); |
| 74 | 77 |
| 75 // Create a number of clients, initialize them, and verify this has resulted | 78 // Create a number of clients, initialize them, and verify this has resulted |
| 76 // in additional FDs being opened. | 79 // in additional FDs being opened. |
| 77 const int kNumClients = 50; | 80 const int kNumClients = 50; |
| 78 for (int i = 0; i < kNumClients; ++i) { | 81 for (int i = 0; i < kNumClients; ++i) { |
| 79 std::unique_ptr<QuicClient> client( | 82 std::unique_ptr<QuicClient> client( |
| 80 CreateAndInitializeQuicClient(&eps, net::test::kTestPort + i)); | 83 CreateAndInitializeQuicClient(&eps, net::test::kTestPort + i)); |
| 81 | 84 |
| 82 // Initializing the client will create a new FD. | 85 // Initializing the client will create a new FD. |
| 83 EXPECT_LT(number_of_open_fds, NumOpenSocketFDs()); | 86 EXPECT_LT(number_of_open_fds, NumOpenSocketFDs()); |
| 84 } | 87 } |
| 85 | 88 |
| 86 // The FDs created by the QuicClients should now be closed. | 89 // The FDs created by the QuicClients should now be closed. |
| 87 EXPECT_EQ(number_of_open_fds, NumOpenSocketFDs()); | 90 EXPECT_EQ(number_of_open_fds, NumOpenSocketFDs()); |
| 88 } | 91 } |
| 89 | 92 |
| 90 TEST(QuicClientTest, CreateAndCleanUpUDPSockets) { | 93 TEST_F(QuicClientTest, CreateAndCleanUpUDPSockets) { |
| 91 // Create a ProofVerifier before counting the number of open FDs to work | 94 // Create a ProofVerifier before counting the number of open FDs to work |
| 92 // around some ASAN weirdness. | 95 // around some ASAN weirdness. |
| 93 crypto_test_utils::ProofVerifierForTesting().reset(); | 96 crypto_test_utils::ProofVerifierForTesting().reset(); |
| 94 | 97 |
| 95 EpollServer eps; | 98 EpollServer eps; |
| 96 size_t number_of_open_fds = NumOpenSocketFDs(); | 99 size_t number_of_open_fds = NumOpenSocketFDs(); |
| 97 | 100 |
| 98 std::unique_ptr<QuicClient> client( | 101 std::unique_ptr<QuicClient> client( |
| 99 CreateAndInitializeQuicClient(&eps, net::test::kTestPort)); | 102 CreateAndInitializeQuicClient(&eps, net::test::kTestPort)); |
| 100 EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); | 103 EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); |
| 101 // Create more UDP sockets. | 104 // Create more UDP sockets. |
| 102 EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); | 105 EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); |
| 103 EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); | 106 EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); |
| 104 EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); | 107 EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); |
| 105 EXPECT_EQ(number_of_open_fds + 3, NumOpenSocketFDs()); | 108 EXPECT_EQ(number_of_open_fds + 3, NumOpenSocketFDs()); |
| 106 | 109 |
| 107 // Clean up UDP sockets. | 110 // Clean up UDP sockets. |
| 108 QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); | 111 QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); |
| 109 EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); | 112 EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); |
| 110 QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); | 113 QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); |
| 111 EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); | 114 EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); |
| 112 } | 115 } |
| 113 | 116 |
| 114 } // namespace | 117 } // namespace |
| 115 } // namespace test | 118 } // namespace test |
| 116 } // namespace net | 119 } // namespace net |
| OLD | NEW |