OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "platform/weborigin/DatabaseIdentifier.h" | |
32 | |
33 #include "platform/weborigin/KURL.h" | |
34 #include "platform/weborigin/SecurityOrigin.h" | |
35 #include "testing/gtest/include/gtest/gtest.h" | |
36 | |
37 namespace blink { | |
38 | |
39 TEST(DatabaseIdentifierTest, CreateIdentifierFromSecurityOrigin) | |
40 { | |
41 struct OriginTestCase { | |
42 String protocol; | |
43 String host; | |
44 int port; | |
45 String expectedIdentifier; | |
46 } cases[] = { | |
47 {"http", "google.com", 80, "http_google.com_0"}, | |
48 {"https", "www.google.com", 443, "https_www.google.com_0"}, | |
49 {"http", "foo_bar_baz.org", 80, "http_foo_bar_baz.org_0"}, | |
50 {"http", "nondefaultport.net", 8001, "http_nondefaultport.net_8001"}, | |
51 {"http", "invalidportnumber.org", 70000, "__0"}, | |
52 {"http", "invalidportnumber.org", -5, "__0"}, | |
53 {"http", "%E2%98%83.unicode.com", 80, "http_xn--n3h.unicode.com_0"}, | |
54 {"http", String::fromUTF8("\xe2\x98\x83.unicode.com"), 80, "http_xn--n3h
.unicode.com_0"}, | |
55 {"http", String::fromUTF8("\xf0\x9f\x92\xa9.unicode.com"), 80, "http_xn-
-ls8h.unicode.com_0"}, | |
56 {"file", "", 0, "file__0"}, | |
57 {"data", "", 0, "__0"}, | |
58 {"about", "blank", 0, "__0"}, | |
59 {"non-standard", "foobar.com", 0, "non-standard__0"}, | |
60 {"http", "[::1]", 0, "http_[__1]_0"}, | |
61 {"http", "[3ffe:2a00:100:7031::1]", 0, "http_[3ffe_2a00_100_7031__1]_0"}
, | |
62 {"http", "[::ffff:8190:3426]", 0, "http_[__ffff_8190_3426]_0"}, | |
63 }; | |
64 | |
65 for (size_t i = 0; i < WTF_ARRAY_LENGTH(cases); ++i) { | |
66 RefPtr<SecurityOrigin> origin = SecurityOrigin::create(cases[i].protocol
, cases[i].host, cases[i].port); | |
67 String identifier = createDatabaseIdentifierFromSecurityOrigin(origin.ge
t()); | |
68 EXPECT_EQ(cases[i].expectedIdentifier, identifier) << "test case " << or
igin->toString(); | |
69 } | |
70 } | |
71 | |
72 // This tests the encoding of a hostname including every character in the range
[\x1f, \x80]. | |
73 TEST(DatabaseIdentifierTest, CreateIdentifierAllHostChars) | |
74 { | |
75 struct Case { | |
76 String hostname; | |
77 String expected; | |
78 bool shouldRoundTrip; | |
79 } cases[] = { | |
80 {"x\x1Fx", "__0", false}, | |
81 | |
82 {"x\x20x", "http_x%20x_0", false}, | |
83 {"x\x21x", "http_x%21x_0", false}, | |
84 {"x\x22x", "http_x%22x_0", false}, | |
85 {"x\x23x", "http_x_0", false}, // 'x#x', the # and following are ignored
. | |
86 {"x\x24x", "http_x%24x_0", false}, | |
87 {"x\x25x", "__0", false}, | |
88 {"x\x26x", "http_x%26x_0", false}, | |
89 {"x\x27x", "http_x%27x_0", false}, | |
90 {"x\x28x", "http_x%28x_0", false}, | |
91 {"x\x29x", "http_x%29x_0", false}, | |
92 {"x\x2ax", "http_x%2ax_0", false}, | |
93 {"x\x2bx", "http_x+x_0", false}, | |
94 {"x\x2cx", "http_x%2cx_0", false}, | |
95 {"x\x2dx", "http_x-x_0", true}, | |
96 {"x\x2ex", "http_x.x_0", true}, | |
97 {"x\x2fx", "http_x_0", false}, // 'x/x', the / and following are ignored
. | |
98 | |
99 {"x\x30x", "http_x0x_0", true}, | |
100 {"x\x31x", "http_x1x_0", true}, | |
101 {"x\x32x", "http_x2x_0", true}, | |
102 {"x\x33x", "http_x3x_0", true}, | |
103 {"x\x34x", "http_x4x_0", true}, | |
104 {"x\x35x", "http_x5x_0", true}, | |
105 {"x\x36x", "http_x6x_0", true}, | |
106 {"x\x37x", "http_x7x_0", true}, | |
107 {"x\x38x", "http_x8x_0", true}, | |
108 {"x\x39x", "http_x9x_0", true}, | |
109 {"x\x3ax", "__0", false}, | |
110 {"x\x3bx", "__0", false}, | |
111 {"x\x3cx", "http_x%3cx_0", false}, | |
112 {"x\x3dx", "http_x%3dx_0", false}, | |
113 {"x\x3ex", "http_x%3ex_0", false}, | |
114 {"x\x3fx", "http_x_0", false}, // 'x?x', the ? and following are ignored
. | |
115 | |
116 {"x\x40x", "http_x_0", false}, // 'x@x', the @ and following are ignored
. | |
117 {"x\x41x", "http_xax_0", true}, | |
118 {"x\x42x", "http_xbx_0", true}, | |
119 {"x\x43x", "http_xcx_0", true}, | |
120 {"x\x44x", "http_xdx_0", true}, | |
121 {"x\x45x", "http_xex_0", true}, | |
122 {"x\x46x", "http_xfx_0", true}, | |
123 {"x\x47x", "http_xgx_0", true}, | |
124 {"x\x48x", "http_xhx_0", true}, | |
125 {"x\x49x", "http_xix_0", true}, | |
126 {"x\x4ax", "http_xjx_0", true}, | |
127 {"x\x4bx", "http_xkx_0", true}, | |
128 {"x\x4cx", "http_xlx_0", true}, | |
129 {"x\x4dx", "http_xmx_0", true}, | |
130 {"x\x4ex", "http_xnx_0", true}, | |
131 {"x\x4fx", "http_xox_0", true}, | |
132 | |
133 {"x\x50x", "http_xpx_0", true}, | |
134 {"x\x51x", "http_xqx_0", true}, | |
135 {"x\x52x", "http_xrx_0", true}, | |
136 {"x\x53x", "http_xsx_0", true}, | |
137 {"x\x54x", "http_xtx_0", true}, | |
138 {"x\x55x", "http_xux_0", true}, | |
139 {"x\x56x", "http_xvx_0", true}, | |
140 {"x\x57x", "http_xwx_0", true}, | |
141 {"x\x58x", "http_xxx_0", true}, | |
142 {"x\x59x", "http_xyx_0", true}, | |
143 {"x\x5ax", "http_xzx_0", true}, | |
144 {"x\x5bx", "__0", false}, | |
145 {"x\x5cx", "http_x_0", false}, // "x\x", the \ and following are ignored
. | |
146 {"x\x5dx", "__0", false}, | |
147 {"x\x5ex", "__0", false}, | |
148 {"x\x5fx", "http_x_x_0", true}, | |
149 | |
150 {"x\x60x", "http_x%60x_0", false}, | |
151 {"x\x61x", "http_xax_0", true}, | |
152 {"x\x62x", "http_xbx_0", true}, | |
153 {"x\x63x", "http_xcx_0", true}, | |
154 {"x\x64x", "http_xdx_0", true}, | |
155 {"x\x65x", "http_xex_0", true}, | |
156 {"x\x66x", "http_xfx_0", true}, | |
157 {"x\x67x", "http_xgx_0", true}, | |
158 {"x\x68x", "http_xhx_0", true}, | |
159 {"x\x69x", "http_xix_0", true}, | |
160 {"x\x6ax", "http_xjx_0", true}, | |
161 {"x\x6bx", "http_xkx_0", true}, | |
162 {"x\x6cx", "http_xlx_0", true}, | |
163 {"x\x6dx", "http_xmx_0", true}, | |
164 {"x\x6ex", "http_xnx_0", true}, | |
165 {"x\x6fx", "http_xox_0", true}, | |
166 | |
167 {"x\x70x", "http_xpx_0", true}, | |
168 {"x\x71x", "http_xqx_0", true}, | |
169 {"x\x72x", "http_xrx_0", true}, | |
170 {"x\x73x", "http_xsx_0", true}, | |
171 {"x\x74x", "http_xtx_0", true}, | |
172 {"x\x75x", "http_xux_0", true}, | |
173 {"x\x76x", "http_xvx_0", true}, | |
174 {"x\x77x", "http_xwx_0", true}, | |
175 {"x\x78x", "http_xxx_0", true}, | |
176 {"x\x79x", "http_xyx_0", true}, | |
177 {"x\x7ax", "http_xzx_0", true}, | |
178 {"x\x7bx", "http_x%7bx_0", false}, | |
179 {"x\x7cx", "http_x%7cx_0", false}, | |
180 {"x\x7dx", "http_x%7dx_0", false}, | |
181 {"x\x7ex", "__0", false}, | |
182 {"x\x7fx", "__0", false}, | |
183 | |
184 {"x\x80x", "__0", false}, | |
185 }; | |
186 | |
187 for (size_t i = 0; i < WTF_ARRAY_LENGTH(cases); ++i) { | |
188 RefPtr<SecurityOrigin> origin = SecurityOrigin::create("http", cases[i].
hostname, 80); | |
189 String identifier = createDatabaseIdentifierFromSecurityOrigin(origin.ge
t()); | |
190 EXPECT_EQ(cases[i].expected, identifier) << "test case " << i << ": \""
<< cases[i].hostname << "\""; | |
191 if (cases[i].shouldRoundTrip) { | |
192 RefPtr<SecurityOrigin> parsedOrigin = createSecurityOriginFromDataba
seIdentifier(identifier); | |
193 EXPECT_EQ(cases[i].hostname.lower(), parsedOrigin->host()) << "test
case " << i << ": \"" << cases[i].hostname << "\""; | |
194 } | |
195 } | |
196 | |
197 } | |
198 | |
199 TEST(DatabaseIdentifierTest, CreateSecurityOriginFromIdentifier) | |
200 { | |
201 struct IdentifierTestCase { | |
202 String identifier; | |
203 String expectedProtocol; | |
204 String expectedHost; | |
205 int expectedPort; | |
206 String expectedStringRepresentation; | |
207 bool expectedUnique; | |
208 }; | |
209 | |
210 IdentifierTestCase validCases[] = { | |
211 {"http_google.com_0", "http", "google.com", 0, "http://google.com", fals
e}, | |
212 {"https_google.com_0", "https", "google.com", 0, "https://google.com", f
alse}, | |
213 {"ftp_google.com_0", "ftp", "google.com", 0, "ftp://google.com", false}, | |
214 {"unknown_google.com_0", "", "", 0, "null", true}, | |
215 {"http_nondefaultport.net_8001", "http", "nondefaultport.net", 8001, "ht
tp://nondefaultport.net:8001", false}, | |
216 {"file__0", "", "", 0, "null", true}, | |
217 {"__0", "", "", 0, "null", true}, | |
218 {"http_foo_bar_baz.org_0", "http", "foo_bar_baz.org", 0, "http://foo_bar
_baz.org", false}, | |
219 {"http_xn--n3h.unicode.com_0", "http", "xn--n3h.unicode.com", 0, "http:/
/xn--n3h.unicode.com", false}, | |
220 {"http_dot.com_0", "http", "dot.com", 0, "http://dot.com", false}, | |
221 {"http_escaped%3Dfun.com_0", "http", "escaped%3dfun.com", 0, "http://esc
aped%3dfun.com", false}, | |
222 }; | |
223 | |
224 for (size_t i = 0; i < WTF_ARRAY_LENGTH(validCases); ++i) { | |
225 RefPtr<SecurityOrigin> origin = createSecurityOriginFromDatabaseIdentifi
er(validCases[i].identifier); | |
226 EXPECT_EQ(validCases[i].expectedProtocol, origin->protocol()) << "test c
ase " << i; | |
227 EXPECT_EQ(validCases[i].expectedHost, origin->host()) << "test case " <<
i; | |
228 EXPECT_EQ(validCases[i].expectedPort, origin->port()) << "test case " <<
i; | |
229 EXPECT_EQ(validCases[i].expectedStringRepresentation, origin->toString()
) << "test case " << i; | |
230 EXPECT_EQ(validCases[i].expectedUnique, origin->isUnique()) << "test cas
e " << i; | |
231 } | |
232 | |
233 String bogusIdentifiers[] = { | |
234 "", "_", "__", | |
235 String("\x00", 1), | |
236 String("http_\x00_0", 8), | |
237 "ht\x7ctp_badprotocol.com_0", | |
238 "http_unescaped_percent_%.com_0", | |
239 "http_port_too_big.net_75000", | |
240 "http_port_too_small.net_-25", | |
241 "http_shouldbeescaped\x7c.com_0", | |
242 "http_latin1\x8a.org_8001", | |
243 String::fromUTF8("http_\xe2\x98\x83.unicode.com_0"), | |
244 "http_dot%252ecom_0", | |
245 "HtTp_NonCanonicalRepresenTation_0", | |
246 "http_non_ascii.\xa1.com_0", | |
247 "http_not_canonical_escape%3d_0", | |
248 }; | |
249 | |
250 for (size_t i = 0; i < WTF_ARRAY_LENGTH(bogusIdentifiers); ++i) { | |
251 RefPtr<SecurityOrigin> origin = createSecurityOriginFromDatabaseIdentifi
er(bogusIdentifiers[i]); | |
252 EXPECT_EQ("null", origin->toString()) << "test case " << i; | |
253 EXPECT_EQ(true, origin->isUnique()) << "test case " << i; | |
254 } | |
255 } | |
256 | |
257 } // namespace blink | |
OLD | NEW |