OLD | NEW |
| (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/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 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", | |
46 }; | |
47 | |
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()); | |
52 } | |
53 } | |
54 | |
55 TEST(HostPortPairTest, Emptiness) { | |
56 HostPortPair foo; | |
57 EXPECT_TRUE(foo.IsEmpty()); | |
58 foo = HostPortPair::FromString("foo.com:8080"); | |
59 EXPECT_FALSE(foo.IsEmpty()); | |
60 } | |
61 | |
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()); | |
66 } | |
67 | |
68 // Test empty hostname. | |
69 HostPortPair foo(string(), 10); | |
70 } | |
71 | |
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()); | |
76 } | |
77 | |
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); | |
83 } | |
84 | |
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); | |
90 | |
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); | |
95 | |
96 EXPECT_FALSE(a_11 < a_10); | |
97 EXPECT_FALSE(a_11 < b_10); | |
98 | |
99 EXPECT_FALSE(b_10 < a_10); | |
100 EXPECT_TRUE(b_10 < a_11); | |
101 | |
102 EXPECT_FALSE(b_11 < a_10); | |
103 } | |
104 | |
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); | |
110 | |
111 HostPortPair new_a_10("a.com", 10); | |
112 | |
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)); | |
117 } | |
118 | |
119 } // namespace | |
120 | |
121 } // namespace net | |
OLD | NEW |