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
, GURLConstruction
) {
72 {"http://192.168.9.1/", "http", "192.168.9.1", 80},
73 {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80},
74 {"http://☃.net/", "http", "xn--n3h.net", 80},
75 {"http://example.com/", "http", "example.com", 80},
76 {"http://example.com:123/", "http", "example.com", 123},
77 {"https://example.com/", "https", "example.com", 443},
78 {"https://example.com:123/", "https", "example.com", 123},
79 {"file:///etc/passwd", "file", "", 0},
80 {"file://example.com/etc/passwd", "file", "example.com", 0},
81 {"http://u:p@example.com/", "http", "example.com", 80},
82 {"http://u:p@example.com/path", "http", "example.com", 80},
83 {"http://u:p@example.com/path?123", "http", "example.com", 80},
84 {"http://u:p@example.com/path?123#hash", "http", "example.com", 80},
87 for (const auto& test
: cases
) {
88 SCOPED_TRACE(test
.url
);
90 EXPECT_TRUE(url
.is_valid());
91 url::SchemeHostPort
tuple(url
);
92 EXPECT_EQ(test
.scheme
, tuple
.scheme());
93 EXPECT_EQ(test
.host
, tuple
.host());
94 EXPECT_EQ(test
.port
, tuple
.port());
95 EXPECT_FALSE(tuple
.IsInvalid());
96 EXPECT_TRUE(tuple
.Equals(tuple
));
100 TEST(SchemeHostPortTest
, Serialization
) {
103 const char* expected
;
105 {"http://192.168.9.1/", "http://192.168.9.1"},
106 {"http://[2001:db8::1]/", "http://[2001:db8::1]"},
107 {"http://☃.net/", "http://xn--n3h.net"},
108 {"http://example.com/", "http://example.com"},
109 {"http://example.com:123/", "http://example.com:123"},
110 {"https://example.com/", "https://example.com"},
111 {"https://example.com:123/", "https://example.com:123"},
112 {"file:///etc/passwd", "file://"},
113 {"file://example.com/etc/passwd", "file://example.com"},
116 for (const auto& test
: cases
) {
117 SCOPED_TRACE(test
.url
);
119 url::SchemeHostPort
tuple(url
);
120 EXPECT_EQ(test
.expected
, tuple
.Serialize());
124 TEST(SchemeHostPortTest
, Comparison
) {
125 // These tuples are arranged in increasing order:
126 struct SchemeHostPorts
{
141 for (size_t i
= 0; i
< arraysize(tuples
); i
++) {
142 url::SchemeHostPort
current(tuples
[i
].scheme
, tuples
[i
].host
,
144 for (size_t j
= i
; j
< arraysize(tuples
); j
++) {
145 url::SchemeHostPort
to_compare(tuples
[j
].scheme
, tuples
[j
].host
,
147 EXPECT_EQ(i
< j
, current
< to_compare
) << i
<< " < " << j
;
148 EXPECT_EQ(j
< i
, to_compare
< current
) << j
<< " < " << i
;