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
|
// Copyright 2018 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/extension.h"
#include <optional>
#include <string_view>
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_feature_list.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/switches.h"
#include "testing/gtest/include/gtest/gtest.h"
using extensions::mojom::ManifestLocation;
namespace extensions {
namespace {
std::string GetVersionTooHighWarning(int max_version, int supplied_version) {
return ErrorUtils::FormatErrorMessage(
manifest_errors::kManifestVersionTooHighWarning,
base::NumberToString(max_version),
base::NumberToString(supplied_version));
}
testing::AssertionResult RunManifestVersionSuccess(
base::Value::Dict manifest,
Manifest::Type expected_type,
int expected_manifest_version,
std::string_view expected_warning = "",
Extension::InitFromValueFlags custom_flag = Extension::NO_FLAGS,
ManifestLocation manifest_location = ManifestLocation::kInternal) {
std::string error;
scoped_refptr<const Extension> extension = Extension::Create(
base::FilePath(), manifest_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();
}
if (extension->manifest_version() != expected_manifest_version) {
return testing::AssertionFailure()
<< "Wrong manifest version: " << extension->manifest_version();
}
std::string manifest_version_warning;
for (const auto& warning : extension->install_warnings()) {
if (warning.key == manifest_keys::kManifestVersion) {
manifest_version_warning = warning.message;
break;
}
}
if (expected_warning != manifest_version_warning) {
return testing::AssertionFailure()
<< "Expected warning: '" << expected_warning << "', Found Warning: '"
<< manifest_version_warning << "'";
}
return testing::AssertionSuccess();
}
testing::AssertionResult RunManifestVersionFailure(
base::Value::Dict manifest,
Extension::InitFromValueFlags custom_flag = Extension::NO_FLAGS) {
std::string error;
scoped_refptr<const Extension> extension =
Extension::Create(base::FilePath(), ManifestLocation::kInternal, manifest,
custom_flag, &error);
if (extension)
return testing::AssertionFailure() << "Extension creation succeeded.";
return testing::AssertionSuccess();
}
testing::AssertionResult RunCreationWithFlags(
const base::Value::Dict& manifest,
mojom::ManifestLocation 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 = [](std::optional<int> manifest_version) {
auto manifest = base::Value::Dict()
.Set("name", "My Extension")
.Set("version", "0.1")
.Set("description", "An awesome extension");
if (manifest_version)
manifest.Set("manifest_version", *manifest_version);
return manifest;
};
const Manifest::Type kType = Manifest::TYPE_EXTENSION;
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(2), kType, 2));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(3), kType, 3));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(4), kType, 4,
GetVersionTooHighWarning(3, 4)));
// Loading an unpacked MV2 extension should emit a warning.
EXPECT_TRUE(RunManifestVersionSuccess(
get_manifest(2), kType, 2,
manifest_errors::kManifestV2IsDeprecatedWarning, Extension::NO_FLAGS,
ManifestLocation::kUnpacked));
// 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(std::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(std::nullopt), kType, 1));
}
}
TEST(ExtensionTest, PlatformAppManifestVersions) {
auto get_manifest = [](std::optional<int> manifest_version) {
base::Value::Dict background;
background.Set("scripts", base::Value::List().Append("background.js"));
auto manifest = base::Value::Dict()
.Set("name", "My Platform App")
.Set("version", "0.1")
.Set("description", "A platform app")
.Set("app", base::Value::Dict().Set(
"background", std::move(background)));
if (manifest_version)
manifest.Set("manifest_version", *manifest_version);
return manifest;
};
const Manifest::Type kType = Manifest::TYPE_PLATFORM_APP;
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(2), kType, 2));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(3), kType, 3));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(4), kType, 4,
GetVersionTooHighWarning(3, 4)));
// Omitting the key defaults to v2 for platform apps.
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(std::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 = [](std::optional<int> manifest_version) {
base::Value::Dict app;
app.Set("urls", base::Value::List().Append("http://example.com"));
auto manifest = base::Value::Dict()
.Set("name", "My Hosted App")
.Set("version", "0.1")
.Set("description", "A hosted app")
.Set("app", std::move(app));
if (manifest_version)
manifest.Set("manifest_version", *manifest_version);
return manifest;
};
const Manifest::Type kType = Manifest::TYPE_HOSTED_APP;
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(2), kType, 2));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(3), kType, 3));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(4), kType, 4,
GetVersionTooHighWarning(3, 4)));
// 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(std::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 = [](std::optional<int> manifest_version) {
auto manifest = base::Value::Dict()
.Set("name", "My Extension")
.Set("version", "0.1")
.Set("description", "An awesome extension")
.Set("converted_from_user_script", true);
if (manifest_version)
manifest.Set("manifest_version", *manifest_version);
return manifest;
};
const Manifest::Type kType = Manifest::TYPE_USER_SCRIPT;
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(2), kType, 2));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(3), kType, 3));
EXPECT_TRUE(RunManifestVersionSuccess(get_manifest(4), kType, 4,
GetVersionTooHighWarning(3, 4)));
// 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(std::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) {
auto manifest = base::Value::Dict()
.Set("name", "My Extension")
.Set("version", "0.1")
.Set("description", "An awesome extension")
.Set("manifest_version", 2);
EXPECT_TRUE(RunCreationWithFlags(manifest, ManifestLocation::kExternalPolicy,
Manifest::TYPE_EXTENSION,
Extension::NO_FLAGS));
EXPECT_TRUE(RunCreationWithFlags(manifest, ManifestLocation::kExternalPolicy,
Manifest::TYPE_LOGIN_SCREEN_EXTENSION,
Extension::FOR_LOGIN_SCREEN));
}
#if BUILDFLAG(ENABLE_DESKTOP_ANDROID_EXTENSIONS)
TEST(ExtensionTest, BlockInstallingExtensionsOnDesktopAndroid) {
auto manifest = base::Value::Dict()
.Set("name", "My Extension")
.Set("version", "0.1")
.Set("description", "An awesome extension")
.Set("manifest_version", 3);
std::string error;
EXPECT_TRUE(Extension::Create(base::FilePath(), ManifestLocation::kInternal,
manifest.Clone(), Extension::NO_FLAGS, &error));
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
extensions_features::kBlockInstallingExtensionsOnDesktopAndroid);
EXPECT_FALSE(Extension::Create(base::FilePath(), ManifestLocation::kInternal,
manifest.Clone(), Extension::NO_FLAGS,
&error));
}
#endif // BUILDFLAG(ENABLE_DESKTOP_ANDROID_EXTENSIONS)
} // namespace extensions
|