File: zipfile_installer_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (346 lines) | stat: -rw-r--r-- 14,173 bytes parent folder | download | duplicates (3)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <vector>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/chrome_zipfile_installer.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_service_test_base.h"
#include "chrome/browser/extensions/load_error_reporter.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_profile.h"
#include "components/services/unzip/content/unzip_service.h"
#include "components/services/unzip/in_process_unzipper.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_features.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/settings/scoped_cros_settings_test_helper.h"
#endif

namespace extensions {

namespace {

struct MockExtensionRegistryObserver : public ExtensionRegistryObserver {
  void WaitForInstall(bool expect_error) {
    extensions::LoadErrorReporter* error_reporter =
        extensions::LoadErrorReporter::GetInstance();
    error_reporter->ClearErrors();
    while (true) {
      base::RunLoop run_loop;
      // We do not get a notification if installation fails. Make sure to wake
      // up and check for errors to get an error better than the test
      // timing-out.
      // TODO(jcivelli): make LoadErrorReporter::Observer report installation
      // failures for packaged extensions so we don't have to poll.
      base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
          FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(100));
      quit_closure = run_loop.QuitClosure();
      run_loop.Run();
      const std::vector<std::u16string>* errors = error_reporter->GetErrors();
      if (!errors->empty()) {
        if (!expect_error) {
          FAIL() << "Error(s) happened when unzipping extension: "
                 << (*errors)[0];
        }
        break;
      }
      if (!last_extension_installed.empty()) {
        // Extension install succeeded.
        EXPECT_FALSE(expect_error);
        break;
      }
    }
  }

  void OnExtensionInstalled(content::BrowserContext* browser_context,
                            const Extension* extension,
                            bool is_update) override {
    last_extension_installed = extension->id();
    last_extension_installed_path = extension->path();
    std::move(quit_closure).Run();
  }

  std::string last_extension_installed;
  base::FilePath last_extension_installed_path;
  base::OnceClosure quit_closure;
};

struct UnzipFileFilterTestCase {
  const base::FilePath::CharType* input;
  const bool should_unzip;
};

}  // namespace

// Assists with testing the non-installation location behavior of the installer.
class ZipFileInstallerTest : public ExtensionServiceTestBase {
 public:
  void SetUp() override {
    ExtensionServiceTestBase::SetUp();
    InitializeEmptyExtensionService();
    extensions::LoadErrorReporter::Init(/*enable_noisy_errors=*/false);
    in_process_utility_thread_helper_ =
        std::make_unique<content::InProcessUtilityThreadHelper>();
    unzip::SetUnzipperLaunchOverrideForTesting(
        base::BindRepeating(&unzip::LaunchInProcessUnzipper));
    registry()->AddObserver(&observer_);
  }

  void TearDown() override {
    registry()->RemoveObserver(&observer_);
    ExtensionServiceTestBase::TearDown();
    // Need to destruct ZipFileInstaller before the message loop since
    // it posts a task to it.
    zipfile_installer_.reset();
    unzip::SetUnzipperLaunchOverrideForTesting(base::NullCallback());
    base::RunLoop().RunUntilIdle();
  }

 protected:
  scoped_refptr<ZipFileInstaller> zipfile_installer_;

  std::unique_ptr<content::InProcessUtilityThreadHelper>
      in_process_utility_thread_helper_;
  MockExtensionRegistryObserver observer_;

 private:
  data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
};

// Assists with testing the zip file filtering behavior of ZipFileInstaller.
class ZipFileInstallerFilterTest : public ZipFileInstallerTest {
 protected:
  void RunZipFileFilterTest(
      const std::vector<UnzipFileFilterTestCase>& cases,
      base::RepeatingCallback<bool(const base::FilePath&)>& filter) {
    for (size_t i = 0; i < cases.size(); ++i) {
      base::FilePath input(cases[i].input);
      bool observed = filter.Run(input);
      EXPECT_EQ(cases[i].should_unzip, observed)
          << "i: " << i << ", input: " << input.value();
    }
  }
};

TEST_F(ZipFileInstallerFilterTest, NonTheme_FileExtractionFilter) {
  const std::vector<UnzipFileFilterTestCase> cases = {
      {FILE_PATH_LITERAL("foo"), true},
      {FILE_PATH_LITERAL("foo.nexe"), true},
      {FILE_PATH_LITERAL("foo.dll"), true},
      {FILE_PATH_LITERAL("foo.jpg.exe"), false},
      {FILE_PATH_LITERAL("foo.exe"), false},
      {FILE_PATH_LITERAL("foo.EXE"), false},
      {FILE_PATH_LITERAL("file_without_extension"), true},
  };
  base::RepeatingCallback<bool(const base::FilePath&)> filter =
      base::BindRepeating(&ZipFileInstaller::ShouldExtractFile, false);
  RunZipFileFilterTest(cases, filter);
}

TEST_F(ZipFileInstallerFilterTest, Theme_FileExtractionFilter) {
  const std::vector<UnzipFileFilterTestCase> cases = {
      {FILE_PATH_LITERAL("image.jpg"), true},
      {FILE_PATH_LITERAL("IMAGE.JPEG"), true},
      {FILE_PATH_LITERAL("test/image.bmp"), true},
      {FILE_PATH_LITERAL("test/IMAGE.gif"), true},
      {FILE_PATH_LITERAL("test/image.WEBP"), true},
      {FILE_PATH_LITERAL("test/dir/file.image.png"), true},
      {FILE_PATH_LITERAL("manifest.json"), true},
      {FILE_PATH_LITERAL("other.html"), false},
      {FILE_PATH_LITERAL("file_without_extension"), true},
  };
  base::RepeatingCallback<bool(const base::FilePath&)> filter =
      base::BindRepeating(&ZipFileInstaller::ShouldExtractFile, true);
  RunZipFileFilterTest(cases, filter);
}

TEST_F(ZipFileInstallerFilterTest, ManifestExtractionFilter) {
  const std::vector<UnzipFileFilterTestCase> cases = {
      {FILE_PATH_LITERAL("manifest.json"), true},
      {FILE_PATH_LITERAL("MANIFEST.JSON"), true},
      {FILE_PATH_LITERAL("test/manifest.json"), false},
      {FILE_PATH_LITERAL("manifest.json/test"), false},
      {FILE_PATH_LITERAL("other.file"), false},
  };
  base::RepeatingCallback<bool(const base::FilePath&)> filter =
      base::BindRepeating(&ZipFileInstaller::IsManifestFile);
  RunZipFileFilterTest(cases, filter);
}

class ZipFileInstallerLocationTest : public ZipFileInstallerTest,
                                     public testing::WithParamInterface<bool> {
 public:
  void SetUp() override {
    ZipFileInstallerTest::SetUp();
    expected_extension_install_directory_ =
        registrar()->unpacked_install_directory();
  }

  // Install the .zip in the test directory with `zip_name` and `expect_error`
  // if it should fail.
  void RunInstaller(const std::string& zip_name,
                    bool expect_error,
                    base::FilePath unzip_dir_root = base::FilePath());

 protected:
  base::FilePath expected_extension_install_directory_;
};

void ZipFileInstallerLocationTest::RunInstaller(const std::string& zip_name,
                                                bool expect_error,
                                                base::FilePath unzip_dir_root) {
  base::FilePath original_zip_path;
  ASSERT_TRUE(
      base::PathService::Get(chrome::DIR_TEST_DATA, &original_zip_path));
  original_zip_path = original_zip_path.AppendASCII("extensions")
                          .AppendASCII("zipfile_installer")
                          .AppendASCII(zip_name);
  ASSERT_TRUE(base::PathExists(original_zip_path)) << original_zip_path.value();
  zipfile_installer_ = ZipFileInstaller::Create(
      GetExtensionFileTaskRunner(),
      MakeRegisterInExtensionServiceCallback(profile()));

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&ZipFileInstaller::InstallZipFileToUnpackedExtensionsDir,
                     zipfile_installer_, original_zip_path,
                     unzip_dir_root.empty()
                         ? registrar()->unpacked_install_directory()
                         : unzip_dir_root));
  observer_.WaitForInstall(expect_error);
  task_environment()->RunUntilIdle();
}

// Tests that a normal .zip is installed into the expected install path.
TEST_F(ZipFileInstallerLocationTest, GoodZip) {
  RunInstaller(/*zip_name=*/"good.zip",
               /*expect_error=*/false);

  // Expect extension install directory to be immediate subdir of expected
  // temp install directory. E.g. /a/b/c/d == /a/b/c + /d.
  //
  // Make sure we're comparing absolute paths to avoid failures like
  // https://crbug.com/1453669 on macOS 14.
  base::FilePath absolute_last_extension_installed_path =
      base::MakeAbsoluteFilePath(observer_.last_extension_installed_path);
  base::FilePath absolute_expected_extension_install_directory =
      base::MakeAbsoluteFilePath(expected_extension_install_directory_.Append(
          observer_.last_extension_installed_path.BaseName()));
  EXPECT_EQ(absolute_last_extension_installed_path,
            absolute_expected_extension_install_directory);
}

TEST_F(ZipFileInstallerLocationTest, BadZip) {
  // Manifestless archive.
  RunInstaller(/*zip_name=*/"bad.zip",
               /*expect_error=*/true);
}

// Tests installing the same .zip twice results in two separate install
// directories.
TEST_F(ZipFileInstallerLocationTest, MultipleSameZipInstallSeparately) {
  RunInstaller(/*zip_name=*/"good.zip",
               /*expect_error=*/false);

  base::FilePath dir_temp;
  base::PathService::Get(base::DIR_TEMP, &dir_temp);
  base::FilePath first_install_path = observer_.last_extension_installed_path;
  // Expect extension install directory to be immediate subdir of expected
  // unpacked install directory. E.g. /a/b/c/d == /a/b/c + /d.
  //
  // Make sure we're comparing absolute paths to avoid failures like
  // https://crbug.com/1453669 on macOS 14.
  base::FilePath absolute_last_extension_installed_path =
      base::MakeAbsoluteFilePath(observer_.last_extension_installed_path);
  base::FilePath absolute_expected_extension_install_directory =
      base::MakeAbsoluteFilePath(expected_extension_install_directory_.Append(
          observer_.last_extension_installed_path.BaseName()));
  EXPECT_EQ(absolute_last_extension_installed_path,
            absolute_expected_extension_install_directory);

  RunInstaller(/*zip_name=*/"good.zip",
               /*expect_error=*/false);

  base::FilePath second_install_path = observer_.last_extension_installed_path;
  // Expect extension install directory to be immediate subdir of expected
  // unpacked install directory. E.g. /a/b/c/d == /a/b/c + /d.
  absolute_last_extension_installed_path =
      base::MakeAbsoluteFilePath(observer_.last_extension_installed_path);
  absolute_expected_extension_install_directory =
      base::MakeAbsoluteFilePath(expected_extension_install_directory_.Append(
          observer_.last_extension_installed_path.BaseName()));
  EXPECT_EQ(absolute_last_extension_installed_path,
            absolute_expected_extension_install_directory);

  // Confirm that the two extensions are installed in two separate
  // directories.
  EXPECT_NE(first_install_path, second_install_path);
}

// Tests that we error when we cannot create the parent directory of where to
// install the .zips to.
TEST_F(ZipFileInstallerLocationTest, CannotCreateContainingDirectoryZip) {
  // TODO(crbug.com/40875193): Have this expect a specific error rather than
  // just an error since other things can cause an error.
  RunInstaller(
      /*zip_name=*/"good.zip", /*expect_error=*/true, /*unzip_dir_root=*/
#if !BUILDFLAG(IS_WIN)
      base::FilePath(
          FILE_PATH_LITERAL("/NonExistentDirectory/UnpackedExtensions"))
#else
      // Windows will create unexpected paths so we use explicitly disallowed
      // characters in the Windows filesystem to ensure creating this directory
      // fails.
      base::FilePath(
          FILE_PATH_LITERAL("|<IllegalWinDirName>|/UnpackedExtensions"))
#endif  // !BUILDFLAG(IS_WIN)
  );
}

// Tests that a .zip with a public key installs with the expected extension ID
// and to the correct path.
TEST_F(ZipFileInstallerLocationTest, ZipWithPublicKey) {
  RunInstaller(/*zip_name=*/"public_key.zip",
               /*expect_error=*/false);
  const char kIdForPublicKey[] = "ikppjpenhoddphklkpdfdfdabbakkpal";
  EXPECT_EQ(observer_.last_extension_installed, kIdForPublicKey);

  // Make sure we compare absolute paths to avoid failures like
  // https://crbug.com/1453669 on macOS 14.
  base::FilePath absolute_last_extension_installed_path =
      base::MakeAbsoluteFilePath(observer_.last_extension_installed_path);
  base::FilePath absolute_expected_extension_install_directory =
      base::MakeAbsoluteFilePath(expected_extension_install_directory_.Append(
          observer_.last_extension_installed_path.BaseName()));
  EXPECT_EQ(absolute_last_extension_installed_path,
            absolute_expected_extension_install_directory);
}

}  // namespace extensions