Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: net/quic/port_suggester_unittest.cc

Issue 107803002: Consistently suggest ephemeral port for QUIC client (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/port_suggester.h"
6
7 #include <climits>
wtc 2013/12/06 23:36:44 Nit: what is <climits> for?
jar (doing other things) 2013/12/07 00:47:58 <oops> I was using MAX_INT, but switched to using
8 #include <set>
9
10 #include "base/basictypes.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace net {
14 namespace test {
15
16 class PortSuggesterTest : public ::testing::Test {
17 protected:
18 PortSuggesterTest()
19 : entropy_(34567),
20 min_ephemeral_port_(1025),
21 max_ephemeral_port_(65535) {
22 }
23
24 uint64 entropy_;
25 int min_ephemeral_port_;
26 int max_ephemeral_port_;
27 };
28
29 TEST_F(PortSuggesterTest, SmallRangeTest) {
30 // When the range is small (one wide), we always get that as our answer.
31 scoped_refptr<PortSuggester> port_suggester =
32 new PortSuggester(HostPortPair("www.com", 443), entropy_);
wtc 2013/12/06 23:36:44 Nit: I suggest "www.example.com".
jar (doing other things) 2013/12/07 00:47:58 Done.
33 // Test this for a few different (small) ranges.
34 for (int port = 2000; port < 2010; ++port) {
35 // Use a min an max for the range of |port|.
wtc 2013/12/06 23:36:44 an => and?
jar (doing other things) 2013/12/07 00:47:58 Improved comment.
36 EXPECT_EQ(port, port_suggester->SuggestPort(port, port));
37 }
38 }
39
40 TEST_F(PortSuggesterTest, SuggestAllPorts) {
41 // We should eventually fill out any range, but we'll just ensure that we
42 // fill out a small range of ports.
43 scoped_refptr<PortSuggester> port_suggester =
44 new PortSuggester(HostPortPair("www.com", 443), entropy_);
45 std::set<int> ports;
46 const int port_range = 20;
47 int insertion_limit = 200; // We should be done by then.
48 for (int i = 0; i < insertion_limit; ++i) {
49 ports.insert(port_suggester->SuggestPort(min_ephemeral_port_,
50 min_ephemeral_port_ + port_range - 1));
51 if (ports.size() == static_cast<size_t>(port_range)) {
52 break;
53 }
54 }
55 EXPECT_EQ(static_cast<size_t>(port_range), ports.size());
56 }
57
58 TEST_F(PortSuggesterTest, AvoidDuplication) {
59 // When the range is large, duplicates are rare, but we'll ask for a few
60 // suggestions and make sure they are unique.
61 scoped_refptr<PortSuggester> port_suggester =
62 new PortSuggester(HostPortPair("www.com", 443), entropy_);
63 std::set<int> ports;
64 const int port_count = 200;
65 for (int i = 0; i < port_count; ++i) {
66 ports.insert(port_suggester->SuggestPort(min_ephemeral_port_,
67 max_ephemeral_port_));
68 }
69 EXPECT_EQ(static_cast<size_t>(port_count), ports.size());
70 }
71
72 TEST_F(PortSuggesterTest, ConsistentPorts) {
73 // For given hostname, port, and entropy, we should always get the same
74 // suggestions.
75 scoped_refptr<PortSuggester> port_suggester1 =
76 new PortSuggester(HostPortPair("www.com", 443), entropy_);
77 scoped_refptr<PortSuggester> port_suggester2 =
78 new PortSuggester(HostPortPair("www.com", 443), entropy_);
79 for (int test_count = 20; test_count > 0; --test_count) {
80 EXPECT_EQ(port_suggester1->SuggestPort(min_ephemeral_port_,
81 min_ephemeral_port_),
82 port_suggester2->SuggestPort(min_ephemeral_port_,
83 min_ephemeral_port_));
84 }
85 }
86
87 TEST_F(PortSuggesterTest, DifferentHostPorts) {
88 // When we have different hosts, port, or entropy, we probably won't collide.
89 scoped_refptr<PortSuggester> port_suggester[] = {
90 new PortSuggester(HostPortPair("www.com", 80), entropy_),
91 new PortSuggester(HostPortPair("www.gov", 80), entropy_),
92 new PortSuggester(HostPortPair("www.com", 81), entropy_),
93 new PortSuggester(HostPortPair("www.com", 80), entropy_ + 1),
94 };
95
96 std::set<int> ports;
97 const int port_count = 40;
98 size_t insertion_count = 0;
99 for (size_t j = 0; j < arraysize(port_suggester); ++j) {
100 for (int i = 0; i < port_count; ++i) {
101 ports.insert(port_suggester[j]->SuggestPort(min_ephemeral_port_,
102 max_ephemeral_port_));
103 ++insertion_count;
104 }
105 }
106 EXPECT_EQ(insertion_count, ports.size());
107 }
108
109 } // namespace test
110 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698