Commit 56a58d7d authored by John Abd-El-Malek's avatar John Abd-El-Malek Committed by Commit Bot

Convert ConditionalCacheDeletionHelperBrowserTest to not depend on...

Convert ConditionalCacheDeletionHelperBrowserTest to not depend on URLRequestContext when network service is enabled.

Bug: 837753
Change-Id: I35b75299bcea1cc2b93a3c47dcbea036cc4e2d03
Reviewed-on: https://chromium-review.googlesource.com/c/1357567Reviewed-by: default avatarClark DuVall <cduvall@chromium.org>
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612858}
parent 74b8c224
......@@ -19,26 +19,28 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/cache_test_util.h"
#include "content/public/test/content_browser_test.h"
#include "content/shell/browser/shell.h"
#include "net/disk_cache/disk_cache.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_cache.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/cpp/features.h"
namespace content {
namespace {
bool KeyIsEven(const disk_cache::Entry* entry) {
int key_as_int = 0;
base::StringToInt(entry->GetKey().c_str(), &key_as_int);
return (key_as_int % 2) == 0;
bool DeletionCondition(const std::set<GURL>& erase_urls,
const disk_cache::Entry* entry) {
return !!erase_urls.count(GURL(entry->GetKey()));
}
bool HasHttpsExampleOrigin(const GURL& url) {
return url.GetOrigin() == "https://example.com/";
bool HasHttpExampleOrigin(const GURL& url) {
return url.host() == "example.com";
}
} // namespace
......@@ -46,6 +48,11 @@ bool HasHttpsExampleOrigin(const GURL& url) {
class ConditionalCacheDeletionHelperBrowserTest : public ContentBrowserTest {
public:
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
if (base::FeatureList::IsEnabled(network::features::kNetworkService))
return;
cache_util_ = std::make_unique<CacheTestUtil>(
content::BrowserContext::GetDefaultStoragePartition(
shell()->web_contents()->GetBrowserContext()));
......@@ -60,6 +67,18 @@ class ConditionalCacheDeletionHelperBrowserTest : public ContentBrowserTest {
void TearDownOnMainThread() override { cache_util_.reset(); }
void CreateCacheEntry(const std::set<GURL>& urls) {
if (base::FeatureList::IsEnabled(network::features::kNetworkService)) {
for (auto& url : urls) {
ASSERT_EQ(net::OK, LoadBasicRequest(
storage_partition()->GetNetworkContext(), url));
}
} else {
for (auto& url : urls)
cache_util_->CreateCacheEntries({url.spec()});
}
}
void DeleteEntries(
const base::Callback<bool(const disk_cache::Entry*)>& condition) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
......@@ -69,12 +88,23 @@ class ConditionalCacheDeletionHelperBrowserTest : public ContentBrowserTest {
helper->DeleteAndDestroySelfWhenFinished(done_callback_);
}
void CompareRemainingKeys(std::set<std::string> expected_set) {
std::vector<std::string> remaining_keys = cache_util_->GetEntryKeys();
std::sort(remaining_keys.begin(), remaining_keys.end());
std::vector<std::string> expected;
expected.assign(expected_set.begin(), expected_set.end());
EXPECT_EQ(expected, remaining_keys);
bool TestCacheEntry(const GURL& url) {
if (base::FeatureList::IsEnabled(network::features::kNetworkService)) {
return LoadBasicRequest(storage_partition()->GetNetworkContext(), url,
0 /* process_id */, 0 /* render_frame_id */,
net::LOAD_ONLY_FROM_CACHE |
net::LOAD_SKIP_CACHE_VALIDATION) == net::OK;
} else {
return base::ContainsValue(cache_util_->GetEntryKeys(), url.spec());
}
}
void CompareRemainingKeys(const std::set<GURL>& expected_urls,
const std::set<GURL>& erase_urls) {
for (auto& url : expected_urls)
EXPECT_TRUE(TestCacheEntry(url));
for (auto& url : erase_urls)
EXPECT_FALSE(TestCacheEntry(url));
}
void DoneCallback(int value) {
......@@ -90,7 +120,15 @@ class ConditionalCacheDeletionHelperBrowserTest : public ContentBrowserTest {
CacheTestUtil* GetCacheTestUtil() { return cache_util_.get(); }
StoragePartition* storage_partition() {
return BrowserContext::GetDefaultStoragePartition(browser_context());
}
private:
BrowserContext* browser_context() {
return shell()->web_contents()->GetBrowserContext();
}
base::Callback<void(int)> done_callback_;
std::unique_ptr<CacheTestUtil> cache_util_;
std::unique_ptr<base::WaitableEvent> waitable_event_;
......@@ -99,22 +137,45 @@ class ConditionalCacheDeletionHelperBrowserTest : public ContentBrowserTest {
// Tests that ConditionalCacheDeletionHelper only deletes those cache entries
// that match the condition.
IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) {
// Create 5 entries.
std::set<std::string> keys = {"123", "47", "56", "81", "42"};
GetCacheTestUtil()->CreateCacheEntries(keys);
// Delete the entries whose keys are even numbers.
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries,
base::Unretained(this), base::Bind(&KeyIsEven)));
WaitForTasksOnIOThread();
// Expect that the keys with values 56 and 42 were deleted.
keys.erase("56");
keys.erase("42");
CompareRemainingKeys(keys);
std::set<GURL> urls = {
embedded_test_server()->GetURL("foo.com", "/title1.html"),
embedded_test_server()->GetURL("bar.com", "/title1.html"),
embedded_test_server()->GetURL("baz.com", "/title1.html"),
embedded_test_server()->GetURL("qux.com", "/title1.html")};
CreateCacheEntry(urls);
std::set<GURL> erase_urls = {
embedded_test_server()->GetURL("bar.com", "/title1.html"),
embedded_test_server()->GetURL("baz.com", "/title1.html"),
};
for (auto& url : erase_urls)
urls.erase(url);
if (base::FeatureList::IsEnabled(network::features::kNetworkService)) {
network::mojom::ClearDataFilterPtr filter =
network::mojom::ClearDataFilter::New();
filter->type = network::mojom::ClearDataFilter::Type::DELETE_MATCHES;
for (auto& url : erase_urls)
filter->origins.push_back(url::Origin::Create(url));
base::RunLoop run_loop;
storage_partition()->GetNetworkContext()->ClearHttpCache(
base::Time(), base::Time::Max(), std::move(filter),
run_loop.QuitClosure());
run_loop.Run();
} else {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(
&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries,
base::Unretained(this),
base::Bind(&DeletionCondition, erase_urls)));
WaitForTasksOnIOThread();
}
CompareRemainingKeys(urls, erase_urls);
}
// Tests that ConditionalCacheDeletionHelper correctly constructs a condition
......@@ -134,14 +195,12 @@ IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) {
#endif
IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest,
MAYBE_TimeAndURL) {
// Create some entries.
std::set<std::string> keys;
keys.insert("https://google.com/index.html");
keys.insert("https://example.com/foo/bar/icon.png");
keys.insert("http://chrome.com");
GetCacheTestUtil()->CreateCacheEntries(keys);
std::set<GURL> urls = {
embedded_test_server()->GetURL("foo.com", "/title1.html"),
embedded_test_server()->GetURL("example.com", "/title1.html"),
embedded_test_server()->GetURL("bar.com", "/title1.html")};
CreateCacheEntry(urls);
// Wait some milliseconds for the cache to write the entries.
// This assures that future entries will have timestamps strictly greater than
......@@ -150,32 +209,53 @@ IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest,
base::Time now = base::Time::Now();
// Create a few more entries with a later timestamp.
std::set<std::string> newer_keys;
newer_keys.insert("https://google.com/");
newer_keys.insert("https://example.com/foo/bar/icon2.png");
newer_keys.insert("https://example.com/foo/bar/icon3.png");
newer_keys.insert("http://example.com/foo/bar/icon4.png");
GetCacheTestUtil()->CreateCacheEntries(newer_keys);
std::set<GURL> newer_urls = {
embedded_test_server()->GetURL("foo.com", "/simple_page.html"),
embedded_test_server()->GetURL("example.com", "/title2.html"),
embedded_test_server()->GetURL("example.com", "/title3.html"),
embedded_test_server()->GetURL("example2.com", "/simple_page.html")};
CreateCacheEntry(newer_urls);
// Create a condition for entries with the "https://example.com" origin
// Create a condition for entries with the "http://example.com" origin
// created after waiting.
base::Callback<bool(const disk_cache::Entry*)> condition =
ConditionalCacheDeletionHelper::CreateURLAndTimeCondition(
base::Bind(&HasHttpsExampleOrigin), now, base::Time::Max());
// Delete the entries.
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries,
base::Unretained(this), std::move(condition)));
WaitForTasksOnIOThread();
// Expect that only "icon2.png" and "icon3.png" were deleted.
keys.insert(newer_keys.begin(), newer_keys.end());
keys.erase("https://example.com/foo/bar/icon2.png");
keys.erase("https://example.com/foo/bar/icon3.png");
CompareRemainingKeys(keys);
if (base::FeatureList::IsEnabled(network::features::kNetworkService)) {
network::mojom::ClearDataFilterPtr filter =
network::mojom::ClearDataFilter::New();
filter->type = network::mojom::ClearDataFilter::Type::DELETE_MATCHES;
filter->origins.push_back(url::Origin::Create(
embedded_test_server()->GetURL("example.com", "/")));
base::RunLoop run_loop;
storage_partition()->GetNetworkContext()->ClearHttpCache(
now, base::Time::Max(), std::move(filter), run_loop.QuitClosure());
run_loop.Run();
} else {
base::Callback<bool(const disk_cache::Entry*)> condition =
ConditionalCacheDeletionHelper::CreateURLAndTimeCondition(
base::Bind(&HasHttpExampleOrigin), now, base::Time::Max());
// Delete the entries.
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(
&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries,
base::Unretained(this), std::move(condition)));
WaitForTasksOnIOThread();
}
// Expect that only "title2.html" and "title3.html" were deleted.
std::set<GURL> erase_urls = {
embedded_test_server()->GetURL("example.com", "/title2.html"),
embedded_test_server()->GetURL("example.com", "/title3.html"),
};
for (auto& url : newer_urls)
urls.insert(url);
for (auto& url : erase_urls)
urls.erase(url);
CompareRemainingKeys(urls, erase_urls);
}
} // namespace content
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment