File: test_extension_dir.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 3,649 bytes parent folder | download | duplicates (6)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/test/test_extension_dir.h"

#include <string_view>
#include <tuple>

#include "base/files/file_util.h"
#include "base/json/json_writer.h"
#include "base/numerics/checked_math.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "extensions/browser/extension_creator.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

TestExtensionDir::TestExtensionDir() {
  base::ScopedAllowBlockingForTesting allow_blocking;
  EXPECT_TRUE(dir_.CreateUniqueTempDir());
  EXPECT_TRUE(crx_dir_.CreateUniqueTempDir());
}

TestExtensionDir::~TestExtensionDir() {
  base::ScopedAllowBlockingForTesting allow_blocking;
  std::ignore = dir_.Delete();
  std::ignore = crx_dir_.Delete();
}

TestExtensionDir::TestExtensionDir(TestExtensionDir&&) noexcept = default;

TestExtensionDir& TestExtensionDir::operator=(TestExtensionDir&&) = default;

void TestExtensionDir::WriteManifest(std::string_view manifest) {
  WriteFile(FILE_PATH_LITERAL("manifest.json"), manifest);
}

void TestExtensionDir::WriteManifest(const base::Value::Dict& manifest) {
  std::string manifest_out;
  base::JSONWriter::WriteWithOptions(
      manifest, base::JSONWriter::OPTIONS_PRETTY_PRINT, &manifest_out);
  WriteManifest(manifest_out);
}

void TestExtensionDir::WriteFile(const base::FilePath::StringType& filename,
                                 std::string_view contents) {
  base::ScopedAllowBlockingForTesting allow_blocking;
  EXPECT_TRUE(base::WriteFile(dir_.GetPath().Append(filename), contents));
}

void TestExtensionDir::CopyFileTo(
    const base::FilePath& from_path,
    const base::FilePath::StringType& local_filename) {
  base::ScopedAllowBlockingForTesting allow_blocking;
  ASSERT_TRUE(base::PathExists(from_path)) << from_path;
  EXPECT_TRUE(base::CopyFile(from_path, dir_.GetPath().Append(local_filename)))
      << "Failed to copy file from " << from_path << " to " << local_filename;
}

base::FilePath TestExtensionDir::Pack(std::string_view custom_path) {
  base::ScopedAllowBlockingForTesting allow_blocking;
  ExtensionCreator creator;
  base::FilePath crx_path = crx_dir_.GetPath().AppendASCII(
      custom_path.empty() ? "ext.crx" : custom_path);
  base::FilePath pem_path =
      crx_dir_.GetPath().Append(FILE_PATH_LITERAL("ext.pem"));
  base::FilePath pem_in_path, pem_out_path;
  if (base::PathExists(pem_path))
    pem_in_path = pem_path;
  else
    pem_out_path = pem_path;
  if (!creator.Run(dir_.GetPath(), crx_path, pem_in_path, pem_out_path,
                   ExtensionCreator::kOverwriteCRX)) {
    ADD_FAILURE() << "ExtensionCreator::Run() failed: "
                  << creator.error_message();
    return base::FilePath();
  }
  if (!base::PathExists(crx_path)) {
    ADD_FAILURE() << crx_path.value() << " was not created.";
    return base::FilePath();
  }
  return crx_path;
}

base::FilePath TestExtensionDir::UnpackedPath() const {
  base::ScopedAllowBlockingForTesting allow_blocking;
  // We make this absolute because it's possible that dir_ contains a symlink as
  // part of it's path. When UnpackedInstaller::GetAbsolutePath() runs as part
  // of loading the extension, the extension's path is converted to an absolute
  // path, which actually does something like `realpath` as part of its
  // resolution. If the tests are comparing paths to UnpackedPath(), then
  // they'll need to compare the same absolute'd path.
  return base::MakeAbsoluteFilePath(dir_.GetPath());
}

}  // namespace extensions