Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: origin_unittest.cc

Issue 2029803003: Update to Chromium //url at Chromium commit 79dc59ac7602413181079ecb463873e29a1d7d0a. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/domokit/gurl@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « origin.cc ('k') | scheme_host_port.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/logging.h"
6 #include "url/origin.h"
5 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
6 #include "url/origin.h" 8 #include "url/gurl.h"
7
8 namespace url {
9 9
10 namespace { 10 namespace {
11 11
12 // Each test examines the Origin is constructed correctly without 12 TEST(OriginTest, UniqueOriginComparison) {
13 // violating DCHECKs. 13 url::Origin unique_origin;
14 TEST(OriginTest, constructEmpty) { 14 EXPECT_EQ("", unique_origin.scheme());
15 Origin origin; 15 EXPECT_EQ("", unique_origin.host());
16 EXPECT_EQ("null", origin.string()); 16 EXPECT_EQ(0, unique_origin.port());
17 } 17 EXPECT_TRUE(unique_origin.unique());
18 18 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin));
19 TEST(OriginTest, constructNull) { 19
20 Origin origin("null"); 20 const char* const urls[] = {"data:text/html,Hello!",
21 EXPECT_EQ("null", origin.string()); 21 "javascript:alert(1)",
22 } 22 "file://example.com:443/etc/passwd",
23 23 "yay",
24 TEST(OriginTest, constructValidOrigin) { 24 "http::///invalid.example.com/"};
25 Origin origin("http://example.com:8080"); 25
26 EXPECT_EQ("http://example.com:8080", origin.string()); 26 for (const auto& test_url : urls) {
27 } 27 SCOPED_TRACE(test_url);
28 28 GURL url(test_url);
29 TEST(OriginTest, constructValidFileOrigin) { 29 url::Origin origin(url);
30 Origin origin("file://"); 30 EXPECT_EQ("", origin.scheme());
31 EXPECT_EQ("file://", origin.string()); 31 EXPECT_EQ("", origin.host());
32 } 32 EXPECT_EQ(0, origin.port());
33 33 EXPECT_TRUE(origin.unique());
34 TEST(OriginTest, constructValidOriginWithoutPort) { 34 EXPECT_FALSE(origin.IsSameOriginWith(origin));
35 Origin origin("wss://example2.com"); 35 EXPECT_FALSE(unique_origin.IsSameOriginWith(origin));
36 EXPECT_EQ("wss://example2.com", origin.string()); 36 EXPECT_FALSE(origin.IsSameOriginWith(unique_origin));
37 } 37 }
38 38 }
39 } // namespace 39
40 TEST(OriginTest, ConstructFromGURL) {
41 url::Origin different_origin(GURL("https://not-in-the-list.test/"));
42
43 struct TestCases {
44 const char* const url;
45 const char* const expected_scheme;
46 const char* const expected_host;
47 const uint16 expected_port;
48 } cases[] = {
49 // IP Addresses
50 {"http://192.168.9.1/", "http", "192.168.9.1", 80},
51 {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80},
52
53 // Punycode
54 {"http://☃.net/", "http", "xn--n3h.net", 80},
55 {"blob:http://☃.net/", "http", "xn--n3h.net", 80},
56
57 // Generic URLs
58 {"http://example.com/", "http", "example.com", 80},
59 {"http://example.com:123/", "http", "example.com", 123},
60 {"https://example.com/", "https", "example.com", 443},
61 {"https://example.com:123/", "https", "example.com", 123},
62 {"http://user:pass@example.com/", "http", "example.com", 80},
63 {"http://example.com:123/?query", "http", "example.com", 123},
64 {"https://example.com/#1234", "https", "example.com", 443},
65 {"https://u:p@example.com:123/?query#1234", "https", "example.com", 123},
66
67 // Registered URLs
68 {"ftp://example.com/", "ftp", "example.com", 21},
69 {"gopher://example.com/", "gopher", "example.com", 70},
70 {"ws://example.com/", "ws", "example.com", 80},
71 {"wss://example.com/", "wss", "example.com", 443},
72
73 // file: URLs
74 {"file:///etc/passwd", "file", "", 0},
75 {"file://example.com/etc/passwd", "file", "example.com", 0},
76
77 // Filesystem:
78 {"filesystem:http://example.com/type/", "http", "example.com", 80},
79 {"filesystem:http://example.com:123/type/", "http", "example.com", 123},
80 {"filesystem:https://example.com/type/", "https", "example.com", 443},
81 {"filesystem:https://example.com:123/type/", "https", "example.com", 123},
82
83 // Blob:
84 {"blob:http://example.com/guid-goes-here", "http", "example.com", 80},
85 {"blob:http://example.com:123/guid-goes-here", "http", "example.com", 123} ,
86 {"blob:https://example.com/guid-goes-here", "https", "example.com", 443},
87 {"blob:http://u:p@example.com/guid-goes-here", "http", "example.com", 80},
88 };
89
90 for (const auto& test_case : cases) {
91 SCOPED_TRACE(test_case.url);
92 GURL url(test_case.url);
93 EXPECT_TRUE(url.is_valid());
94 url::Origin origin(url);
95 EXPECT_EQ(test_case.expected_scheme, origin.scheme());
96 EXPECT_EQ(test_case.expected_host, origin.host());
97 EXPECT_EQ(test_case.expected_port, origin.port());
98 EXPECT_FALSE(origin.unique());
99 EXPECT_TRUE(origin.IsSameOriginWith(origin));
100 EXPECT_FALSE(different_origin.IsSameOriginWith(origin));
101 EXPECT_FALSE(origin.IsSameOriginWith(different_origin));
102 }
103 }
104
105 TEST(OriginTest, Serialization) {
106 struct TestCases {
107 const char* const url;
108 const char* const expected;
109 } cases[] = {
110 {"http://192.168.9.1/", "http://192.168.9.1"},
111 {"http://[2001:db8::1]/", "http://[2001:db8::1]"},
112 {"http://☃.net/", "http://xn--n3h.net"},
113 {"http://example.com/", "http://example.com"},
114 {"http://example.com:123/", "http://example.com:123"},
115 {"https://example.com/", "https://example.com"},
116 {"https://example.com:123/", "https://example.com:123"},
117 {"file:///etc/passwd", "file://"},
118 {"file://example.com/etc/passwd", "file://"},
119 };
120
121 for (const auto& test_case : cases) {
122 SCOPED_TRACE(test_case.url);
123 GURL url(test_case.url);
124 EXPECT_TRUE(url.is_valid());
125 url::Origin origin(url);
126 EXPECT_EQ(test_case.expected, origin.Serialize());
127
128 // The '<<' operator should produce the same serialization as Serialize().
129 std::stringstream out;
130 out << origin;
131 EXPECT_EQ(test_case.expected, out.str());
132 }
133 }
134
135 TEST(OriginTest, Comparison) {
136 // These URLs are arranged in increasing order:
137 const char* const urls[] = {
138 "data:uniqueness",
139 "http://a:80",
140 "http://b:80",
141 "https://a:80",
142 "https://b:80",
143 "http://a:81",
144 "http://b:81",
145 "https://a:81",
146 "https://b:81",
147 };
148
149 for (size_t i = 0; i < arraysize(urls); i++) {
150 GURL current_url(urls[i]);
151 url::Origin current(current_url);
152 for (size_t j = i; j < arraysize(urls); j++) {
153 GURL compare_url(urls[j]);
154 url::Origin to_compare(compare_url);
155 EXPECT_EQ(i < j, current < to_compare) << i << " < " << j;
156 EXPECT_EQ(j < i, to_compare < current) << j << " < " << i;
157 }
158 }
159 }
160
161 TEST(OriginTest, UnsafelyCreate) {
162 struct TestCase {
163 const char* scheme;
164 const char* host;
165 uint16 port;
166 } cases[] = {
167 {"http", "example.com", 80},
168 {"http", "example.com", 123},
169 {"https", "example.com", 443},
170 {"https", "example.com", 123},
171 {"file", "", 0},
172 {"file", "example.com", 0},
173 };
174
175 for (const auto& test : cases) {
176 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
177 << test.port);
178 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization(
179 test.scheme, test.host, test.port);
180 EXPECT_EQ(test.scheme, origin.scheme());
181 EXPECT_EQ(test.host, origin.host());
182 EXPECT_EQ(test.port, origin.port());
183 EXPECT_FALSE(origin.unique());
184 EXPECT_TRUE(origin.IsSameOriginWith(origin));
185 }
186 }
187
188 TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) {
189 struct TestCases {
190 const char* scheme;
191 const char* host;
192 uint16 port;
193 } cases[] = {{"", "", 0},
194 {"data", "", 0},
195 {"blob", "", 0},
196 {"filesystem", "", 0},
197 {"data", "example.com", 80},
198 {"http", "☃.net", 80},
199 {"http\nmore", "example.com", 80},
200 {"http\rmore", "example.com", 80},
201 {"http\n", "example.com", 80},
202 {"http\r", "example.com", 80},
203 {"http", "example.com\nnot-example.com", 80},
204 {"http", "example.com\rnot-example.com", 80},
205 {"http", "example.com\n", 80},
206 {"http", "example.com\r", 80},
207 {"http", "example.com", 0},
208 {"file", "", 80}};
209
210 for (const auto& test : cases) {
211 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
212 << test.port);
213 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization(
214 test.scheme, test.host, test.port);
215 EXPECT_EQ("", origin.scheme());
216 EXPECT_EQ("", origin.host());
217 EXPECT_EQ(0, origin.port());
218 EXPECT_TRUE(origin.unique());
219 EXPECT_FALSE(origin.IsSameOriginWith(origin));
220 }
221 }
222
223 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) {
224 struct TestCases {
225 const char* scheme;
226 size_t scheme_length;
227 const char* host;
228 size_t host_length;
229 uint16 port;
230 } cases[] = {{"http\0more", 9, "example.com", 11, 80},
231 {"http\0", 5, "example.com", 11, 80},
232 {"\0http", 5, "example.com", 11, 80},
233 {"http", 4, "example.com\0not-example.com", 27, 80},
234 {"http", 4, "example.com\0", 12, 80},
235 {"http", 4, "\0example.com", 12, 80}};
236
237 for (const auto& test : cases) {
238 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
239 << test.port);
240 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization(
241 std::string(test.scheme, test.scheme_length),
242 std::string(test.host, test.host_length), test.port);
243 EXPECT_EQ("", origin.scheme());
244 EXPECT_EQ("", origin.host());
245 EXPECT_EQ(0, origin.port());
246 EXPECT_TRUE(origin.unique());
247 EXPECT_FALSE(origin.IsSameOriginWith(origin));
248 }
249 }
40 250
41 } // namespace url 251 } // namespace url
OLDNEW
« no previous file with comments | « origin.cc ('k') | scheme_host_port.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698