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

Side by Side Diff: url/origin_unittest.cc

Issue 1153763002: Hardening the 'url::Origin' implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More. Created 5 years, 6 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
« no previous file with comments | « url/origin.cc ('k') | url/url_constants.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "url/gurl.h"
6 #include "url/origin.h" 7 #include "url/origin.h"
7 8
8 namespace url { 9 namespace url {
9 10
10 namespace { 11 namespace {
11 12
12 // Each test examines the Origin is constructed correctly without 13 // Each test examines the Origin is constructed correctly without
13 // violating DCHECKs. 14 // violating DCHECKs.
14 TEST(OriginTest, constructEmpty) { 15 TEST(OriginTest, constructEmpty) {
15 Origin origin; 16 Origin origin;
16 EXPECT_EQ("null", origin.string()); 17 EXPECT_EQ("null", origin.serialize());
17 } 18 }
18 19
19 TEST(OriginTest, constructNull) { 20 TEST(OriginTest, constructNull) {
20 Origin origin("null"); 21 Origin origin("null");
21 EXPECT_EQ("null", origin.string()); 22 EXPECT_EQ("null", origin.serialize());
22 } 23 }
23 24
24 TEST(OriginTest, constructValidOrigin) { 25 TEST(OriginTest, constructValidOrigin) {
25 Origin origin("http://example.com:8080"); 26 Origin origin("http://example.com:8080");
26 EXPECT_EQ("http://example.com:8080", origin.string()); 27 EXPECT_EQ("http://example.com:8080", origin.serialize());
27 } 28 }
28 29
29 TEST(OriginTest, constructValidFileOrigin) { 30 TEST(OriginTest, constructValidFileOrigin) {
30 Origin origin("file://"); 31 Origin origin("file://");
31 EXPECT_EQ("file://", origin.string()); 32 EXPECT_EQ("file://", origin.serialize());
32 } 33 }
33 34
34 TEST(OriginTest, constructValidOriginWithoutPort) { 35 TEST(OriginTest, constructValidOriginWithoutPort) {
35 Origin origin("wss://example2.com"); 36 Origin origin("wss://example2.com");
36 EXPECT_EQ("wss://example2.com", origin.string()); 37 EXPECT_EQ("wss://example2.com", origin.serialize());
38 }
39
40 TEST(OriginTest, Constructors) {
41 struct TestCases {
42 const char* url;
43 const char* scheme;
44 const char* host;
45 unsigned short port;
46 bool unique;
47 bool valid;
48 const char* serialized;
49 } cases[] = {
50 // Invalid/unique URLs
51 {"", "", "", 0, true, false, "null"},
52 {"null", "", "", 0, true, false, "null"},
53 {"!", "", "", 0, true, false, "null"},
54 {"javascript:window.alert(\"hello,world\");",
55 "",
56 "",
57 0,
58 true,
59 true,
60 "null"},
61
62 // File URLs
63 {"file:///path/to/file", "file", "", 0, false, true, "file://"},
64 {"file:///path/to/another/file", "file", "", 0, false, true, "file://"},
65
66 // Webby URLs
67 {"http://example.com/",
68 "http",
69 "example.com",
70 80,
71 false,
72 true,
73 "http://example.com"},
74 {"http://example.com:80/",
75 "http",
76 "example.com",
77 80,
78 false,
79 true,
80 "http://example.com"},
81 {"http://example.com:81/",
82 "http",
83 "example.com",
84 81,
85 false,
86 true,
87 "http://example.com:81"},
88 {"http://user:pass@example.com:81/blah#baz",
89 "http",
90 "example.com",
91 81,
92 false,
93 true,
94 "http://example.com:81"},
95 {"https://example.com/",
96 "https",
97 "example.com",
98 443,
99 false,
100 true,
101 "https://example.com"},
102 {"https://example.com:443/",
103 "https",
104 "example.com",
105 443,
106 false,
107 true,
108 "https://example.com"},
109 {"https://example.com:444/",
110 "https",
111 "example.com",
112 444,
113 false,
114 true,
115 "https://example.com:444"},
116 {"https://user:pass@example.com:444/blah#baz",
117 "https",
118 "example.com",
119 444,
120 false,
121 true,
122 "https://example.com:444"},
123
124 // Inner URLs
125 {"filesystem:http://example.com/temporary/",
126 "http",
127 "example.com",
128 80,
129 false,
130 true,
131 "http://example.com"},
132 {"filesystem:http://user:pass@example.com:21/blah#baz",
133 "http",
134 "example.com",
135 21,
136 false,
137 true,
138 "http://example.com:21"}};
139
140 for (const auto& test : cases) {
141 SCOPED_TRACE(::testing::Message() << "Testing '" << test.url << "'.");
142 Origin origin_from_gurl(GURL(test.url));
143 Origin origin_from_string(test.url);
144 Origin origin_from_tuple(test.scheme, test.host, test.port);
145
146 EXPECT_EQ(!test.unique,
147 origin_from_gurl.IsSameOriginWith(origin_from_string));
148 EXPECT_EQ(!test.unique,
149 origin_from_string.IsSameOriginWith(origin_from_gurl));
150 EXPECT_EQ(!test.unique,
151 origin_from_tuple.IsSameOriginWith(origin_from_gurl));
152
153 EXPECT_EQ(test.scheme, origin_from_gurl.scheme());
154 EXPECT_EQ(test.host, origin_from_gurl.host());
155 EXPECT_EQ(test.port, origin_from_gurl.port());
156 EXPECT_EQ(test.serialized, origin_from_gurl.serialize());
157 EXPECT_EQ(test.valid, origin_from_gurl.is_valid());
158
159 EXPECT_EQ(test.scheme, origin_from_string.scheme());
160 EXPECT_EQ(test.host, origin_from_string.host());
161 EXPECT_EQ(test.port, origin_from_string.port());
162 EXPECT_EQ(test.serialized, origin_from_string.serialize());
163 EXPECT_EQ(test.valid, origin_from_gurl.is_valid());
164
165 EXPECT_EQ(test.scheme, origin_from_tuple.scheme());
166 EXPECT_EQ(test.host, origin_from_tuple.host());
167 EXPECT_EQ(test.port, origin_from_tuple.port());
168 EXPECT_EQ(test.serialized, origin_from_tuple.serialize());
169 }
37 } 170 }
38 171
39 } // namespace 172 } // namespace
40 173
41 } // namespace url 174 } // namespace url
OLDNEW
« no previous file with comments | « url/origin.cc ('k') | url/url_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698