This source file includes following definitions.
- max_ephemeral_port_
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "net/quic/port_suggester.h"
#include <set>
#include "base/basictypes.h"
#include "net/base/host_port_pair.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace test {
class PortSuggesterTest : public ::testing::Test {
protected:
PortSuggesterTest()
: entropy_(1345689),
min_ephemeral_port_(1025),
max_ephemeral_port_(65535) {
}
uint64 entropy_;
int min_ephemeral_port_;
int max_ephemeral_port_;
};
TEST_F(PortSuggesterTest, SmallRangeTest) {
scoped_refptr<PortSuggester> port_suggester =
new PortSuggester(HostPortPair("www.example.com", 443), entropy_);
for (int port = 2000; port < 2010; ++port) {
EXPECT_EQ(port, port_suggester->SuggestPort(port, port));
EXPECT_EQ(port, port_suggester->previous_suggestion());
}
}
TEST_F(PortSuggesterTest, SuggestAllPorts) {
scoped_refptr<PortSuggester> port_suggester =
new PortSuggester(HostPortPair("www.example.com", 443), entropy_);
std::set<int> ports;
const uint32 port_range = 20;
const int insertion_limit = 200;
for (int i = 0; i < insertion_limit; ++i) {
ports.insert(port_suggester->SuggestPort(min_ephemeral_port_,
min_ephemeral_port_ + port_range - 1));
if (ports.size() == port_range) {
break;
}
}
EXPECT_EQ(port_range, ports.size());
}
TEST_F(PortSuggesterTest, AvoidDuplication) {
scoped_refptr<PortSuggester> port_suggester =
new PortSuggester(HostPortPair("www.example.com", 80), entropy_);
std::set<int> ports;
const size_t port_count = 200;
for (size_t i = 0; i < port_count; ++i) {
ports.insert(port_suggester->SuggestPort(min_ephemeral_port_,
max_ephemeral_port_));
}
EXPECT_EQ(port_suggester->call_count(), port_count);
EXPECT_EQ(port_count, ports.size());
}
TEST_F(PortSuggesterTest, ConsistentPorts) {
scoped_refptr<PortSuggester> port_suggester1 =
new PortSuggester(HostPortPair("www.example.com", 443), entropy_);
scoped_refptr<PortSuggester> port_suggester2 =
new PortSuggester(HostPortPair("www.example.com", 443), entropy_);
for (int test_count = 20; test_count > 0; --test_count) {
EXPECT_EQ(port_suggester1->SuggestPort(min_ephemeral_port_,
min_ephemeral_port_),
port_suggester2->SuggestPort(min_ephemeral_port_,
min_ephemeral_port_));
}
}
TEST_F(PortSuggesterTest, DifferentHostPortEntropy) {
scoped_refptr<PortSuggester> port_suggester[] = {
new PortSuggester(HostPortPair("www.example.com", 80), entropy_),
new PortSuggester(HostPortPair("www.example.ORG", 80), entropy_),
new PortSuggester(HostPortPair("www.example.com", 443), entropy_),
new PortSuggester(HostPortPair("www.example.com", 80), entropy_ + 123456),
};
std::set<int> ports;
const int port_count = 40;
size_t insertion_count = 0;
for (size_t j = 0; j < arraysize(port_suggester); ++j) {
for (int i = 0; i < port_count; ++i) {
ports.insert(port_suggester[j]->SuggestPort(min_ephemeral_port_,
max_ephemeral_port_));
++insertion_count;
}
}
EXPECT_EQ(insertion_count, ports.size());
}
}
}