Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: net/base/sdch_filter_unittest.cc

Issue 134903002: Split sdch_manager_unittest.cc out of sdch_filter_unittest.cc (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | net/base/sdch_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <limits.h> 5 #include <limits.h>
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 1097 matching lines...) Expand 10 before | Expand all | Expand 10 after
1108 Filter::FixupEncodingTypes(filter_context, &filter_types); 1108 Filter::FixupEncodingTypes(filter_context, &filter_types);
1109 ASSERT_EQ(filter_types.size(), 3u); 1109 ASSERT_EQ(filter_types.size(), 3u);
1110 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH_POSSIBLE); 1110 EXPECT_EQ(filter_types[0], Filter::FILTER_TYPE_SDCH_POSSIBLE);
1111 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH); 1111 EXPECT_EQ(filter_types[1], Filter::FILTER_TYPE_GZIP_HELPING_SDCH);
1112 EXPECT_EQ(filter_types[2], Filter::FILTER_TYPE_GZIP); 1112 EXPECT_EQ(filter_types[2], Filter::FILTER_TYPE_GZIP);
1113 1113
1114 // First try with a large buffer (larger than test input, or compressed data). 1114 // First try with a large buffer (larger than test input, or compressed data).
1115 filter_context.SetURL(url); 1115 filter_context.SetURL(url);
1116 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); 1116 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
1117 1117
1118
1119 // Verify that chained filter is waiting for data. 1118 // Verify that chained filter is waiting for data.
1120 char tiny_output_buffer[10]; 1119 char tiny_output_buffer[10];
1121 int tiny_output_size = sizeof(tiny_output_buffer); 1120 int tiny_output_size = sizeof(tiny_output_buffer);
1122 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA, 1121 EXPECT_EQ(Filter::FILTER_NEED_MORE_DATA,
1123 filter->ReadData(tiny_output_buffer, &tiny_output_size)); 1122 filter->ReadData(tiny_output_buffer, &tiny_output_size));
1124 1123
1125 size_t feed_block_size = 100; 1124 size_t feed_block_size = 100;
1126 size_t output_block_size = 100; 1125 size_t output_block_size = 100;
1127 std::string output; 1126 std::string output;
1128 EXPECT_TRUE(FilterTestData(double_gzip_compressed_sdch, feed_block_size, 1127 EXPECT_TRUE(FilterTestData(double_gzip_compressed_sdch, feed_block_size,
1129 output_block_size, filter.get(), &output)); 1128 output_block_size, filter.get(), &output));
1130 EXPECT_EQ(output, expanded_); 1129 EXPECT_EQ(output, expanded_);
1131 1130
1132 // Next try with a tiny buffer to cover edge effects. 1131 // Next try with a tiny buffer to cover edge effects.
1133 filter.reset(Filter::Factory(filter_types, filter_context)); 1132 filter.reset(Filter::Factory(filter_types, filter_context));
1134 1133
1135 feed_block_size = 1; 1134 feed_block_size = 1;
1136 output_block_size = 1; 1135 output_block_size = 1;
1137 output.clear(); 1136 output.clear();
1138 EXPECT_TRUE(FilterTestData(double_gzip_compressed_sdch, feed_block_size, 1137 EXPECT_TRUE(FilterTestData(double_gzip_compressed_sdch, feed_block_size,
1139 output_block_size, filter.get(), &output)); 1138 output_block_size, filter.get(), &output));
1140 EXPECT_EQ(output, expanded_); 1139 EXPECT_EQ(output, expanded_);
1141 } 1140 }
1142 1141
1143 TEST_F(SdchFilterTest, DomainSupported) {
1144 GURL google_url("http://www.google.com");
1145
1146 net::SdchManager::EnableSdchSupport(false);
1147 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(google_url));
1148 net::SdchManager::EnableSdchSupport(true);
1149 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(google_url));
1150 }
1151
1152 TEST_F(SdchFilterTest, DomainBlacklisting) {
1153 GURL test_url("http://www.test.com");
1154 GURL google_url("http://www.google.com");
1155
1156 SdchManager::BlacklistDomain(test_url);
1157 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(test_url));
1158 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(google_url));
1159
1160 SdchManager::BlacklistDomain(google_url);
1161 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(google_url));
1162 }
1163
1164 TEST_F(SdchFilterTest, DomainBlacklistingCaseSensitivity) {
1165 GURL test_url("http://www.TesT.com");
1166 GURL test2_url("http://www.tEst.com");
1167
1168 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(test_url));
1169 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(test2_url));
1170 SdchManager::BlacklistDomain(test_url);
1171 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(test2_url));
1172 }
1173
1174 TEST_F(SdchFilterTest, BlacklistingReset) {
1175 GURL gurl("http://mytest.DoMain.com");
1176 std::string domain(gurl.host());
1177
1178 SdchManager::ClearBlacklistings();
1179 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 0);
1180 EXPECT_EQ(SdchManager::BlacklistDomainExponential(domain), 0);
1181 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(gurl));
1182 }
1183
1184 TEST_F(SdchFilterTest, BlacklistingSingleBlacklist) {
1185 GURL gurl("http://mytest.DoMain.com");
1186 std::string domain(gurl.host());
1187 SdchManager::ClearBlacklistings();
1188
1189 SdchManager::Global()->BlacklistDomain(gurl);
1190 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 1);
1191 EXPECT_EQ(SdchManager::BlacklistDomainExponential(domain), 1);
1192
1193 // Check that any domain lookup reduces the blacklist counter.
1194 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(gurl));
1195 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 0);
1196 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(gurl));
1197 }
1198
1199 TEST_F(SdchFilterTest, BlacklistingExponential) {
1200 GURL gurl("http://mytest.DoMain.com");
1201 std::string domain(gurl.host());
1202 SdchManager::ClearBlacklistings();
1203
1204 int exponential = 1;
1205 for (int i = 1; i < 100; ++i) {
1206 SdchManager::Global()->BlacklistDomain(gurl);
1207 EXPECT_EQ(SdchManager::BlacklistDomainExponential(domain), exponential);
1208
1209 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), exponential);
1210 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(gurl));
1211 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), exponential - 1);
1212
1213 // Simulate a large number of domain checks (which eventually remove the
1214 // blacklisting).
1215 SdchManager::ClearDomainBlacklisting(domain);
1216 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 0);
1217 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(gurl));
1218
1219 // Predict what exponential backoff will be.
1220 exponential = 1 + 2 * exponential;
1221 if (exponential < 0)
1222 exponential = INT_MAX; // We don't wrap.
1223 }
1224 }
1225
1226 TEST_F(SdchFilterTest, CanSetExactMatchDictionary) {
1227 std::string dictionary_domain("x.y.z.google.com");
1228 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1229
1230 // Perfect match should work.
1231 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
1232 GURL("http://" + dictionary_domain)));
1233 }
1234
1235 TEST_F(SdchFilterTest, CanAdvertiseDictionaryOverHTTP) {
1236 std::string dictionary_domain("x.y.z.google.com");
1237 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1238
1239 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
1240 GURL("http://" + dictionary_domain)));
1241
1242 std::string dictionary_list;
1243 // HTTP target URL can advertise dictionary.
1244 sdch_manager_->GetAvailDictionaryList(
1245 GURL("http://" + dictionary_domain + "/test"),
1246 &dictionary_list);
1247 EXPECT_FALSE(dictionary_list.empty());
1248 }
1249
1250 TEST_F(SdchFilterTest, CanNotAdvertiseDictionaryOverHTTPS) {
1251 std::string dictionary_domain("x.y.z.google.com");
1252 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1253
1254 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
1255 GURL("http://" + dictionary_domain)));
1256
1257 std::string dictionary_list;
1258 // HTTPS target URL should NOT advertise dictionary.
1259 sdch_manager_->GetAvailDictionaryList(
1260 GURL("https://" + dictionary_domain + "/test"),
1261 &dictionary_list);
1262 EXPECT_TRUE(dictionary_list.empty());
1263 }
1264
1265 TEST_F(SdchFilterTest, CanUseHTTPSDictionaryOverHTTPSIfEnabled) {
1266 std::string dictionary_domain("x.y.z.google.com");
1267 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1268
1269 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
1270 GURL("https://" + dictionary_domain)));
1271
1272 GURL target_url("https://" + dictionary_domain + "/test");
1273 std::string dictionary_list;
1274 // HTTPS target URL should advertise dictionary if secure scheme support is
1275 // enabled.
1276 sdch_manager_->EnableSecureSchemeSupport(true);
1277 sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list);
1278 EXPECT_FALSE(dictionary_list.empty());
1279
1280 // Dictionary should be available.
1281 SdchManager::Dictionary* dictionary = NULL;
1282 std::string client_hash;
1283 std::string server_hash;
1284 sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash);
1285 sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary);
1286 EXPECT_TRUE(dictionary != NULL);
1287 }
1288
1289 TEST_F(SdchFilterTest, CanNotUseHTTPDictionaryOverHTTPS) {
1290 std::string dictionary_domain("x.y.z.google.com");
1291 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1292
1293 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
1294 GURL("http://" + dictionary_domain)));
1295
1296 GURL target_url("https://" + dictionary_domain + "/test");
1297 std::string dictionary_list;
1298 // HTTPS target URL should not advertise dictionary acquired over HTTP even if
1299 // secure scheme support is enabled.
1300 sdch_manager_->EnableSecureSchemeSupport(true);
1301 sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list);
1302 EXPECT_TRUE(dictionary_list.empty());
1303
1304 SdchManager::Dictionary* dictionary = NULL;
1305 std::string client_hash;
1306 std::string server_hash;
1307 sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash);
1308 sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary);
1309 EXPECT_TRUE(dictionary == NULL);
1310 }
1311
1312 TEST_F(SdchFilterTest, FailToSetDomainMismatchDictionary) {
1313 std::string dictionary_domain("x.y.z.google.com");
1314 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1315
1316 // Fail the "domain match" requirement.
1317 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text,
1318 GURL("http://y.z.google.com")));
1319 }
1320
1321 TEST_F(SdchFilterTest, FailToSetDotHostPrefixDomainDictionary) {
1322 std::string dictionary_domain("x.y.z.google.com");
1323 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1324
1325 // Fail the HD with D being the domain and H having a dot requirement.
1326 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text,
1327 GURL("http://w.x.y.z.google.com")));
1328 }
1329
1330 TEST_F(SdchFilterTest, FailToSetRepeatPrefixWithDotDictionary) {
1331 // Make sure that a prefix that matches the domain postfix won't confuse
1332 // the validation checks.
1333 std::string dictionary_domain("www.google.com");
1334 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1335
1336 // Fail the HD with D being the domain and H having a dot requirement.
1337 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text,
1338 GURL("http://www.google.com.www.google.com")));
1339 }
1340
1341 TEST_F(SdchFilterTest, CanSetLeadingDotDomainDictionary) {
1342 // Make sure that a prefix that matches the domain postfix won't confuse
1343 // the validation checks.
1344 std::string dictionary_domain(".google.com");
1345 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1346
1347 // Verify that a leading dot in the domain is acceptable, as long as the host
1348 // name does not contain any dots preceding the matched domain name.
1349 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
1350 GURL("http://www.google.com")));
1351 }
1352
1353 // Make sure the order of the tests is not helping us or confusing things.
1354 // See test CanSetExactMatchDictionary above for first try.
1355 TEST_F(SdchFilterTest, CanStillSetExactMatchDictionary) {
1356 std::string dictionary_domain("x.y.z.google.com");
1357 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1358
1359 // Perfect match should *STILL* work.
1360 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
1361 GURL("http://" + dictionary_domain)));
1362 }
1363
1364 // Make sure the DOS protection precludes the addition of too many dictionaries.
1365 TEST_F(SdchFilterTest, TooManyDictionaries) {
1366 std::string dictionary_domain(".google.com");
1367 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1368
1369 size_t count = 0;
1370 while (count <= SdchManager::kMaxDictionaryCount + 1) {
1371 if (!sdch_manager_->AddSdchDictionary(dictionary_text,
1372 GURL("http://www.google.com")))
1373 break;
1374
1375 dictionary_text += " "; // Create dictionary with different SHA signature.
1376 ++count;
1377 }
1378 EXPECT_EQ(SdchManager::kMaxDictionaryCount, count);
1379 }
1380
1381 TEST_F(SdchFilterTest, DictionaryNotTooLarge) {
1382 std::string dictionary_domain(".google.com");
1383 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1384
1385 dictionary_text.append(
1386 SdchManager::kMaxDictionarySize - dictionary_text.size(), ' ');
1387 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
1388 GURL("http://" + dictionary_domain)));
1389 }
1390
1391 TEST_F(SdchFilterTest, DictionaryTooLarge) {
1392 std::string dictionary_domain(".google.com");
1393 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
1394
1395 dictionary_text.append(
1396 SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' ');
1397 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text,
1398 GURL("http://" + dictionary_domain)));
1399 }
1400
1401 TEST_F(SdchFilterTest, PathMatch) {
1402 bool (*PathMatch)(const std::string& path, const std::string& restriction) =
1403 SdchManager::Dictionary::PathMatch;
1404 // Perfect match is supported.
1405 EXPECT_TRUE(PathMatch("/search", "/search"));
1406 EXPECT_TRUE(PathMatch("/search/", "/search/"));
1407
1408 // Prefix only works if last character of restriction is a slash, or first
1409 // character in path after a match is a slash. Validate each case separately.
1410
1411 // Rely on the slash in the path (not at the end of the restriction).
1412 EXPECT_TRUE(PathMatch("/search/something", "/search"));
1413 EXPECT_TRUE(PathMatch("/search/s", "/search"));
1414 EXPECT_TRUE(PathMatch("/search/other", "/search"));
1415 EXPECT_TRUE(PathMatch("/search/something", "/search"));
1416
1417 // Rely on the slash at the end of the restriction.
1418 EXPECT_TRUE(PathMatch("/search/something", "/search/"));
1419 EXPECT_TRUE(PathMatch("/search/s", "/search/"));
1420 EXPECT_TRUE(PathMatch("/search/other", "/search/"));
1421 EXPECT_TRUE(PathMatch("/search/something", "/search/"));
1422
1423 // Make sure less that sufficient prefix match is false.
1424 EXPECT_FALSE(PathMatch("/sear", "/search"));
1425 EXPECT_FALSE(PathMatch("/", "/search"));
1426 EXPECT_FALSE(PathMatch(std::string(), "/search"));
1427
1428 // Add examples with several levels of direcories in the restriction.
1429 EXPECT_FALSE(PathMatch("/search/something", "search/s"));
1430 EXPECT_FALSE(PathMatch("/search/", "/search/s"));
1431
1432 // Make sure adding characters to path will also fail.
1433 EXPECT_FALSE(PathMatch("/searching", "/search/"));
1434 EXPECT_FALSE(PathMatch("/searching", "/search"));
1435
1436 // Make sure we're case sensitive.
1437 EXPECT_FALSE(PathMatch("/ABC", "/abc"));
1438 EXPECT_FALSE(PathMatch("/abc", "/ABC"));
1439 }
1440
1441 // The following are only applicable while we have a latency test in the code,
1442 // and can be removed when that functionality is stripped.
1443 TEST_F(SdchFilterTest, LatencyTestControls) {
1444 GURL url("http://www.google.com");
1445 GURL url2("http://www.google2.com");
1446
1447 // First make sure we default to false.
1448 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url));
1449 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2));
1450
1451 // That we can set each to true.
1452 sdch_manager_->SetAllowLatencyExperiment(url, true);
1453 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url));
1454 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2));
1455
1456 sdch_manager_->SetAllowLatencyExperiment(url2, true);
1457 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url));
1458 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url2));
1459
1460 // And can reset them to false.
1461 sdch_manager_->SetAllowLatencyExperiment(url, false);
1462 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url));
1463 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url2));
1464
1465 sdch_manager_->SetAllowLatencyExperiment(url2, false);
1466 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url));
1467 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2));
1468 }
1469
1470 } // namespace net 1142 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/base/sdch_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698