File: manifest_test.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 (357 lines) | stat: -rw-r--r-- 13,006 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
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
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2012 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/common/manifest_test.h"

#include <optional>
#include <string_view>
#include <utility>

#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/path_service.h"
#include "base/strings/pattern.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "extensions/common/extension_l10n_util.h"
#include "extensions/common/extension_paths.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/l10n/l10n_util.h"

using extensions::mojom::ManifestLocation;

namespace extensions {
namespace {

std::string GetNameFromManifest(const base::Value::Dict& manifest) {
  const std::string* name = manifest.FindString(manifest_keys::kName);
  return name ? *name : std::string();
}

// |manifest_path| is an absolute path to a manifest file.
std::optional<base::Value::Dict> LoadManifestFile(
    const base::FilePath& manifest_path,
    std::string* error) {
  base::FilePath extension_path = manifest_path.DirName();

  EXPECT_TRUE(base::PathExists(manifest_path)) <<
      "Couldn't find " << manifest_path.value();

  JSONFileValueDeserializer deserializer(manifest_path);
  std::unique_ptr<base::Value> manifest =
      deserializer.Deserialize(nullptr, error);

  if (!manifest || !manifest->is_dict()) {
    return std::nullopt;
  }

  // Most unit tests don't need localization, and they'll fail if we try to
  // localize them, since their manifests don't have a default_locale key.
  // Only localize manifests that indicate they want to be localized.
  // Calling LocalizeExtension at this point mirrors file_util::LoadExtension.
  if (base::Contains(manifest_path.value(), FILE_PATH_LITERAL("localized"))) {
    extension_l10n_util::LocalizeExtension(
        extension_path, manifest->GetIfDict(),
        extension_l10n_util::GzippedMessagesPermission::kDisallow, error);
  }

  return std::move(*manifest).TakeDict();
}

}  // namespace

ManifestTest::ManifestTest()
    : enable_apps_(true) {
}

ManifestTest::~ManifestTest() = default;

// Helper class that simplifies creating methods that take either a filename
// to a manifest or the manifest itself.
ManifestTest::ManifestData::ManifestData(std::string_view name) : name_(name) {}

ManifestTest::ManifestData::ManifestData(base::Value::Dict manifest,
                                         std::string_view name)
    : name_(name), manifest_(std::move(manifest)) {}

ManifestTest::ManifestData::ManifestData(base::Value::Dict manifest)
    : name_(GetNameFromManifest(manifest)), manifest_(std::move(manifest)) {}

ManifestTest::ManifestData::ManifestData(ManifestData&& other) = default;
ManifestTest::ManifestData::~ManifestData() = default;

const std::optional<base::Value::Dict>& ManifestTest::ManifestData::GetManifest(
    const base::FilePath& test_data_dir,
    std::string* error) const {
  if (!manifest_) {
    base::FilePath manifest_path = test_data_dir.AppendASCII(name_);
    manifest_ = LoadManifestFile(manifest_path, error);
  }
  return manifest_;
}

// static
ManifestTest::ManifestData ManifestTest::ManifestData::FromJSON(
    std::string_view json) {
  // ParseJsonDict() will ADD_FAILURE() if `json` is not a valid dict.
  base::Value::Dict manifest_dict = base::test::ParseJsonDict(json);
  return ManifestData(std::move(manifest_dict));
}

std::string ManifestTest::GetTestExtensionID() const {
  return std::string();
}

base::FilePath ManifestTest::GetTestDataDir() {
  base::FilePath path;
  base::PathService::Get(DIR_TEST_DATA, &path);
  return path.AppendASCII("manifest_tests");
}

std::optional<base::Value::Dict> ManifestTest::LoadManifest(
    char const* manifest_name,
    std::string* error) {
  base::FilePath manifest_path = GetTestDataDir().AppendASCII(manifest_name);
  return LoadManifestFile(manifest_path, error);
}

scoped_refptr<Extension> ManifestTest::LoadExtension(
    const ManifestData& manifest,
    std::string* error,
    ManifestLocation location,
    int flags) {
  base::FilePath test_data_dir = GetTestDataDir();
  const std::optional<base::Value::Dict>& dict =
      manifest.GetManifest(test_data_dir, error);
  if (!dict) {
    return nullptr;
  }
  return Extension::Create(test_data_dir.DirName(), location, *dict, flags,
                           GetTestExtensionID(), error);
}

scoped_refptr<Extension> ManifestTest::LoadAndExpectSuccess(
    const ManifestData& manifest,
    ManifestLocation location,
    int flags) {
  std::string error;
  scoped_refptr<Extension> extension =
      LoadExtension(manifest, &error, location, flags);
  EXPECT_TRUE(extension.get()) << manifest.name();
  EXPECT_EQ(std::string(), error) << manifest.name();
  return extension;
}

scoped_refptr<Extension> ManifestTest::LoadAndExpectSuccess(
    char const* manifest_name,
    ManifestLocation location,
    int flags) {
  return LoadAndExpectSuccess(ManifestData(manifest_name), location, flags);
}

scoped_refptr<Extension> ManifestTest::LoadAndExpectWarning(
    const ManifestData& manifest,
    const std::string& expected_warning,
    ManifestLocation location,
    int flags) {
  std::string error;
  scoped_refptr<Extension> extension =
      LoadExtension(manifest, &error, location, flags);
  EXPECT_TRUE(extension.get()) << manifest.name();
  EXPECT_EQ(std::string(), error) << manifest.name();
  EXPECT_EQ(1u, extension->install_warnings().size());
  if (extension->install_warnings().size() == 1) {
    EXPECT_EQ(expected_warning, extension->install_warnings()[0].message);
  }
  return extension;
}

scoped_refptr<Extension> ManifestTest::LoadAndExpectWarning(
    char const* manifest_name,
    const std::string& expected_warning,
    ManifestLocation location,
    int flags) {
  return LoadAndExpectWarning(
      ManifestData(manifest_name), expected_warning, location, flags);
}

scoped_refptr<Extension> ManifestTest::LoadAndExpectWarnings(
    const ManifestData& manifest,
    const std::vector<std::string>& expected_warnings,
    ManifestLocation location,
    int flags) {
  std::string error;
  scoped_refptr<Extension> extension =
      LoadExtension(manifest, &error, location, flags);
  EXPECT_TRUE(extension) << manifest.name();
  EXPECT_EQ(std::string(), error) << manifest.name();
  EXPECT_EQ(expected_warnings.size(), extension->install_warnings().size());

  std::vector<std::string> warning_messages;
  warning_messages.reserve(extension->install_warnings().size());
  for (const auto& warning : extension->install_warnings()) {
    warning_messages.push_back(warning.message);
  }

  EXPECT_THAT(warning_messages,
              testing::UnorderedElementsAreArray(expected_warnings));
  return extension;
}

scoped_refptr<Extension> ManifestTest::LoadAndExpectWarnings(
    char const* manifest_name,
    const std::vector<std::string>& expected_warnings,
    ManifestLocation location,
    int flags) {
  std::string error;
  scoped_refptr<Extension> extension =
      LoadExtension(ManifestData(manifest_name), &error, location, flags);
  EXPECT_TRUE(extension.get()) << manifest_name;
  EXPECT_EQ(std::string(), error) << manifest_name;

  std::vector<std::string> warning_messages;
  warning_messages.reserve(extension->install_warnings().size());
  for (const auto& warning : extension->install_warnings()) {
    warning_messages.push_back(warning.message);
  }

  EXPECT_THAT(warning_messages,
              testing::UnorderedElementsAreArray(expected_warnings));
  return extension;
}

void ManifestTest::VerifyExpectedError(
    Extension* extension,
    const std::string& name,
    const std::string& error,
    const std::string& expected_error) {
  EXPECT_FALSE(extension) <<
      "Expected failure loading extension '" << name <<
      "', but didn't get one.";
  EXPECT_TRUE(base::MatchPattern(error, expected_error))
      << name << " expected '" << expected_error << "' but got '" << error
      << "'";
}

void ManifestTest::LoadAndExpectError(const ManifestData& manifest,
                                      const std::string& expected_error,
                                      ManifestLocation location,
                                      int flags) {
  std::string error;
  scoped_refptr<Extension> extension(
      LoadExtension(manifest, &error, location, flags));
  VerifyExpectedError(extension.get(), manifest.name(), error,
                      expected_error);
}

void ManifestTest::LoadAndExpectError(const ManifestData& manifest,
                                      const std::u16string& expected_error,
                                      ManifestLocation location,
                                      int flags) {
  return LoadAndExpectError(manifest, base::UTF16ToUTF8(expected_error),
                            location, flags);
}

void ManifestTest::LoadAndExpectError(char const* manifest_name,
                                      const std::string& expected_error,
                                      ManifestLocation location,
                                      int flags) {
  return LoadAndExpectError(
      ManifestData(manifest_name), expected_error, location, flags);
}

void ManifestTest::LoadAndExpectError(char const* manifest_name,
                                      const std::u16string& expected_error,
                                      ManifestLocation location,
                                      int flags) {
  return LoadAndExpectError(ManifestData(manifest_name),
                            base::UTF16ToUTF8(expected_error), location, flags);
}

void ManifestTest::AddPattern(extensions::URLPatternSet* extent,
                              const std::string& pattern) {
  int schemes = URLPattern::SCHEME_ALL;
  extent->AddPattern(URLPattern(schemes, pattern));
}

ManifestTest::Testcase::Testcase(const std::string& manifest_filename,
                                 const std::string& expected_error,
                                 ManifestLocation location,
                                 int flags)
    : manifest_filename_(manifest_filename),
      expected_error_(expected_error),
      location_(location),
      flags_(flags) {}

ManifestTest::Testcase::Testcase(const std::string& manifest_filename,
                                 const std::u16string& expected_error,
                                 ManifestLocation location,
                                 int flags)
    : Testcase(manifest_filename,
               base::UTF16ToUTF8(expected_error),
               location,
               flags) {}

ManifestTest::Testcase::Testcase(const std::string& manifest_filename,
                                 const std::string& expected_error)
    : manifest_filename_(manifest_filename),
      expected_error_(expected_error),
      location_(ManifestLocation::kInternal),
      flags_(Extension::NO_FLAGS) {}

ManifestTest::Testcase::Testcase(const std::string& manifest_filename,
                                 const std::u16string& expected_error)
    : Testcase(manifest_filename, base::UTF16ToUTF8(expected_error)) {}

ManifestTest::Testcase::Testcase(const std::string& manifest_filename)
    : manifest_filename_(manifest_filename),
      location_(ManifestLocation::kInternal),
      flags_(Extension::NO_FLAGS) {}

ManifestTest::Testcase::Testcase(const std::string& manifest_filename,
                                 ManifestLocation location,
                                 int flags)
    : manifest_filename_(manifest_filename),
      location_(location),
      flags_(flags) {}

void ManifestTest::RunTestcases(base::span<const Testcase> testcases,
                                ExpectType type) {
  for (const auto& testcase : testcases) {
    RunTestcase(testcase, type);
  }
}

void ManifestTest::RunTestcase(const Testcase& testcase, ExpectType type) {
  SCOPED_TRACE(base::StringPrintf("Testing file '%s'",
                                  testcase.manifest_filename_.c_str()));

  switch (type) {
    case EXPECT_TYPE_ERROR:
      LoadAndExpectError(testcase.manifest_filename_.c_str(),
                         testcase.expected_error_,
                         testcase.location_,
                         testcase.flags_);
      break;
    case EXPECT_TYPE_WARNING:
      LoadAndExpectWarning(testcase.manifest_filename_.c_str(),
                           testcase.expected_error_,
                           testcase.location_,
                           testcase.flags_);
      break;
    case EXPECT_TYPE_SUCCESS:
      LoadAndExpectSuccess(testcase.manifest_filename_.c_str(),
                           testcase.location_,
                           testcase.flags_);
      break;
   }
}

}  // namespace extensions