This source file includes following definitions.
- NewSdchDictionary
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <limits.h>
#include <string>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "net/base/sdch_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
static const char kTestVcdiffDictionary[] = "DictionaryFor"
"SdchCompression1SdchCompression2SdchCompression3SdchCompression\n";
class SdchManagerTest : public testing::Test {
protected:
SdchManagerTest()
: sdch_manager_(new SdchManager) {
}
scoped_ptr<SdchManager> sdch_manager_;
};
static std::string NewSdchDictionary(const std::string& domain) {
std::string dictionary;
if (!domain.empty()) {
dictionary.append("Domain: ");
dictionary.append(domain);
dictionary.append("\n");
}
dictionary.append("\n");
dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1);
return dictionary;
}
TEST_F(SdchManagerTest, DomainSupported) {
GURL google_url("http://www.google.com");
net::SdchManager::EnableSdchSupport(false);
EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(google_url));
net::SdchManager::EnableSdchSupport(true);
EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(google_url));
}
TEST_F(SdchManagerTest, DomainBlacklisting) {
GURL test_url("http://www.test.com");
GURL google_url("http://www.google.com");
SdchManager::BlacklistDomain(test_url);
EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(test_url));
EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(google_url));
SdchManager::BlacklistDomain(google_url);
EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(google_url));
}
TEST_F(SdchManagerTest, DomainBlacklistingCaseSensitivity) {
GURL test_url("http://www.TesT.com");
GURL test2_url("http://www.tEst.com");
EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(test_url));
EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(test2_url));
SdchManager::BlacklistDomain(test_url);
EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(test2_url));
}
TEST_F(SdchManagerTest, BlacklistingReset) {
GURL gurl("http://mytest.DoMain.com");
std::string domain(gurl.host());
SdchManager::ClearBlacklistings();
EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 0);
EXPECT_EQ(SdchManager::BlacklistDomainExponential(domain), 0);
EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(gurl));
}
TEST_F(SdchManagerTest, BlacklistingSingleBlacklist) {
GURL gurl("http://mytest.DoMain.com");
std::string domain(gurl.host());
SdchManager::ClearBlacklistings();
SdchManager::Global()->BlacklistDomain(gurl);
EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 1);
EXPECT_EQ(SdchManager::BlacklistDomainExponential(domain), 1);
EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(gurl));
EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 0);
EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(gurl));
}
TEST_F(SdchManagerTest, BlacklistingExponential) {
GURL gurl("http://mytest.DoMain.com");
std::string domain(gurl.host());
SdchManager::ClearBlacklistings();
int exponential = 1;
for (int i = 1; i < 100; ++i) {
SdchManager::Global()->BlacklistDomain(gurl);
EXPECT_EQ(SdchManager::BlacklistDomainExponential(domain), exponential);
EXPECT_EQ(SdchManager::BlackListDomainCount(domain), exponential);
EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(gurl));
EXPECT_EQ(SdchManager::BlackListDomainCount(domain), exponential - 1);
SdchManager::ClearDomainBlacklisting(domain);
EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 0);
EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(gurl));
exponential = 1 + 2 * exponential;
if (exponential < 0)
exponential = INT_MAX;
}
}
TEST_F(SdchManagerTest, CanSetExactMatchDictionary) {
std::string dictionary_domain("x.y.z.google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://" + dictionary_domain)));
}
TEST_F(SdchManagerTest, CanAdvertiseDictionaryOverHTTP) {
std::string dictionary_domain("x.y.z.google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://" + dictionary_domain)));
std::string dictionary_list;
sdch_manager_->GetAvailDictionaryList(
GURL("http://" + dictionary_domain + "/test"),
&dictionary_list);
EXPECT_FALSE(dictionary_list.empty());
}
TEST_F(SdchManagerTest, CanNotAdvertiseDictionaryOverHTTPS) {
std::string dictionary_domain("x.y.z.google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://" + dictionary_domain)));
std::string dictionary_list;
sdch_manager_->GetAvailDictionaryList(
GURL("https://" + dictionary_domain + "/test"),
&dictionary_list);
EXPECT_TRUE(dictionary_list.empty());
}
TEST_F(SdchManagerTest, CanUseHTTPSDictionaryOverHTTPSIfEnabled) {
std::string dictionary_domain("x.y.z.google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("https://" + dictionary_domain)));
GURL target_url("https://" + dictionary_domain + "/test");
std::string dictionary_list;
sdch_manager_->EnableSecureSchemeSupport(true);
sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list);
EXPECT_FALSE(dictionary_list.empty());
SdchManager::Dictionary* dictionary = NULL;
std::string client_hash;
std::string server_hash;
sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash);
sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary);
EXPECT_TRUE(dictionary != NULL);
}
TEST_F(SdchManagerTest, CanNotUseHTTPDictionaryOverHTTPS) {
std::string dictionary_domain("x.y.z.google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://" + dictionary_domain)));
GURL target_url("https://" + dictionary_domain + "/test");
std::string dictionary_list;
sdch_manager_->EnableSecureSchemeSupport(true);
sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list);
EXPECT_TRUE(dictionary_list.empty());
SdchManager::Dictionary* dictionary = NULL;
std::string client_hash;
std::string server_hash;
sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash);
sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary);
EXPECT_TRUE(dictionary == NULL);
}
TEST_F(SdchManagerTest, FailToSetDomainMismatchDictionary) {
std::string dictionary_domain("x.y.z.google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://y.z.google.com")));
}
TEST_F(SdchManagerTest, FailToSetDotHostPrefixDomainDictionary) {
std::string dictionary_domain("x.y.z.google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://w.x.y.z.google.com")));
}
TEST_F(SdchManagerTest, FailToSetRepeatPrefixWithDotDictionary) {
std::string dictionary_domain("www.google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://www.google.com.www.google.com")));
}
TEST_F(SdchManagerTest, CanSetLeadingDotDomainDictionary) {
std::string dictionary_domain(".google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://www.google.com")));
}
TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) {
std::string dictionary_domain("x.y.z.google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://" + dictionary_domain)));
}
TEST_F(SdchManagerTest, TooManyDictionaries) {
std::string dictionary_domain(".google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
size_t count = 0;
while (count <= SdchManager::kMaxDictionaryCount + 1) {
if (!sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://www.google.com")))
break;
dictionary_text += " ";
++count;
}
EXPECT_EQ(SdchManager::kMaxDictionaryCount, count);
}
TEST_F(SdchManagerTest, DictionaryNotTooLarge) {
std::string dictionary_domain(".google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
dictionary_text.append(
SdchManager::kMaxDictionarySize - dictionary_text.size(), ' ');
EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://" + dictionary_domain)));
}
TEST_F(SdchManagerTest, DictionaryTooLarge) {
std::string dictionary_domain(".google.com");
std::string dictionary_text(NewSdchDictionary(dictionary_domain));
dictionary_text.append(
SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' ');
EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text,
GURL("http://" + dictionary_domain)));
}
TEST_F(SdchManagerTest, PathMatch) {
bool (*PathMatch)(const std::string& path, const std::string& restriction) =
SdchManager::Dictionary::PathMatch;
EXPECT_TRUE(PathMatch("/search", "/search"));
EXPECT_TRUE(PathMatch("/search/", "/search/"));
EXPECT_TRUE(PathMatch("/search/something", "/search"));
EXPECT_TRUE(PathMatch("/search/s", "/search"));
EXPECT_TRUE(PathMatch("/search/other", "/search"));
EXPECT_TRUE(PathMatch("/search/something", "/search"));
EXPECT_TRUE(PathMatch("/search/something", "/search/"));
EXPECT_TRUE(PathMatch("/search/s", "/search/"));
EXPECT_TRUE(PathMatch("/search/other", "/search/"));
EXPECT_TRUE(PathMatch("/search/something", "/search/"));
EXPECT_FALSE(PathMatch("/sear", "/search"));
EXPECT_FALSE(PathMatch("/", "/search"));
EXPECT_FALSE(PathMatch(std::string(), "/search"));
EXPECT_FALSE(PathMatch("/search/something", "search/s"));
EXPECT_FALSE(PathMatch("/search/", "/search/s"));
EXPECT_FALSE(PathMatch("/searching", "/search/"));
EXPECT_FALSE(PathMatch("/searching", "/search"));
EXPECT_FALSE(PathMatch("/ABC", "/abc"));
EXPECT_FALSE(PathMatch("/abc", "/ABC"));
}
TEST_F(SdchManagerTest, LatencyTestControls) {
GURL url("http://www.google.com");
GURL url2("http://www.google2.com");
EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url));
EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2));
sdch_manager_->SetAllowLatencyExperiment(url, true);
EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url));
EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2));
sdch_manager_->SetAllowLatencyExperiment(url2, true);
EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url));
EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url2));
sdch_manager_->SetAllowLatencyExperiment(url, false);
EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url));
EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url2));
sdch_manager_->SetAllowLatencyExperiment(url2, false);
EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url));
EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2));
}
}