| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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/quic/chromium/port_suggester.h" | |
| 6 | |
| 7 #include <set> | |
| 8 | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace net { | |
| 12 namespace test { | |
| 13 | |
| 14 class PortSuggesterTest : public ::testing::Test { | |
| 15 protected: | |
| 16 PortSuggesterTest() | |
| 17 : entropy_(1345689), | |
| 18 min_ephemeral_port_(1025), | |
| 19 max_ephemeral_port_(65535) {} | |
| 20 | |
| 21 uint64_t entropy_; | |
| 22 int min_ephemeral_port_; | |
| 23 int max_ephemeral_port_; | |
| 24 }; | |
| 25 | |
| 26 TEST_F(PortSuggesterTest, SmallRangeTest) { | |
| 27 // When the range is small (one wide), we always get that as our answer. | |
| 28 scoped_refptr<PortSuggester> port_suggester = | |
| 29 new PortSuggester(HostPortPair("www.example.com", 443), entropy_); | |
| 30 // Test this for a few different (small) ranges. | |
| 31 for (int port = 2000; port < 2010; ++port) { | |
| 32 // Use |port| for both |min| and |max| delimiting the suggestion range. | |
| 33 EXPECT_EQ(port, port_suggester->SuggestPort(port, port)); | |
| 34 EXPECT_EQ(port, port_suggester->previous_suggestion()); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 TEST_F(PortSuggesterTest, SuggestAllPorts) { | |
| 39 // We should eventually fill out any range, but we'll just ensure that we | |
| 40 // fill out a small range of ports. | |
| 41 scoped_refptr<PortSuggester> port_suggester = | |
| 42 new PortSuggester(HostPortPair("www.example.com", 443), entropy_); | |
| 43 std::set<int> ports; | |
| 44 const uint32_t port_range = 20; | |
| 45 const int insertion_limit = 200; // We should be done by then. | |
| 46 for (int i = 0; i < insertion_limit; ++i) { | |
| 47 ports.insert(port_suggester->SuggestPort( | |
| 48 min_ephemeral_port_, min_ephemeral_port_ + port_range - 1)); | |
| 49 if (ports.size() == port_range) { | |
| 50 break; | |
| 51 } | |
| 52 } | |
| 53 EXPECT_EQ(port_range, ports.size()); | |
| 54 } | |
| 55 | |
| 56 TEST_F(PortSuggesterTest, AvoidDuplication) { | |
| 57 // When the range is large, duplicates are rare, but we'll ask for a few | |
| 58 // suggestions and make sure they are unique. | |
| 59 scoped_refptr<PortSuggester> port_suggester = | |
| 60 new PortSuggester(HostPortPair("www.example.com", 80), entropy_); | |
| 61 std::set<int> ports; | |
| 62 const size_t port_count = 200; | |
| 63 for (size_t i = 0; i < port_count; ++i) { | |
| 64 ports.insert( | |
| 65 port_suggester->SuggestPort(min_ephemeral_port_, max_ephemeral_port_)); | |
| 66 } | |
| 67 EXPECT_EQ(port_suggester->call_count(), port_count); | |
| 68 EXPECT_EQ(port_count, ports.size()); | |
| 69 } | |
| 70 | |
| 71 TEST_F(PortSuggesterTest, ConsistentPorts) { | |
| 72 // For given hostname, port, and entropy, we should always get the same | |
| 73 // suggestions. | |
| 74 scoped_refptr<PortSuggester> port_suggester1 = | |
| 75 new PortSuggester(HostPortPair("www.example.com", 443), entropy_); | |
| 76 scoped_refptr<PortSuggester> port_suggester2 = | |
| 77 new PortSuggester(HostPortPair("www.example.com", 443), entropy_); | |
| 78 for (int test_count = 20; test_count > 0; --test_count) { | |
| 79 EXPECT_EQ( | |
| 80 port_suggester1->SuggestPort(min_ephemeral_port_, min_ephemeral_port_), | |
| 81 port_suggester2->SuggestPort(min_ephemeral_port_, min_ephemeral_port_)); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 TEST_F(PortSuggesterTest, DifferentHostPortEntropy) { | |
| 86 // When we have different hosts, port, or entropy, we probably won't collide. | |
| 87 scoped_refptr<PortSuggester> port_suggester[] = { | |
| 88 new PortSuggester(HostPortPair("www.example.com", 80), entropy_), | |
| 89 new PortSuggester(HostPortPair("www.example.ORG", 80), entropy_), | |
| 90 new PortSuggester(HostPortPair("www.example.com", 443), entropy_), | |
| 91 new PortSuggester(HostPortPair("www.example.com", 80), entropy_ + 123456), | |
| 92 }; | |
| 93 | |
| 94 std::set<int> ports; | |
| 95 const int port_count = 40; | |
| 96 size_t insertion_count = 0; | |
| 97 for (size_t j = 0; j < arraysize(port_suggester); ++j) { | |
| 98 for (int i = 0; i < port_count; ++i) { | |
| 99 ports.insert(port_suggester[j]->SuggestPort(min_ephemeral_port_, | |
| 100 max_ephemeral_port_)); | |
| 101 ++insertion_count; | |
| 102 } | |
| 103 } | |
| 104 EXPECT_EQ(insertion_count, ports.size()); | |
| 105 } | |
| 106 | |
| 107 } // namespace test | |
| 108 } // namespace net | |
| OLD | NEW |