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