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"
7 #include "url/scheme_host_port.h"
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
) {
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
) {
45 {"http", "example.com", 80},
46 {"http", "example.com", 123},
47 {"https", "example.com", 443},
48 {"https", "example.com", 123},
50 {"file", "example.com", 0},
53 for (const auto& test
: cases
) {
54 SCOPED_TRACE(testing::Message() << test
.scheme
<< "://" << test
.host
<< ":"
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
) {
70 } cases
[] = {{"", "", 0},
73 {"filesystem", "", 0},
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},
88 for (const auto& test
: cases
) {
89 SCOPED_TRACE(testing::Message() << test
.scheme
<< "://" << test
.host
<< ":"
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
) {
103 size_t scheme_length
;
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
<< ":"
117 url::SchemeHostPort
tuple(std::string(test
.scheme
, test
.scheme_length
),
118 std::string(test
.host
, test
.host_length
),
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
) {
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
);
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
) {
165 const char* expected
;
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
);
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
{
203 for (size_t i
= 0; i
< arraysize(tuples
); i
++) {
204 url::SchemeHostPort
current(tuples
[i
].scheme
, tuples
[i
].host
,
206 for (size_t j
= i
; j
< arraysize(tuples
); j
++) {
207 url::SchemeHostPort
to_compare(tuples
[j
].scheme
, tuples
[j
].host
,
209 EXPECT_EQ(i
< j
, current
< to_compare
) << i
<< " < " << j
;
210 EXPECT_EQ(j
< i
, to_compare
< current
) << j
<< " < " << i
;