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

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

Powered by Google App Engine
This is Rietveld 408576698