Skip to content

Commit

Permalink
[Extensions] Use CreateUpdateManifest in a number of tests
Browse files Browse the repository at this point in the history
The new function in `extension_downloader_test_helper.h`,
`CreateUpdateManifest`, gives a simple interface to create an update
manifest for extension downloads. This commit uses the function in
different tests, which have hard-coded manifests in place before.

Bug: 1061475
Change-Id: I240c0c51833b0270f00f07e79c23e5a79a48c7e5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3780914
Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org>
Auto-Submit: Oleg Davydov <burunduk@chromium.org>
Commit-Queue: Daniel Murphy <dmurph@chromium.org>
Reviewed-by: Daniel Murphy <dmurph@chromium.org>
Reviewed-by: Alexander Hendrich <hendrich@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1034066}
  • Loading branch information
burunduk3 authored and Chromium LUCI CQ committed Aug 11, 2022
1 parent 03124a0 commit 6190688
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 140 deletions.
20 changes: 9 additions & 11 deletions chrome/browser/ash/policy/core/device_cloud_policy_browsertest.cc
Expand Up @@ -47,6 +47,7 @@
#include "crypto/sha2.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/updater/extension_downloader_test_helper.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/switches.h"
Expand Down Expand Up @@ -244,12 +245,6 @@ class SigninExtensionsDeviceCloudPolicyBrowserTest
"extensions/signin_screen_managed_storage/extension.crx";
static constexpr const char* kTestExtensionUpdateManifestPath =
"/extensions/signin_screen_managed_storage/update_manifest.xml";
static constexpr const char* kTestExtensionUpdateManifest =
R"(<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
<app appid='$1'>
<updatecheck codebase='http://$2/$3' version='1.0' />
</app>
</gupdate>)";
static constexpr const char* kFakePolicyPath = "/test-policy.json";
static constexpr const char* kFakePolicy =
"{\"string-policy\": {\"Value\": \"value\"}}";
Expand Down Expand Up @@ -322,11 +317,14 @@ class SigninExtensionsDeviceCloudPolicyBrowserTest
// Create update manifest for the test extension, setting the extension URL
// with a test server URL pointing to the extension under the test data
// path.
std::string manifest_response = base::ReplaceStringPlaceholders(
kTestExtensionUpdateManifest,
{kTestExtensionId, embedded_test_server()->host_port_pair().ToString(),
kTestExtensionPath},
nullptr);
std::string manifest_response = extensions::CreateUpdateManifest(
{extensions::UpdateManifestItem(kTestExtensionId)
.version("1.0")
.codebase(base::ReplaceStringPlaceholders(
"http://$1/$2",
{embedded_test_server()->host_port_pair().ToString(),
kTestExtensionPath},
nullptr))});

auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_content_type("text/xml");
Expand Down
22 changes: 9 additions & 13 deletions chrome/browser/ash/policy/core/device_local_account_browsertest.cc
Expand Up @@ -131,6 +131,7 @@
#include "extensions/browser/management_policy.h"
#include "extensions/browser/notification_types.h"
#include "extensions/browser/sandboxed_unpacker.h"
#include "extensions/browser/updater/extension_downloader_test_helper.h"
#include "extensions/common/extension.h"
#include "net/base/url_util.h"
#include "net/http/http_status_code.h"
Expand Down Expand Up @@ -170,14 +171,6 @@ const char* const kStartupURLs[] = {
const char kExistentTermsOfServicePath[] = "chromeos/enterprise/tos.txt";
const char kNonexistentTermsOfServicePath[] = "chromeos/enterprise/tos404.txt";
const char kRelativeUpdateURL[] = "/service/update2/crx";
const char kUpdateManifestHeader[] =
"<?xml version='1.0' encoding='UTF-8'?>\n"
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>\n";
const char kUpdateManifestTemplate[] =
" <app appid='%s'>\n"
" <updatecheck codebase='%s' version='%s' />\n"
" </app>\n";
const char kUpdateManifestFooter[] = "</gupdate>\n";
const char kHostedAppID[] = "kbmnembihfiondgfjekmnmcbddelicoi";
const char kHostedAppCRXPath[] = "extensions/hosted_app.crx";
const char kHostedAppVersion[] = "1.0.0.0";
Expand Down Expand Up @@ -317,7 +310,7 @@ TestingUpdateManifestProvider::HandleRequest(
if (url.path() != relative_update_url_)
return nullptr;

std::string content = kUpdateManifestHeader;
std::vector<extensions::UpdateManifestItem> update_manifest;
for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) {
if (it.GetKey() != "x")
continue;
Expand All @@ -328,12 +321,15 @@ TestingUpdateManifestProvider::HandleRequest(
"id", &id);
UpdateMap::const_iterator entry = updates_.find(id);
if (entry != updates_.end()) {
content += base::StringPrintf(kUpdateManifestTemplate, id.c_str(),
entry->second.crx_url.spec().c_str(),
entry->second.version.c_str());
update_manifest.emplace_back(extensions::UpdateManifestItem(id)
.version(entry->second.version)
.codebase(entry->second.crx_url.spec()));
}
}
content += kUpdateManifestFooter;

std::string content =
extensions::CreateUpdateManifest(std::move(update_manifest));

std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
new net::test_server::BasicHttpResponse);
http_response->set_code(net::HTTP_OK);
Expand Down
17 changes: 6 additions & 11 deletions chrome/browser/extensions/external_provider_impl_unittest.cc
Expand Up @@ -36,6 +36,7 @@
#include "content/public/test/test_utils.h"
#include "extensions/browser/pref_names.h"
#include "extensions/browser/updater/extension_cache_fake.h"
#include "extensions/browser/updater/extension_downloader_test_helper.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
Expand Down Expand Up @@ -207,17 +208,11 @@ class ExternalProviderImplTest : public ExtensionServiceTestBase {
if (url.path() == test_extension.update_path) {
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_code(net::HTTP_OK);
response->set_content(base::StringPrintf(
"<?xml version='1.0' encoding='UTF-8'?>\n"
"<gupdate xmlns='http://www.google.com/update2/response' "
"protocol='2.0'>\n"
" <app appid='%s'>\n"
" <updatecheck codebase='%s' version='%s' />\n"
" </app>\n"
"</gupdate>",
test_extension.app_id,
test_server_->GetURL(test_extension.app_path).spec().c_str(),
test_extension.version));
response->set_content(CreateUpdateManifest(
{UpdateManifestItem(test_extension.app_id)
.version(test_extension.version)
.codebase(
test_server_->GetURL(test_extension.app_path).spec())}));
response->set_content_type("text/xml");
return std::move(response);
}
Expand Down
16 changes: 5 additions & 11 deletions chrome/browser/policy/extension_policy_browsertest.cc
Expand Up @@ -69,6 +69,7 @@
#include "extensions/browser/scoped_ignore_content_verifier_for_test.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/browser/updater/extension_cache_fake.h"
#include "extensions/browser/updater/extension_downloader_test_helper.h"
#include "extensions/common/constants.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/features/feature_channel.h"
Expand Down Expand Up @@ -701,17 +702,10 @@ std::string GetUpdateManifestBody(const std::string& id,
// "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>";
return extensions::CreateUpdateManifest({extensions::UpdateManifestItem(id)
.version(version)
.status("ok")
.codebase(crx_path)});
}

std::string GetUpdateManifestHeader() {
Expand Down
Expand Up @@ -39,6 +39,7 @@
#include "extensions/browser/extension_system.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/browser/updater/extension_cache_fake.h"
#include "extensions/browser/updater/extension_downloader_test_helper.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
Expand Down Expand Up @@ -118,18 +119,11 @@ class PreinstalledWebAppMigrationBrowserTest
if (request_path == kExtensionUpdatePath) {
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_code(net::HTTP_OK);
response->set_content(base::ReplaceStringPlaceholders(
R"(<?xml version='1.0' encoding='UTF-8'?>
<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
<app appid='$1'>
<updatecheck codebase='$2' version='$3' />
</app>
</gupdate>
)",
{kExtensionId,
embedded_test_server()->GetURL(kExtensionCrxPath).spec(),
kExtensionVersion},
nullptr));
response->set_content(extensions::CreateUpdateManifest(
{extensions::UpdateManifestItem(kExtensionId)
.version(kExtensionVersion)
.codebase(
embedded_test_server()->GetURL(kExtensionCrxPath).spec())}));
response->set_content_type("text/xml");
return std::move(response);
}
Expand Down
4 changes: 4 additions & 0 deletions extensions/browser/updater/extension_downloader_test_helper.h
Expand Up @@ -187,6 +187,10 @@ struct UpdateManifestItem {
updatecheck_params.emplace("hash", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& hash_sha256(std::string value) && {
updatecheck_params.emplace("hash_sha256", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& info(std::string value) && {
updatecheck_params.emplace("info", std::move(value));
return std::move(*this);
Expand Down
119 changes: 37 additions & 82 deletions extensions/browser/updater/safe_manifest_parser_unittest.cc
Expand Up @@ -7,6 +7,7 @@
#include "base/bind.h"
#include "base/run_loop.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/updater/extension_downloader_test_helper.h"
#include "extensions/browser/updater/safe_manifest_parser.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand Down Expand Up @@ -81,14 +82,9 @@ TEST_F(ExtensionUpdateManifestTest, MissingAppId) {
}

TEST_F(ExtensionUpdateManifestTest, InvalidCodebase) {
TestParseUpdateManifest(
"<?xml version='1.0'?>"
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>"
" <app appid='12345' status='ok'>"
" <updatecheck codebase='example.com/extension_1.2.3.4.crx'"
" version='1.2.3.4' />"
" </app>"
"</gupdate>");
TestParseUpdateManifest(CreateUpdateManifest(
{UpdateManifestItem("12345").version("1.2.3.4").codebase(
"example.com/extension_1.2.3.4.crx")}));
ASSERT_EQ(1u, results()->update_list.size());
EXPECT_TRUE(results()->update_list.at(0).parse_error);
EXPECT_EQ(results()->update_list.at(0).parse_error.value().error,
Expand All @@ -98,12 +94,8 @@ TEST_F(ExtensionUpdateManifestTest, InvalidCodebase) {

TEST_F(ExtensionUpdateManifestTest, MissingVersion) {
TestParseUpdateManifest(
"<?xml version='1.0'?>"
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>"
" <app appid='12345' status='ok'>"
" <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' />"
" </app>"
"</gupdate>");
CreateUpdateManifest({UpdateManifestItem("12345").codebase(
"http://example.com/extension_1.2.3.4.crx")}));
ASSERT_EQ(1u, results()->update_list.size());
EXPECT_TRUE(results()->update_list.at(0).parse_error);
EXPECT_EQ(results()->update_list.at(0).parse_error.value().error,
Expand All @@ -112,14 +104,9 @@ TEST_F(ExtensionUpdateManifestTest, MissingVersion) {
}

TEST_F(ExtensionUpdateManifestTest, InvalidVersion) {
TestParseUpdateManifest(
"<?xml version='1.0'?>"
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>"
" <app appid='12345' status='ok'>"
" <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' "
" version='1.2.3.a'/>"
" </app>"
"</gupdate>");
TestParseUpdateManifest(CreateUpdateManifest(
{UpdateManifestItem("12345").version("1.2.3.a").codebase(
"http://example.com/extension_1.2.3.4.crx")}));
ASSERT_EQ(1u, results()->update_list.size());
EXPECT_TRUE(results()->update_list.at(0).parse_error);
EXPECT_EQ(results()->update_list.at(0).parse_error.value().error,
Expand All @@ -128,14 +115,11 @@ TEST_F(ExtensionUpdateManifestTest, InvalidVersion) {
}

TEST_F(ExtensionUpdateManifestTest, ValidXml) {
TestParseUpdateManifest(
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>"
" <app appid='12345'>"
" <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
" version='1.2.3.4' prodversionmin='2.0.143.0' />"
" </app>"
"</gupdate>");
TestParseUpdateManifest(CreateUpdateManifest(
{UpdateManifestItem("12345")
.version("1.2.3.4")
.prodversionmin("2.0.143.0")
.codebase("http://example.com/extension_1.2.3.4.crx")}));
ExpectNoError();
ASSERT_TRUE(results());
EXPECT_EQ(1U, results()->update_list.size());
Expand Down Expand Up @@ -196,15 +180,12 @@ TEST_F(ExtensionUpdateManifestTest, SimilarTagnames) {
}

TEST_F(ExtensionUpdateManifestTest, XmlWithHash) {
TestParseUpdateManifest(
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>"
" <app appid='12345'>"
" <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
" version='1.2.3.4' prodversionmin='2.0.143.0' "
" hash_sha256='1234'/>"
" </app>"
"</gupdate>");
TestParseUpdateManifest(CreateUpdateManifest(
{UpdateManifestItem("12345")
.version("1.2.3.4")
.prodversionmin("2.0.143.0")
.codebase("http://example.com/extension_1.2.3.4.crx")
.hash_sha256("1234")}));
ExpectNoError();
ASSERT_TRUE(results());
EXPECT_EQ(1U, results()->update_list.size());
Expand All @@ -230,12 +211,7 @@ TEST_F(ExtensionUpdateManifestTest, XmlWithDaystart) {

TEST_F(ExtensionUpdateManifestTest, NoUpdateResponse) {
TestParseUpdateManifest(
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>"
" <app appid='12345'>"
" <updatecheck status='noupdate' />"
" </app>"
"</gupdate>");
CreateUpdateManifest({UpdateManifestItem("12345").status("noupdate")}));
ExpectNoError();
ASSERT_TRUE(results());
ASSERT_FALSE(results()->update_list.empty());
Expand Down Expand Up @@ -267,21 +243,14 @@ TEST_F(ExtensionUpdateManifestTest, TwoAppsOneError) {

TEST_F(ExtensionUpdateManifestTest, Duplicates) {
TestParseUpdateManifest(
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>"
" <app appid='aaaaaaaa'>"
" <updatecheck status='noupdate' />"
" </app>"
" <app appid='bbbbbbbb'>"
" <updatecheck codebase='http://example.com/b_3.1.crx' version='3.1'/>"
" </app>"
" <app appid='aaaaaaaa'>"
" <updatecheck status='noupdate' />"
" </app>"
" <app appid='aaaaaaaa'>"
" <updatecheck codebase='http://example.com/a_2.0.crx' version='2.0'/>"
" </app>"
"</gupdate>");
CreateUpdateManifest({UpdateManifestItem("aaaaaaaa").status("noupdate"),
UpdateManifestItem("bbbbbbbb")
.version("3.1")
.codebase("http://example.com/b_3.1.crx"),
UpdateManifestItem("aaaaaaaa").status("noupdate"),
UpdateManifestItem("aaaaaaaa")
.version("2.0")
.codebase("http://example.com/a_2.0.crx")}));

ExpectNoError();
ASSERT_TRUE(results());
Expand Down Expand Up @@ -309,28 +278,14 @@ TEST_F(ExtensionUpdateManifestTest, Duplicates) {
}

TEST_F(ExtensionUpdateManifestTest, GroupByID) {
TestParseUpdateManifest(
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>"
" <app appid='aaaaaaaa'>"
" <updatecheck status='noupdate' />"
" </app>"
" <app appid='bbbbbbbb'>"
" <updatecheck status='noupdate' />"
" </app>"
" <app appid='aaaaaaaa'>"
" <updatecheck status='noupdate' />"
" </app>"
" <app appid='bbbbbbbb'>"
" <updatecheck status='noupdate' />"
" </app>"
" <app appid='cccccccc'>"
" <updatecheck status='noupdate' />"
" </app>"
" <app appid='aaaaaaaa'>"
" <updatecheck status='noupdate' />"
" </app>"
"</gupdate>");
TestParseUpdateManifest(CreateUpdateManifest({
UpdateManifestItem("aaaaaaaa").status("noupdate"),
UpdateManifestItem("bbbbbbbb").status("noupdate"),
UpdateManifestItem("aaaaaaaa").status("noupdate"),
UpdateManifestItem("bbbbbbbb").status("noupdate"),
UpdateManifestItem("cccccccc").status("noupdate"),
UpdateManifestItem("aaaaaaaa").status("noupdate"),
}));

ExpectNoError();
ASSERT_TRUE(results());
Expand Down

0 comments on commit 6190688

Please sign in to comment.