File: manifest_test.h

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 (194 lines) | stat: -rw-r--r-- 6,786 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
// 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.

#ifndef EXTENSIONS_COMMON_MANIFEST_TEST_H_
#define EXTENSIONS_COMMON_MANIFEST_TEST_H_

#include <stddef.h>

#include <memory>
#include <optional>
#include <string>
#include <string_view>

#include "base/containers/span.h"
#include "base/memory/scoped_refptr.h"
#include "base/values.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest.h"
#include "extensions/common/mojom/manifest.mojom-shared.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {
class FilePath;
}

namespace extensions {

// Base class for tests that parse a manifest file.
class ManifestTest : public testing::Test {
 public:
  ManifestTest();

  ManifestTest(const ManifestTest&) = delete;
  ManifestTest& operator=(const ManifestTest&) = delete;

  ~ManifestTest() override;

 protected:
  // Helper class that simplifies creating methods that take either a filename
  // to a manifest or the manifest itself.
  class ManifestData {
   public:
    explicit ManifestData(std::string_view name);
    explicit ManifestData(base::Value::Dict manifest);
    ManifestData(base::Value::Dict manifest, std::string_view name);
    ManifestData(ManifestData&& other);
    ~ManifestData();

    // Constructs a ManifestData object from the given `json` string.
    // Calls ADD_FAILURE() if `json` is not valid JSON.
    static ManifestData FromJSON(std::string_view json);

    const std::string& name() const { return name_; }

    const std::optional<base::Value::Dict>& GetManifest(
        const base::FilePath& manifest_path,
        std::string* error) const;

   private:
    const std::string name_;
    mutable std::optional<base::Value::Dict> manifest_;
  };

  // Allows the test implementation to override a loaded test manifest's
  // extension ID. Useful for testing features behind a allowlist.
  virtual std::string GetTestExtensionID() const;

  // Returns the path in which to find test manifest data files, for example
  // extensions/test/data/manifest_tests.
  virtual base::FilePath GetTestDataDir();

  std::optional<base::Value::Dict> LoadManifest(char const* manifest_name,
                                                std::string* error);

  scoped_refptr<extensions::Extension> LoadExtension(
      const ManifestData& manifest,
      std::string* error,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  scoped_refptr<extensions::Extension> LoadAndExpectSuccess(
      const ManifestData& manifest,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  scoped_refptr<extensions::Extension> LoadAndExpectSuccess(
      char const* manifest_name,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  scoped_refptr<extensions::Extension> LoadAndExpectWarning(
      const ManifestData& manifest,
      const std::string& expected_warning,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  scoped_refptr<extensions::Extension> LoadAndExpectWarning(
      char const* manifest_name,
      const std::string& expected_warning,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  scoped_refptr<Extension> LoadAndExpectWarnings(
      const ManifestData& manifest,
      const std::vector<std::string>& expected_warnings,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  scoped_refptr<extensions::Extension> LoadAndExpectWarnings(
      char const* manifest_name,
      const std::vector<std::string>& expected_warnings,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  void VerifyExpectedError(extensions::Extension* extension,
                           const std::string& name,
                           const std::string& error,
                           const std::string& expected_error);

  void LoadAndExpectError(
      char const* manifest_name,
      const std::string& expected_error,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  void LoadAndExpectError(
      char const* manifest_name,
      const std::u16string& expected_error,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  void LoadAndExpectError(
      const ManifestData& manifest,
      const std::string& expected_error,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  void LoadAndExpectError(
      const ManifestData& manifest,
      const std::u16string& expected_error,
      mojom::ManifestLocation location = mojom::ManifestLocation::kInternal,
      int flags = extensions::Extension::NO_FLAGS);

  void AddPattern(extensions::URLPatternSet* extent,
                  const std::string& pattern);

  // used to differentiate between calls to LoadAndExpectError,
  // LoadAndExpectWarning and LoadAndExpectSuccess via function RunTestcases.
  enum ExpectType {
    EXPECT_TYPE_ERROR,
    EXPECT_TYPE_WARNING,
    EXPECT_TYPE_SUCCESS
  };

  struct Testcase {
    const std::string manifest_filename_;
    std::string expected_error_;  // only used for ExpectedError tests
    mojom::ManifestLocation location_;
    int flags_;

    Testcase(const std::string& manifest_filename,
             const std::string& expected_error,
             mojom::ManifestLocation location,
             int flags);

    Testcase(const std::string& manifest_filename,
             const std::u16string& expected_error,
             mojom::ManifestLocation location,
             int flags);

    Testcase(const std::string& manifest_filename,
             const std::string& expected_error);

    Testcase(const std::string& manifest_filename,
             const std::u16string& expected_error);

    explicit Testcase(const std::string& manifest_filename);

    Testcase(const std::string& manifest_filename,
             mojom::ManifestLocation location,
             int flags);
  };

  void RunTestcases(base::span<const Testcase> testcases, ExpectType type);

  void RunTestcase(const Testcase& testcase, ExpectType type);

  bool enable_apps_;
};

}  // namespace extensions

#endif  // EXTENSIONS_COMMON_MANIFEST_TEST_H_