OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "url/origin.h" | |
6 #include "testing/gtest/include/gtest/gtest.h" | |
7 #include "url/gurl.h" | |
8 | |
9 namespace { | |
10 | |
11 TEST(OriginTest, UniqueOriginComparison) { | |
12 url::Origin unique_origin; | |
13 EXPECT_EQ("", unique_origin.scheme()); | |
14 EXPECT_EQ("", unique_origin.host()); | |
15 EXPECT_EQ(0, unique_origin.port()); | |
16 EXPECT_TRUE(unique_origin.unique()); | |
17 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); | |
18 | |
19 const char* const urls[] = {"data:text/html,Hello!", | |
20 "javascript:alert(1)", | |
21 "file://example.com:443/etc/passwd", | |
22 "yay", | |
23 "http::///invalid.example.com/"}; | |
24 | |
25 for (const auto& test : urls) { | |
Ryan Sleevi
2015/07/17 19:48:18
nit: test_url ?
| |
26 SCOPED_TRACE(test); | |
27 GURL url(test); | |
28 url::Origin origin(url); | |
29 EXPECT_EQ("", origin.scheme()); | |
30 EXPECT_EQ("", origin.host()); | |
31 EXPECT_EQ(0, origin.port()); | |
32 EXPECT_TRUE(origin.unique()); | |
33 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | |
34 EXPECT_FALSE(unique_origin.IsSameOriginWith(origin)); | |
35 EXPECT_FALSE(origin.IsSameOriginWith(unique_origin)); | |
36 } | |
37 } | |
38 | |
39 TEST(OriginTest, ConstructFromGURL) { | |
40 url::Origin different_origin(GURL("https://not-in-the-list.test/")); | |
41 | |
42 struct TestCases { | |
43 const char* const url; | |
44 const char* const expected_scheme; | |
45 const char* const expected_host; | |
46 const uint16 expected_port; | |
47 } cases[] = { | |
48 // IP Addresses | |
49 {"http://192.168.9.1/", "http", "192.168.9.1", 80}, | |
50 {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80}, | |
51 | |
52 // Punycode | |
53 {"http://☃.net/", "http", "xn--n3h.net", 80}, | |
54 {"blob:http://☃.net/", "http", "xn--n3h.net", 80}, | |
55 | |
56 // Generic URLs | |
57 {"http://example.com/", "http", "example.com", 80}, | |
58 {"http://example.com:123/", "http", "example.com", 123}, | |
59 {"https://example.com/", "https", "example.com", 443}, | |
60 {"https://example.com:123/", "https", "example.com", 123}, | |
61 {"http://user:pass@example.com/", "http", "example.com", 80}, | |
62 {"http://example.com:123/?query", "http", "example.com", 123}, | |
63 {"https://example.com/#1234", "https", "example.com", 443}, | |
64 {"https://u:p@example.com:123/?query#1234", "https", "example.com", 123}, | |
65 | |
66 // Registered URLs | |
67 {"ftp://example.com/", "ftp", "example.com", 21}, | |
68 {"gopher://example.com/", "gopher", "example.com", 70}, | |
69 {"ws://example.com/", "ws", "example.com", 80}, | |
70 {"wss://example.com/", "wss", "example.com", 443}, | |
71 | |
72 // file: URLs | |
73 {"file:///etc/passwd", "file", "", 0}, | |
74 {"file://example.com/etc/passwd", "file", "example.com", 0}, | |
75 | |
76 // Filesystem: | |
77 {"filesystem:http://example.com/type/", "http", "example.com", 80}, | |
78 {"filesystem:http://example.com:123/type/", "http", "example.com", 123}, | |
79 {"filesystem:https://example.com/type/", "https", "example.com", 443}, | |
80 {"filesystem:https://example.com:123/type/", "https", "example.com", 123}, | |
81 | |
82 // Blob: | |
83 {"blob:http://example.com/guid-goes-here", "http", "example.com", 80}, | |
84 {"blob:http://example.com:123/guid-goes-here", "http", "example.com", 123} , | |
85 {"blob:https://example.com/guid-goes-here", "https", "example.com", 443}, | |
86 {"blob:http://u:p@example.com/guid-goes-here", "http", "example.com", 80}, | |
87 }; | |
88 | |
89 for (const auto& test : cases) { | |
Ryan Sleevi
2015/07/17 19:48:18
nit: test_case ?
| |
90 SCOPED_TRACE(test.url); | |
91 GURL url(test.url); | |
92 EXPECT_TRUE(url.is_valid()); | |
93 url::Origin origin(url); | |
94 EXPECT_EQ(test.expected_scheme, origin.scheme()); | |
95 EXPECT_EQ(test.expected_host, origin.host()); | |
96 EXPECT_EQ(test.expected_port, origin.port()); | |
97 EXPECT_FALSE(origin.unique()); | |
98 EXPECT_TRUE(origin.IsSameOriginWith(origin)); | |
99 EXPECT_FALSE(different_origin.IsSameOriginWith(origin)); | |
100 EXPECT_FALSE(origin.IsSameOriginWith(different_origin)); | |
101 } | |
102 } | |
103 | |
104 TEST(OriginTest, Serialization) { | |
105 struct TestCases { | |
106 const char* const url; | |
107 const char* const expected; | |
108 } cases[] = { | |
109 {"http://192.168.9.1/", "http://192.168.9.1"}, | |
110 {"http://[2001:db8::1]/", "http://[2001:db8::1]"}, | |
111 {"http://☃.net/", "http://xn--n3h.net"}, | |
112 {"http://example.com/", "http://example.com"}, | |
113 {"http://example.com:123/", "http://example.com:123"}, | |
114 {"https://example.com/", "https://example.com"}, | |
115 {"https://example.com:123/", "https://example.com:123"}, | |
116 {"file:///etc/passwd", "file://"}, | |
117 {"file://example.com/etc/passwd", "file://"}, | |
118 }; | |
119 | |
120 for (const auto& test : cases) { | |
Ryan Sleevi
2015/07/17 19:48:18
nit: test_case ?
| |
121 SCOPED_TRACE(test.url); | |
122 GURL url(test.url); | |
123 EXPECT_TRUE(url.is_valid()); | |
124 url::Origin origin(url); | |
125 EXPECT_EQ(test.expected, origin.Serialize()); | |
126 } | |
127 } | |
128 | |
129 TEST(OriginTest, Comparison) { | |
130 // These URLs are arranged in increasing order: | |
131 const char* const urls[] = { | |
132 "data:uniqueness", | |
133 "http://a:80", | |
134 "http://b:80", | |
135 "https://a:80", | |
136 "https://b:80", | |
137 "http://a:81", | |
138 "http://b:81", | |
139 "https://a:81", | |
140 "https://b:81", | |
141 }; | |
142 | |
143 for (size_t i = 0; i < arraysize(urls); i++) { | |
144 GURL current_url(urls[i]); | |
145 url::Origin current(current_url); | |
146 for (size_t j = i; j < arraysize(urls); j++) { | |
147 GURL compare_url(urls[j]); | |
148 url::Origin to_compare(compare_url); | |
149 EXPECT_EQ(i < j, current < to_compare) << i << " < " << j; | |
150 EXPECT_EQ(j < i, to_compare < current) << j << " < " << i; | |
151 } | |
152 } | |
153 } | |
154 | |
155 } // namespace url | |
OLD | NEW |