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.
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
{
23 std::vector
<std::string
> SingleKey(const std::string
& key
) {
24 return std::vector
<std::string
>(1, key
);
29 class ScopedTestingManifestHandlerRegistry
{
31 ScopedTestingManifestHandlerRegistry() {
32 old_registry_
= ManifestHandlerRegistry::SetForTesting(®istry_
);
35 ~ScopedTestingManifestHandlerRegistry() {
36 ManifestHandlerRegistry::SetForTesting(old_registry_
);
39 ManifestHandlerRegistry registry_
;
40 ManifestHandlerRegistry
* old_registry_
;
43 class ManifestHandlerTest
: public testing::Test
{
45 class ParsingWatcher
{
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() {
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();
61 for (size_t i
= 0; i
< parsed_names_
.size(); ++i
) {
62 if (parsed_names_
[i
] == name_before
)
64 if (parsed_names_
[i
] == name_after
)
67 if (i_before
< i_after
)
73 // The order of manifest handlers that we watched parsing.
74 std::vector
<std::string
> parsed_names_
;
77 class TestManifestHandler
: public ManifestHandler
{
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_
);
91 const std::vector
<std::string
> PrerequisiteKeys() const override
{
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
{
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_
);
118 class AlwaysParseTestManifestHandler
: public TestManifestHandler
{
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
{
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
),
140 bool Parse(Extension
* extension
, base::string16
* error
) override
{
144 bool Validate(const Extension
* extension
,
146 std::vector
<InstallWarning
>* warnings
) const override
{
147 return return_value_
;
150 bool AlwaysValidateForType(Manifest::Type type
) const override
{
151 return always_validate_
;
155 const std::vector
<std::string
> Keys() const override
{ return keys_
; }
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
))->
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();
179 prereqs
.push_back("b");
180 prereqs
.push_back("k");
181 (new TestManifestHandler("C.D", SingleKey("c.d"), prereqs
, &watcher
))->
183 ManifestHandler::FinalizeRegistration();
185 scoped_refptr
<Extension
> extension
= ExtensionBuilder()
186 .SetManifest(DictionaryBuilder()
187 .Set("name", "no name")
189 .Set("manifest_version", 2)
192 .Set("c", DictionaryBuilder()
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
210 scoped_ptr
<base::DictionaryValue
> manifest_a(
212 .Set("name", "no name")
214 .Set("manifest_version", 2)
218 // Succeeds when "a" is not recognized.
220 scoped_refptr
<Extension
> extension
= Extension::Create(
222 Manifest::INVALID_LOCATION
,
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(
236 Manifest::INVALID_LOCATION
,
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")
250 .Set("manifest_version", 2)
254 EXPECT_TRUE(extension
.get());
257 std::vector
<InstallWarning
> warnings
;
258 // Always validates and fails.
259 (new TestManifestValidator(false, true, SingleKey("c")))->Register();
261 ManifestHandler::ValidateExtension(extension
.get(), &error
, &warnings
));
263 // This overrides the registered handler for "c".
264 (new TestManifestValidator(false, false, SingleKey("c")))->Register();
266 ManifestHandler::ValidateExtension(extension
.get(), &error
, &warnings
));
268 // Validates "a" and fails.
269 (new TestManifestValidator(false, true, SingleKey("a")))->Register();
271 ManifestHandler::ValidateExtension(extension
.get(), &error
, &warnings
));
274 } // namespace extensions