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/base/host_port_pair.h" 6 7 #include "base/logging.h" 8 #include "net/test/gtest_util.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 using std::string; 12 13 namespace net { 14 15 namespace { 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 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)); 37 } 38 39 TEST(HostPortPairTest, BadString) { 40 HostPortPair foo = HostPortPair::FromString("foo.com:2:3"); 41 EXPECT_TRUE(foo.host().empty()); 42 EXPECT_EQ(0, foo.port()); 43 44 HostPortPair bar = HostPortPair::FromString("bar.com:two"); 45 EXPECT_TRUE(bar.host().empty()); 46 EXPECT_EQ(0, bar.port()); 47 } 48 49 TEST(HostPortPairTest, Emptiness) { 50 HostPortPair foo; 51 EXPECT_TRUE(foo.IsEmpty()); 52 foo = HostPortPair::FromString("foo.com:8080"); 53 EXPECT_FALSE(foo.IsEmpty()); 54 } 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.\0com", 7); 74 HostPortPair bar(bar_hostname, 80); 75 string expected_error("Host has a null char: a%00.%00com"); 76 EXPECT_DFATAL(bar.HostForURL(), expected_error); 77 } 78 79 TEST(HostPortPairTest, LessThan) { 80 HostPortPair a_10("a.com", 10); 81 HostPortPair a_11("a.com", 11); 82 HostPortPair b_10("b.com", 10); 83 HostPortPair b_11("b.com", 11); 84 85 EXPECT_FALSE(a_10 < a_10); 86 EXPECT_TRUE(a_10 < a_11); 87 EXPECT_TRUE(a_10 < b_10); 88 EXPECT_TRUE(a_10 < b_11); 89 90 EXPECT_FALSE(a_11 < a_10); 91 EXPECT_FALSE(a_11 < b_10); 92 93 EXPECT_FALSE(b_10 < a_10); 94 EXPECT_TRUE(b_10 < a_11); 95 96 EXPECT_FALSE(b_11 < a_10); 97 } 98 99 TEST(HostPortPairTest, Equals) { 100 HostPortPair a_10("a.com", 10); 101 HostPortPair a_11("a.com", 11); 102 HostPortPair b_10("b.com", 10); 103 HostPortPair b_11("b.com", 11); 104 105 HostPortPair new_a_10("a.com", 10); 106 107 EXPECT_TRUE(new_a_10.Equals(a_10)); 108 EXPECT_FALSE(new_a_10.Equals(a_11)); 109 EXPECT_FALSE(new_a_10.Equals(b_10)); 110 EXPECT_FALSE(new_a_10.Equals(b_11)); 111 } 112 113 } // namespace 114 115 } // namespace net 116