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