Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / webkit / common / database / database_identifier_unittest.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "webkit/common/database/database_identifier.h"
6
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/gurl.h"
10
11 namespace webkit_database {
12 namespace {
13
14 TEST(DatabaseIdentifierTest, CreateIdentifierFromOrigin) {
15   struct OriginTestCase {
16     std::string origin;
17     std::string expectedIdentifier;
18   } cases[] = {
19     {"http://google.com", "http_google.com_0"},
20     {"http://google.com:80", "http_google.com_0"},
21     {"https://www.google.com", "https_www.google.com_0"},
22     {"https://www.google.com:443", "https_www.google.com_0"},
23     {"http://foo_bar_baz.org", "http_foo_bar_baz.org_0"},
24     {"http://nondefaultport.net:8001", "http_nondefaultport.net_8001"},
25     {"http://invalidportnumber.org:70000", "__0"},
26     {"http://invalidportnumber.org:-6", "__0"},
27     {"http://%E2%98%83.unicode.com", "http_xn--n3h.unicode.com_0"},
28     {"http://\xe2\x98\x83.unicode.com", "http_xn--n3h.unicode.com_0"},
29     {"http://\xf0\x9f\x92\xa9.unicode.com", "http_xn--ls8h.unicode.com_0"},
30     {"file:///", "file__0"},
31     {"data:", "__0"},
32     {"about:blank", "__0"},
33     {"non-standard://foobar.com", "__0"},
34   };
35
36   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
37     GURL origin(cases[i].origin);
38     DatabaseIdentifier identifier =
39         DatabaseIdentifier::CreateFromOrigin(origin);
40     EXPECT_EQ(cases[i].expectedIdentifier, identifier.ToString())
41         << "test case " << cases[i].origin;
42   }
43 }
44
45 // This tests the encoding of a hostname including every character in the range
46 // [\x1f, \x80].
47 TEST(DatabaseIdentifierTest, CreateIdentifierAllHostChars) {
48   struct Case {
49     std::string hostname;
50     std::string expected;
51     bool shouldRoundTrip;
52   } cases[] = {
53     {"x\x1Fx", "__0", false},
54     {"x\x20x", "http_x%20x_0", false},
55     {"x\x21x", "http_x%21x_0", false},
56     {"x\x22x", "http_x%22x_0", false},
57     {"x\x23x", "http_x_0", false},  // 'x#x', the # and following are ignored.
58     {"x\x24x", "http_x%24x_0", false},
59     {"x\x25x", "__0", false},
60     {"x\x26x", "http_x%26x_0", false},
61     {"x\x27x", "http_x%27x_0", false},
62     {"x\x28x", "http_x%28x_0", false},
63     {"x\x29x", "http_x%29x_0", false},
64     {"x\x2ax", "http_x%2ax_0", false},
65     {"x\x2bx", "http_x+x_0", false},
66     {"x\x2cx", "http_x%2cx_0", false},
67     {"x\x2dx", "http_x-x_0", true},
68     {"x\x2ex", "http_x.x_0", true},
69     {"x\x2fx", "http_x_0", false},  // 'x/x', the / and following are ignored.
70     {"x\x30x", "http_x0x_0", true},
71     {"x\x31x", "http_x1x_0", true},
72     {"x\x32x", "http_x2x_0", true},
73     {"x\x33x", "http_x3x_0", true},
74     {"x\x34x", "http_x4x_0", true},
75     {"x\x35x", "http_x5x_0", true},
76     {"x\x36x", "http_x6x_0", true},
77     {"x\x37x", "http_x7x_0", true},
78     {"x\x38x", "http_x8x_0", true},
79     {"x\x39x", "http_x9x_0", true},
80     {"x\x3ax", "__0", false},
81     {"x\x3bx", "__0", false},
82     {"x\x3cx", "http_x%3cx_0", false},
83     {"x\x3dx", "http_x%3dx_0", false},
84     {"x\x3ex", "http_x%3ex_0", false},
85     {"x\x3fx", "http_x_0", false},  // 'x?x', the ? and following are ignored.
86     {"x\x40x", "http_x_0", false},  // 'x@x', the @ and following are ignored.
87     {"x\x41x", "http_xax_0", true},
88     {"x\x42x", "http_xbx_0", true},
89     {"x\x43x", "http_xcx_0", true},
90     {"x\x44x", "http_xdx_0", true},
91     {"x\x45x", "http_xex_0", true},
92     {"x\x46x", "http_xfx_0", true},
93     {"x\x47x", "http_xgx_0", true},
94     {"x\x48x", "http_xhx_0", true},
95     {"x\x49x", "http_xix_0", true},
96     {"x\x4ax", "http_xjx_0", true},
97     {"x\x4bx", "http_xkx_0", true},
98     {"x\x4cx", "http_xlx_0", true},
99     {"x\x4dx", "http_xmx_0", true},
100     {"x\x4ex", "http_xnx_0", true},
101     {"x\x4fx", "http_xox_0", true},
102     {"x\x50x", "http_xpx_0", true},
103     {"x\x51x", "http_xqx_0", true},
104     {"x\x52x", "http_xrx_0", true},
105     {"x\x53x", "http_xsx_0", true},
106     {"x\x54x", "http_xtx_0", true},
107     {"x\x55x", "http_xux_0", true},
108     {"x\x56x", "http_xvx_0", true},
109     {"x\x57x", "http_xwx_0", true},
110     {"x\x58x", "http_xxx_0", true},
111     {"x\x59x", "http_xyx_0", true},
112     {"x\x5ax", "http_xzx_0", true},
113     {"x\x5bx", "__0", false},
114     {"x\x5cx", "http_x_0", false},  // "x\x", the \ and following are ignored.
115     {"x\x5dx", "__0", false},
116     {"x\x5ex", "__0", false},
117     {"x\x5fx", "http_x_x_0", true},
118     {"x\x60x", "http_x%60x_0", false},
119     {"x\x61x", "http_xax_0", true},
120     {"x\x62x", "http_xbx_0", true},
121     {"x\x63x", "http_xcx_0", true},
122     {"x\x64x", "http_xdx_0", true},
123     {"x\x65x", "http_xex_0", true},
124     {"x\x66x", "http_xfx_0", true},
125     {"x\x67x", "http_xgx_0", true},
126     {"x\x68x", "http_xhx_0", true},
127     {"x\x69x", "http_xix_0", true},
128     {"x\x6ax", "http_xjx_0", true},
129     {"x\x6bx", "http_xkx_0", true},
130     {"x\x6cx", "http_xlx_0", true},
131     {"x\x6dx", "http_xmx_0", true},
132     {"x\x6ex", "http_xnx_0", true},
133     {"x\x6fx", "http_xox_0", true},
134     {"x\x70x", "http_xpx_0", true},
135     {"x\x71x", "http_xqx_0", true},
136     {"x\x72x", "http_xrx_0", true},
137     {"x\x73x", "http_xsx_0", true},
138     {"x\x74x", "http_xtx_0", true},
139     {"x\x75x", "http_xux_0", true},
140     {"x\x76x", "http_xvx_0", true},
141     {"x\x77x", "http_xwx_0", true},
142     {"x\x78x", "http_xxx_0", true},
143     {"x\x79x", "http_xyx_0", true},
144     {"x\x7ax", "http_xzx_0", true},
145     {"x\x7bx", "http_x%7bx_0", false},
146     {"x\x7cx", "http_x%7cx_0", false},
147     {"x\x7dx", "http_x%7dx_0", false},
148     {"x\x7ex", "__0", false},
149     {"x\x7fx", "__0", false},
150     {"x\x80x", "__0", false},
151   };
152
153   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
154     GURL origin("http://" + cases[i].hostname);
155     DatabaseIdentifier identifier =
156         DatabaseIdentifier::CreateFromOrigin(origin);
157     EXPECT_EQ(cases[i].expected, identifier.ToString())
158         << "test case " << i << " :\"" << cases[i].hostname << "\"";
159     if (cases[i].shouldRoundTrip) {
160       DatabaseIdentifier parsed_identifier =
161           DatabaseIdentifier::Parse(identifier.ToString());
162       EXPECT_EQ(identifier.ToString(), parsed_identifier.ToString())
163           << "test case " << i << " :\"" << cases[i].hostname << "\"";
164     }
165   }
166 }
167
168 TEST(DatabaseIdentifierTest, ExtractOriginDataFromIdentifier) {
169   struct IdentifierTestCase {
170     std::string str;
171     std::string expected_scheme;
172     std::string expected_host;
173     int expected_port;
174     GURL expected_origin;
175     bool expected_unique;
176   };
177
178   IdentifierTestCase valid_cases[] = {
179     {"http_google.com_0",
180      "http", "google.com", 0, GURL("http://google.com"), false},
181     {"https_google.com_0",
182      "https", "google.com", 0, GURL("https://google.com"), false},
183     {"ftp_google.com_0",
184      "ftp", "google.com", 0, GURL("ftp://google.com"), false},
185     {"unknown_google.com_0",
186      "unknown", "", 0, GURL("unknown://"), false},
187     {"http_nondefaultport.net_8001",
188      "http", "nondefaultport.net", 8001,
189      GURL("http://nondefaultport.net:8001"), false},
190     {"file__0",
191      "", "", 0, GURL("file:///"), true},
192     {"__0",
193      "", "", 0, GURL(), true},
194     {"http_foo_bar_baz.org_0",
195      "http", "foo_bar_baz.org", 0, GURL("http://foo_bar_baz.org"), false},
196     {"http_xn--n3h.unicode.com_0",
197      "http", "xn--n3h.unicode.com", 0,
198       GURL("http://xn--n3h.unicode.com"), false},
199     {"http_dot.com_0", "http", "dot.com", 0, GURL("http://dot.com"), false},
200     {"http_escaped%3Dfun.com_0", "http", "escaped%3dfun.com", 0,
201       GURL("http://escaped%3dfun.com"), false},
202   };
203
204   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid_cases); ++i) {
205     DatabaseIdentifier identifier =
206         DatabaseIdentifier::Parse(valid_cases[i].str);
207     EXPECT_EQ(valid_cases[i].expected_scheme, identifier.scheme())
208         << "test case " << valid_cases[i].str;
209     EXPECT_EQ(valid_cases[i].expected_host, identifier.hostname())
210         << "test case " << valid_cases[i].str;
211     EXPECT_EQ(valid_cases[i].expected_port, identifier.port())
212         << "test case " << valid_cases[i].str;
213     EXPECT_EQ(valid_cases[i].expected_origin, identifier.ToOrigin())
214         << "test case " << valid_cases[i].str;
215     EXPECT_EQ(valid_cases[i].expected_unique, identifier.is_unique())
216         << "test case " << valid_cases[i].str;
217   }
218
219   std::string bogus_components[] = {
220     "", "_", "__", std::string("\x00", 1), std::string("http_\x00_0", 8),
221     "ht\x7ctp_badscheme.com_0", "http_unescaped_percent_%.com_0",
222     "http_port_too_big.net_75000", "http_port_too_small.net_-25",
223     "http_shouldbeescaped\x7c.com_0", "http_latin1\x8a.org_8001",
224     "http_\xe2\x98\x83.unicode.com_0",
225     "http_dot%252ecom_0",
226     "HtTp_NonCanonicalRepresenTation_0",
227     "http_non_ascii.\xa1.com_0",
228     "http_not_canonical_escape%3d_0",
229     "http_bytes_after_port_0abcd",
230   };
231
232   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bogus_components); ++i) {
233     DatabaseIdentifier identifier =
234         DatabaseIdentifier::Parse(bogus_components[i]);
235     EXPECT_EQ("__0", identifier.ToString())
236         << "test case " << bogus_components[i];
237     EXPECT_EQ(GURL("null"), identifier.ToOrigin())
238         << "test case " << bogus_components[i];
239     EXPECT_EQ(true, identifier.is_unique())
240         << "test case " << bogus_components[i];
241   }
242 }
243
244 }  // namespace
245 }  // namespace webkit_database