Chromium Code Reviews| Index: url/origin_unittest.cc |
| diff --git a/url/origin_unittest.cc b/url/origin_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3d8e4d7b405a18ef6dd81c47d7c753350f28fa29 |
| --- /dev/null |
| +++ b/url/origin_unittest.cc |
| @@ -0,0 +1,150 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "url/origin.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| + |
| +TEST(OriginTest, uniqueOrigin) { |
|
Ryan Sleevi
2015/07/10 11:59:54
Not in Blink, so Chromium naming
TEST(OriginTest,
Mike West
2015/07/17 09:58:10
GRRRRR.
|
| + url::Origin uniqueOrigin; |
|
Ryan Sleevi
2015/07/10 11:59:54
unique_origin
Mike West
2015/07/17 09:58:09
GRRR.
|
| + EXPECT_EQ("", uniqueOrigin.scheme()); |
| + EXPECT_EQ("", uniqueOrigin.host()); |
| + EXPECT_EQ(0, uniqueOrigin.port()); |
| + EXPECT_TRUE(uniqueOrigin.unique()); |
| + EXPECT_FALSE(uniqueOrigin.IsSameOriginWith(uniqueOrigin)); |
| + |
| + const char* urls[] = {"data:text/html,Hello!", |
|
Ryan Sleevi
2015/07/10 11:59:54
const char* const urls[]
Mike West
2015/07/17 09:58:09
const char* const const_urls[] const
|
| + "javascript:alert(1)", |
| + "file://example.com:443/etc/passwd", |
| + "yay", |
| + "http::///invalid.example.com/"}; |
| + |
| + for (const auto& test : urls) { |
| + SCOPED_TRACE(test); |
| + GURL url(test); |
| + url::Origin origin(url); |
| + EXPECT_EQ("", origin.scheme()); |
| + EXPECT_EQ("", origin.host()); |
| + EXPECT_EQ(0, origin.port()); |
| + EXPECT_TRUE(origin.unique()); |
| + EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| + EXPECT_FALSE(uniqueOrigin.IsSameOriginWith(origin)); |
| + EXPECT_FALSE(origin.IsSameOriginWith(uniqueOrigin)); |
| + } |
| +} |
| + |
| +TEST(OriginTest, GURLConstruction) { |
|
Ryan Sleevi
2015/07/10 11:59:54
nit: TEST(OriginTest, ConstructFromGURL) {
|
| + url::Origin differentOrigin(GURL("https://not-in-the-list.test/")); |
|
Ryan Sleevi
2015/07/10 11:59:54
different_origin
|
| + |
| + struct TestCases { |
| + const char* url; |
|
Ryan Sleevi
2015/07/10 11:59:54
const char* const (and friends)
|
| + const char* scheme; |
| + const char* host; |
| + uint16 port; |
|
Ryan Sleevi
2015/07/10 11:59:54
These are all expected_scheme, expected_host, expe
|
| + } cases[] = { |
|
Ryan Sleevi
2015/07/10 11:59:54
Perhaps add some test cases for non-generic but re
|
| + // IP Addresses |
| + {"http://192.168.9.1/", "http", "192.168.9.1", 80}, |
| + {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80}, |
| + |
| + // Punycode |
| + {"http://☃.net/", "http", "xn--n3h.net", 80}, |
| + {"blob:http://☃.net/", "http", "xn--n3h.net", 80}, |
| + |
| + // Webby URLs |
|
Ryan Sleevi
2015/07/10 11:59:54
I cringe somewhat at the colloquial "webby" - "gen
Mike West
2015/07/17 09:58:09
You are no fun. :(
|
| + {"http://example.com/", "http", "example.com", 80}, |
| + {"http://example.com:123/", "http", "example.com", 123}, |
| + {"https://example.com/", "https", "example.com", 443}, |
| + {"https://example.com:123/", "https", "example.com", 123}, |
| + {"http://user:pass@example.com/", "http", "example.com", 80}, |
| + {"http://example.com:123/?query", "http", "example.com", 123}, |
| + {"https://example.com/#1234", "https", "example.com", 443}, |
| + {"https://u:p@example.com:123/?query#1234", "https", "example.com", 123}, |
| + |
| + // file: URLs |
| + {"file:///etc/passwd", "file", "", 0}, |
| + {"file://example.com/etc/passwd", "file", "example.com", 0}, |
| + |
| + // Filesystem: |
| + {"filesystem:http://example.com/type/", "http", "example.com", 80}, |
| + {"filesystem:http://example.com:123/type/", "http", "example.com", 123}, |
| + {"filesystem:https://example.com/type/", "https", "example.com", 443}, |
| + {"filesystem:https://example.com:123/type/", "https", "example.com", 123}, |
| + |
| + // Blob: |
| + {"blob:http://example.com/guid-goes-here", "http", "example.com", 80}, |
| + {"blob:http://example.com:123/guid-goes-here", "http", "example.com", 123}, |
| + {"blob:https://example.com/guid-goes-here", "https", "example.com", 443}, |
| + {"blob:http://u:p@example.com/guid-goes-here", "http", "example.com", 80}, |
| + }; |
| + |
| + for (const auto& test : cases) { |
| + SCOPED_TRACE(test.url); |
| + GURL url(test.url); |
|
Ryan Sleevi
2015/07/10 11:59:54
perhaps your test should make sure that the GURL i
|
| + url::Origin origin(url); |
| + EXPECT_EQ(test.scheme, origin.scheme()); |
| + EXPECT_EQ(test.host, origin.host()); |
| + EXPECT_EQ(test.port, origin.port()); |
| + EXPECT_FALSE(origin.unique()); |
| + EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| + EXPECT_FALSE(differentOrigin.IsSameOriginWith(origin)); |
| + EXPECT_FALSE(origin.IsSameOriginWith(differentOrigin)); |
| + } |
| +} |
| +/* |
| +TEST(SchemeHostPortTest, Serialization) { |
| + struct TestCases { |
| + const char* url; |
|
Ryan Sleevi
2015/07/10 11:59:54
const char* const (and friends)
|
| + const char* expected; |
| + } cases[] = { |
| + {"http://192.168.9.1/", "http://192.168.9.1"}, |
| + {"http://[2001:db8::1]/", "http://[2001:db8::1]"}, |
| + {"http://☃.net/", "http://xn--n3h.net"}, |
| + {"http://example.com/", "http://example.com"}, |
| + {"http://example.com:123/", "http://example.com:123"}, |
| + {"https://example.com/", "https://example.com"}, |
| + {"https://example.com:123/", "https://example.com:123"}, |
| + {"file:///etc/passwd", "file://"}, |
| + {"file://example.com/etc/passwd", "file://example.com"}, |
| + }; |
| + |
| + for (const auto& test : cases) { |
| + SCOPED_TRACE(test.url); |
| + GURL url(test.url); |
| + url::SchemeHostPort tuple(url); |
| + EXPECT_EQ(test.expected, tuple.Serialize()); |
| + } |
| +} |
| + |
| +TEST(SchemeHostPortTest, Comparison) { |
| + // These tuples are arranged in increasing order: |
| + struct SchemeHostPorts { |
| + const char* scheme; |
| + const char* host; |
| + uint16 port; |
| + } tuples[] = { |
| + {"http", "a", 80}, |
| + {"http", "a", 81}, |
| + {"http", "b", 80}, |
| + {"http", "b", 81}, |
| + {"https", "a", 80}, |
| + {"https", "a", 81}, |
| + {"https", "b", 80}, |
| + {"https", "b", 81}, |
| + }; |
| + |
| + for (size_t i = 0; i < arraysize(tuples); i++) { |
| + url::SchemeHostPort current(tuples[i].scheme, tuples[i].host, |
| + tuples[i].port); |
| + for (size_t j = 0; j < arraysize(tuples); j++) { |
| + url::SchemeHostPort toCompare(tuples[j].scheme, tuples[j].host, |
| + tuples[j].port); |
| + EXPECT_EQ(i < j, current < toCompare) << i << " < " << j; |
| + EXPECT_EQ(i > j, current > toCompare) << i << " > " << j; |
| + } |
| + } |
| +} |
| +*/ |
| +} // namespace url |