blob: 6b79956142f74ba33106a347fabb37f8a212be64 [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);
Daniel Cheng88186bd52017-10-20 08:14:4655 url::Origin origin = url::Origin::Create(url);
mkwst9f2cc892015-07-22 06:03:2556 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) {
Daniel Cheng88186bd52017-10-20 08:14:46106 url::Origin different_origin =
107 url::Origin::Create(GURL("https://not-in-the-list.test/"));
mkwst9f2cc892015-07-22 06:03:25108
109 struct TestCases {
110 const char* const url;
111 const char* const expected_scheme;
112 const char* const expected_host;
avic0c60312015-12-21 21:03:50113 const uint16_t expected_port;
mkwst9f2cc892015-07-22 06:03:25114 } cases[] = {
115 // IP Addresses
116 {"http://192.168.9.1/", "http", "192.168.9.1", 80},
117 {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80},
118
119 // Punycode
120 {"http://☃.net/", "http", "xn--n3h.net", 80},
121 {"blob:http://☃.net/", "http", "xn--n3h.net", 80},
122
123 // Generic URLs
124 {"http://example.com/", "http", "example.com", 80},
125 {"http://example.com:123/", "http", "example.com", 123},
126 {"https://example.com/", "https", "example.com", 443},
127 {"https://example.com:123/", "https", "example.com", 123},
128 {"http://user:[email protected]/", "http", "example.com", 80},
129 {"http://example.com:123/?query", "http", "example.com", 123},
130 {"https://example.com/#1234", "https", "example.com", 443},
131 {"https://u:[email protected]:123/?query#1234", "https", "example.com", 123},
132
133 // Registered URLs
134 {"ftp://example.com/", "ftp", "example.com", 21},
135 {"gopher://example.com/", "gopher", "example.com", 70},
136 {"ws://example.com/", "ws", "example.com", 80},
137 {"wss://example.com/", "wss", "example.com", 443},
138
139 // file: URLs
140 {"file:///etc/passwd", "file", "", 0},
141 {"file://example.com/etc/passwd", "file", "example.com", 0},
142
143 // Filesystem:
144 {"filesystem:http://example.com/type/", "http", "example.com", 80},
145 {"filesystem:http://example.com:123/type/", "http", "example.com", 123},
146 {"filesystem:https://example.com/type/", "https", "example.com", 443},
147 {"filesystem:https://example.com:123/type/", "https", "example.com", 123},
148
149 // Blob:
150 {"blob:http://example.com/guid-goes-here", "http", "example.com", 80},
151 {"blob:http://example.com:123/guid-goes-here", "http", "example.com", 123},
152 {"blob:https://example.com/guid-goes-here", "https", "example.com", 443},
153 {"blob:http://u:[email protected]/guid-goes-here", "http", "example.com", 80},
154 };
155
156 for (const auto& test_case : cases) {
157 SCOPED_TRACE(test_case.url);
158 GURL url(test_case.url);
159 EXPECT_TRUE(url.is_valid());
Daniel Cheng88186bd52017-10-20 08:14:46160 url::Origin origin = url::Origin::Create(url);
mkwst9f2cc892015-07-22 06:03:25161 EXPECT_EQ(test_case.expected_scheme, origin.scheme());
162 EXPECT_EQ(test_case.expected_host, origin.host());
163 EXPECT_EQ(test_case.expected_port, origin.port());
164 EXPECT_FALSE(origin.unique());
165 EXPECT_TRUE(origin.IsSameOriginWith(origin));
166 EXPECT_FALSE(different_origin.IsSameOriginWith(origin));
167 EXPECT_FALSE(origin.IsSameOriginWith(different_origin));
csharrison048bee12016-10-04 00:08:21168
169 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL());
mkwst9f2cc892015-07-22 06:03:25170 }
171}
172
173TEST(OriginTest, Serialization) {
174 struct TestCases {
175 const char* const url;
176 const char* const expected;
177 } cases[] = {
178 {"http://192.168.9.1/", "http://192.168.9.1"},
179 {"http://[2001:db8::1]/", "http://[2001:db8::1]"},
180 {"http://☃.net/", "http://xn--n3h.net"},
181 {"http://example.com/", "http://example.com"},
182 {"http://example.com:123/", "http://example.com:123"},
183 {"https://example.com/", "https://example.com"},
184 {"https://example.com:123/", "https://example.com:123"},
185 {"file:///etc/passwd", "file://"},
186 {"file://example.com/etc/passwd", "file://"},
187 };
188
189 for (const auto& test_case : cases) {
190 SCOPED_TRACE(test_case.url);
191 GURL url(test_case.url);
192 EXPECT_TRUE(url.is_valid());
Daniel Cheng88186bd52017-10-20 08:14:46193 url::Origin origin = url::Origin::Create(url);
jww04480402016-10-25 02:50:33194 EXPECT_TRUE(origin.suborigin().empty());
csharrison048bee12016-10-04 00:08:21195 std::string serialized = origin.Serialize();
jww04480402016-10-25 02:50:33196 std::string serialized_physical_origin =
197 origin.GetPhysicalOrigin().Serialize();
csharrison048bee12016-10-04 00:08:21198 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL());
199
200 EXPECT_EQ(test_case.expected, serialized);
jww04480402016-10-25 02:50:33201 EXPECT_EQ(test_case.expected, serialized_physical_origin);
mkwst9f2cc892015-07-22 06:03:25202
203 // The '<<' operator should produce the same serialization as Serialize().
204 std::stringstream out;
205 out << origin;
206 EXPECT_EQ(test_case.expected, out.str());
207 }
208}
209
jww04480402016-10-25 02:50:33210TEST(OriginTest, SuboriginSerialization) {
211 struct TestCases {
212 const char* const url;
213 const char* const expected;
214 const char* const expected_physical_origin;
215 const char* const expected_suborigin;
216 } cases[] = {
217 {"http-so://foobar.example.com/", "http-so://foobar.example.com",
218 "http://example.com", "foobar"},
219 {"http-so://foobar.example.com:123/", "http-so://foobar.example.com:123",
220 "http://example.com:123", "foobar"},
221 {"https-so://foobar.example.com/", "https-so://foobar.example.com",
222 "https://example.com", "foobar"},
223 {"https-so://foobar.example.com:123/",
224 "https-so://foobar.example.com:123", "https://example.com:123",
225 "foobar"},
226 {"http://example.com/", "http://example.com", "http://example.com", ""},
227 {"http-so://foobar.example.com/some/path", "http-so://foobar.example.com",
228 "http://example.com", "foobar"},
229 {"http-so://foobar.example.com/some/path?query",
230 "http-so://foobar.example.com", "http://example.com", "foobar"},
231 {"http-so://foobar.example.com/some/path#fragment",
232 "http-so://foobar.example.com", "http://example.com", "foobar"},
233 {"http-so://foobar.example.com/some/path?query#fragment",
234 "http-so://foobar.example.com", "http://example.com", "foobar"},
235 {"http-so://foobar.example.com:1234/some/path?query#fragment",
236 "http-so://foobar.example.com:1234", "http://example.com:1234",
237 "foobar"},
238 };
239
240 for (const auto& test_case : cases) {
241 SCOPED_TRACE(test_case.url);
242 GURL url(test_case.url);
243 EXPECT_TRUE(url.is_valid());
Daniel Cheng88186bd52017-10-20 08:14:46244 url::Origin origin = url::Origin::Create(url);
jww04480402016-10-25 02:50:33245 std::string serialized = origin.Serialize();
246 std::string serialized_physical_origin =
247 origin.GetPhysicalOrigin().Serialize();
248 EXPECT_FALSE(origin.unique());
249 EXPECT_EQ(test_case.expected_suborigin, origin.suborigin());
250 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL());
251
252 EXPECT_EQ(test_case.expected, serialized);
253 EXPECT_EQ(test_case.expected_physical_origin, serialized_physical_origin);
254
255 // The '<<' operator should produce the same serialization as Serialize().
256 std::stringstream out;
257 out << origin;
258 EXPECT_EQ(test_case.expected, out.str());
259 }
260
261 const char* const failure_cases[] = {
262 "http-so://.", "http-so://foo", "http-so://.foo", "http-so://foo.",
263 "https-so://.", "https-so://foo", "https-so://.foo", "https-so://foo.",
264 };
265
vmpstr6d9996c82017-02-23 00:43:25266 for (auto* test_case : failure_cases) {
jww04480402016-10-25 02:50:33267 SCOPED_TRACE(test_case);
268 GURL url(test_case);
269 EXPECT_TRUE(url.is_valid());
Daniel Cheng88186bd52017-10-20 08:14:46270 url::Origin origin = url::Origin::Create(url);
jww04480402016-10-25 02:50:33271 std::string serialized = origin.Serialize();
272 std::string serialized_physical_origin =
273 origin.GetPhysicalOrigin().Serialize();
274 EXPECT_TRUE(origin.unique());
275 EXPECT_EQ("", origin.suborigin());
276 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL());
277
278 EXPECT_EQ("null", serialized);
279 EXPECT_EQ("null", serialized_physical_origin);
280 }
281}
282
283TEST(OriginTest, SuboriginIsSameOriginWith) {
284 struct TestCases {
285 const char* const url1;
286 const char* const url2;
287 bool is_same_origin;
288 bool is_same_physical_origin;
289 } cases[]{
290 {"http-so://foobar1.example.com/", "http-so://foobar1.example.com", true,
291 true},
292 {"http-so://foobar2.example.com/", "https-so://foobar2.example.com",
293 false, false},
294 {"http-so://foobar3.example.com/", "http://example.com", false, true},
295 {"https-so://foobar4.example.com/", "https-so://foobar4.example.com",
296 true, true},
297 {"https-so://foobar5.example.com/", "https://example.com", false, true},
298 {"http-so://foobar6.example.com/", "http-so://bazbar.example.com", false,
299 true},
300 {"http-so://foobar7.example.com/", "http-so://foobar7.google.com", false,
301 false},
302 };
303
304 for (const auto& test_case : cases) {
305 SCOPED_TRACE(test_case.url1);
Daniel Cheng88186bd52017-10-20 08:14:46306 url::Origin origin1 = url::Origin::Create(GURL(test_case.url1));
307 url::Origin origin2 = url::Origin::Create(GURL(test_case.url2));
jww04480402016-10-25 02:50:33308
309 EXPECT_TRUE(origin1.IsSameOriginWith(origin1));
310 EXPECT_TRUE(origin2.IsSameOriginWith(origin2));
311 EXPECT_EQ(test_case.is_same_origin, origin1.IsSameOriginWith(origin2));
312 EXPECT_EQ(test_case.is_same_origin, origin2.IsSameOriginWith(origin1));
313
314 EXPECT_TRUE(origin1.IsSamePhysicalOriginWith(origin1));
315 EXPECT_TRUE(origin2.IsSamePhysicalOriginWith(origin2));
316 EXPECT_EQ(test_case.is_same_physical_origin,
317 origin1.IsSamePhysicalOriginWith(origin2));
318 EXPECT_EQ(test_case.is_same_physical_origin,
319 origin2.IsSamePhysicalOriginWith(origin1));
320 }
321}
322
mkwst9f2cc892015-07-22 06:03:25323TEST(OriginTest, Comparison) {
324 // These URLs are arranged in increasing order:
325 const char* const urls[] = {
326 "data:uniqueness",
327 "http://a:80",
328 "http://b:80",
329 "https://a:80",
330 "https://b:80",
331 "http://a:81",
332 "http://b:81",
333 "https://a:81",
334 "https://b:81",
335 };
336
337 for (size_t i = 0; i < arraysize(urls); i++) {
338 GURL current_url(urls[i]);
Daniel Cheng88186bd52017-10-20 08:14:46339 url::Origin current = url::Origin::Create(current_url);
mkwst9f2cc892015-07-22 06:03:25340 for (size_t j = i; j < arraysize(urls); j++) {
341 GURL compare_url(urls[j]);
Daniel Cheng88186bd52017-10-20 08:14:46342 url::Origin to_compare = url::Origin::Create(compare_url);
mkwst9f2cc892015-07-22 06:03:25343 EXPECT_EQ(i < j, current < to_compare) << i << " < " << j;
344 EXPECT_EQ(j < i, to_compare < current) << j << " < " << i;
345 }
346 }
347}
348
mkwstd8335d982015-07-25 05:18:48349TEST(OriginTest, UnsafelyCreate) {
350 struct TestCase {
351 const char* scheme;
352 const char* host;
avic0c60312015-12-21 21:03:50353 uint16_t port;
mkwstd8335d982015-07-25 05:18:48354 } cases[] = {
355 {"http", "example.com", 80},
356 {"http", "example.com", 123},
357 {"https", "example.com", 443},
358 {"https", "example.com", 123},
359 {"file", "", 0},
360 {"file", "example.com", 0},
361 };
362
363 for (const auto& test : cases) {
364 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
365 << test.port);
366 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization(
mek92c259682017-05-09 18:01:06367 test.scheme, test.host, test.port, "");
mkwstd8335d982015-07-25 05:18:48368 EXPECT_EQ(test.scheme, origin.scheme());
369 EXPECT_EQ(test.host, origin.host());
370 EXPECT_EQ(test.port, origin.port());
371 EXPECT_FALSE(origin.unique());
372 EXPECT_TRUE(origin.IsSameOriginWith(origin));
csharrison048bee12016-10-04 00:08:21373
374 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL());
mkwstd8335d982015-07-25 05:18:48375 }
376}
377
378TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) {
379 struct TestCases {
380 const char* scheme;
381 const char* host;
avic0c60312015-12-21 21:03:50382 uint16_t port;
mkwstd8335d982015-07-25 05:18:48383 } cases[] = {{"", "", 0},
384 {"data", "", 0},
385 {"blob", "", 0},
386 {"filesystem", "", 0},
387 {"data", "example.com", 80},
388 {"http", "☃.net", 80},
389 {"http\nmore", "example.com", 80},
390 {"http\rmore", "example.com", 80},
391 {"http\n", "example.com", 80},
392 {"http\r", "example.com", 80},
393 {"http", "example.com\nnot-example.com", 80},
394 {"http", "example.com\rnot-example.com", 80},
395 {"http", "example.com\n", 80},
396 {"http", "example.com\r", 80},
397 {"http", "example.com", 0},
398 {"file", "", 80}};
399
400 for (const auto& test : cases) {
401 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
402 << test.port);
403 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization(
mek92c259682017-05-09 18:01:06404 test.scheme, test.host, test.port, "");
mkwstd8335d982015-07-25 05:18:48405 EXPECT_EQ("", origin.scheme());
406 EXPECT_EQ("", origin.host());
407 EXPECT_EQ(0, origin.port());
408 EXPECT_TRUE(origin.unique());
409 EXPECT_FALSE(origin.IsSameOriginWith(origin));
csharrison048bee12016-10-04 00:08:21410
411 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL());
mkwstd8335d982015-07-25 05:18:48412 }
413}
414
415TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) {
416 struct TestCases {
417 const char* scheme;
418 size_t scheme_length;
419 const char* host;
420 size_t host_length;
avic0c60312015-12-21 21:03:50421 uint16_t port;
mkwstd8335d982015-07-25 05:18:48422 } cases[] = {{"http\0more", 9, "example.com", 11, 80},
423 {"http\0", 5, "example.com", 11, 80},
424 {"\0http", 5, "example.com", 11, 80},
425 {"http", 4, "example.com\0not-example.com", 27, 80},
426 {"http", 4, "example.com\0", 12, 80},
427 {"http", 4, "\0example.com", 12, 80}};
428
429 for (const auto& test : cases) {
430 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
431 << test.port);
432 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization(
433 std::string(test.scheme, test.scheme_length),
mek92c259682017-05-09 18:01:06434 std::string(test.host, test.host_length), test.port, "");
mkwstd8335d982015-07-25 05:18:48435 EXPECT_EQ("", origin.scheme());
436 EXPECT_EQ("", origin.host());
437 EXPECT_EQ(0, origin.port());
438 EXPECT_TRUE(origin.unique());
439 EXPECT_FALSE(origin.IsSameOriginWith(origin));
csharrison048bee12016-10-04 00:08:21440
441 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL());
mkwstd8335d982015-07-25 05:18:48442 }
443}
444
pkalinnikov054f4032016-08-31 10:54:17445TEST(OriginTest, DomainIs) {
446 const struct {
447 const char* url;
448 const char* lower_ascii_domain;
449 bool expected_domain_is;
450 } kTestCases[] = {
451 {"http://google.com/foo", "google.com", true},
452 {"http://www.google.com:99/foo", "google.com", true},
453 {"http://www.google.com.cn/foo", "google.com", false},
454 {"http://www.google.comm", "google.com", false},
455 {"http://www.iamnotgoogle.com/foo", "google.com", false},
456 {"http://www.google.com/foo", "Google.com", false},
457
458 // If the host ends with a dot, it matches domains with or without a dot.
459 {"http://www.google.com./foo", "google.com", true},
460 {"http://www.google.com./foo", "google.com.", true},
461 {"http://www.google.com./foo", ".com", true},
462 {"http://www.google.com./foo", ".com.", true},
463
464 // But, if the host doesn't end with a dot and the input domain does, then
465 // it's considered to not match.
466 {"http://google.com/foo", "google.com.", false},
467
468 // If the host ends with two dots, it doesn't match.
469 {"http://www.google.com../foo", "google.com", false},
470
471 // Filesystem scheme.
472 {"filesystem:http://www.google.com:99/foo/", "google.com", true},
473 {"filesystem:http://www.iamnotgoogle.com/foo/", "google.com", false},
474
475 // File scheme.
476 {"file:///home/user/text.txt", "", false},
477 {"file:///home/user/text.txt", "txt", false},
478 };
479
480 for (const auto& test_case : kTestCases) {
481 SCOPED_TRACE(testing::Message() << "(url, domain): (" << test_case.url
482 << ", " << test_case.lower_ascii_domain
483 << ")");
484 GURL url(test_case.url);
485 ASSERT_TRUE(url.is_valid());
Daniel Cheng88186bd52017-10-20 08:14:46486 url::Origin origin = url::Origin::Create(url);
pkalinnikov054f4032016-08-31 10:54:17487
488 EXPECT_EQ(test_case.expected_domain_is,
489 origin.DomainIs(test_case.lower_ascii_domain));
490 }
491
492 // If the URL is invalid, DomainIs returns false.
493 GURL invalid_url("google.com");
494 ASSERT_FALSE(invalid_url.is_valid());
Daniel Cheng88186bd52017-10-20 08:14:46495 EXPECT_FALSE(url::Origin::Create(invalid_url).DomainIs("google.com"));
pkalinnikov054f4032016-08-31 10:54:17496
497 // Unique origins.
498 EXPECT_FALSE(url::Origin().DomainIs(""));
499 EXPECT_FALSE(url::Origin().DomainIs("com"));
500}
501
mkwst9f2cc892015-07-22 06:03:25502} // namespace url