OLD | NEW |
1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 std::string(test.scheme, test.scheme_length), | 245 std::string(test.scheme, test.scheme_length), |
246 std::string(test.host, test.host_length), test.port); | 246 std::string(test.host, test.host_length), test.port); |
247 EXPECT_EQ("", origin.scheme()); | 247 EXPECT_EQ("", origin.scheme()); |
248 EXPECT_EQ("", origin.host()); | 248 EXPECT_EQ("", origin.host()); |
249 EXPECT_EQ(0, origin.port()); | 249 EXPECT_EQ(0, origin.port()); |
250 EXPECT_TRUE(origin.unique()); | 250 EXPECT_TRUE(origin.unique()); |
251 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 251 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
252 } | 252 } |
253 } | 253 } |
254 | 254 |
| 255 TEST(OriginTest, DomainIs) { |
| 256 const struct { |
| 257 const char* url; |
| 258 const char* lower_ascii_domain; |
| 259 bool expected_domain_is; |
| 260 } kTestCases[] = { |
| 261 {"http://google.com/foo", "google.com", true}, |
| 262 {"http://www.google.com:99/foo", "google.com", true}, |
| 263 {"http://www.google.com.cn/foo", "google.com", false}, |
| 264 {"http://www.google.comm", "google.com", false}, |
| 265 {"http://www.iamnotgoogle.com/foo", "google.com", false}, |
| 266 {"http://www.google.com/foo", "Google.com", false}, |
| 267 |
| 268 // If the host ends with a dot, it matches domains with or without a dot. |
| 269 {"http://www.google.com./foo", "google.com", true}, |
| 270 {"http://www.google.com./foo", "google.com.", true}, |
| 271 {"http://www.google.com./foo", ".com", true}, |
| 272 {"http://www.google.com./foo", ".com.", true}, |
| 273 |
| 274 // But, if the host doesn't end with a dot and the input domain does, then |
| 275 // it's considered to not match. |
| 276 {"http://google.com/foo", "google.com.", false}, |
| 277 |
| 278 // If the host ends with two dots, it doesn't match. |
| 279 {"http://www.google.com../foo", "google.com", false}, |
| 280 |
| 281 // Filesystem scheme. |
| 282 {"filesystem:http://www.google.com:99/foo/", "google.com", true}, |
| 283 {"filesystem:http://www.iamnotgoogle.com/foo/", "google.com", false}, |
| 284 |
| 285 // File scheme. |
| 286 {"file:///home/user/text.txt", "", false}, |
| 287 {"file:///home/user/text.txt", "txt", false}, |
| 288 }; |
| 289 |
| 290 for (const auto& test_case : kTestCases) { |
| 291 SCOPED_TRACE(testing::Message() << "(url, domain): (" << test_case.url |
| 292 << ", " << test_case.lower_ascii_domain |
| 293 << ")"); |
| 294 GURL url(test_case.url); |
| 295 ASSERT_TRUE(url.is_valid()); |
| 296 url::Origin origin(url); |
| 297 |
| 298 EXPECT_EQ(test_case.expected_domain_is, |
| 299 origin.DomainIs(test_case.lower_ascii_domain)); |
| 300 } |
| 301 |
| 302 // If the URL is invalid, DomainIs returns false. |
| 303 GURL invalid_url("google.com"); |
| 304 ASSERT_FALSE(invalid_url.is_valid()); |
| 305 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); |
| 306 |
| 307 // Unique origins. |
| 308 EXPECT_FALSE(url::Origin().DomainIs("")); |
| 309 EXPECT_FALSE(url::Origin().DomainIs("com")); |
| 310 } |
| 311 |
255 } // namespace url | 312 } // namespace url |
OLD | NEW |