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