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 "config.h"
32#include "platform/weborigin/DatabaseIdentifier.h"
33
34#include "platform/weborigin/KURL.h"
35#include "platform/weborigin/SecurityOrigin.h"
36#include "wtf/testing/WTFTestHelpers.h"
37
38#include <gtest/gtest.h>
39
40using WebCore::SecurityOrigin;
41using WebCore::createDatabaseIdentifierFromSecurityOrigin;
42using WebCore::createSecurityOriginFromDatabaseIdentifier;
43
44namespace {
45
46TEST(DatabaseIdentifierTest, CreateIdentifierFromSecurityOrigin)
47{
48    struct OriginTestCase {
49        String protocol;
50        String host;
51        int port;
52        String expectedIdentifier;
53    } cases[] = {
54        {"http", "google.com", 80, "http_google.com_0"},
55        {"https", "www.google.com", 443, "https_www.google.com_0"},
56        {"http", "foo_bar_baz.org", 80, "http_foo_bar_baz.org_0"},
57        {"http", "nondefaultport.net", 8001, "http_nondefaultport.net_8001"},
58        {"http", "invalidportnumber.org", 70000, "__0"},
59        {"http", "invalidportnumber.org", -5, "__0"},
60        {"http", "%E2%98%83.unicode.com", 80, "http_xn--n3h.unicode.com_0"},
61        {"http", String::fromUTF8("\xe2\x98\x83.unicode.com"), 80, "http_xn--n3h.unicode.com_0"},
62        {"http", String::fromUTF8("\xf0\x9f\x92\xa9.unicode.com"), 80, "http_xn--ls8h.unicode.com_0"},
63        {"file", "", 0, "file__0"},
64        {"data", "", 0, "__0"},
65        {"about", "blank", 0, "__0"},
66        {"non-standard", "foobar.com", 0, "non-standard__0"},
67    };
68
69    for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
70        RefPtr<SecurityOrigin> origin = SecurityOrigin::create(cases[i].protocol, cases[i].host, cases[i].port);
71        String identifier = createDatabaseIdentifierFromSecurityOrigin(origin.get());
72        EXPECT_EQ(cases[i].expectedIdentifier, identifier) << "test case " << origin->toString();
73    }
74}
75
76// This tests the encoding of a hostname including every character in the range [\x1f, \x80].
77TEST(DatabaseIdentifierTest, CreateIdentifierAllHostChars)
78{
79    struct Case {
80        String hostname;
81        String expected;
82        bool shouldRoundTrip;
83    } cases[] = {
84        {"x\x1Fx", "__0", false},
85
86        {"x\x20x", "http_x%20x_0", false},
87        {"x\x21x", "http_x%21x_0", false},
88        {"x\x22x", "http_x%22x_0", false},
89        {"x\x23x", "http_x_0", false}, // 'x#x', the # and following are ignored.
90        {"x\x24x", "http_x%24x_0", false},
91        {"x\x25x", "__0", false},
92        {"x\x26x", "http_x%26x_0", false},
93        {"x\x27x", "http_x%27x_0", false},
94        {"x\x28x", "http_x%28x_0", false},
95        {"x\x29x", "http_x%29x_0", false},
96        {"x\x2ax", "http_x%2ax_0", false},
97        {"x\x2bx", "http_x+x_0", false},
98        {"x\x2cx", "http_x%2cx_0", false},
99        {"x\x2dx", "http_x-x_0", true},
100        {"x\x2ex", "http_x.x_0", true},
101        {"x\x2fx", "http_x_0", false}, // 'x/x', the / and following are ignored.
102
103        {"x\x30x", "http_x0x_0", true},
104        {"x\x31x", "http_x1x_0", true},
105        {"x\x32x", "http_x2x_0", true},
106        {"x\x33x", "http_x3x_0", true},
107        {"x\x34x", "http_x4x_0", true},
108        {"x\x35x", "http_x5x_0", true},
109        {"x\x36x", "http_x6x_0", true},
110        {"x\x37x", "http_x7x_0", true},
111        {"x\x38x", "http_x8x_0", true},
112        {"x\x39x", "http_x9x_0", true},
113        {"x\x3ax", "__0", false},
114        {"x\x3bx", "__0", false},
115        {"x\x3cx", "http_x%3cx_0", false},
116        {"x\x3dx", "http_x%3dx_0", false},
117        {"x\x3ex", "http_x%3ex_0", false},
118        {"x\x3fx", "http_x_0", false}, // 'x?x', the ? and following are ignored.
119
120        {"x\x40x", "http_x_0", false}, // 'x@x', the @ and following are ignored.
121        {"x\x41x", "http_xax_0", true},
122        {"x\x42x", "http_xbx_0", true},
123        {"x\x43x", "http_xcx_0", true},
124        {"x\x44x", "http_xdx_0", true},
125        {"x\x45x", "http_xex_0", true},
126        {"x\x46x", "http_xfx_0", true},
127        {"x\x47x", "http_xgx_0", true},
128        {"x\x48x", "http_xhx_0", true},
129        {"x\x49x", "http_xix_0", true},
130        {"x\x4ax", "http_xjx_0", true},
131        {"x\x4bx", "http_xkx_0", true},
132        {"x\x4cx", "http_xlx_0", true},
133        {"x\x4dx", "http_xmx_0", true},
134        {"x\x4ex", "http_xnx_0", true},
135        {"x\x4fx", "http_xox_0", true},
136
137        {"x\x50x", "http_xpx_0", true},
138        {"x\x51x", "http_xqx_0", true},
139        {"x\x52x", "http_xrx_0", true},
140        {"x\x53x", "http_xsx_0", true},
141        {"x\x54x", "http_xtx_0", true},
142        {"x\x55x", "http_xux_0", true},
143        {"x\x56x", "http_xvx_0", true},
144        {"x\x57x", "http_xwx_0", true},
145        {"x\x58x", "http_xxx_0", true},
146        {"x\x59x", "http_xyx_0", true},
147        {"x\x5ax", "http_xzx_0", true},
148        {"x\x5bx", "__0", false},
149        {"x\x5cx", "http_x_0", false}, // "x\x", the \ and following are ignored.
150        {"x\x5dx", "__0", false},
151        {"x\x5ex", "__0", false},
152        {"x\x5fx", "http_x_x_0", true},
153
154        {"x\x60x", "http_x%60x_0", false},
155        {"x\x61x", "http_xax_0", true},
156        {"x\x62x", "http_xbx_0", true},
157        {"x\x63x", "http_xcx_0", true},
158        {"x\x64x", "http_xdx_0", true},
159        {"x\x65x", "http_xex_0", true},
160        {"x\x66x", "http_xfx_0", true},
161        {"x\x67x", "http_xgx_0", true},
162        {"x\x68x", "http_xhx_0", true},
163        {"x\x69x", "http_xix_0", true},
164        {"x\x6ax", "http_xjx_0", true},
165        {"x\x6bx", "http_xkx_0", true},
166        {"x\x6cx", "http_xlx_0", true},
167        {"x\x6dx", "http_xmx_0", true},
168        {"x\x6ex", "http_xnx_0", true},
169        {"x\x6fx", "http_xox_0", true},
170
171        {"x\x70x", "http_xpx_0", true},
172        {"x\x71x", "http_xqx_0", true},
173        {"x\x72x", "http_xrx_0", true},
174        {"x\x73x", "http_xsx_0", true},
175        {"x\x74x", "http_xtx_0", true},
176        {"x\x75x", "http_xux_0", true},
177        {"x\x76x", "http_xvx_0", true},
178        {"x\x77x", "http_xwx_0", true},
179        {"x\x78x", "http_xxx_0", true},
180        {"x\x79x", "http_xyx_0", true},
181        {"x\x7ax", "http_xzx_0", true},
182        {"x\x7bx", "http_x%7bx_0", false},
183        {"x\x7cx", "http_x%7cx_0", false},
184        {"x\x7dx", "http_x%7dx_0", false},
185        {"x\x7ex", "__0", false},
186        {"x\x7fx", "__0", false},
187
188        {"x\x80x", "__0", false},
189    };
190
191    for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
192        RefPtr<SecurityOrigin> origin = SecurityOrigin::create("http", cases[i].hostname, 80);
193        String identifier = createDatabaseIdentifierFromSecurityOrigin(origin.get());
194        EXPECT_EQ(cases[i].expected, identifier) << "test case " << i << ": \"" << cases[i].hostname << "\"";
195        if (cases[i].shouldRoundTrip) {
196            RefPtr<SecurityOrigin> parsedOrigin = createSecurityOriginFromDatabaseIdentifier(identifier);
197            EXPECT_EQ(cases[i].hostname.lower(), parsedOrigin->host()) << "test case " << i << ": \"" << cases[i].hostname << "\"";
198        }
199    }
200
201}
202
203TEST(DatabaseIdentifierTest, CreateSecurityOriginFromIdentifier)
204{
205    struct IdentifierTestCase {
206        String identifier;
207        String expectedProtocol;
208        String expectedHost;
209        int expectedPort;
210        String expectedStringRepresentation;
211        bool expectedUnique;
212    };
213
214    IdentifierTestCase validCases[] = {
215        {"http_google.com_0", "http", "google.com", 0, "http://google.com", false},
216        {"https_google.com_0", "https", "google.com", 0, "https://google.com", false},
217        {"ftp_google.com_0", "ftp", "google.com", 0, "ftp://google.com", false},
218        {"unknown_google.com_0", "", "", 0, "null", true},
219        {"http_nondefaultport.net_8001", "http", "nondefaultport.net", 8001, "http://nondefaultport.net:8001", false},
220        {"file__0", "", "", 0, "null", true},
221        {"__0", "", "", 0, "null", true},
222        {"http_foo_bar_baz.org_0", "http", "foo_bar_baz.org", 0, "http://foo_bar_baz.org", false},
223        {"http_xn--n3h.unicode.com_0", "http", "xn--n3h.unicode.com", 0, "http://xn--n3h.unicode.com", false},
224        {"http_dot.com_0", "http", "dot.com", 0, "http://dot.com", false},
225        {"http_escaped%3Dfun.com_0", "http", "escaped%3dfun.com", 0, "http://escaped%3dfun.com", false},
226    };
227
228    for (size_t i = 0; i < ARRAYSIZE_UNSAFE(validCases); ++i) {
229        RefPtr<SecurityOrigin> origin = createSecurityOriginFromDatabaseIdentifier(validCases[i].identifier);
230        EXPECT_EQ(validCases[i].expectedProtocol, origin->protocol()) << "test case " << i;
231        EXPECT_EQ(validCases[i].expectedHost, origin->host()) << "test case " << i;
232        EXPECT_EQ(validCases[i].expectedPort, origin->port()) << "test case " << i;
233        EXPECT_EQ(validCases[i].expectedStringRepresentation, origin->toString()) << "test case " << i;
234        EXPECT_EQ(validCases[i].expectedUnique, origin->isUnique()) << "test case " << i;
235    }
236
237    String bogusIdentifiers[] = {
238        "", "_", "__",
239        String("\x00", 1),
240        String("http_\x00_0", 8),
241        "ht\x7ctp_badprotocol.com_0",
242        "http_unescaped_percent_%.com_0",
243        "http_port_too_big.net_75000",
244        "http_port_too_small.net_-25",
245        "http_shouldbeescaped\x7c.com_0",
246        "http_latin1\x8a.org_8001",
247        String::fromUTF8("http_\xe2\x98\x83.unicode.com_0"),
248        "http_dot%252ecom_0",
249        "HtTp_NonCanonicalRepresenTation_0",
250        "http_non_ascii.\xa1.com_0",
251        "http_not_canonical_escape%3d_0",
252    };
253
254    for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bogusIdentifiers); ++i) {
255        RefPtr<SecurityOrigin> origin = createSecurityOriginFromDatabaseIdentifier(bogusIdentifiers[i]);
256        EXPECT_EQ("null", origin->toString()) << "test case " << i;
257        EXPECT_EQ(true, origin->isUnique()) << "test case " << i;
258    }
259}
260
261} // namespace
262
263