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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 {"https://example.com:123/", "https://example.com:123"}, | 146 {"https://example.com:123/", "https://example.com:123"}, |
147 {"file:///etc/passwd", "file://"}, | 147 {"file:///etc/passwd", "file://"}, |
148 {"file://example.com/etc/passwd", "file://"}, | 148 {"file://example.com/etc/passwd", "file://"}, |
149 }; | 149 }; |
150 | 150 |
151 for (const auto& test_case : cases) { | 151 for (const auto& test_case : cases) { |
152 SCOPED_TRACE(test_case.url); | 152 SCOPED_TRACE(test_case.url); |
153 GURL url(test_case.url); | 153 GURL url(test_case.url); |
154 EXPECT_TRUE(url.is_valid()); | 154 EXPECT_TRUE(url.is_valid()); |
155 url::Origin origin(url); | 155 url::Origin origin(url); |
| 156 EXPECT_TRUE(origin.suborigin().empty()); |
156 std::string serialized = origin.Serialize(); | 157 std::string serialized = origin.Serialize(); |
| 158 std::string serialized_physical_origin = |
| 159 origin.GetPhysicalOrigin().Serialize(); |
157 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); | 160 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); |
158 | 161 |
159 EXPECT_EQ(test_case.expected, serialized); | 162 EXPECT_EQ(test_case.expected, serialized); |
| 163 EXPECT_EQ(test_case.expected, serialized_physical_origin); |
160 | 164 |
161 // The '<<' operator should produce the same serialization as Serialize(). | 165 // The '<<' operator should produce the same serialization as Serialize(). |
162 std::stringstream out; | 166 std::stringstream out; |
163 out << origin; | 167 out << origin; |
164 EXPECT_EQ(test_case.expected, out.str()); | 168 EXPECT_EQ(test_case.expected, out.str()); |
165 } | 169 } |
166 } | 170 } |
167 | 171 |
| 172 TEST(OriginTest, SuboriginSerialization) { |
| 173 struct TestCases { |
| 174 const char* const url; |
| 175 const char* const expected; |
| 176 const char* const expected_physical_origin; |
| 177 const char* const expected_suborigin; |
| 178 } cases[] = { |
| 179 {"http-so://foobar.example.com/", "http-so://foobar.example.com", |
| 180 "http://example.com", "foobar"}, |
| 181 {"http-so://foobar.example.com:123/", "http-so://foobar.example.com:123", |
| 182 "http://example.com:123", "foobar"}, |
| 183 {"https-so://foobar.example.com/", "https-so://foobar.example.com", |
| 184 "https://example.com", "foobar"}, |
| 185 {"https-so://foobar.example.com:123/", |
| 186 "https-so://foobar.example.com:123", "https://example.com:123", |
| 187 "foobar"}, |
| 188 {"http://example.com/", "http://example.com", "http://example.com", ""}, |
| 189 {"http-so://foobar.example.com/some/path", "http-so://foobar.example.com", |
| 190 "http://example.com", "foobar"}, |
| 191 {"http-so://foobar.example.com/some/path?query", |
| 192 "http-so://foobar.example.com", "http://example.com", "foobar"}, |
| 193 {"http-so://foobar.example.com/some/path#fragment", |
| 194 "http-so://foobar.example.com", "http://example.com", "foobar"}, |
| 195 {"http-so://foobar.example.com/some/path?query#fragment", |
| 196 "http-so://foobar.example.com", "http://example.com", "foobar"}, |
| 197 {"http-so://foobar.example.com:1234/some/path?query#fragment", |
| 198 "http-so://foobar.example.com:1234", "http://example.com:1234", |
| 199 "foobar"}, |
| 200 }; |
| 201 |
| 202 for (const auto& test_case : cases) { |
| 203 SCOPED_TRACE(test_case.url); |
| 204 GURL url(test_case.url); |
| 205 EXPECT_TRUE(url.is_valid()); |
| 206 url::Origin origin(url); |
| 207 std::string serialized = origin.Serialize(); |
| 208 std::string serialized_physical_origin = |
| 209 origin.GetPhysicalOrigin().Serialize(); |
| 210 EXPECT_FALSE(origin.unique()); |
| 211 EXPECT_EQ(test_case.expected_suborigin, origin.suborigin()); |
| 212 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); |
| 213 |
| 214 EXPECT_EQ(test_case.expected, serialized); |
| 215 EXPECT_EQ(test_case.expected_physical_origin, serialized_physical_origin); |
| 216 |
| 217 // The '<<' operator should produce the same serialization as Serialize(). |
| 218 std::stringstream out; |
| 219 out << origin; |
| 220 EXPECT_EQ(test_case.expected, out.str()); |
| 221 } |
| 222 |
| 223 const char* const failure_cases[] = { |
| 224 "http-so://.", "http-so://foo", "http-so://.foo", "http-so://foo.", |
| 225 "https-so://.", "https-so://foo", "https-so://.foo", "https-so://foo.", |
| 226 }; |
| 227 |
| 228 for (const auto& test_case : failure_cases) { |
| 229 SCOPED_TRACE(test_case); |
| 230 GURL url(test_case); |
| 231 EXPECT_TRUE(url.is_valid()); |
| 232 url::Origin origin(url); |
| 233 std::string serialized = origin.Serialize(); |
| 234 std::string serialized_physical_origin = |
| 235 origin.GetPhysicalOrigin().Serialize(); |
| 236 EXPECT_TRUE(origin.unique()); |
| 237 EXPECT_EQ("", origin.suborigin()); |
| 238 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); |
| 239 |
| 240 EXPECT_EQ("null", serialized); |
| 241 EXPECT_EQ("null", serialized_physical_origin); |
| 242 } |
| 243 } |
| 244 |
| 245 TEST(OriginTest, SuboriginIsSameOriginWith) { |
| 246 struct TestCases { |
| 247 const char* const url1; |
| 248 const char* const url2; |
| 249 bool is_same_origin; |
| 250 bool is_same_physical_origin; |
| 251 } cases[]{ |
| 252 {"http-so://foobar1.example.com/", "http-so://foobar1.example.com", true, |
| 253 true}, |
| 254 {"http-so://foobar2.example.com/", "https-so://foobar2.example.com", |
| 255 false, false}, |
| 256 {"http-so://foobar3.example.com/", "http://example.com", false, true}, |
| 257 {"https-so://foobar4.example.com/", "https-so://foobar4.example.com", |
| 258 true, true}, |
| 259 {"https-so://foobar5.example.com/", "https://example.com", false, true}, |
| 260 {"http-so://foobar6.example.com/", "http-so://bazbar.example.com", false, |
| 261 true}, |
| 262 {"http-so://foobar7.example.com/", "http-so://foobar7.google.com", false, |
| 263 false}, |
| 264 }; |
| 265 |
| 266 for (const auto& test_case : cases) { |
| 267 SCOPED_TRACE(test_case.url1); |
| 268 url::Origin origin1(GURL(test_case.url1)); |
| 269 url::Origin origin2(GURL(test_case.url2)); |
| 270 |
| 271 EXPECT_TRUE(origin1.IsSameOriginWith(origin1)); |
| 272 EXPECT_TRUE(origin2.IsSameOriginWith(origin2)); |
| 273 EXPECT_EQ(test_case.is_same_origin, origin1.IsSameOriginWith(origin2)); |
| 274 EXPECT_EQ(test_case.is_same_origin, origin2.IsSameOriginWith(origin1)); |
| 275 |
| 276 EXPECT_TRUE(origin1.IsSamePhysicalOriginWith(origin1)); |
| 277 EXPECT_TRUE(origin2.IsSamePhysicalOriginWith(origin2)); |
| 278 EXPECT_EQ(test_case.is_same_physical_origin, |
| 279 origin1.IsSamePhysicalOriginWith(origin2)); |
| 280 EXPECT_EQ(test_case.is_same_physical_origin, |
| 281 origin2.IsSamePhysicalOriginWith(origin1)); |
| 282 } |
| 283 } |
| 284 |
168 TEST(OriginTest, Comparison) { | 285 TEST(OriginTest, Comparison) { |
169 // These URLs are arranged in increasing order: | 286 // These URLs are arranged in increasing order: |
170 const char* const urls[] = { | 287 const char* const urls[] = { |
171 "data:uniqueness", | 288 "data:uniqueness", |
172 "http://a:80", | 289 "http://a:80", |
173 "http://b:80", | 290 "http://b:80", |
174 "https://a:80", | 291 "https://a:80", |
175 "https://b:80", | 292 "https://b:80", |
176 "http://a:81", | 293 "http://a:81", |
177 "http://b:81", | 294 "http://b:81", |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 GURL invalid_url("google.com"); | 455 GURL invalid_url("google.com"); |
339 ASSERT_FALSE(invalid_url.is_valid()); | 456 ASSERT_FALSE(invalid_url.is_valid()); |
340 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); | 457 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); |
341 | 458 |
342 // Unique origins. | 459 // Unique origins. |
343 EXPECT_FALSE(url::Origin().DomainIs("")); | 460 EXPECT_FALSE(url::Origin().DomainIs("")); |
344 EXPECT_FALSE(url::Origin().DomainIs("com")); | 461 EXPECT_FALSE(url::Origin().DomainIs("com")); |
345 } | 462 } |
346 | 463 |
347 } // namespace url | 464 } // namespace url |
OLD | NEW |