blob: 4e1139c4bac58cda3d18a47df3954db38713baa3 [file] [log] [blame]
mkwst9f2cc892015-07-22 06:03:251// 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.
4
avic0c60312015-12-21 21:03:505#include <stddef.h>
6#include <stdint.h>
7
mkwstd8335d982015-07-25 05:18:488#include "base/logging.h"
avic0c60312015-12-21 21:03:509#include "base/macros.h"
mkwst9f2cc892015-07-22 06:03:2510#include "testing/gtest/include/gtest/gtest.h"
11#include "url/gurl.h"
avic0c60312015-12-21 21:03:5012#include "url/origin.h"
mkwst9f2cc892015-07-22 06:03:2513
14namespace {
15
csharrison048bee12016-10-04 00:08:2116void ExpectParsedUrlsEqual(const GURL& a, const GURL& b) {
17 EXPECT_EQ(a, b);
18 const url::Parsed& a_parsed = a.parsed_for_possibly_invalid_spec();
19 const url::Parsed& b_parsed = b.parsed_for_possibly_invalid_spec();
csharrison6fbc9fa2016-10-08 03:31:5020 EXPECT_EQ(a_parsed.scheme.begin, b_parsed.scheme.begin);
21 EXPECT_EQ(a_parsed.scheme.len, b_parsed.scheme.len);
22 EXPECT_EQ(a_parsed.username.begin, b_parsed.username.begin);
23 EXPECT_EQ(a_parsed.username.len, b_parsed.username.len);
24 EXPECT_EQ(a_parsed.password.begin, b_parsed.password.begin);
25 EXPECT_EQ(a_parsed.password.len, b_parsed.password.len);
26 EXPECT_EQ(a_parsed.host.begin, b_parsed.host.begin);
27 EXPECT_EQ(a_parsed.host.len, b_parsed.host.len);
28 EXPECT_EQ(a_parsed.port.begin, b_parsed.port.begin);
29 EXPECT_EQ(a_parsed.port.len, b_parsed.port.len);
30 EXPECT_EQ(a_parsed.path.begin, b_parsed.path.begin);
31 EXPECT_EQ(a_parsed.path.len, b_parsed.path.len);
32 EXPECT_EQ(a_parsed.query.begin, b_parsed.query.begin);
33 EXPECT_EQ(a_parsed.query.len, b_parsed.query.len);
34 EXPECT_EQ(a_parsed.ref.begin, b_parsed.ref.begin);
35 EXPECT_EQ(a_parsed.ref.len, b_parsed.ref.len);
csharrison048bee12016-10-04 00:08:2136}
37
mkwst9f2cc892015-07-22 06:03:2538TEST(OriginTest, UniqueOriginComparison) {
39 url::Origin unique_origin;
40 EXPECT_EQ("", unique_origin.scheme());
41 EXPECT_EQ("", unique_origin.host());
42 EXPECT_EQ(0, unique_origin.port());
43 EXPECT_TRUE(unique_origin.unique());
44 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin));
45
46 const char* const urls[] = {"data:text/html,Hello!",
47 "javascript:alert(1)",
48 "file://example.com:443/etc/passwd",
49 "yay",
50 "http::///invalid.example.com/"};
51
vmpstr981aa5a2016-07-14 01:05:0852 for (auto* test_url : urls) {
mkwst9f2cc892015-07-22 06:03:2553 SCOPED_TRACE(test_url);
54 GURL url(test_url);
55 url::Origin origin(url);
56 EXPECT_EQ("", origin.scheme());
57 EXPECT_EQ("", origin.host());
58 EXPECT_EQ(0, origin.port());
59 EXPECT_TRUE(origin.unique());
60 EXPECT_FALSE(origin.IsSameOriginWith(origin));
61 EXPECT_FALSE(unique_origin.IsSameOriginWith(origin));
62 EXPECT_FALSE(origin.IsSameOriginWith(unique_origin));
csharrison048bee12016-10-04 00:08:2163
64 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL());
mkwst9f2cc892015-07-22 06:03:2565 }
66}
67
jww908428c2016-10-26 21:51:4668TEST(OriginTest, ConstructFromTuple) {
69 struct TestCases {
70 const char* const scheme;
71 const char* const host;
72 const uint16_t port;
73 const char* const suborigin;
74 } cases[] = {
75 {"http", "example.com", 80, ""},
76 {"http", "example.com", 123, ""},
77 {"https", "example.com", 443, ""},
78 {"http-so", "foobar.example.com", 80, "foobar"},
79 {"http-so", "foobar.example.com", 123, "foobar"},
80 {"https-so", "foobar.example.com", 443, "foobar"},
81 };
82
83 for (const auto& test_case : cases) {
84 testing::Message scope_message;
85 if (test_case.suborigin != std::string()) {
86 scope_message << test_case.scheme << "-so://" << test_case.suborigin
87 << "." << test_case.host << ":" << test_case.port;
88 } else {
89 scope_message << test_case.scheme << "://" << test_case.host << ":"
90 << test_case.port;
91 }
92 SCOPED_TRACE(scope_message);
jww908428c2016-10-26 21:51:4693 url::Origin origin_with_suborigin =
94 url::Origin::CreateFromNormalizedTupleWithSuborigin(
95 test_case.scheme, test_case.host, test_case.port,
96 test_case.suborigin);
97
jww908428c2016-10-26 21:51:4698 EXPECT_EQ(test_case.scheme, origin_with_suborigin.scheme());
99 EXPECT_EQ(test_case.host, origin_with_suborigin.host());
100 EXPECT_EQ(test_case.port, origin_with_suborigin.port());
101 EXPECT_EQ(test_case.suborigin, origin_with_suborigin.suborigin());
102 }
103}
104
mkwst9f2cc892015-07-22 06:03:25105TEST(OriginTest, ConstructFromGURL) {
106 url::Origin different_origin(GURL("https://not-in-the-list.test/"));
107
108 struct TestCases {
109 const char* const url;
110 const char* const expected_scheme;
111 const char* const expected_host;
avic0c60312015-12-21 21:03:50112 const uint16_t expected_port;
mkwst9f2cc892015-07-22 06:03:25113 } cases[] = {
114 // IP Addresses
115 {"http://192.168.9.1/", "http", "192.168.9.1", 80},
116 {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80},
117
118 // Punycode
119 {"http://☃.net/", "http", "xn--n3h.net", 80},
120 {"blob:http://☃.net/", "http", "xn--n3h.net", 80},
121
122 // Generic URLs
123 {"http://example.com/", "http", "example.com", 80},
124 {"http://example.com:123/", "http", "example.com", 123},
125 {"https://example.com/", "https", "example.com", 443},
126 {"https://example.com:123/", "https", "example.com", 123},
127 {"http://user:[email protected]/", "http", "example.com", 80},
128 {"http://example.com:123/?query", "http", "example.com", 123},
129 {"https://example.com/#1234", "https", "example.com", 443},
130 {"https://u:[email protected]:123/?query#1234", "https", "example.com", 123},
131
132 // Registered URLs
133 {"ftp://example.com/", "ftp", "example.com", 21},
134 {"gopher://example.com/", "gopher", "example.com", 70},
135 {"ws://example.com/", "ws", "example.com", 80},
136 {"wss://example.com/", "wss", "example.com", 443},
137
138 // file: URLs
139 {"file:///etc/passwd", "file", "", 0},
140 {"file://example.com/etc/passwd", "file", "example.com", 0},
141
142 // Filesystem:
143 {"filesystem:http://example.com/type/", "http", "example.com", 80},
144 {"filesystem:http://example.com:123/type/", "http", "example.com", 123},
145 {"filesystem:https://example.com/type/", "https", "example.com", 443},
146 {"filesystem:https://example.com:123/type/", "https", "example.com", 123},
147
148 // Blob:
149 {"blob:http://example.com/guid-goes-here", "http", "example.com", 80},
150 {"blob:http://example.com:123/guid-goes-here", "http", "example.com", 123},
151 {"blob:https://example.com/guid-goes-here", "https", "example.com", 443},
152 {"blob:http://u:[email protected]/guid-goes-here", "http", "example.com", 80},
153 };
154
155 for (const auto& test_case : cases) {
156 SCOPED_TRACE(test_case.url);
157 GURL url(test_case.url);
158 EXPECT_TRUE(url.is_valid());
159 url::Origin origin(url);
160 EXPECT_EQ(test_case.expected_scheme, origin.scheme());
161 EXPECT_EQ(test_case.expected_host, origin.host());
162 EXPECT_EQ(test_case.expected_port, origin.port());
163 EXPECT_FALSE(origin.unique());
164 EXPECT_TRUE(origin.IsSameOriginWith(origin));
165 EXPECT_FALSE(different_origin.IsSameOriginWith(origin));
166 EXPECT_FALSE(origin.IsSameOriginWith(different_origin));
csharrison048bee12016-10-04 00:08:21167
168 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL());
mkwst9f2cc892015-07-22 06:03:25169 }
170}
171
172TEST(OriginTest, Serialization) {
173 struct TestCases {
174 const char* const url;
175 const char* const expected;
176 } cases[] = {
177 {"http://192.168.9.1/", "http://192.168.9.1"},
178 {"http://[2001:db8::1]/", "http://[2001:db8::1]"},
179 {"http://☃.net/", "http://xn--n3h.net"},
180 {"http://example.com/", "http://example.com"},
181 {"http://example.com:123/", "http://example.com:123"},
182 {"https://example.com/", "https://example.com"},
183 {"https://example.com:123/", "https://example.com:123"},
184 {"file:///etc/passwd", "file://"},
185 {"file://example.com/etc/passwd", "file://"},
186 };
187
188 for (const auto& test_case : cases) {
189 SCOPED_TRACE(test_case.url);
190 GURL url(test_case.url);
191 EXPECT_TRUE(url.is_valid());
192 url::Origin origin(url);
jww04480402016-10-25 02:50:33193 EXPECT_TRUE(origin.suborigin().empty());
csharrison048bee12016-10-04 00:08:21194 std::string serialized = origin.Serialize();
jww04480402016-10-25 02:50:33195 std::string serialized_physical_origin =
196 origin.GetPhysicalOrigin().Serialize();
csharrison048bee12016-10-04 00:08:21197 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL());
198
199 EXPECT_EQ(test_case.expected, serialized);
jww04480402016-10-25 02:50:33200 EXPECT_EQ(test_case.expected, serialized_physical_origin);
mkwst9f2cc892015-07-22 06:03:25201
202 // The '<<' operator should produce the same serialization as Serialize().
203 std::stringstream out;
204 out << origin;
205 EXPECT_EQ(test_case.expected, out.str());
206 }
207}
208
jww04480402016-10-25 02:50:33209TEST(OriginTest, SuboriginSerialization) {
210 struct TestCases {
211 const char* const url;
212 const char* const expected;
213 const char* const expected_physical_origin;
214 const char* const expected_suborigin;
215 } cases[] = {
216 {"http-so://foobar.example.com/", "http-so://foobar.example.com",
217 "http://example.com", "foobar"},
218 {"http-so://foobar.example.com:123/", "http-so://foobar.example.com:123",
219 "http://example.com:123", "foobar"},
220 {"https-so://foobar.example.com/", "https-so://foobar.example.com",
221 "https://example.com", "foobar"},
222 {"https-so://foobar.example.com:123/",
223 "https-so://foobar.example.com:123", "https://example.com:123",
224 "foobar"},
225 {"http://example.com/", "http://example.com", "http://example.com", ""},
226 {"http-so://foobar.example.com/some/path", "http-so://foobar.example.com",
227 "http://example.com", "foobar"},
228 {"http-so://foobar.example.com/some/path?query",
229 "http-so://foobar.example.com", "http://example.com", "foobar"},
230 {"http-so://foobar.example.com/some/path#fragment",
231 "http-so://foobar.example.com", "http://example.com", "foobar"},
232 {"http-so://foobar.example.com/some/path?query#fragment",
233 "http-so://foobar.example.com", "http://example.com", "foobar"},
234 {"http-so://foobar.example.com:1234/some/path?query#fragment",
235 "http-so://foobar.example.com:1234", "http://example.com:1234",
236 "foobar"},
237 };
238
239 for (const auto& test_case : cases) {
240 SCOPED_TRACE(test_case.url);
241 GURL url(test_case.url);
242 EXPECT_TRUE(url.is_valid());
243 url::Origin origin(url);
244 std::string serialized = origin.Serialize();
245 std::string serialized_physical_origin =
246 origin.GetPhysicalOrigin().Serialize();
247 EXPECT_FALSE(origin.unique());
248 EXPECT_EQ(test_case.expected_suborigin, origin.suborigin());
249 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL());
250
251 EXPECT_EQ(test_case.expected, serialized);
252 EXPECT_EQ(test_case.expected_physical_origin, serialized_physical_origin);
253
254 // The '<<' operator should produce the same serialization as Serialize().
255 std::stringstream out;
256 out << origin;
257 EXPECT_EQ(test_case.expected, out.str());
258 }
259
260 const char* const failure_cases[] = {
261 "http-so://.", "http-so://foo", "http-so://.foo", "http-so://foo.",
262 "https-so://.", "https-so://foo", "https-so://.foo", "https-so://foo.",
263 };
264
vmpstr6d9996c82017-02-23 00:43:25265 for (auto* test_case : failure_cases) {
jww04480402016-10-25 02:50:33266 SCOPED_TRACE(test_case);
267 GURL url(test_case);
268 EXPECT_TRUE(url.is_valid());
269 url::Origin origin(url);
270 std::string serialized = origin.Serialize();
271 std::string serialized_physical_origin =
272 origin.GetPhysicalOrigin().Serialize();
273 EXPECT_TRUE(origin.unique());
274 EXPECT_EQ("", origin.suborigin());
275 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL());
276
277 EXPECT_EQ("null", serialized);
278 EXPECT_EQ("null", serialized_physical_origin);
279 }
280}
281
282TEST(OriginTest, SuboriginIsSameOriginWith) {
283 struct TestCases {
284 const char* const url1;
285 const char* const url2;
286 bool is_same_origin;
287 bool is_same_physical_origin;
288 } cases[]{
289 {"http-so://foobar1.example.com/", "http-so://foobar1.example.com", true,
290 true},
291 {"http-so://foobar2.example.com/", "https-so://foobar2.example.com",
292 false, false},
293 {"http-so://foobar3.example.com/", "http://example.com", false, true},
294 {"https-so://foobar4.example.com/", "https-so://foobar4.example.com",
295 true, true},
296 {"https-so://foobar5.example.com/", "https://example.com", false, true},
297 {"http-so://foobar6.example.com/", "http-so://bazbar.example.com", false,
298 true},
299 {"http-so://foobar7.example.com/", "http-so://foobar7.google.com", false,
300 false},
301 };
302
303 for (const auto& test_case : cases) {
304 SCOPED_TRACE(test_case.url1);
305 url::Origin origin1(GURL(test_case.url1));
306 url::Origin origin2(GURL(test_case.url2));
307
308 EXPECT_TRUE(origin1.IsSameOriginWith(origin1));
309 EXPECT_TRUE(origin2.IsSameOriginWith(origin2));
310 EXPECT_EQ(test_case.is_same_origin, origin1.IsSameOriginWith(origin2));
311 EXPECT_EQ(test_case.is_same_origin, origin2.IsSameOriginWith(origin1));
312
313 EXPECT_TRUE(origin1.IsSamePhysicalOriginWith(origin1));
314 EXPECT_TRUE(origin2.IsSamePhysicalOriginWith(origin2));
315 EXPECT_EQ(test_case.is_same_physical_origin,
316 origin1.IsSamePhysicalOriginWith(origin2));
317 EXPECT_EQ(test_case.is_same_physical_origin,
318 origin2.IsSamePhysicalOriginWith(origin1));
319 }
320}
321
mkwst9f2cc892015-07-22 06:03:25322TEST(OriginTest, Comparison) {
323 // These URLs are arranged in increasing order:
324 const char* const urls[] = {
325 "data:uniqueness",
326 "http://a:80",
327 "http://b:80",
328 "https://a:80",
329 "https://b:80",
330 "http://a:81",
331 "http://b:81",
332 "https://a:81",
333 "https://b:81",
334 };
335
336 for (size_t i = 0; i < arraysize(urls); i++) {
337 GURL current_url(urls[i]);
338 url::Origin current(current_url);
339 for (size_t j = i; j < arraysize(urls); j++) {
340 GURL compare_url(urls[j]);
341 url::Origin to_compare(compare_url);
342 EXPECT_EQ(i < j, current < to_compare) << i << " < " << j;
343 EXPECT_EQ(j < i, to_compare < current) << j << " < " << i;
344 }
345 }
346}
347
mkwstd8335d982015-07-25 05:18:48348TEST(OriginTest, UnsafelyCreate) {
349 struct TestCase {
350 const char* scheme;
351 const char* host;
avic0c60312015-12-21 21:03:50352 uint16_t port;
mkwstd8335d982015-07-25 05:18:48353 } cases[] = {
354 {"http", "example.com", 80},
355 {"http", "example.com", 123},
356 {"https", "example.com", 443},
357 {"https", "example.com", 123},
358 {"file", "", 0},
359 {"file", "example.com", 0},
360 };
361
362 for (const auto& test : cases) {
363 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
364 << test.port);
365 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization(
366 test.scheme, test.host, test.port);
367 EXPECT_EQ(test.scheme, origin.scheme());
368 EXPECT_EQ(test.host, origin.host());
369 EXPECT_EQ(test.port, origin.port());
370 EXPECT_FALSE(origin.unique());
371 EXPECT_TRUE(origin.IsSameOriginWith(origin));
csharrison048bee12016-10-04 00:08:21372
373 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL());
mkwstd8335d982015-07-25 05:18:48374 }
375}
376
377TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) {
378 struct TestCases {
379 const char* scheme;
380 const char* host;
avic0c60312015-12-21 21:03:50381 uint16_t port;
mkwstd8335d982015-07-25 05:18:48382 } cases[] = {{"", "", 0},
383 {"data", "", 0},
384 {"blob", "", 0},
385 {"filesystem", "", 0},
386 {"data", "example.com", 80},
387 {"http", "☃.net", 80},
388 {"http\nmore", "example.com", 80},
389 {"http\rmore", "example.com", 80},
390 {"http\n", "example.com", 80},
391 {"http\r", "example.com", 80},
392 {"http", "example.com\nnot-example.com", 80},
393 {"http", "example.com\rnot-example.com", 80},
394 {"http", "example.com\n", 80},
395 {"http", "example.com\r", 80},
396 {"http", "example.com", 0},
397 {"file", "", 80}};
398
399 for (const auto& test : cases) {
400 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
401 << test.port);
402 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization(
403 test.scheme, test.host, test.port);
404 EXPECT_EQ("", origin.scheme());
405 EXPECT_EQ("", origin.host());
406 EXPECT_EQ(0, origin.port());
407 EXPECT_TRUE(origin.unique());
408 EXPECT_FALSE(origin.IsSameOriginWith(origin));
csharrison048bee12016-10-04 00:08:21409
410 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL());
mkwstd8335d982015-07-25 05:18:48411 }
412}
413
414TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) {
415 struct TestCases {
416 const char* scheme;
417 size_t scheme_length;
418 const char* host;
419 size_t host_length;
avic0c60312015-12-21 21:03:50420 uint16_t port;
mkwstd8335d982015-07-25 05:18:48421 } cases[] = {{"http\0more", 9, "example.com", 11, 80},
422 {"http\0", 5, "example.com", 11, 80},
423 {"\0http", 5, "example.com", 11, 80},
424 {"http", 4, "example.com\0not-example.com", 27, 80},
425 {"http", 4, "example.com\0", 12, 80},
426 {"http", 4, "\0example.com", 12, 80}};
427
428 for (const auto& test : cases) {
429 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
430 << test.port);
431 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization(
432 std::string(test.scheme, test.scheme_length),
433 std::string(test.host, test.host_length), test.port);
434 EXPECT_EQ("", origin.scheme());
435 EXPECT_EQ("", origin.host());
436 EXPECT_EQ(0, origin.port());
437 EXPECT_TRUE(origin.unique());
438 EXPECT_FALSE(origin.IsSameOriginWith(origin));
csharrison048bee12016-10-04 00:08:21439
440 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL());
mkwstd8335d982015-07-25 05:18:48441 }
442}
443
pkalinnikov054f4032016-08-31 10:54:17444TEST(OriginTest, DomainIs) {
445 const struct {
446 const char* url;
447 const char* lower_ascii_domain;
448 bool expected_domain_is;
449 } kTestCases[] = {
450 {"http://google.com/foo", "google.com", true},
451 {"http://www.google.com:99/foo", "google.com", true},
452 {"http://www.google.com.cn/foo", "google.com", false},
453 {"http://www.google.comm", "google.com", false},
454 {"http://www.iamnotgoogle.com/foo", "google.com", false},
455 {"http://www.google.com/foo", "Google.com", false},
456
457 // If the host ends with a dot, it matches domains with or without a dot.
458 {"http://www.google.com./foo", "google.com", true},
459 {"http://www.google.com./foo", "google.com.", true},
460 {"http://www.google.com./foo", ".com", true},
461 {"http://www.google.com./foo", ".com.", true},
462
463 // But, if the host doesn't end with a dot and the input domain does, then
464 // it's considered to not match.
465 {"http://google.com/foo", "google.com.", false},
466
467 // If the host ends with two dots, it doesn't match.
468 {"http://www.google.com../foo", "google.com", false},
469
470 // Filesystem scheme.
471 {"filesystem:http://www.google.com:99/foo/", "google.com", true},
472 {"filesystem:http://www.iamnotgoogle.com/foo/", "google.com", false},
473
474 // File scheme.
475 {"file:///home/user/text.txt", "", false},
476 {"file:///home/user/text.txt", "txt", false},
477 };
478
479 for (const auto& test_case : kTestCases) {
480 SCOPED_TRACE(testing::Message() << "(url, domain): (" << test_case.url
481 << ", " << test_case.lower_ascii_domain
482 << ")");
483 GURL url(test_case.url);
484 ASSERT_TRUE(url.is_valid());
485 url::Origin origin(url);
486
487 EXPECT_EQ(test_case.expected_domain_is,
488 origin.DomainIs(test_case.lower_ascii_domain));
489 }
490
491 // If the URL is invalid, DomainIs returns false.
492 GURL invalid_url("google.com");
493 ASSERT_FALSE(invalid_url.is_valid());
494 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com"));
495
496 // Unique origins.
497 EXPECT_FALSE(url::Origin().DomainIs(""));
498 EXPECT_FALSE(url::Origin().DomainIs("com"));
499}
500
mkwst9f2cc892015-07-22 06:03:25501} // namespace url