Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / extensions / common / manifest_handler_unittest.cc
blob4c31c99364dec38f5306e788980f6f392a1b36f2
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <string>
6 #include <vector>
8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/stl_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_builder.h"
14 #include "extensions/common/install_warning.h"
15 #include "extensions/common/manifest_handler.h"
16 #include "extensions/common/value_builder.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace extensions {
21 namespace {
23 std::vector<std::string> SingleKey(const std::string& key) {
24 return std::vector<std::string>(1, key);
27 } // namespace
29 class ScopedTestingManifestHandlerRegistry {
30 public:
31 ScopedTestingManifestHandlerRegistry() {
32 old_registry_ = ManifestHandlerRegistry::SetForTesting(&registry_);
35 ~ScopedTestingManifestHandlerRegistry() {
36 ManifestHandlerRegistry::SetForTesting(old_registry_);
39 ManifestHandlerRegistry registry_;
40 ManifestHandlerRegistry* old_registry_;
43 class ManifestHandlerTest : public testing::Test {
44 public:
45 class ParsingWatcher {
46 public:
47 // Called when a manifest handler parses.
48 void Record(const std::string& name) {
49 parsed_names_.push_back(name);
52 const std::vector<std::string>& parsed_names() {
53 return parsed_names_;
56 // Returns true if |name_before| was parsed before |name_after|.
57 bool ParsedBefore(const std::string& name_before,
58 const std::string& name_after) {
59 size_t i_before = parsed_names_.size();
60 size_t i_after = 0;
61 for (size_t i = 0; i < parsed_names_.size(); ++i) {
62 if (parsed_names_[i] == name_before)
63 i_before = i;
64 if (parsed_names_[i] == name_after)
65 i_after = i;
67 if (i_before < i_after)
68 return true;
69 return false;
72 private:
73 // The order of manifest handlers that we watched parsing.
74 std::vector<std::string> parsed_names_;
77 class TestManifestHandler : public ManifestHandler {
78 public:
79 TestManifestHandler(const std::string& name,
80 const std::vector<std::string>& keys,
81 const std::vector<std::string>& prereqs,
82 ParsingWatcher* watcher)
83 : name_(name), keys_(keys), prereqs_(prereqs), watcher_(watcher) {
86 bool Parse(Extension* extension, base::string16* error) override {
87 watcher_->Record(name_);
88 return true;
91 const std::vector<std::string> PrerequisiteKeys() const override {
92 return prereqs_;
95 protected:
96 std::string name_;
97 std::vector<std::string> keys_;
98 std::vector<std::string> prereqs_;
99 ParsingWatcher* watcher_;
101 const std::vector<std::string> Keys() const override { return keys_; }
104 class FailingTestManifestHandler : public TestManifestHandler {
105 public:
106 FailingTestManifestHandler(const std::string& name,
107 const std::vector<std::string>& keys,
108 const std::vector<std::string>& prereqs,
109 ParsingWatcher* watcher)
110 : TestManifestHandler(name, keys, prereqs, watcher) {
112 bool Parse(Extension* extension, base::string16* error) override {
113 *error = base::ASCIIToUTF16(name_);
114 return false;
118 class AlwaysParseTestManifestHandler : public TestManifestHandler {
119 public:
120 AlwaysParseTestManifestHandler(const std::string& name,
121 const std::vector<std::string>& keys,
122 const std::vector<std::string>& prereqs,
123 ParsingWatcher* watcher)
124 : TestManifestHandler(name, keys, prereqs, watcher) {
127 bool AlwaysParseForType(Manifest::Type type) const override { return true; }
130 class TestManifestValidator : public ManifestHandler {
131 public:
132 TestManifestValidator(bool return_value,
133 bool always_validate,
134 std::vector<std::string> keys)
135 : return_value_(return_value),
136 always_validate_(always_validate),
137 keys_(keys) {
140 bool Parse(Extension* extension, base::string16* error) override {
141 return true;
144 bool Validate(const Extension* extension,
145 std::string* error,
146 std::vector<InstallWarning>* warnings) const override {
147 return return_value_;
150 bool AlwaysValidateForType(Manifest::Type type) const override {
151 return always_validate_;
154 private:
155 const std::vector<std::string> Keys() const override { return keys_; }
157 protected:
158 bool return_value_;
159 bool always_validate_;
160 std::vector<std::string> keys_;
164 TEST_F(ManifestHandlerTest, DependentHandlers) {
165 ScopedTestingManifestHandlerRegistry registry;
166 ParsingWatcher watcher;
167 std::vector<std::string> prereqs;
168 (new TestManifestHandler("A", SingleKey("a"), prereqs, &watcher))->Register();
169 (new TestManifestHandler("B", SingleKey("b"), prereqs, &watcher))->Register();
170 (new TestManifestHandler("J", SingleKey("j"), prereqs, &watcher))->Register();
171 (new AlwaysParseTestManifestHandler("K", SingleKey("k"), prereqs, &watcher))->
172 Register();
173 prereqs.push_back("c.d");
174 std::vector<std::string> keys;
175 keys.push_back("c.e");
176 keys.push_back("c.z");
177 (new TestManifestHandler("C.EZ", keys, prereqs, &watcher))->Register();
178 prereqs.clear();
179 prereqs.push_back("b");
180 prereqs.push_back("k");
181 (new TestManifestHandler("C.D", SingleKey("c.d"), prereqs, &watcher))->
182 Register();
183 ManifestHandler::FinalizeRegistration();
185 scoped_refptr<Extension> extension = ExtensionBuilder()
186 .SetManifest(DictionaryBuilder()
187 .Set("name", "no name")
188 .Set("version", "0")
189 .Set("manifest_version", 2)
190 .Set("a", 1)
191 .Set("b", 2)
192 .Set("c", DictionaryBuilder()
193 .Set("d", 3)
194 .Set("e", 4)
195 .Set("f", 5))
196 .Set("g", 6))
197 .Build();
199 // A, B, C.EZ, C.D, K
200 EXPECT_EQ(5u, watcher.parsed_names().size());
201 EXPECT_TRUE(watcher.ParsedBefore("B", "C.D"));
202 EXPECT_TRUE(watcher.ParsedBefore("K", "C.D"));
203 EXPECT_TRUE(watcher.ParsedBefore("C.D", "C.EZ"));
206 TEST_F(ManifestHandlerTest, FailingHandlers) {
207 ScopedTestingManifestHandlerRegistry registry;
208 // Can't use ExtensionBuilder, because this extension will fail to
209 // be parsed.
210 scoped_ptr<base::DictionaryValue> manifest_a(
211 DictionaryBuilder()
212 .Set("name", "no name")
213 .Set("version", "0")
214 .Set("manifest_version", 2)
215 .Set("a", 1)
216 .Build());
218 // Succeeds when "a" is not recognized.
219 std::string error;
220 scoped_refptr<Extension> extension = Extension::Create(
221 base::FilePath(),
222 Manifest::INVALID_LOCATION,
223 *manifest_a,
224 Extension::NO_FLAGS,
225 &error);
226 EXPECT_TRUE(extension.get());
228 // Register a handler for "a" that fails.
229 ParsingWatcher watcher;
230 (new FailingTestManifestHandler(
231 "A", SingleKey("a"), std::vector<std::string>(), &watcher))->Register();
232 ManifestHandler::FinalizeRegistration();
234 extension = Extension::Create(
235 base::FilePath(),
236 Manifest::INVALID_LOCATION,
237 *manifest_a,
238 Extension::NO_FLAGS,
239 &error);
240 EXPECT_FALSE(extension.get());
241 EXPECT_EQ("A", error);
244 TEST_F(ManifestHandlerTest, Validate) {
245 ScopedTestingManifestHandlerRegistry registry;
246 scoped_refptr<Extension> extension = ExtensionBuilder()
247 .SetManifest(DictionaryBuilder()
248 .Set("name", "no name")
249 .Set("version", "0")
250 .Set("manifest_version", 2)
251 .Set("a", 1)
252 .Set("b", 2))
253 .Build();
254 EXPECT_TRUE(extension.get());
256 std::string error;
257 std::vector<InstallWarning> warnings;
258 // Always validates and fails.
259 (new TestManifestValidator(false, true, SingleKey("c")))->Register();
260 EXPECT_FALSE(
261 ManifestHandler::ValidateExtension(extension.get(), &error, &warnings));
263 // This overrides the registered handler for "c".
264 (new TestManifestValidator(false, false, SingleKey("c")))->Register();
265 EXPECT_TRUE(
266 ManifestHandler::ValidateExtension(extension.get(), &error, &warnings));
268 // Validates "a" and fails.
269 (new TestManifestValidator(false, true, SingleKey("a")))->Register();
270 EXPECT_FALSE(
271 ManifestHandler::ValidateExtension(extension.get(), &error, &warnings));
274 } // namespace extensions