Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / base / host_port_pair_unittest.cc
blob8047cc3181401dffd0cc917b90969bf58e1aacb9
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.
5 #include "net/base/host_port_pair.h"
7 #include "base/logging.h"
8 #include "net/test/gtest_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using std::string;
13 namespace net {
15 namespace {
17 struct TestData {
18 string host;
19 uint16_t port;
20 string to_string;
21 string host_for_url;
22 } tests[] = {
23 { "www.google.com", 80, "www.google.com:80", "www.google.com" },
24 { "www.google.com", 443, "www.google.com:443", "www.google.com" },
25 { "127.0.0.1", 80, "127.0.0.1:80", "127.0.0.1" },
26 { "192.168.1.1", 80, "192.168.1.1:80", "192.168.1.1" },
27 { "::1", 80, "[::1]:80", "[::1]" },
28 { "2001:db8::42", 80, "[2001:db8::42]:80", "[2001:db8::42]" },
31 TEST(HostPortPairTest, Parsing) {
32 HostPortPair foo("foo.com", 10);
33 string foo_str = foo.ToString();
34 EXPECT_EQ("foo.com:10", foo_str);
35 HostPortPair bar = HostPortPair::FromString(foo_str);
36 EXPECT_TRUE(foo.Equals(bar));
39 TEST(HostPortPairTest, BadString) {
40 const char* kBadStrings[] = {
41 "foo.com:2:3",
42 "bar.com:two",
43 "www.google.com:-1",
44 "127.0.0.1:65536",
45 "[2001:db8::42]:65536",
48 for (size_t index = 0; index < arraysize(kBadStrings); ++index) {
49 HostPortPair foo = HostPortPair::FromString(kBadStrings[index]);
50 EXPECT_TRUE(foo.host().empty());
51 EXPECT_EQ(0, foo.port());
55 TEST(HostPortPairTest, Emptiness) {
56 HostPortPair foo;
57 EXPECT_TRUE(foo.IsEmpty());
58 foo = HostPortPair::FromString("foo.com:8080");
59 EXPECT_FALSE(foo.IsEmpty());
62 TEST(HostPortPairTest, ToString) {
63 for (size_t index = 0; index < arraysize(tests); ++index) {
64 HostPortPair foo(tests[index].host, tests[index].port);
65 EXPECT_EQ(tests[index].to_string, foo.ToString());
68 // Test empty hostname.
69 HostPortPair foo(string(), 10);
72 TEST(HostPortPairTest, HostForURL) {
73 for (size_t index = 0; index < arraysize(tests); ++index) {
74 HostPortPair foo(tests[index].host, tests[index].port);
75 EXPECT_EQ(tests[index].host_for_url, foo.HostForURL());
78 // Test hostname with null character.
79 string bar_hostname("a\0.\0com", 7);
80 HostPortPair bar(bar_hostname, 80);
81 string expected_error("Host has a null char: a%00.%00com");
82 EXPECT_DFATAL(bar.HostForURL(), expected_error);
85 TEST(HostPortPairTest, LessThan) {
86 HostPortPair a_10("a.com", 10);
87 HostPortPair a_11("a.com", 11);
88 HostPortPair b_10("b.com", 10);
89 HostPortPair b_11("b.com", 11);
91 EXPECT_FALSE(a_10 < a_10);
92 EXPECT_TRUE(a_10 < a_11);
93 EXPECT_TRUE(a_10 < b_10);
94 EXPECT_TRUE(a_10 < b_11);
96 EXPECT_FALSE(a_11 < a_10);
97 EXPECT_FALSE(a_11 < b_10);
99 EXPECT_FALSE(b_10 < a_10);
100 EXPECT_TRUE(b_10 < a_11);
102 EXPECT_FALSE(b_11 < a_10);
105 TEST(HostPortPairTest, Equals) {
106 HostPortPair a_10("a.com", 10);
107 HostPortPair a_11("a.com", 11);
108 HostPortPair b_10("b.com", 10);
109 HostPortPair b_11("b.com", 11);
111 HostPortPair new_a_10("a.com", 10);
113 EXPECT_TRUE(new_a_10.Equals(a_10));
114 EXPECT_FALSE(new_a_10.Equals(a_11));
115 EXPECT_FALSE(new_a_10.Equals(b_10));
116 EXPECT_FALSE(new_a_10.Equals(b_11));
119 } // namespace
121 } // namespace net