Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: url/origin_unittest.cc

Issue 2403713002: Add suborigin logic to url::Origin (Closed)
Patch Set: Rebase on ToT Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 = origin.SerializePhysicalOrigin();
157 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); 159 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL());
158 160
159 EXPECT_EQ(test_case.expected, serialized); 161 EXPECT_EQ(test_case.expected, serialized);
162 EXPECT_EQ(test_case.expected, serialized_physical_origin);
160 163
161 // The '<<' operator should produce the same serialization as Serialize(). 164 // The '<<' operator should produce the same serialization as Serialize().
162 std::stringstream out; 165 std::stringstream out;
163 out << origin; 166 out << origin;
164 EXPECT_EQ(test_case.expected, out.str()); 167 EXPECT_EQ(test_case.expected, out.str());
165 } 168 }
166 } 169 }
167 170
171 TEST(OriginTest, SuboriginSerialization) {
172 struct TestCases {
173 const char* const url;
174 const char* const expected;
175 const char* const expected_physical_origin;
176 const char* const expected_suborigin;
177 } cases[] = {
178 {"http-so://foobar.example.com/", "http-so://foobar.example.com",
179 "http://example.com", "foobar"},
180 {"http-so://foobar.example.com:123/", "http-so://foobar.example.com:123",
181 "http://example.com:123", "foobar"},
182 {"https-so://foobar.example.com/", "https-so://foobar.example.com",
183 "https://example.com", "foobar"},
184 {"https-so://foobar.example.com:123/",
185 "https-so://foobar.example.com:123", "https://example.com:123",
186 "foobar"},
187 {"http://example.com/", "http://example.com", "http://example.com", ""},
188 {"http-so://foobar.example.com/some/path", "http-so://foobar.example.com",
189 "http://example.com", "foobar"},
190 {"http-so://foobar.example.com/some/path?query",
191 "http-so://foobar.example.com", "http://example.com", "foobar"},
192 {"http-so://foobar.example.com/some/path#fragment",
193 "http-so://foobar.example.com", "http://example.com", "foobar"},
194 {"http-so://foobar.example.com/some/path?query#fragment",
195 "http-so://foobar.example.com", "http://example.com", "foobar"},
196 {"http-so://foobar.example.com:1234/some/path?query#fragment",
197 "http-so://foobar.example.com:1234", "http://example.com:1234",
198 "foobar"},
199 };
200
201 for (const auto& test_case : cases) {
202 SCOPED_TRACE(test_case.url);
203 GURL url(test_case.url);
204 EXPECT_TRUE(url.is_valid());
205 url::Origin origin(url);
206 std::string serialized = origin.Serialize();
207 std::string serialized_physical_origin = origin.SerializePhysicalOrigin();
208 EXPECT_FALSE(origin.unique());
209 EXPECT_EQ(test_case.expected_suborigin, origin.suborigin());
210 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL());
211
212 EXPECT_EQ(test_case.expected, serialized);
213 EXPECT_EQ(test_case.expected_physical_origin, serialized_physical_origin);
214
215 // The '<<' operator should produce the same serialization as Serialize().
216 std::stringstream out;
217 out << origin;
218 EXPECT_EQ(test_case.expected, out.str());
219 }
220
221 const char* const failure_cases[] = {
222 "http-so://.", "http-so://foo", "http-so://.foo", "http-so://foo.",
223 "https-so://.", "https-so://foo", "https-so://.foo", "https-so://foo.",
224 };
225
226 for (const auto& test_case : failure_cases) {
227 SCOPED_TRACE(test_case);
228 GURL url(test_case);
229 EXPECT_TRUE(url.is_valid());
230 url::Origin origin(url);
231 std::string serialized = origin.Serialize();
232 std::string serialized_physical_origin = origin.SerializePhysicalOrigin();
233 EXPECT_TRUE(origin.unique());
234 EXPECT_EQ("", origin.suborigin());
235 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL());
236
237 EXPECT_EQ("null", serialized);
238 EXPECT_EQ("null", serialized_physical_origin);
239 }
240 }
241
242 TEST(OriginTest, SuboriginIsSameOriginWith) {
243 struct TestCases {
244 const char* const url1;
245 const char* const url2;
246 bool is_same_origin;
247 bool is_same_physical_origin;
248 } cases[]{
249 {"http-so://foobar1.example.com/", "http-so://foobar1.example.com", true,
250 true},
251 {"http-so://foobar2.example.com/", "https-so://foobar2.example.com",
252 false, false},
253 {"http-so://foobar3.example.com/", "http://example.com", false, true},
254 {"https-so://foobar4.example.com/", "https-so://foobar4.example.com",
255 true, true},
256 {"https-so://foobar5.example.com/", "https://example.com", false, true},
257 {"http-so://foobar6.example.com/", "http-so://bazbar.example.com", false,
258 true},
259 {"http-so://foobar7.example.com/", "http-so://foobar7.google.com", false,
260 false},
261 };
262
263 for (const auto& test_case : cases) {
264 SCOPED_TRACE(test_case.url1);
265 url::Origin origin1(GURL(test_case.url1));
266 url::Origin origin2(GURL(test_case.url2));
267
268 EXPECT_TRUE(origin1.IsSameOriginWith(origin1));
269 EXPECT_TRUE(origin2.IsSameOriginWith(origin2));
270 EXPECT_EQ(test_case.is_same_origin, origin1.IsSameOriginWith(origin2));
271 EXPECT_EQ(test_case.is_same_origin, origin2.IsSameOriginWith(origin1));
272
273 EXPECT_TRUE(origin1.IsSamePhysicalOriginWith(origin1));
274 EXPECT_TRUE(origin2.IsSamePhysicalOriginWith(origin2));
275 EXPECT_EQ(test_case.is_same_physical_origin,
276 origin1.IsSamePhysicalOriginWith(origin2));
277 EXPECT_EQ(test_case.is_same_physical_origin,
278 origin2.IsSamePhysicalOriginWith(origin1));
279 }
280 }
281
168 TEST(OriginTest, Comparison) { 282 TEST(OriginTest, Comparison) {
169 // These URLs are arranged in increasing order: 283 // These URLs are arranged in increasing order:
170 const char* const urls[] = { 284 const char* const urls[] = {
171 "data:uniqueness", 285 "data:uniqueness",
172 "http://a:80", 286 "http://a:80",
173 "http://b:80", 287 "http://b:80",
174 "https://a:80", 288 "https://a:80",
175 "https://b:80", 289 "https://b:80",
176 "http://a:81", 290 "http://a:81",
177 "http://b:81", 291 "http://b:81",
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 GURL invalid_url("google.com"); 452 GURL invalid_url("google.com");
339 ASSERT_FALSE(invalid_url.is_valid()); 453 ASSERT_FALSE(invalid_url.is_valid());
340 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); 454 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com"));
341 455
342 // Unique origins. 456 // Unique origins.
343 EXPECT_FALSE(url::Origin().DomainIs("")); 457 EXPECT_FALSE(url::Origin().DomainIs(""));
344 EXPECT_FALSE(url::Origin().DomainIs("com")); 458 EXPECT_FALSE(url::Origin().DomainIs("com"));
345 } 459 }
346 460
347 } // namespace url 461 } // namespace url
OLDNEW
« url/origin.h ('K') | « url/origin.cc ('k') | url/url_canon_stdurl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698