forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension_unittest.cc
242 lines (206 loc) · 9.22 KB
/
extension_unittest.cc
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/common/extension.h"
#include "base/command_line.h"
#include "base/optional.h"
#include "base/test/scoped_command_line.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/switches.h"
#include "extensions/common/value_builder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace {
testing::AssertionResult RunManifestVersionSuccess(
std::unique_ptr<base::DictionaryValue> manifest,
Manifest::Type expected_type,
int expected_manifest_version,
bool expect_warning = false,
Extension::InitFromValueFlags custom_flag = Extension::NO_FLAGS) {
std::string error;
scoped_refptr<const Extension> extension = Extension::Create(
base::FilePath(), Manifest::INTERNAL, *manifest, custom_flag, &error);
if (!extension) {
return testing::AssertionFailure()
<< "Extension creation failed: " << error;
}
if (extension->GetType() != expected_type) {
return testing::AssertionFailure()
<< "Wrong type: " << extension->GetType();
}
if (extension->manifest_version() != expected_manifest_version) {
return testing::AssertionFailure()
<< "Wrong manifest version: " << extension->manifest_version();
}
bool has_manifest_version_warning = false;
for (const auto& warning : extension->install_warnings()) {
if (warning.key == manifest_keys::kManifestVersion) {
has_manifest_version_warning = true;
break;
}
}
if (has_manifest_version_warning != expect_warning)
return testing::AssertionFailure()
<< "Expected warning: " << expect_warning
<< ", Found Warning: " << has_manifest_version_warning;
return testing::AssertionSuccess();
}
testing::AssertionResult RunManifestVersionFailure(
std::unique_ptr<base::DictionaryValue> manifest,
Extension::InitFromValueFlags custom_flag = Extension::NO_FLAGS) {
std::string error;
scoped_refptr<const Extension> extension = Extension::Create(
base::FilePath(), Manifest::INTERNAL, *manifest, custom_flag, &error);
if (extension)
return testing::AssertionFailure() << "Extension creation succeeded.";
return testing::AssertionSuccess();
}
testing::AssertionResult RunCreationWithFlags(
const base::DictionaryValue* manifest,
Manifest::Location location,
Manifest::Type expected_type,
Extension::InitFromValueFlags custom_flag = Extension::NO_FLAGS) {
std::string error;
scoped_refptr<const Extension> extension = Extension::Create(
base::FilePath(), location, *manifest, custom_flag, &error);
if (!extension) {
return testing::AssertionFailure()
<< "Extension creation failed: " << error;
}
if (extension->GetType() != expected_type) {
return testing::AssertionFailure()
<< "Wrong type: " << extension->GetType();
}
return testing::AssertionSuccess();
}
} // namespace
// TODO(devlin): Move tests from chrome/common/extensions/extension_unittest.cc
// that don't depend on //chrome into here.
TEST(ExtensionTest, ExtensionManifestVersions) {
auto get_manifest = [](base::Optional<int> manifest_version) {
DictionaryBuilder builder;
builder.Set("name", "My Extension")
.Set("version", "0.1")
.Set("description", "An awesome extension");
if (manifest_version)
builder.Set("manifest_version", *manifest_version);
return builder.Build();
};
const Manifest::Type kType = Manifest::TYPE_EXTENSION;
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(2), kType, 2));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(3), kType, 3,
true /* expect warning */));
// Manifest v1 is deprecated, and should not load.
EXPECT_TRUE(RunManifestVersionFailure(get_manifest(1)));
// Omitting the key defaults to v1 for extensions.
EXPECT_TRUE(RunManifestVersionFailure(get_manifest(base::nullopt)));
// '0' and '-1' are invalid values.
EXPECT_TRUE(RunManifestVersionFailure(get_manifest(0)));
EXPECT_TRUE(RunManifestVersionFailure(get_manifest(-1)));
{
// Manifest v1 should only load if a command line switch is used.
base::test::ScopedCommandLine command_line;
command_line.GetProcessCommandLine()->AppendSwitch(
switches::kAllowLegacyExtensionManifests);
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(1), kType, 1));
EXPECT_TRUE(
RunManifestVersionSuccess(get_manifest(base::nullopt), kType, 1));
}
}
TEST(ExtensionTest, PlatformAppManifestVersions) {
auto get_manifest = [](base::Optional<int> manifest_version) {
DictionaryBuilder background;
background.Set("scripts", ListBuilder().Append("background.js").Build());
DictionaryBuilder builder;
builder.Set("name", "My Platform App")
.Set("version", "0.1")
.Set("description", "A platform app")
.Set("app",
DictionaryBuilder().Set("background", background.Build()).Build());
if (manifest_version)
builder.Set("manifest_version", *manifest_version);
return builder.Build();
};
const Manifest::Type kType = Manifest::TYPE_PLATFORM_APP;
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(2), kType, 2));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(3), kType, 3,
true /* expect warning */));
// Omitting the key defaults to v2 for platform apps.
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(base::nullopt), kType, 2));
// Manifest v1 is deprecated, and should not load.
EXPECT_TRUE(RunManifestVersionFailure(get_manifest(1)));
// '0' and '-1' are invalid values.
EXPECT_TRUE(RunManifestVersionFailure(get_manifest(0)));
EXPECT_TRUE(RunManifestVersionFailure(get_manifest(-1)));
{
// Manifest v1 should not load for platform apps, even with the command line
// switch.
base::test::ScopedCommandLine command_line;
command_line.GetProcessCommandLine()->AppendSwitch(
switches::kAllowLegacyExtensionManifests);
EXPECT_TRUE(RunManifestVersionFailure(get_manifest(1)));
}
}
TEST(ExtensionTest, HostedAppManifestVersions) {
auto get_manifest = [](base::Optional<int> manifest_version) {
DictionaryBuilder builder;
DictionaryBuilder app;
app.Set("urls", ListBuilder().Append("http://example.com").Build());
builder.Set("name", "My Hosted App")
.Set("version", "0.1")
.Set("description", "A hosted app")
.Set("app", app.Build());
if (manifest_version)
builder.Set("manifest_version", *manifest_version);
return builder.Build();
};
const Manifest::Type kType = Manifest::TYPE_HOSTED_APP;
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(2), kType, 2));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(3), kType, 3,
true /* expect warning */));
// Manifest v1 is deprecated, but should still load for hosted apps.
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(1), kType, 1));
// Omitting the key defaults to v1 for hosted apps, and v1 is still allowed.
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(base::nullopt), kType, 1));
// Requiring the modern manifest version should make hosted apps require v2.
EXPECT_TRUE(RunManifestVersionFailure(
get_manifest(1), Extension::REQUIRE_MODERN_MANIFEST_VERSION));
}
TEST(ExtensionTest, UserScriptManifestVersions) {
auto get_manifest = [](base::Optional<int> manifest_version) {
DictionaryBuilder builder;
builder.Set("name", "My Extension")
.Set("version", "0.1")
.Set("description", "An awesome extension")
.Set("converted_from_user_script", true);
if (manifest_version)
builder.Set("manifest_version", *manifest_version);
return builder.Build();
};
const Manifest::Type kType = Manifest::TYPE_USER_SCRIPT;
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(2), kType, 2));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(3), kType, 3,
true /* expect warning */));
// Manifest v1 is deprecated, but should still load for user scripts.
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(1), kType, 1));
// Omitting the key defaults to v1 for user scripts, but v1 is still allowed.
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(base::nullopt), kType, 1));
// Requiring the modern manifest version should make user scripts require v2.
EXPECT_TRUE(RunManifestVersionFailure(
get_manifest(1), Extension::REQUIRE_MODERN_MANIFEST_VERSION));
}
TEST(ExtensionTest, LoginScreenFlag) {
DictionaryBuilder builder;
builder.Set("name", "My Extension")
.Set("version", "0.1")
.Set("description", "An awesome extension")
.Set("manifest_version", 2);
std::unique_ptr<base::DictionaryValue> manifest = builder.Build();
EXPECT_TRUE(RunCreationWithFlags(manifest.get(), Manifest::EXTERNAL_POLICY,
Manifest::TYPE_EXTENSION,
Extension::NO_FLAGS));
EXPECT_TRUE(RunCreationWithFlags(manifest.get(), Manifest::EXTERNAL_POLICY,
Manifest::TYPE_LOGIN_SCREEN_EXTENSION,
Extension::FOR_LOGIN_SCREEN));
}
} // namespace extensions