| 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> |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 // Creates a new QuicClient and Initializes it. Caller is responsible for | 48 // Creates a new QuicClient and Initializes it. Caller is responsible for |
| 49 // deletion. | 49 // deletion. |
| 50 QuicClient* CreateAndInitializeQuicClient(EpollServer* eps, uint16_t port) { | 50 QuicClient* CreateAndInitializeQuicClient(EpollServer* eps, uint16_t port) { |
| 51 QuicSocketAddress server_address( | 51 QuicSocketAddress server_address( |
| 52 QuicSocketAddress(QuicIpAddress::Loopback4(), port)); | 52 QuicSocketAddress(QuicIpAddress::Loopback4(), port)); |
| 53 QuicServerId server_id("hostname", server_address.port(), | 53 QuicServerId server_id("hostname", server_address.port(), |
| 54 PRIVACY_MODE_DISABLED); | 54 PRIVACY_MODE_DISABLED); |
| 55 QuicVersionVector versions = AllSupportedVersions(); | 55 QuicVersionVector versions = AllSupportedVersions(); |
| 56 QuicClient* client = | 56 QuicClient* client = |
| 57 new QuicClient(server_address, server_id, versions, eps, | 57 new QuicClient(server_address, server_id, versions, eps, |
| 58 CryptoTestUtils::ProofVerifierForTesting()); | 58 crypto_test_utils::ProofVerifierForTesting()); |
| 59 EXPECT_TRUE(client->Initialize()); | 59 EXPECT_TRUE(client->Initialize()); |
| 60 return client; | 60 return client; |
| 61 } | 61 } |
| 62 | 62 |
| 63 TEST(QuicClientTest, DoNotLeakFDs) { | 63 TEST(QuicClientTest, DoNotLeakFDs) { |
| 64 // Make sure that the QuicClient doesn't leak socket FDs. Doing so could cause | 64 // 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. | 65 // port exhaustion in long running processes which repeatedly create clients. |
| 66 | 66 |
| 67 // Create a ProofVerifier before counting the number of open FDs to work | 67 // Create a ProofVerifier before counting the number of open FDs to work |
| 68 // around some ASAN weirdness. | 68 // around some ASAN weirdness. |
| 69 CryptoTestUtils::ProofVerifierForTesting().reset(); | 69 crypto_test_utils::ProofVerifierForTesting().reset(); |
| 70 | 70 |
| 71 // Make sure that the QuicClient doesn't leak FDs. Doing so could cause port | 71 // Make sure that the QuicClient doesn't leak FDs. Doing so could cause port |
| 72 // exhaustion in long running processes which repeatedly create clients. | 72 // exhaustion in long running processes which repeatedly create clients. |
| 73 | 73 |
| 74 // Record initial number of FDs, after creation of EpollServer. | 74 // Record initial number of FDs, after creation of EpollServer. |
| 75 EpollServer eps; | 75 EpollServer eps; |
| 76 size_t number_of_open_fds = NumOpenSocketFDs(); | 76 size_t number_of_open_fds = NumOpenSocketFDs(); |
| 77 | 77 |
| 78 // 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 |
| 79 // in additional FDs being opened. | 79 // in additional FDs being opened. |
| 80 const int kNumClients = 50; | 80 const int kNumClients = 50; |
| 81 for (int i = 0; i < kNumClients; ++i) { | 81 for (int i = 0; i < kNumClients; ++i) { |
| 82 std::unique_ptr<QuicClient> client( | 82 std::unique_ptr<QuicClient> client( |
| 83 CreateAndInitializeQuicClient(&eps, net::test::kTestPort + i)); | 83 CreateAndInitializeQuicClient(&eps, net::test::kTestPort + i)); |
| 84 | 84 |
| 85 // Initializing the client will create a new FD. | 85 // Initializing the client will create a new FD. |
| 86 EXPECT_LT(number_of_open_fds, NumOpenSocketFDs()); | 86 EXPECT_LT(number_of_open_fds, NumOpenSocketFDs()); |
| 87 } | 87 } |
| 88 | 88 |
| 89 // The FDs created by the QuicClients should now be closed. | 89 // The FDs created by the QuicClients should now be closed. |
| 90 EXPECT_EQ(number_of_open_fds, NumOpenSocketFDs()); | 90 EXPECT_EQ(number_of_open_fds, NumOpenSocketFDs()); |
| 91 } | 91 } |
| 92 | 92 |
| 93 TEST(QuicClientTest, CreateAndCleanUpUDPSockets) { | 93 TEST(QuicClientTest, CreateAndCleanUpUDPSockets) { |
| 94 // 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 |
| 95 // around some ASAN weirdness. | 95 // around some ASAN weirdness. |
| 96 CryptoTestUtils::ProofVerifierForTesting().reset(); | 96 crypto_test_utils::ProofVerifierForTesting().reset(); |
| 97 | 97 |
| 98 EpollServer eps; | 98 EpollServer eps; |
| 99 size_t number_of_open_fds = NumOpenSocketFDs(); | 99 size_t number_of_open_fds = NumOpenSocketFDs(); |
| 100 | 100 |
| 101 std::unique_ptr<QuicClient> client( | 101 std::unique_ptr<QuicClient> client( |
| 102 CreateAndInitializeQuicClient(&eps, net::test::kTestPort)); | 102 CreateAndInitializeQuicClient(&eps, net::test::kTestPort)); |
| 103 EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); | 103 EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); |
| 104 // Create more UDP sockets. | 104 // Create more UDP sockets. |
| 105 EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); | 105 EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); |
| 106 EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); | 106 EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); |
| 107 EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); | 107 EXPECT_TRUE(QuicClientPeer::CreateUDPSocketAndBind(client.get())); |
| 108 EXPECT_EQ(number_of_open_fds + 3, NumOpenSocketFDs()); | 108 EXPECT_EQ(number_of_open_fds + 3, NumOpenSocketFDs()); |
| 109 | 109 |
| 110 // Clean up UDP sockets. | 110 // Clean up UDP sockets. |
| 111 QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); | 111 QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); |
| 112 EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); | 112 EXPECT_EQ(number_of_open_fds + 2, NumOpenSocketFDs()); |
| 113 QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); | 113 QuicClientPeer::CleanUpUDPSocket(client.get(), client->GetLatestFD()); |
| 114 EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); | 114 EXPECT_EQ(number_of_open_fds + 1, NumOpenSocketFDs()); |
| 115 } | 115 } |
| 116 | 116 |
| 117 } // namespace | 117 } // namespace |
| 118 } // namespace test | 118 } // namespace test |
| 119 } // namespace net | 119 } // namespace net |
| OLD | NEW |