Roll src/third_party/WebKit 049340a:b6c48e6 (svn 201128:201129)
[chromium-blink-merge.git] / url / scheme_host_port_unittest.cc
blob817631d74fdabb8eebe08ab693214e9206ab4e5f
1 // Copyright 2015 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 "testing/gtest/include/gtest/gtest.h"
6 #include "url/gurl.h"
7 #include "url/scheme_host_port.h"
9 namespace {
11 TEST(SchemeHostPortTest, Invalid) {
12 url::SchemeHostPort invalid;
13 EXPECT_EQ("", invalid.scheme());
14 EXPECT_EQ("", invalid.host());
15 EXPECT_EQ(0, invalid.port());
16 EXPECT_TRUE(invalid.IsInvalid());
17 EXPECT_TRUE(invalid.Equals(invalid));
19 const char* urls[] = {"data:text/html,Hello!",
20 "javascript:alert(1)",
21 "file://example.com:443/etc/passwd",
22 "blob:https://example.com/uuid-goes-here",
23 "filesystem:https://example.com/temporary/yay.png"};
25 for (const auto& test : urls) {
26 SCOPED_TRACE(test);
27 GURL url(test);
28 url::SchemeHostPort tuple(url);
29 EXPECT_EQ("", tuple.scheme());
30 EXPECT_EQ("", tuple.host());
31 EXPECT_EQ(0, tuple.port());
32 EXPECT_TRUE(tuple.IsInvalid());
33 EXPECT_TRUE(tuple.Equals(tuple));
34 EXPECT_TRUE(tuple.Equals(invalid));
35 EXPECT_TRUE(invalid.Equals(tuple));
39 TEST(SchemeHostPortTest, ExplicitConstruction) {
40 struct TestCases {
41 const char* scheme;
42 const char* host;
43 uint16 port;
44 } cases[] = {
45 {"http", "example.com", 80},
46 {"http", "example.com", 123},
47 {"https", "example.com", 443},
48 {"https", "example.com", 123},
49 {"file", "", 0},
50 {"file", "example.com", 0},
53 for (const auto& test : cases) {
54 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
55 << test.port);
56 url::SchemeHostPort tuple(test.scheme, test.host, test.port);
57 EXPECT_EQ(test.scheme, tuple.scheme());
58 EXPECT_EQ(test.host, tuple.host());
59 EXPECT_EQ(test.port, tuple.port());
60 EXPECT_FALSE(tuple.IsInvalid());
61 EXPECT_TRUE(tuple.Equals(tuple));
65 TEST(SchemeHostPortTest, InvalidConstruction) {
66 struct TestCases {
67 const char* scheme;
68 const char* host;
69 uint16 port;
70 } cases[] = {{"", "", 0},
71 {"data", "", 0},
72 {"blob", "", 0},
73 {"filesystem", "", 0},
74 {"http", "", 80},
75 {"data", "example.com", 80},
76 {"http", "☃.net", 80},
77 {"http\nmore", "example.com", 80},
78 {"http\rmore", "example.com", 80},
79 {"http\n", "example.com", 80},
80 {"http\r", "example.com", 80},
81 {"http", "example.com\nnot-example.com", 80},
82 {"http", "example.com\rnot-example.com", 80},
83 {"http", "example.com\n", 80},
84 {"http", "example.com\r", 80},
85 {"http", "example.com", 0},
86 {"file", "", 80}};
88 for (const auto& test : cases) {
89 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
90 << test.port);
91 url::SchemeHostPort tuple(test.scheme, test.host, test.port);
92 EXPECT_EQ("", tuple.scheme());
93 EXPECT_EQ("", tuple.host());
94 EXPECT_EQ(0, tuple.port());
95 EXPECT_TRUE(tuple.IsInvalid());
96 EXPECT_TRUE(tuple.Equals(tuple));
100 TEST(SchemeHostPortTest, InvalidConstructionWithEmbeddedNulls) {
101 struct TestCases {
102 const char* scheme;
103 size_t scheme_length;
104 const char* host;
105 size_t host_length;
106 uint16 port;
107 } cases[] = {{"http\0more", 9, "example.com", 11, 80},
108 {"http\0", 5, "example.com", 11, 80},
109 {"\0http", 5, "example.com", 11, 80},
110 {"http", 4, "example.com\0not-example.com", 27, 80},
111 {"http", 4, "example.com\0", 12, 80},
112 {"http", 4, "\0example.com", 12, 80}};
114 for (const auto& test : cases) {
115 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
116 << test.port);
117 url::SchemeHostPort tuple(std::string(test.scheme, test.scheme_length),
118 std::string(test.host, test.host_length),
119 test.port);
120 EXPECT_EQ("", tuple.scheme());
121 EXPECT_EQ("", tuple.host());
122 EXPECT_EQ(0, tuple.port());
123 EXPECT_TRUE(tuple.IsInvalid());
127 TEST(SchemeHostPortTest, GURLConstruction) {
128 struct TestCases {
129 const char* url;
130 const char* scheme;
131 const char* host;
132 uint16 port;
133 } cases[] = {
134 {"http://192.168.9.1/", "http", "192.168.9.1", 80},
135 {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80},
136 {"http://☃.net/", "http", "xn--n3h.net", 80},
137 {"http://example.com/", "http", "example.com", 80},
138 {"http://example.com:123/", "http", "example.com", 123},
139 {"https://example.com/", "https", "example.com", 443},
140 {"https://example.com:123/", "https", "example.com", 123},
141 {"file:///etc/passwd", "file", "", 0},
142 {"file://example.com/etc/passwd", "file", "example.com", 0},
143 {"http://u:p@example.com/", "http", "example.com", 80},
144 {"http://u:p@example.com/path", "http", "example.com", 80},
145 {"http://u:p@example.com/path?123", "http", "example.com", 80},
146 {"http://u:p@example.com/path?123#hash", "http", "example.com", 80},
149 for (const auto& test : cases) {
150 SCOPED_TRACE(test.url);
151 GURL url(test.url);
152 EXPECT_TRUE(url.is_valid());
153 url::SchemeHostPort tuple(url);
154 EXPECT_EQ(test.scheme, tuple.scheme());
155 EXPECT_EQ(test.host, tuple.host());
156 EXPECT_EQ(test.port, tuple.port());
157 EXPECT_FALSE(tuple.IsInvalid());
158 EXPECT_TRUE(tuple.Equals(tuple));
162 TEST(SchemeHostPortTest, Serialization) {
163 struct TestCases {
164 const char* url;
165 const char* expected;
166 } cases[] = {
167 {"http://192.168.9.1/", "http://192.168.9.1"},
168 {"http://[2001:db8::1]/", "http://[2001:db8::1]"},
169 {"http://☃.net/", "http://xn--n3h.net"},
170 {"http://example.com/", "http://example.com"},
171 {"http://example.com:123/", "http://example.com:123"},
172 {"https://example.com/", "https://example.com"},
173 {"https://example.com:123/", "https://example.com:123"},
174 {"file:///etc/passwd", "file://"},
175 {"file://example.com/etc/passwd", "file://example.com"},
178 for (const auto& test : cases) {
179 SCOPED_TRACE(test.url);
180 GURL url(test.url);
181 url::SchemeHostPort tuple(url);
182 EXPECT_EQ(test.expected, tuple.Serialize());
186 TEST(SchemeHostPortTest, Comparison) {
187 // These tuples are arranged in increasing order:
188 struct SchemeHostPorts {
189 const char* scheme;
190 const char* host;
191 uint16 port;
192 } tuples[] = {
193 {"http", "a", 80},
194 {"http", "b", 80},
195 {"https", "a", 80},
196 {"https", "b", 80},
197 {"http", "a", 81},
198 {"http", "b", 81},
199 {"https", "a", 81},
200 {"https", "b", 81},
203 for (size_t i = 0; i < arraysize(tuples); i++) {
204 url::SchemeHostPort current(tuples[i].scheme, tuples[i].host,
205 tuples[i].port);
206 for (size_t j = i; j < arraysize(tuples); j++) {
207 url::SchemeHostPort to_compare(tuples[j].scheme, tuples[j].host,
208 tuples[j].port);
209 EXPECT_EQ(i < j, current < to_compare) << i << " < " << j;
210 EXPECT_EQ(j < i, to_compare < current) << j << " < " << i;
215 } // namespace url