| 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> |
| 6 #include <stdint.h> |
| 7 |
| 5 #include "base/logging.h" | 8 #include "base/logging.h" |
| 6 #include "url/origin.h" | 9 #include "base/macros.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 12 #include "url/origin.h" |
| 9 | 13 |
| 10 namespace { | 14 namespace { |
| 11 | 15 |
| 12 TEST(OriginTest, UniqueOriginComparison) { | 16 TEST(OriginTest, UniqueOriginComparison) { |
| 13 url::Origin unique_origin; | 17 url::Origin unique_origin; |
| 14 EXPECT_EQ("", unique_origin.scheme()); | 18 EXPECT_EQ("", unique_origin.scheme()); |
| 15 EXPECT_EQ("", unique_origin.host()); | 19 EXPECT_EQ("", unique_origin.host()); |
| 16 EXPECT_EQ(0, unique_origin.port()); | 20 EXPECT_EQ(0, unique_origin.port()); |
| 17 EXPECT_TRUE(unique_origin.unique()); | 21 EXPECT_TRUE(unique_origin.unique()); |
| 18 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); | 22 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 37 } | 41 } |
| 38 } | 42 } |
| 39 | 43 |
| 40 TEST(OriginTest, ConstructFromGURL) { | 44 TEST(OriginTest, ConstructFromGURL) { |
| 41 url::Origin different_origin(GURL("https://not-in-the-list.test/")); | 45 url::Origin different_origin(GURL("https://not-in-the-list.test/")); |
| 42 | 46 |
| 43 struct TestCases { | 47 struct TestCases { |
| 44 const char* const url; | 48 const char* const url; |
| 45 const char* const expected_scheme; | 49 const char* const expected_scheme; |
| 46 const char* const expected_host; | 50 const char* const expected_host; |
| 47 const uint16 expected_port; | 51 const uint16_t expected_port; |
| 48 } cases[] = { | 52 } cases[] = { |
| 49 // IP Addresses | 53 // IP Addresses |
| 50 {"http://192.168.9.1/", "http", "192.168.9.1", 80}, | 54 {"http://192.168.9.1/", "http", "192.168.9.1", 80}, |
| 51 {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80}, | 55 {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80}, |
| 52 | 56 |
| 53 // Punycode | 57 // Punycode |
| 54 {"http://☃.net/", "http", "xn--n3h.net", 80}, | 58 {"http://☃.net/", "http", "xn--n3h.net", 80}, |
| 55 {"blob:http://☃.net/", "http", "xn--n3h.net", 80}, | 59 {"blob:http://☃.net/", "http", "xn--n3h.net", 80}, |
| 56 | 60 |
| 57 // Generic URLs | 61 // Generic URLs |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 EXPECT_EQ(i < j, current < to_compare) << i << " < " << j; | 159 EXPECT_EQ(i < j, current < to_compare) << i << " < " << j; |
| 156 EXPECT_EQ(j < i, to_compare < current) << j << " < " << i; | 160 EXPECT_EQ(j < i, to_compare < current) << j << " < " << i; |
| 157 } | 161 } |
| 158 } | 162 } |
| 159 } | 163 } |
| 160 | 164 |
| 161 TEST(OriginTest, UnsafelyCreate) { | 165 TEST(OriginTest, UnsafelyCreate) { |
| 162 struct TestCase { | 166 struct TestCase { |
| 163 const char* scheme; | 167 const char* scheme; |
| 164 const char* host; | 168 const char* host; |
| 165 uint16 port; | 169 uint16_t port; |
| 166 } cases[] = { | 170 } cases[] = { |
| 167 {"http", "example.com", 80}, | 171 {"http", "example.com", 80}, |
| 168 {"http", "example.com", 123}, | 172 {"http", "example.com", 123}, |
| 169 {"https", "example.com", 443}, | 173 {"https", "example.com", 443}, |
| 170 {"https", "example.com", 123}, | 174 {"https", "example.com", 123}, |
| 171 {"file", "", 0}, | 175 {"file", "", 0}, |
| 172 {"file", "example.com", 0}, | 176 {"file", "example.com", 0}, |
| 173 }; | 177 }; |
| 174 | 178 |
| 175 for (const auto& test : cases) { | 179 for (const auto& test : cases) { |
| 176 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 180 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 177 << test.port); | 181 << test.port); |
| 178 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 182 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 179 test.scheme, test.host, test.port); | 183 test.scheme, test.host, test.port); |
| 180 EXPECT_EQ(test.scheme, origin.scheme()); | 184 EXPECT_EQ(test.scheme, origin.scheme()); |
| 181 EXPECT_EQ(test.host, origin.host()); | 185 EXPECT_EQ(test.host, origin.host()); |
| 182 EXPECT_EQ(test.port, origin.port()); | 186 EXPECT_EQ(test.port, origin.port()); |
| 183 EXPECT_FALSE(origin.unique()); | 187 EXPECT_FALSE(origin.unique()); |
| 184 EXPECT_TRUE(origin.IsSameOriginWith(origin)); | 188 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 185 } | 189 } |
| 186 } | 190 } |
| 187 | 191 |
| 188 TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) { | 192 TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) { |
| 189 struct TestCases { | 193 struct TestCases { |
| 190 const char* scheme; | 194 const char* scheme; |
| 191 const char* host; | 195 const char* host; |
| 192 uint16 port; | 196 uint16_t port; |
| 193 } cases[] = {{"", "", 0}, | 197 } cases[] = {{"", "", 0}, |
| 194 {"data", "", 0}, | 198 {"data", "", 0}, |
| 195 {"blob", "", 0}, | 199 {"blob", "", 0}, |
| 196 {"filesystem", "", 0}, | 200 {"filesystem", "", 0}, |
| 197 {"data", "example.com", 80}, | 201 {"data", "example.com", 80}, |
| 198 {"http", "☃.net", 80}, | 202 {"http", "☃.net", 80}, |
| 199 {"http\nmore", "example.com", 80}, | 203 {"http\nmore", "example.com", 80}, |
| 200 {"http\rmore", "example.com", 80}, | 204 {"http\rmore", "example.com", 80}, |
| 201 {"http\n", "example.com", 80}, | 205 {"http\n", "example.com", 80}, |
| 202 {"http\r", "example.com", 80}, | 206 {"http\r", "example.com", 80}, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 219 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 223 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| 220 } | 224 } |
| 221 } | 225 } |
| 222 | 226 |
| 223 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { | 227 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { |
| 224 struct TestCases { | 228 struct TestCases { |
| 225 const char* scheme; | 229 const char* scheme; |
| 226 size_t scheme_length; | 230 size_t scheme_length; |
| 227 const char* host; | 231 const char* host; |
| 228 size_t host_length; | 232 size_t host_length; |
| 229 uint16 port; | 233 uint16_t port; |
| 230 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, | 234 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, |
| 231 {"http\0", 5, "example.com", 11, 80}, | 235 {"http\0", 5, "example.com", 11, 80}, |
| 232 {"\0http", 5, "example.com", 11, 80}, | 236 {"\0http", 5, "example.com", 11, 80}, |
| 233 {"http", 4, "example.com\0not-example.com", 27, 80}, | 237 {"http", 4, "example.com\0not-example.com", 27, 80}, |
| 234 {"http", 4, "example.com\0", 12, 80}, | 238 {"http", 4, "example.com\0", 12, 80}, |
| 235 {"http", 4, "\0example.com", 12, 80}}; | 239 {"http", 4, "\0example.com", 12, 80}}; |
| 236 | 240 |
| 237 for (const auto& test : cases) { | 241 for (const auto& test : cases) { |
| 238 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 242 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 239 << test.port); | 243 << test.port); |
| 240 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 244 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 241 std::string(test.scheme, test.scheme_length), | 245 std::string(test.scheme, test.scheme_length), |
| 242 std::string(test.host, test.host_length), test.port); | 246 std::string(test.host, test.host_length), test.port); |
| 243 EXPECT_EQ("", origin.scheme()); | 247 EXPECT_EQ("", origin.scheme()); |
| 244 EXPECT_EQ("", origin.host()); | 248 EXPECT_EQ("", origin.host()); |
| 245 EXPECT_EQ(0, origin.port()); | 249 EXPECT_EQ(0, origin.port()); |
| 246 EXPECT_TRUE(origin.unique()); | 250 EXPECT_TRUE(origin.unique()); |
| 247 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 251 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| 248 } | 252 } |
| 249 } | 253 } |
| 250 | 254 |
| 251 } // namespace url | 255 } // namespace url |
| OLD | NEW |