Commit 7897e08e 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.

Reland after the revert by disabling the test when network service is enabled.

TBR=cudvall@chromium.org

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