Skip to content

Commit

Permalink
[Extensions] Use CreateUpdateManifest helper in ExtensionUpdaterTest
Browse files Browse the repository at this point in the history
Move the CreateUpdateManifest method from internals of the
ExtensionDownloaderTest to extension_downloader_test_helper.h (as a
standalone function) so it could be used in other places, e.g. in
extension_updater_unittest.cc.

Bug: 1061475
Change-Id: Ib272687a1bfe21b5a635e6611a6a5209170cefef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3779669
Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org>
Commit-Queue: Oleg Davydov <burunduk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1032479}
  • Loading branch information
burunduk3 authored and Chromium LUCI CQ committed Aug 8, 2022
1 parent 46d2bf1 commit a83bf7b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 99 deletions.
57 changes: 16 additions & 41 deletions chrome/browser/extensions/updater/extension_updater_unittest.cc
Expand Up @@ -237,27 +237,6 @@ class MockUpdateService : public UpdateService {
base::OnceClosure callback));
};

#if BUILDFLAG(IS_CHROMEOS_ASH)
// TODO (crbug.com/1061475) : Move this to a utility file.
std::string CreateUpdateManifest(const std::string& extension_id,
const std::string& extension_version,
const std::string& extension_hash) {
return "<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response'"
" protocol='2.0'>"
" <app appid='" +
extension_id +
"'>"
" <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
" version='" +
extension_version +
(extension_hash.size() ? "' hash='" + extension_hash : "'") +
"' prodversionmin='1.1' />"
" </app>"
"</gupdate>";
}
#endif

} // namespace

// Base class for further specialized test classes.
Expand Down Expand Up @@ -1247,15 +1226,11 @@ class ExtensionUpdaterTest : public testing::Test {
{
helper.StartUpdateCheck(std::move(fetch3));
RunUntilIdle();
const std::string kNoUpdate =
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response'"
" protocol='2.0'>"
" <app appid='3333'>"
" <updatecheck codebase='http://example.com/extension_3.0.0.0.crx'"
" version='3.0.0.0' prodversionmin='3.0.0.0' />"
" </app>"
"</gupdate>";
const std::string kNoUpdate = CreateUpdateManifest(
{UpdateManifestItem("3333")
.version("3.0.0.0")
.prodversionmin("3.0.0.0")
.codebase("http://example.com/extension_3.0.0.0.crx")});
helper.test_url_loader_factory().AddResponse(fetch3_url.spec(), kNoUpdate,
net::HTTP_OK);
// The third fetcher doesn't have an update available.
Expand All @@ -1282,15 +1257,11 @@ class ExtensionUpdaterTest : public testing::Test {
RunUntilIdle();
// The last fetcher has an update.
NotificationsObserver observer;
const std::string kUpdateAvailable =
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response'"
" protocol='2.0'>"
" <app appid='4444'>"
" <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
" version='4.0.42.0' prodversionmin='4.0.42.0' />"
" </app>"
"</gupdate>";
const std::string kUpdateAvailable = CreateUpdateManifest(
{UpdateManifestItem("4444")
.version("4.0.42.0")
.prodversionmin("4.0.42.0")
.codebase("http://example.com/extension_1.2.3.4.crx")});
helper.test_url_loader_factory().AddResponse(
fetch4_url.spec(), kUpdateAvailable, net::HTTP_OK);
EXPECT_CALL(delegate, IsExtensionPending("4444")).WillOnce(Return(false));
Expand Down Expand Up @@ -1649,8 +1620,12 @@ class ExtensionUpdaterTest : public testing::Test {
CreateManifestFetchData(kUpdateURL));
AddExtensionToFetchDataForTesting(fetch.get(), kTestExtensionId, "1.0",
kUpdateURL);
const std::string manifest =
CreateUpdateManifest(kTestExtensionId, version, hash);
const std::string manifest = CreateUpdateManifest(
{UpdateManifestItem(kTestExtensionId)
.version(version)
.hash(hash)
.codebase("http://example.com/extension_1.2.3.4.crx")
.prodversionmin("1.1")});
helper.test_url_loader_factory().AddResponse(fetch->full_url().spec(),
manifest, net::HTTP_OK);

Expand Down
31 changes: 31 additions & 0 deletions extensions/browser/updater/extension_downloader_test_helper.cc
Expand Up @@ -161,4 +161,35 @@ void AddExtensionToFetchDataForTesting(ManifestFetchData* fetch_data,
ExtensionDownloaderTestHelper::kNeverPingedData);
}

UpdateManifestItem::UpdateManifestItem(ExtensionId id) : id(std::move(id)) {}
UpdateManifestItem::~UpdateManifestItem() = default;
UpdateManifestItem::UpdateManifestItem(const UpdateManifestItem&) = default;
UpdateManifestItem& UpdateManifestItem::operator=(const UpdateManifestItem&) =
default;
UpdateManifestItem::UpdateManifestItem(UpdateManifestItem&&) = default;
UpdateManifestItem& UpdateManifestItem::operator=(UpdateManifestItem&&) =
default;

std::string CreateUpdateManifest(
const std::vector<UpdateManifestItem>& extensions) {
std::string content =
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response'"
" protocol='2.0'>";
for (const auto& update_item : extensions) {
content += base::StringPrintf(
" <app appid='%s'>"
" <updatecheck",
update_item.id.c_str());
for (const auto& [name, value] : update_item.updatecheck_params) {
content += base::StringPrintf(" %s='%s'", name.c_str(), value.c_str());
}
content +=
" />"
" </app>";
}
content += "</gupdate>";
return content;
}

} // namespace extensions
46 changes: 46 additions & 0 deletions extensions/browser/updater/extension_downloader_test_helper.h
Expand Up @@ -168,6 +168,52 @@ void AddExtensionToFetchDataForTesting(ManifestFetchData* fetch_data,
const std::string& version,
const GURL& update_url);

// Struct for creating app entries in the update manifest XML.
struct UpdateManifestItem {
explicit UpdateManifestItem(ExtensionId id);
~UpdateManifestItem();
// We need copy items to be able to use them to initialize e.g. vector of
// items via {item1, item2, ...} syntax.
UpdateManifestItem(const UpdateManifestItem&);
UpdateManifestItem& operator=(const UpdateManifestItem&);
UpdateManifestItem(UpdateManifestItem&&);
UpdateManifestItem& operator=(UpdateManifestItem&&);

UpdateManifestItem&& codebase(std::string value) && {
updatecheck_params.emplace("codebase", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& hash(std::string value) && {
updatecheck_params.emplace("hash", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& info(std::string value) && {
updatecheck_params.emplace("info", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& prodversionmin(std::string value) && {
updatecheck_params.emplace("prodversionmin", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& status(std::string value) && {
updatecheck_params.emplace("status", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& version(std::string value) && {
updatecheck_params.emplace("version", std::move(value));
return std::move(*this);
}

ExtensionId id;
std::map<std::string, std::string> updatecheck_params;
};

// A generic method to create an XML update manifest. For each extension an
// extension ID should be provided along with parameters of the updatecheck
// tag.
std::string CreateUpdateManifest(
const std::vector<UpdateManifestItem>& extensions);

} // namespace extensions

#endif // EXTENSIONS_BROWSER_UPDATER_EXTENSION_DOWNLOADER_TEST_HELPER_H_
62 changes: 4 additions & 58 deletions extensions/browser/updater/extension_downloader_unittest.cc
Expand Up @@ -75,60 +75,6 @@ class ExtensionDownloaderTest : public ExtensionsTest {
return helper->downloader().pending_tasks_;
}

// Struct for creating app entries in the update manifest XML.
struct UpdateManifestItem {
explicit UpdateManifestItem(ExtensionId id) : id(std::move(id)) {}

UpdateManifestItem&& codebase(std::string value) && {
updatecheck_params.emplace("codebase", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& info(std::string value) && {
updatecheck_params.emplace("info", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& prodversionmin(std::string value) && {
updatecheck_params.emplace("prodversionmin", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& status(std::string value) && {
updatecheck_params.emplace("status", std::move(value));
return std::move(*this);
}
UpdateManifestItem&& version(std::string value) && {
updatecheck_params.emplace("version", std::move(value));
return std::move(*this);
}

ExtensionId id;
std::map<std::string, std::string> updatecheck_params;
};

// A generic method to create an XML update manifest. For each extension an
// extension ID should be provided along with parameters of the updatecheck
// tag.
std::string CreateUpdateManifest(
const std::vector<UpdateManifestItem>& extensions) {
std::string content =
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response'"
" protocol='2.0'>";
for (const auto& update_item : extensions) {
content += base::StringPrintf(
" <app appid='%s'>"
" <updatecheck",
update_item.id.c_str());
for (const auto& [name, value] : update_item.updatecheck_params) {
content += base::StringPrintf(" %s='%s'", name.c_str(), value.c_str());
}
content +=
" />"
" </app>";
}
content += "</gupdate>";
return content;
}

// Creates an update manifest for several extensions. Provided values should
// be tuples of (extension id, version, URL to the CRX file).
std::string CreateUpdateManifest(
Expand All @@ -141,7 +87,7 @@ class ExtensionDownloaderTest : public ExtensionsTest {
.version(version)
.prodversionmin("1.1"));
}
return CreateUpdateManifest(extensions_raw);
return extensions::CreateUpdateManifest(extensions_raw);
}

// Create an update manifest with one test extension.
Expand Down Expand Up @@ -361,9 +307,9 @@ TEST_F(ExtensionDownloaderTest, TestNoUpdatesManifestReports) {
GURL fetch_url = fetch->full_url();

const std::string kManifest =
CreateUpdateManifest({UpdateManifestItem(kTestExtensionId)
.status("noupdate")
.info("bandwidth limit")});
extensions::CreateUpdateManifest({UpdateManifestItem(kTestExtensionId)
.status("noupdate")
.info("bandwidth limit")});
helper.test_url_loader_factory().AddResponse(fetch_url.spec(), kManifest,
net::HTTP_OK);

Expand Down

0 comments on commit a83bf7b

Please sign in to comment.