OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/base/host_port_pair.h" | 5 #include "net/base/host_port_pair.h" |
6 | 6 |
| 7 #include "base/logging.h" |
| 8 #include "net/test/gtest_util.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
8 | 10 |
| 11 using std::string; |
| 12 |
9 namespace net { | 13 namespace net { |
10 | 14 |
11 namespace { | 15 namespace { |
12 | 16 |
| 17 struct TestData { |
| 18 string host; |
| 19 uint16 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]" }, |
| 29 }; |
| 30 |
13 TEST(HostPortPairTest, Parsing) { | 31 TEST(HostPortPairTest, Parsing) { |
14 HostPortPair foo("foo.com", 10); | 32 HostPortPair foo("foo.com", 10); |
15 std::string foo_str = foo.ToString(); | 33 string foo_str = foo.ToString(); |
16 EXPECT_EQ("foo.com:10", foo_str); | 34 EXPECT_EQ("foo.com:10", foo_str); |
17 HostPortPair bar = HostPortPair::FromString(foo_str); | 35 HostPortPair bar = HostPortPair::FromString(foo_str); |
18 EXPECT_TRUE(foo.Equals(bar)); | 36 EXPECT_TRUE(foo.Equals(bar)); |
19 } | 37 } |
20 | 38 |
21 TEST(HostPortPairTest, BadString) { | 39 TEST(HostPortPairTest, BadString) { |
22 HostPortPair foo = HostPortPair::FromString("foo.com:2:3"); | 40 HostPortPair foo = HostPortPair::FromString("foo.com:2:3"); |
23 EXPECT_TRUE(foo.host().empty()); | 41 EXPECT_TRUE(foo.host().empty()); |
24 EXPECT_EQ(0, foo.port()); | 42 EXPECT_EQ(0, foo.port()); |
25 | 43 |
26 HostPortPair bar = HostPortPair::FromString("bar.com:two"); | 44 HostPortPair bar = HostPortPair::FromString("bar.com:two"); |
27 EXPECT_TRUE(bar.host().empty()); | 45 EXPECT_TRUE(bar.host().empty()); |
28 EXPECT_EQ(0, bar.port()); | 46 EXPECT_EQ(0, bar.port()); |
29 } | 47 } |
30 | 48 |
31 TEST(HostPortPairTest, Emptiness) { | 49 TEST(HostPortPairTest, Emptiness) { |
32 HostPortPair foo; | 50 HostPortPair foo; |
33 EXPECT_TRUE(foo.IsEmpty()); | 51 EXPECT_TRUE(foo.IsEmpty()); |
34 foo = HostPortPair::FromString("foo.com:8080"); | 52 foo = HostPortPair::FromString("foo.com:8080"); |
35 EXPECT_FALSE(foo.IsEmpty()); | 53 EXPECT_FALSE(foo.IsEmpty()); |
36 } | 54 } |
37 | 55 |
| 56 TEST(HostPortPairTest, ToString) { |
| 57 for (size_t index = 0; index < arraysize(tests); ++index) { |
| 58 HostPortPair foo(tests[index].host, tests[index].port); |
| 59 EXPECT_EQ(tests[index].to_string, foo.ToString()); |
| 60 } |
| 61 |
| 62 // Test empty hostname. |
| 63 HostPortPair foo(string(), 10); |
| 64 } |
| 65 |
| 66 TEST(HostPortPairTest, HostForURL) { |
| 67 for (size_t index = 0; index < arraysize(tests); ++index) { |
| 68 HostPortPair foo(tests[index].host, tests[index].port); |
| 69 EXPECT_EQ(tests[index].host_for_url, foo.HostForURL()); |
| 70 } |
| 71 |
| 72 // Test hostname with null character. |
| 73 string bar_hostname("a\0.com", 6); |
| 74 HostPortPair bar(bar_hostname, 80); |
| 75 string expected_error("Host has a null char: "); |
| 76 expected_error.append(bar_hostname); |
| 77 EXPECT_DFATAL(bar.HostForURL(), expected_error); |
| 78 } |
| 79 |
| 80 TEST(HostPortPairTest, LessThan) { |
| 81 HostPortPair a_10("a.com", 10); |
| 82 HostPortPair a_11("a.com", 11); |
| 83 HostPortPair b_10("b.com", 10); |
| 84 HostPortPair b_11("b.com", 11); |
| 85 |
| 86 EXPECT_FALSE(a_10 < a_10); |
| 87 EXPECT_TRUE(a_10 < a_11); |
| 88 EXPECT_TRUE(a_10 < b_10); |
| 89 EXPECT_TRUE(a_10 < b_11); |
| 90 |
| 91 EXPECT_FALSE(a_11 < a_10); |
| 92 EXPECT_FALSE(a_11 < b_10); |
| 93 |
| 94 EXPECT_FALSE(b_10 < a_10); |
| 95 EXPECT_TRUE(b_10 < a_11); |
| 96 |
| 97 EXPECT_FALSE(b_11 < a_10); |
| 98 } |
| 99 |
| 100 TEST(HostPortPairTest, Equals) { |
| 101 HostPortPair a_10("a.com", 10); |
| 102 HostPortPair a_11("a.com", 11); |
| 103 HostPortPair b_10("b.com", 10); |
| 104 HostPortPair b_11("b.com", 11); |
| 105 |
| 106 HostPortPair new_a_10("a.com", 10); |
| 107 |
| 108 EXPECT_TRUE(new_a_10.Equals(a_10)); |
| 109 EXPECT_FALSE(new_a_10.Equals(a_11)); |
| 110 EXPECT_FALSE(new_a_10.Equals(b_10)); |
| 111 EXPECT_FALSE(new_a_10.Equals(b_11)); |
| 112 } |
| 113 |
38 } // namespace | 114 } // namespace |
39 | 115 |
40 } // namespace net | 116 } // namespace net |
OLD | NEW |