Roll src/third_party/skia 10b371c:c71239b
[chromium-blink-merge.git] / url / scheme_host_port_unittest.cc
blob3001d2423ab7b25de684361752c558b9de0788bf
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, GURLConstruction) {
66 struct TestCases {
67 const char* url;
68 const char* scheme;
69 const char* host;
70 uint16 port;
71 } cases[] = {
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);
89 GURL url(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) {
101 struct TestCases {
102 const char* url;
103 const char* expected;
104 } cases[] = {
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);
118 GURL url(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 {
127 const char* scheme;
128 const char* host;
129 uint16 port;
130 } tuples[] = {
131 {"http", "a", 80},
132 {"http", "b", 80},
133 {"https", "a", 80},
134 {"https", "b", 80},
135 {"http", "a", 81},
136 {"http", "b", 81},
137 {"https", "a", 81},
138 {"https", "b", 81},
141 for (size_t i = 0; i < arraysize(tuples); i++) {
142 url::SchemeHostPort current(tuples[i].scheme, tuples[i].host,
143 tuples[i].port);
144 for (size_t j = i; j < arraysize(tuples); j++) {
145 url::SchemeHostPort to_compare(tuples[j].scheme, tuples[j].host,
146 tuples[j].port);
147 EXPECT_EQ(i < j, current < to_compare) << i << " < " << j;
148 EXPECT_EQ(j < i, to_compare < current) << j << " < " << i;
153 } // namespace url