1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/update_client/op_zucchini.h"
#include <optional>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/sequence_checker.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/types/expected.h"
#include "base/values.h"
#include "components/services/patch/in_process_file_patcher.h"
#include "components/update_client/crx_cache.h"
#include "components/update_client/patch/patch_impl.h"
#include "components/update_client/protocol_definition.h"
#include "components/update_client/test_utils.h"
#include "components/update_client/update_client_errors.h"
#include "components/zucchini/zucchini.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace update_client {
class ZucchiniOperationTest : public testing::Test {
private:
// env_ must be constructed before sequence_checker_.
base::test::TaskEnvironment env_;
protected:
void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
base::FilePath TempPath(const std::string& basename) {
return temp_dir_.GetPath().AppendUTF8(basename);
}
base::FilePath CopyToTemp(const std::string& src) {
base::FilePath dest =
TempPath(base::FilePath().AppendUTF8(src).BaseName().AsUTF8Unsafe());
EXPECT_TRUE(base::CopyFile(GetTestFilePath(src.c_str()), dest));
return dest;
}
base::RepeatingCallback<void(base::Value::Dict)> MakePingCallback() {
return base::BindLambdaForTesting(
[&](base::Value::Dict ping) { pings_.push_back(std::move(ping)); });
}
SEQUENCE_CHECKER(sequence_checker_);
base::RunLoop loop_;
std::vector<base::Value::Dict> pings_;
private:
base::ScopedTempDir temp_dir_;
};
TEST_F(ZucchiniOperationTest, Success) {
auto cache = base::MakeRefCounted<CrxCache>(TempPath("cache"));
// `ZucchiniOperation` deletes the patch file, so copy it to the temp dir.
base::FilePath patch_file =
CopyToTemp("zucchini_patch_test/app1_to_app2.zucchini");
// `CrxCache::Put` will move the v1 file, so copy it into the temp dir.
base::FilePath old_file = CopyToTemp("zucchini_patch_test/app1.zip");
cache->Put(
old_file, "appid", "hash1", {},
base::BindLambdaForTesting([&](base::expected<base::FilePath,
UnpackerError> r) {
ASSERT_TRUE(r.has_value());
ZucchiniOperation(
cache,
base::MakeRefCounted<PatchChromiumFactory>(
base::BindRepeating(&patch::LaunchInProcessFilePatcher))
->Create(),
MakePingCallback(), "hash1",
"30ab1a10edb5b33a63f61263f702b71c2ad3043773fef2a2122111ea542b765a",
patch_file,
base::BindLambdaForTesting(
[&](base::expected<base::FilePath, CategorizedError> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
loop_.Quit();
ASSERT_TRUE(result.has_value());
EXPECT_TRUE(base::ContentsEqual(
GetTestFilePath("zucchini_patch_test/app2.zip"),
result.value()));
}));
}));
loop_.Run();
EXPECT_FALSE(base::PathExists(patch_file));
ASSERT_EQ(pings_.size(), 1u);
EXPECT_EQ(pings_[0].FindInt("eventtype"), protocol_request::kEventZucchini);
EXPECT_EQ(pings_[0].FindInt("eventresult"),
protocol_request::kEventResultSuccess);
EXPECT_EQ(pings_[0].Find("errorcat"), nullptr);
EXPECT_EQ(pings_[0].Find("errorcode"), nullptr);
EXPECT_EQ(pings_[0].Find("extracode1"), nullptr);
}
TEST_F(ZucchiniOperationTest, BadPatch) {
auto cache = base::MakeRefCounted<CrxCache>(TempPath("cache"));
// ZucchiniOperation deletes the patch file, so copy it to the temp dir. For
// this test, use an malformed patch file - a copy of app2.zip is good
// enough.
base::FilePath patch_file = CopyToTemp("zucchini_patch_test/app2.zip");
// CrxCache::Put will move the v1 file, so copy it into the temp dir.
base::FilePath old_file = CopyToTemp("zucchini_patch_test/app1.zip");
cache->Put(
old_file, "appid", "hash1", {},
base::BindLambdaForTesting([&](base::expected<base::FilePath,
UnpackerError> r) {
ASSERT_TRUE(r.has_value());
ZucchiniOperation(
cache,
base::MakeRefCounted<PatchChromiumFactory>(
base::BindRepeating(&patch::LaunchInProcessFilePatcher))
->Create(),
MakePingCallback(), "hash1",
"30ab1a10edb5b33a63f61263f702b71c2ad3043773fef2a2122111ea542b765a",
patch_file,
base::BindLambdaForTesting(
[&](base::expected<base::FilePath, CategorizedError> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
loop_.Quit();
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error().code,
static_cast<int>(UnpackerError::kDeltaOperationFailure));
}));
}));
loop_.Run();
EXPECT_FALSE(base::PathExists(patch_file));
ASSERT_EQ(pings_.size(), 1u);
EXPECT_EQ(pings_[0].FindInt("eventtype"), protocol_request::kEventZucchini);
EXPECT_EQ(pings_[0].FindInt("eventresult"),
protocol_request::kEventResultError);
EXPECT_EQ(pings_[0].FindInt("errorcat"),
static_cast<int>(ErrorCategory::kUnpack));
EXPECT_EQ(pings_[0].FindInt("errorcode"),
static_cast<int>(UnpackerError::kDeltaOperationFailure));
EXPECT_EQ(pings_[0].FindInt("extracode1"),
static_cast<int>(zucchini::status::kStatusPatchReadError));
}
TEST_F(ZucchiniOperationTest, NotInCache) {
auto cache = base::MakeRefCounted<CrxCache>(TempPath("cache"));
// ZucchiniOperation deletes the patch file, so copy it to the temp dir.
base::FilePath patch_file =
CopyToTemp("zucchini_patch_test/app1_to_app2.zucchini");
ZucchiniOperation(
cache,
base::MakeRefCounted<PatchChromiumFactory>(
base::BindRepeating(&patch::LaunchInProcessFilePatcher))
->Create(),
MakePingCallback(), {},
"30ab1a10edb5b33a63f61263f702b71c2ad3043773fef2a2122111ea542b765a",
patch_file,
base::BindLambdaForTesting(
[&](base::expected<base::FilePath, CategorizedError> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
loop_.Quit();
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code,
static_cast<int>(UnpackerError::kCrxCacheFileNotCached));
}));
loop_.Run();
EXPECT_FALSE(base::PathExists(patch_file));
ASSERT_EQ(pings_.size(), 1u);
EXPECT_EQ(pings_[0].FindInt("eventtype"), protocol_request::kEventZucchini);
EXPECT_EQ(pings_[0].FindInt("eventresult"),
protocol_request::kEventResultError);
EXPECT_EQ(pings_[0].FindInt("errorcat"),
static_cast<int>(ErrorCategory::kUnpack));
EXPECT_EQ(pings_[0].FindInt("errorcode"),
static_cast<int>(UnpackerError::kCrxCacheFileNotCached));
EXPECT_EQ(pings_[0].Find("extracode1"), nullptr);
}
TEST_F(ZucchiniOperationTest, NoCache) {
// ZucchiniOperation deletes the patch file, so copy it to the temp dir.
base::FilePath patch_file =
CopyToTemp("zucchini_patch_test/app1_to_app2.zucchini");
ZucchiniOperation(
base::MakeRefCounted<CrxCache>(std::nullopt),
base::MakeRefCounted<PatchChromiumFactory>(
base::BindRepeating(&patch::LaunchInProcessFilePatcher))
->Create(),
MakePingCallback(), {},
"30ab1a10edb5b33a63f61263f702b71c2ad3043773fef2a2122111ea542b765a",
patch_file,
base::BindLambdaForTesting(
[&](base::expected<base::FilePath, CategorizedError> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
loop_.Quit();
ASSERT_FALSE(result.has_value());
EXPECT_EQ(result.error().code,
static_cast<int>(UnpackerError::kCrxCacheNotProvided));
}));
loop_.Run();
EXPECT_FALSE(base::PathExists(patch_file));
ASSERT_EQ(pings_.size(), 1u);
EXPECT_EQ(pings_[0].FindInt("eventtype"), protocol_request::kEventZucchini);
EXPECT_EQ(pings_[0].FindInt("eventresult"),
protocol_request::kEventResultError);
EXPECT_EQ(pings_[0].FindInt("errorcat"),
static_cast<int>(ErrorCategory::kUnpack));
EXPECT_EQ(pings_[0].FindInt("errorcode"),
static_cast<int>(UnpackerError::kCrxCacheNotProvided));
EXPECT_EQ(pings_[0].Find("extracode1"), nullptr);
}
TEST_F(ZucchiniOperationTest, OutHashMismatch) {
auto cache = base::MakeRefCounted<CrxCache>(TempPath("cache"));
// `ZucchiniOperation` deletes the patch file, so copy it to the temp dir.
base::FilePath patch_file =
CopyToTemp("zucchini_patch_test/app1_to_app2.zucchini");
// `CrxCache::Put` will move the v1 file, so copy it into the temp dir.
base::FilePath old_file = CopyToTemp("zucchini_patch_test/app1.zip");
cache->Put(
old_file, "appid", "hash1", {},
base::BindLambdaForTesting([&](base::expected<base::FilePath,
UnpackerError> r) {
ASSERT_TRUE(r.has_value());
ZucchiniOperation(
cache,
base::MakeRefCounted<PatchChromiumFactory>(
base::BindRepeating(&patch::LaunchInProcessFilePatcher))
->Create(),
MakePingCallback(), "hash1", "incorrecthash", patch_file,
base::BindLambdaForTesting(
[&](base::expected<base::FilePath, CategorizedError> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
loop_.Quit();
ASSERT_FALSE(result.has_value());
EXPECT_EQ(
result.error().code,
static_cast<int>(UnpackerError::kPatchOutHashMismatch));
}));
}));
loop_.Run();
EXPECT_FALSE(base::PathExists(patch_file));
ASSERT_EQ(pings_.size(), 1u);
EXPECT_EQ(pings_[0].FindInt("eventtype"), protocol_request::kEventZucchini);
EXPECT_EQ(pings_[0].FindInt("eventresult"),
protocol_request::kEventResultError);
EXPECT_EQ(pings_[0].FindInt("errorcat"),
static_cast<int>(ErrorCategory::kUnpack));
EXPECT_EQ(pings_[0].FindInt("errorcode"),
static_cast<int>(UnpackerError::kPatchOutHashMismatch));
EXPECT_EQ(pings_[0].Find("extracode1"), nullptr);
}
} // namespace update_client
|