Commit 1d1ff918 authored by John Abd-El-Malek's avatar John Abd-El-Malek Committed by Commit Bot

Fix extensions management browser tests with network service.

Bug: 769401
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: Ie6dff50674507ec518d5c8043cba7e3202b6f2ff
Reviewed-on: https://chromium-review.googlesource.com/1168736Reviewed-by: default avatarDoug Turner <dougt@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582190}
parent 76934ebf
......@@ -12,6 +12,7 @@
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/post_task.h"
#include "base/test/bind_test_util.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_management.h"
......@@ -31,6 +32,7 @@
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
......@@ -343,18 +345,32 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_AutoUpdate) {
basedir.AppendASCII("v2"), temp_dir.GetPath().AppendASCII("v2.crx"),
pem_path, base::FilePath());
// Note: This interceptor gets requests on the IO thread.
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
base::CreateTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}));
interceptor.SetResponseIgnoreQuery(
GURL("http://localhost/autoupdate/manifest"),
basedir.AppendASCII("manifest_v2.xml"));
interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
v2_path);
content::URLLoaderInterceptor interceptor(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) -> bool {
if (params->url_request.url.path() == "/autoupdate/v2.crx") {
content::URLLoaderInterceptor::WriteResponse(v2_path,
params->client.get());
return true;
}
if (params->url_request.url.path() == "/autoupdate/v3.crx") {
content::URLLoaderInterceptor::WriteResponse(
basedir.AppendASCII("v3.crx"), params->client.get());
return true;
}
if (params->url_request.url.path() == "/autoupdate/manifest") {
static bool first = true;
if (first) {
content::URLLoaderInterceptor::WriteResponse(
basedir.AppendASCII("manifest_v2.xml"), params->client.get());
first = false;
} else {
content::URLLoaderInterceptor::WriteResponse(
basedir.AppendASCII("manifest_v3.xml"), params->client.get());
}
return true;
}
return false;
}));
// Install version 1 of the extension.
ExtensionTestMessageListener listener1("v1 installed", false);
......@@ -394,11 +410,6 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_AutoUpdate) {
// Now try doing an update to version 3, which has been incorrectly
// signed. This should fail.
interceptor.SetResponseIgnoreQuery(
GURL("http://localhost/autoupdate/manifest"),
basedir.AppendASCII("manifest_v3.xml"));
interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v3.crx"),
basedir.AppendASCII("v3.crx"));
extensions::ExtensionUpdater::CheckParams params2;
params2.callback = base::BindOnce(&NotificationListener::OnFinished,
......@@ -447,18 +458,20 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
basedir.AppendASCII("v2"), temp_dir.GetPath().AppendASCII("v2.crx"),
pem_path, base::FilePath());
// Note: This interceptor gets requests on the IO thread.
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
base::CreateTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}));
interceptor.SetResponseIgnoreQuery(
GURL("http://localhost/autoupdate/manifest"),
basedir.AppendASCII("manifest_v2.xml"));
interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
v2_path);
content::URLLoaderInterceptor interceptor(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) -> bool {
if (params->url_request.url.path() == "/autoupdate/manifest") {
content::URLLoaderInterceptor::WriteResponse(
basedir.AppendASCII("manifest_v2.xml"), params->client.get());
return true;
}
if (params->url_request.url.path() == "/autoupdate/v2.crx") {
content::URLLoaderInterceptor::WriteResponse(v2_path,
params->client.get());
return true;
}
return false;
}));
// Install version 1 of the extension.
ExtensionTestMessageListener listener1("v1 installed", false);
......@@ -523,18 +536,20 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalUrlUpdate) {
basedir.AppendASCII("v2"), temp_dir.GetPath().AppendASCII("v2.crx"),
pem_path, base::FilePath());
// Note: This interceptor gets requests on the IO thread.
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
base::CreateTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}));
interceptor.SetResponseIgnoreQuery(
GURL("http://localhost/autoupdate/manifest"),
basedir.AppendASCII("manifest_v2.xml"));
interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
v2_path);
content::URLLoaderInterceptor interceptor(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) -> bool {
if (params->url_request.url.path() == "/autoupdate/manifest") {
content::URLLoaderInterceptor::WriteResponse(
basedir.AppendASCII("manifest_v2.xml"), params->client.get());
return true;
}
if (params->url_request.url.path() == "/autoupdate/v2.crx") {
content::URLLoaderInterceptor::WriteResponse(v2_path,
params->client.get());
return true;
}
return false;
}));
ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
const size_t size_before = registry->enabled_extensions().size();
......@@ -630,18 +645,20 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalPolicyRefresh) {
basedir.AppendASCII("v2"), temp_dir.GetPath().AppendASCII("v2.crx"),
pem_path, base::FilePath());
// Note: This interceptor gets requests on the IO thread.
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
base::CreateTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}));
interceptor.SetResponseIgnoreQuery(
GURL("http://localhost/autoupdate/manifest"),
basedir.AppendASCII("manifest_v2.xml"));
interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
v2_path);
content::URLLoaderInterceptor interceptor(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) -> bool {
if (params->url_request.url.path() == "/autoupdate/manifest") {
content::URLLoaderInterceptor::WriteResponse(
basedir.AppendASCII("manifest_v2.xml"), params->client.get());
return true;
}
if (params->url_request.url.path() == "/autoupdate/v2.crx") {
content::URLLoaderInterceptor::WriteResponse(v2_path,
params->client.get());
return true;
}
return false;
}));
ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
const size_t size_before = registry->enabled_extensions().size();
......@@ -722,18 +739,20 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
basedir.AppendASCII("v2"), temp_dir.GetPath().AppendASCII("v2.crx"),
pem_path, base::FilePath());
// Note: This interceptor gets requests on the IO thread.
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
base::CreateTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}));
interceptor.SetResponseIgnoreQuery(
GURL("http://localhost/autoupdate/manifest"),
basedir.AppendASCII("manifest_v2.xml"));
interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
v2_path);
content::URLLoaderInterceptor interceptor(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) -> bool {
if (params->url_request.url.path() == "/autoupdate/manifest") {
content::URLLoaderInterceptor::WriteResponse(
basedir.AppendASCII("manifest_v2.xml"), params->client.get());
return true;
}
if (params->url_request.url.path() == "/autoupdate/v2.crx") {
content::URLLoaderInterceptor::WriteResponse(v2_path,
params->client.get());
return true;
}
return false;
}));
// Check that the policy is initially empty.
ASSERT_TRUE(extensions::ExtensionManagementFactory::GetForBrowserContext(
......
......@@ -9,6 +9,7 @@
#include "base/path_service.h"
#include "base/test/bind_test_util.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "content/browser/frame_host/render_frame_host_impl.h"
#include "content/browser/loader/navigation_url_loader_impl.h"
#include "content/browser/loader/resource_message_filter.h"
......@@ -32,11 +33,9 @@ base::FilePath GetDataFilePath(const std::string& relative_path) {
return root_path.AppendASCII(relative_path);
}
// Returns the contents of the given filename relative to the root source
// directory.
static std::string ReadFile(const std::string& relative_path) {
static std::string ReadFile(const base::FilePath& path) {
std::string contents;
CHECK(base::ReadFileToString(GetDataFilePath(relative_path), &contents));
CHECK(base::ReadFileToString(path, &contents));
return contents;
}
......@@ -295,24 +294,38 @@ void URLLoaderInterceptor::WriteResponse(
network::mojom::URLLoaderClient* client,
const std::string* headers,
base::Optional<net::SSLInfo> ssl_info) {
return WriteResponse(GetDataFilePath(relative_path), client, headers,
std::move(ssl_info));
}
void URLLoaderInterceptor::WriteResponse(
const base::FilePath& file_path,
network::mojom::URLLoaderClient* client,
const std::string* headers,
base::Optional<net::SSLInfo> ssl_info) {
base::ScopedAllowBlockingForTesting allow_io;
std::string headers_str;
if (headers) {
headers_str = *headers;
} else {
std::string headers_path =
relative_path + "." + net::test_server::kMockHttpHeadersExtension;
if (base::PathExists(GetDataFilePath(headers_path))) {
base::FilePath::StringPieceType mock_headers_extension;
#if defined(OS_WIN)
base::string16 temp =
base::ASCIIToUTF16(net::test_server::kMockHttpHeadersExtension);
mock_headers_extension = temp;
#else
mock_headers_extension = net::test_server::kMockHttpHeadersExtension;
#endif
base::FilePath headers_path(file_path.AddExtension(mock_headers_extension));
if (base::PathExists(headers_path)) {
headers_str = ReadFile(headers_path);
} else {
headers_str = "HTTP/1.0 200 OK\nContent-type: " +
net::test_server::GetContentType(
base::FilePath().AppendASCII(relative_path)) +
"\n\n";
net::test_server::GetContentType(file_path) + "\n\n";
}
}
WriteResponse(headers_str, ReadFile(relative_path), client,
std::move(ssl_info));
WriteResponse(headers_str, ReadFile(file_path), client, std::move(ssl_info));
}
void URLLoaderInterceptor::CreateURLLoaderFactoryForSubresources(
......
......@@ -7,6 +7,7 @@
#include <set>
#include "base/files/file_path.h"
#include "base/macros.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
......@@ -94,6 +95,13 @@ class URLLoaderInterceptor {
const std::string* headers = nullptr,
base::Optional<net::SSLInfo> ssl_info = base::nullopt);
// Like above, but uses an absolute file path.
static void WriteResponse(
const base::FilePath& file_path,
network::mojom::URLLoaderClient* client,
const std::string* headers = nullptr,
base::Optional<net::SSLInfo> ssl_info = base::nullopt);
private:
class BrowserProcessWrapper;
class Interceptor;
......
......@@ -29,14 +29,6 @@
# https://crbug.com/853251
-DomainReliabilityBrowserTest.Upload
# extensions/browser/updater/extension_downloader.cc still uses URLFetcher.
# https://crbug.com/844928
-ExtensionManagementTest.AutoUpdate
-ExtensionManagementTest.AutoUpdateDisabledExtensions
-ExtensionManagementTest.ExternalPolicyRefresh
-ExtensionManagementTest.ExternalUrlUpdate
-ExtensionManagementTest.PolicyOverridesUserInstall
# This test fails flakily. Only enable it once
# SocketExtensionWithDnsLookupFunction::StartDnsLookup uses the Mojo
# HostResolver API.
......
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