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

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: Created 5 years, 7 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 const char* serialized;
47 } cases[] = {
48 // Invalid/unique URLs
49 {"", "", "", 0, true, "null"},
50 {"null", "", "", 0, true, "null"},
51 {"!", "", "", 0, true, "null"},
52 {"javascript:window.alert(\"hello,world\");", "", "", 0, true, "null"},
53
54 // File URLs
55 {"file:///path/to/file", "file", "", 0, false, "file://"},
56 {"file:///path/to/another/file", "file", "", 0, false, "file://"},
57
58 // Webby URLs
59 {"http://example.com/",
60 "http",
61 "example.com",
62 80,
63 false,
64 "http://example.com"},
65 {"http://example.com:80/",
66 "http",
67 "example.com",
68 80,
69 false,
70 "http://example.com"},
71 {"http://example.com:81/",
72 "http",
73 "example.com",
74 81,
75 false,
76 "http://example.com:81"},
77 {"http://user:pass@example.com:81/blah#baz",
78 "http",
79 "example.com",
80 81,
81 false,
82 "http://example.com:81"},
83 {"https://example.com/",
84 "https",
85 "example.com",
86 443,
87 false,
88 "https://example.com"},
89 {"https://example.com:443/",
90 "https",
91 "example.com",
92 443,
93 false,
94 "https://example.com"},
95 {"https://example.com:444/",
96 "https",
97 "example.com",
98 444,
99 false,
100 "https://example.com:444"},
101 {"https://user:pass@example.com:444/blah#baz",
102 "https",
103 "example.com",
104 444,
105 false,
106 "https://example.com:444"},
107
108 // Inner URLs
109 {"filesystem:http://example.com/temporary/",
110 "http",
111 "example.com",
112 80,
113 false,
114 "http://example.com"},
115 {"filesystem:http://user:pass@example.com:21/blah#baz",
116 "http",
117 "example.com",
118 21,
119 false,
120 "http://example.com:21"}};
121
122 for (const auto& test : cases) {
123 SCOPED_TRACE(test.url);
124 Origin origin_from_gurl(GURL(test.url));
125 Origin origin_from_string(test.url);
126 Origin origin_from_tuple(test.scheme, test.host, test.port);
127
128 EXPECT_EQ(!test.unique,
129 origin_from_gurl.IsSameOriginWith(origin_from_string));
130 EXPECT_EQ(!test.unique,
131 origin_from_string.IsSameOriginWith(origin_from_gurl));
132 EXPECT_EQ(!test.unique,
133 origin_from_tuple.IsSameOriginWith(origin_from_gurl));
134
135 EXPECT_EQ(test.scheme, origin_from_gurl.scheme());
136 EXPECT_EQ(test.host, origin_from_gurl.host());
137 EXPECT_EQ(test.port, origin_from_gurl.port());
138 EXPECT_EQ(test.unique, origin_from_gurl.unique());
139 EXPECT_EQ(test.serialized, origin_from_gurl.serialize());
140
141 EXPECT_EQ(test.scheme, origin_from_string.scheme());
142 EXPECT_EQ(test.host, origin_from_string.host());
143 EXPECT_EQ(test.port, origin_from_string.port());
144 EXPECT_EQ(test.unique, origin_from_string.unique());
145 EXPECT_EQ(test.serialized, origin_from_string.serialize());
146
147 EXPECT_EQ(test.scheme, origin_from_tuple.scheme());
148 EXPECT_EQ(test.host, origin_from_tuple.host());
149 EXPECT_EQ(test.port, origin_from_tuple.port());
150 EXPECT_EQ(test.unique, origin_from_tuple.unique());
151 EXPECT_EQ(test.serialized, origin_from_tuple.serialize());
152 }
37 } 153 }
38 154
39 } // namespace 155 } // namespace
40 156
41 } // namespace url 157 } // namespace url
OLDNEW
« url/origin.cc ('K') | « url/origin.cc ('k') | url/url_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698