Skip to content

Commit

Permalink
[Extensions] Extract logic for writing manifest response into a method
Browse files Browse the repository at this point in the history
Currently ExtensionPinningTest tests have the logic for writing the
response for fetching update manifest in the test itself. This CL
moves the common logic into a method.

Bug: b/182887200
Change-Id: I2918d3be66ac7173f9f0c99e25268bcf2c11b48e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3314566
Reviewed-by: Sergey Poromov <poromov@chromium.org>
Commit-Queue: Swapnil Gupta <swapnilgupta@google.com>
Cr-Commit-Position: refs/heads/main@{#948481}
  • Loading branch information
swapnil119 authored and Chromium LUCI CQ committed Dec 6, 2021
1 parent 4819e8d commit 9d571e0
Showing 1 changed file with 70 additions and 120 deletions.
190 changes: 70 additions & 120 deletions chrome/browser/policy/extension_policy_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
Expand Down Expand Up @@ -684,6 +685,51 @@ class MockedInstallationCollectorObserver
raw_ptr<const content::BrowserContext> context_;
};

std::string GetUpdateManifestBody(const std::string& id,
const std::string& crx_name,
const std::string& version) {
// "example.com" is a placeholder that gets substituted with the test
// server address at runtime.
std::string crx_path = "http://example.com/" + crx_name;
return "<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response' "
"protocol='2.0'>"
" <app appid='" +
id +
"'>"
" <updatecheck status='ok' codebase='" +
crx_path + "' version='" + version +
"' />"
" </app>"
"</gupdate>";
}

std::string GetUpdateManifestHeader() {
return "HTTP/1.1 200 OK\nContent-Type: application/json; "
"charset=utf-8\n";
}

bool WriteManifestResponse(content::URLLoaderInterceptor::RequestParams* params,
const std::string& id,
const std::string& update_manifest_name,
const std::string& crx_name,
const std::string& version,
const base::FilePath& install_crx_path) {
if (params->url_request.url.path() == update_manifest_name) {
content::URLLoaderInterceptor::WriteResponse(
GetUpdateManifestHeader(), GetUpdateManifestBody(id, crx_name, version),
params->client.get());
return true;
}

if (params->url_request.url.path() == "/" + crx_name) {
content::URLLoaderInterceptor::WriteResponse(install_crx_path,
params->client.get());
return true;
}
return false;
}

} // namespace

// Verifies that if extension is installed manually by user and then added to
Expand Down Expand Up @@ -1177,30 +1223,6 @@ class ExtensionPinningTest : public extensions::ExtensionBrowserTest {
BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
}

std::string GetUpdateManifestBody(const std::string& id,
const std::string& crx_name,
const std::string& version) {
// "example.com" is a placeholder that gets substituted with the test
// server address at runtime.
std::string crx_path = "http://example.com/" + crx_name;
return "<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response' "
"protocol='2.0'>"
" <app appid='" +
id +
"'>"
" <updatecheck status='ok' codebase='" +
crx_path + "' version='" + version +
"' />"
" </app>"
"</gupdate>";
}

std::string GetUpdateManifestHeader() {
return "HTTP/1.1 200 OK\nContent-Type: application/json; "
"charset=utf-8\n";
}

base::FilePath PackLocalExtension(const std::string& relative_dir_path,
const std::string& relative_pem_path,
const base::FilePath& crx_path) {
Expand Down Expand Up @@ -1237,24 +1259,11 @@ IN_PROC_BROWSER_TEST_F(ExtensionPinningTest,
// unexpected request.
ExtensionRequestInterceptor interceptor;
ASSERT_TRUE(embedded_test_server()->Start());
// TODO(b/172205862): Find a way to move these requests out of the test.
interceptor.set_interceptor_hook(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.path() == "/update_manifest_v1.xml") {
content::URLLoaderInterceptor::WriteResponse(
GetUpdateManifestHeader(),
GetUpdateManifestBody(kPinnedExtensionCrxId, "v1.crx",
/*version=*/"1"),
params->client.get());
return true;
}

if (params->url_request.url.path() == "/v1.crx") {
content::URLLoaderInterceptor::WriteResponse(install_crx_path,
params->client.get());
return true;
}
return false;
return WriteManifestResponse(params, kPinnedExtensionCrxId,
"/update_manifest_v1.xml", "v1.crx",
/*version=*/"1", install_crx_path);
}));

extensions::TestExtensionRegistryObserver observer(extension_registry(),
Expand All @@ -1275,21 +1284,9 @@ IN_PROC_BROWSER_TEST_F(ExtensionPinningTest,
// extension.
interceptor.set_interceptor_hook(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.path() == "/update_manifest_v2.xml") {
content::URLLoaderInterceptor::WriteResponse(
GetUpdateManifestHeader(),
GetUpdateManifestBody(kPinnedExtensionCrxId, "v2.crx",
/*version=*/"2"),
params->client.get());
return true;
}

if (params->url_request.url.path() == "/v2.crx") {
content::URLLoaderInterceptor::WriteResponse(updated_crx_path,
params->client.get());
return true;
}
return false;
return WriteManifestResponse(params, kPinnedExtensionCrxId,
"/update_manifest_v2.xml", "v2.crx",
/*version=*/"2", updated_crx_path);
}));
// Change |update_url| to point to version 2 of the extension.
SetExtensionSettingsPolicy("/update_manifest_v2.xml", kPinnedExtensionCrxId);
Expand Down Expand Up @@ -1318,24 +1315,11 @@ IN_PROC_BROWSER_TEST_F(ExtensionPinningTest, UpdateExtensionWithUrlInManifest) {
// unexpected request.
ExtensionRequestInterceptor interceptor;
ASSERT_TRUE(embedded_test_server()->Start());
// TODO(b/172205862): Find a way to move these requests out of the test.
interceptor.set_interceptor_hook(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.path() == "/update_manifest_v1.xml") {
content::URLLoaderInterceptor::WriteResponse(
GetUpdateManifestHeader(),
GetUpdateManifestBody(kPinnedExtensionCrxId, "v1.crx",
/*version=*/"1"),
params->client.get());
return true;
}

if (params->url_request.url.path() == "/v1.crx") {
content::URLLoaderInterceptor::WriteResponse(install_crx_path,
params->client.get());
return true;
}
return false;
return WriteManifestResponse(params, kPinnedExtensionCrxId,
"/update_manifest_v1.xml", "v1.crx",
/*version=*/"1", install_crx_path);
}));

extensions::TestExtensionRegistryObserver observer(extension_registry(),
Expand All @@ -1355,21 +1339,9 @@ IN_PROC_BROWSER_TEST_F(ExtensionPinningTest, UpdateExtensionWithUrlInManifest) {
// extension.
interceptor.set_interceptor_hook(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.path() == "/update_manifest_v2.xml") {
content::URLLoaderInterceptor::WriteResponse(
GetUpdateManifestHeader(),
GetUpdateManifestBody(kPinnedExtensionCrxId, "v2.crx",
/*version=*/"2"),
params->client.get());
return true;
}

if (params->url_request.url.path() == "/v2.crx") {
content::URLLoaderInterceptor::WriteResponse(updated_crx_path,
params->client.get());
return true;
}
return false;
return WriteManifestResponse(params, kPinnedExtensionCrxId,
"/update_manifest_v2.xml", "v2.crx",
/*version=*/"2", updated_crx_path);
}));
// Change |update_url| to point to version 2 of the extension.
SetExtensionSettingsPolicy("/update_manifest_v2.xml", kPinnedExtensionCrxId);
Expand Down Expand Up @@ -1397,24 +1369,11 @@ IN_PROC_BROWSER_TEST_F(ExtensionPinningTest, IgnoreUpdateUrlInManifest) {
// unexpected request.
ExtensionRequestInterceptor interceptor;
ASSERT_TRUE(embedded_test_server()->Start());
// TODO(b/172205862): Find a way to move these requests out of the test.
interceptor.set_interceptor_hook(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.path() == "/update_manifest_v1.xml") {
content::URLLoaderInterceptor::WriteResponse(
GetUpdateManifestHeader(),
GetUpdateManifestBody(kPinnedExtensionCrxId, "v1.crx",
/*version=*/"1"),
params->client.get());
return true;
}

if (params->url_request.url.path() == "/v1.crx") {
content::URLLoaderInterceptor::WriteResponse(install_crx_path,
params->client.get());
return true;
}
return false;
return WriteManifestResponse(params, kPinnedExtensionCrxId,
"/update_manifest_v1.xml", "v1.crx",
/*version=*/"1", install_crx_path);
}));

extensions::TestExtensionRegistryObserver observer(extension_registry(),
Expand Down Expand Up @@ -1450,26 +1409,17 @@ IN_PROC_BROWSER_TEST_F(ExtensionPinningTest,
// unexpected request.
ExtensionRequestInterceptor interceptor;
ASSERT_TRUE(embedded_test_server()->Start());
// TODO(b/172205862): Find a way to move these requests out of the test.

interceptor.set_interceptor_hook(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.path() == "/update_manifest.xml") {
content::URLLoaderInterceptor::WriteResponse(
GetUpdateManifestHeader(),
GetUpdateManifestBody(kGoogleCalendarCrxId, "calendar.crx",
"3.1.0"),
params->client.get());
return true;
}

if (params->url_request.url.path() == "/calendar.crx") {
content::URLLoaderInterceptor::WriteResponse(
"chrome/test/data/extensions/pinning/calendar/"
"gmbgaklkmjakoegficnlkhebmhkjfich-google-calendar-3.1.0.crx",
params->client.get());
return true;
}
return false;
base::FilePath path;
base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path);
path = path.AppendASCII(
"chrome/test/data/extensions/pinning/calendar/"
"gmbgaklkmjakoegficnlkhebmhkjfich-google-calendar-3.1.0.crx");
return WriteManifestResponse(params, kGoogleCalendarCrxId,
"/update_manifest.xml", "calendar.crx",
"3.1.0", path);
}));

extensions::TestExtensionRegistryObserver observer(extension_registry(),
Expand Down

0 comments on commit 9d571e0

Please sign in to comment.