Chromium Code Reviews| 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" |
| 11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 12 #include "url/origin.h" | 12 #include "url/origin.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 TEST(OriginTest, UniqueOriginComparison) { | 16 TEST(OriginTest, UniqueOriginComparison) { |
| 17 url::Origin unique_origin; | 17 url::Origin unique_origin; |
| 18 EXPECT_EQ("", unique_origin.scheme()); | 18 EXPECT_EQ("", unique_origin.scheme()); |
| 19 EXPECT_EQ("", unique_origin.host()); | 19 EXPECT_EQ("", unique_origin.host()); |
| 20 EXPECT_EQ(0, unique_origin.port()); | 20 EXPECT_EQ(0, unique_origin.port()); |
| 21 EXPECT_TRUE(unique_origin.unique()); | 21 EXPECT_TRUE(unique_origin.unique()); |
| 22 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); | 22 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); |
| 23 | 23 |
| 24 const char* const urls[] = {"data:text/html,Hello!", | 24 const char* const urls[] = {"data:text/html,Hello!", |
| 25 "javascript:alert(1)", | 25 "javascript:alert(1)", |
| 26 "file://example.com:443/etc/passwd", | 26 "file://example.com:443/etc/passwd", |
| 27 "yay", | |
| 28 "http::///invalid.example.com/"}; | 27 "http::///invalid.example.com/"}; |
| 29 | 28 |
| 30 for (const auto& test_url : urls) { | 29 for (const auto& test_url : urls) { |
| 31 SCOPED_TRACE(test_url); | 30 SCOPED_TRACE(test_url); |
| 32 GURL url(test_url); | 31 GURL url(test_url); |
| 33 url::Origin origin(url); | 32 url::Origin origin(url); |
| 34 EXPECT_EQ("", origin.scheme()); | 33 EXPECT_EQ("", origin.scheme()); |
| 35 EXPECT_EQ("", origin.host()); | 34 EXPECT_EQ("", origin.host()); |
| 36 EXPECT_EQ(0, origin.port()); | 35 EXPECT_EQ(0, origin.port()); |
| 37 EXPECT_TRUE(origin.unique()); | 36 EXPECT_TRUE(origin.unique()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 {"file://example.com/etc/passwd", "file", "example.com", 0}, | 78 {"file://example.com/etc/passwd", "file", "example.com", 0}, |
| 80 | 79 |
| 81 // Filesystem: | 80 // Filesystem: |
| 82 {"filesystem:http://example.com/type/", "http", "example.com", 80}, | 81 {"filesystem:http://example.com/type/", "http", "example.com", 80}, |
| 83 {"filesystem:http://example.com:123/type/", "http", "example.com", 123}, | 82 {"filesystem:http://example.com:123/type/", "http", "example.com", 123}, |
| 84 {"filesystem:https://example.com/type/", "https", "example.com", 443}, | 83 {"filesystem:https://example.com/type/", "https", "example.com", 443}, |
| 85 {"filesystem:https://example.com:123/type/", "https", "example.com", 123}, | 84 {"filesystem:https://example.com:123/type/", "https", "example.com", 123}, |
| 86 | 85 |
| 87 // Blob: | 86 // Blob: |
| 88 {"blob:http://example.com/guid-goes-here", "http", "example.com", 80}, | 87 {"blob:http://example.com/guid-goes-here", "http", "example.com", 80}, |
| 89 {"blob:http://example.com:123/guid-goes-here", "http", "example.com", 123} , | 88 {"blob:http://example.com:123/guid-goes-here", "http", "example.com", |
| 89 123}, | |
| 90 {"blob:https://example.com/guid-goes-here", "https", "example.com", 443}, | 90 {"blob:https://example.com/guid-goes-here", "https", "example.com", 443}, |
| 91 {"blob:http://u:p@example.com/guid-goes-here", "http", "example.com", 80}, | 91 {"blob:http://u:p@example.com/guid-goes-here", "http", "example.com", 80}, |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 for (const auto& test_case : cases) { | 94 for (const auto& test_case : cases) { |
| 95 SCOPED_TRACE(test_case.url); | 95 SCOPED_TRACE(test_case.url); |
| 96 GURL url(test_case.url); | 96 GURL url(test_case.url); |
| 97 EXPECT_TRUE(url.is_valid()); | 97 EXPECT_TRUE(url.is_valid()); |
| 98 url::Origin origin(url); | 98 url::Origin origin(url); |
| 99 EXPECT_EQ(test_case.expected_scheme, origin.scheme()); | 99 EXPECT_EQ(test_case.expected_scheme, origin.scheme()); |
| 100 EXPECT_EQ(test_case.expected_host, origin.host()); | 100 EXPECT_EQ(test_case.expected_host, origin.host()); |
| 101 EXPECT_EQ(test_case.expected_port, origin.port()); | 101 EXPECT_EQ(test_case.expected_port, origin.port()); |
| 102 EXPECT_FALSE(origin.unique()); | 102 EXPECT_FALSE(origin.unique()); |
| 103 EXPECT_TRUE(origin.IsSameOriginWith(origin)); | 103 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 104 EXPECT_FALSE(different_origin.IsSameOriginWith(origin)); | 104 EXPECT_FALSE(different_origin.IsSameOriginWith(origin)); |
| 105 EXPECT_FALSE(origin.IsSameOriginWith(different_origin)); | 105 EXPECT_FALSE(origin.IsSameOriginWith(different_origin)); |
| 106 } | 106 } |
| 107 | |
| 108 const char* empties[] = {"yay", ""}; | |
|
Mike West
2016/03/04 06:09:56
Hrm. Why is "GURL('yay')" empty? I would have expe
palmer
2016/03/08 01:20:00
GURL("yay") is invalid and empty.
I'm not sure wh
| |
| 109 for (const auto& empty : empties) { | |
| 110 GURL test_url(empty); | |
| 111 url::Origin origin(test_url); | |
| 112 EXPECT_EQ("", origin.scheme()); | |
| 113 EXPECT_EQ("", origin.host()); | |
| 114 EXPECT_EQ(0, origin.port()); | |
| 115 EXPECT_TRUE(origin.empty()); | |
| 116 } | |
| 107 } | 117 } |
| 108 | 118 |
| 109 TEST(OriginTest, Serialization) { | 119 TEST(OriginTest, Serialization) { |
| 110 struct TestCases { | 120 struct TestCases { |
| 111 const char* const url; | 121 const char* const url; |
| 112 const char* const expected; | 122 const char* const expected; |
| 113 } cases[] = { | 123 } cases[] = { |
| 114 {"http://192.168.9.1/", "http://192.168.9.1"}, | 124 {"http://192.168.9.1/", "http://192.168.9.1"}, |
| 115 {"http://[2001:db8::1]/", "http://[2001:db8::1]"}, | 125 {"http://[2001:db8::1]/", "http://[2001:db8::1]"}, |
| 116 {"http://☃.net/", "http://xn--n3h.net"}, | 126 {"http://☃.net/", "http://xn--n3h.net"}, |
| 117 {"http://example.com/", "http://example.com"}, | 127 {"http://example.com/", "http://example.com"}, |
| 118 {"http://example.com:123/", "http://example.com:123"}, | 128 {"http://example.com:123/", "http://example.com:123"}, |
| 119 {"https://example.com/", "https://example.com"}, | 129 {"https://example.com/", "https://example.com"}, |
| 120 {"https://example.com:123/", "https://example.com:123"}, | 130 {"https://example.com:123/", "https://example.com:123"}, |
| 121 {"file:///etc/passwd", "file://"}, | 131 {"file:///etc/passwd", "file://"}, |
| 122 {"file://example.com/etc/passwd", "file://"}, | 132 {"file://example.com/etc/passwd", "file://"}, |
| 133 {"data:uniqueness", "null"}, | |
| 123 }; | 134 }; |
| 124 | 135 |
| 125 for (const auto& test_case : cases) { | 136 for (const auto& test_case : cases) { |
| 126 SCOPED_TRACE(test_case.url); | 137 SCOPED_TRACE(test_case.url); |
| 127 GURL url(test_case.url); | 138 GURL url(test_case.url); |
| 128 EXPECT_TRUE(url.is_valid()); | 139 EXPECT_TRUE(url.is_valid()); |
| 129 url::Origin origin(url); | 140 url::Origin origin(url); |
| 130 EXPECT_EQ(test_case.expected, origin.Serialize()); | 141 EXPECT_EQ(test_case.expected, origin.Serialize()); |
| 131 | 142 |
| 132 // The '<<' operator should produce the same serialization as Serialize(). | 143 // The '<<' operator should produce the same serialization as Serialize(). |
| 133 std::stringstream out; | 144 std::stringstream out; |
| 134 out << origin; | 145 out << origin; |
| 135 EXPECT_EQ(test_case.expected, out.str()); | 146 EXPECT_EQ(test_case.expected, out.str()); |
| 136 } | 147 } |
| 148 | |
| 149 GURL empty_url(""); | |
| 150 EXPECT_FALSE(empty_url.is_valid()); | |
| 151 url::Origin empty_origin(empty_url); | |
| 152 EXPECT_EQ("", empty_origin.Serialize()); | |
| 153 std::stringstream out; | |
| 154 out << empty_origin; | |
| 155 EXPECT_EQ("", out.str()); | |
| 137 } | 156 } |
| 138 | 157 |
| 139 TEST(OriginTest, Comparison) { | 158 TEST(OriginTest, Comparison) { |
| 140 // These URLs are arranged in increasing order: | 159 // These URLs are arranged in increasing order: |
| 141 const char* const urls[] = { | 160 const char* const urls[] = { |
| 142 "data:uniqueness", | 161 "data:uniqueness", |
| 143 "http://a:80", | 162 "http://a:80", |
| 144 "http://b:80", | 163 "http://b:80", |
| 145 "https://a:80", | 164 "https://a:80", |
| 146 "https://b:80", | 165 "https://b:80", |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 EXPECT_FALSE(origin.unique()); | 206 EXPECT_FALSE(origin.unique()); |
| 188 EXPECT_TRUE(origin.IsSameOriginWith(origin)); | 207 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 189 } | 208 } |
| 190 } | 209 } |
| 191 | 210 |
| 192 TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) { | 211 TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) { |
| 193 struct TestCases { | 212 struct TestCases { |
| 194 const char* scheme; | 213 const char* scheme; |
| 195 const char* host; | 214 const char* host; |
| 196 uint16_t port; | 215 uint16_t port; |
| 197 } cases[] = {{"", "", 0}, | 216 } cases[] = {{"data", "", 0}, |
| 198 {"data", "", 0}, | |
| 199 {"blob", "", 0}, | 217 {"blob", "", 0}, |
| 200 {"filesystem", "", 0}, | 218 {"filesystem", "", 0}, |
| 201 {"data", "example.com", 80}, | 219 {"data", "example.com", 80}, |
| 202 {"http", "☃.net", 80}, | 220 {"http", "☃.net", 80}, |
| 203 {"http\nmore", "example.com", 80}, | 221 {"http\nmore", "example.com", 80}, |
| 204 {"http\rmore", "example.com", 80}, | 222 {"http\rmore", "example.com", 80}, |
| 205 {"http\n", "example.com", 80}, | 223 {"http\n", "example.com", 80}, |
| 206 {"http\r", "example.com", 80}, | 224 {"http\r", "example.com", 80}, |
| 207 {"http", "example.com\nnot-example.com", 80}, | 225 {"http", "example.com\nnot-example.com", 80}, |
| 208 {"http", "example.com\rnot-example.com", 80}, | 226 {"http", "example.com\rnot-example.com", 80}, |
| 209 {"http", "example.com\n", 80}, | 227 {"http", "example.com\n", 80}, |
| 210 {"http", "example.com\r", 80}, | 228 {"http", "example.com\r", 80}, |
| 211 {"http", "example.com", 0}, | 229 {"http", "example.com", 0}, |
| 212 {"file", "", 80}}; | 230 {"file", "", 80}}; |
| 213 | 231 |
| 214 for (const auto& test : cases) { | 232 for (const auto& test : cases) { |
| 215 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 233 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 216 << test.port); | 234 << test.port); |
| 217 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 235 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 218 test.scheme, test.host, test.port); | 236 test.scheme, test.host, test.port); |
| 219 EXPECT_EQ("", origin.scheme()); | 237 EXPECT_EQ("", origin.scheme()); |
| 220 EXPECT_EQ("", origin.host()); | 238 EXPECT_EQ("", origin.host()); |
| 221 EXPECT_EQ(0, origin.port()); | 239 EXPECT_EQ(0, origin.port()); |
| 222 EXPECT_TRUE(origin.unique()); | 240 EXPECT_TRUE(origin.unique()); |
| 223 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 241 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| 224 } | 242 } |
| 243 | |
| 244 url::Origin origin = | |
| 245 url::Origin::UnsafelyCreateOriginWithoutNormalization("", "", 0); | |
| 246 EXPECT_EQ("", origin.scheme()); | |
| 247 EXPECT_EQ("", origin.host()); | |
| 248 EXPECT_EQ(0, origin.port()); | |
| 249 EXPECT_TRUE(origin.unique()); | |
| 250 EXPECT_TRUE(origin.IsSameOriginWith(origin)); | |
|
Mike West
2016/03/04 06:09:56
Nit: `EXPECT_TRUE(origin.empty());`
palmer
2016/03/08 01:20:00
Done.
| |
| 225 } | 251 } |
| 226 | 252 |
| 227 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { | 253 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { |
| 228 struct TestCases { | 254 struct TestCases { |
| 229 const char* scheme; | 255 const char* scheme; |
| 230 size_t scheme_length; | 256 size_t scheme_length; |
| 231 const char* host; | 257 const char* host; |
| 232 size_t host_length; | 258 size_t host_length; |
| 233 uint16_t port; | 259 uint16_t port; |
| 234 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, | 260 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 246 std::string(test.host, test.host_length), test.port); | 272 std::string(test.host, test.host_length), test.port); |
| 247 EXPECT_EQ("", origin.scheme()); | 273 EXPECT_EQ("", origin.scheme()); |
| 248 EXPECT_EQ("", origin.host()); | 274 EXPECT_EQ("", origin.host()); |
| 249 EXPECT_EQ(0, origin.port()); | 275 EXPECT_EQ(0, origin.port()); |
| 250 EXPECT_TRUE(origin.unique()); | 276 EXPECT_TRUE(origin.unique()); |
| 251 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 277 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| 252 } | 278 } |
| 253 } | 279 } |
| 254 | 280 |
| 255 } // namespace url | 281 } // namespace url |
| OLD | NEW |