1 // Copyright (c) 2012 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 "base/command_line.h"
6 #include "base/json/json_file_value_serializer.h"
7 #include "base/memory/linked_ptr.h"
8 #include "chrome/common/extensions/manifest_tests/chrome_manifest_test.h"
9 #include "extensions/common/error_utils.h"
10 #include "extensions/common/features/simple_feature.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "extensions/common/manifest_handlers/app_isolation_info.h"
13 #include "extensions/common/manifest_handlers/csp_info.h"
14 #include "extensions/common/manifest_handlers/incognito_info.h"
15 #include "extensions/common/switches.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace extensions
{
20 namespace errors
= manifest_errors
;
22 class PlatformAppsManifestTest
: public ChromeManifestTest
{
25 TEST_F(PlatformAppsManifestTest
, PlatformApps
) {
26 scoped_refptr
<Extension
> extension
=
27 LoadAndExpectSuccess("init_valid_platform_app.json");
28 EXPECT_TRUE(AppIsolationInfo::HasIsolatedStorage(extension
.get()));
29 EXPECT_FALSE(IncognitoInfo::IsSplitMode(extension
.get()));
32 LoadAndExpectSuccess("init_valid_platform_app_no_manifest_version.json");
33 EXPECT_EQ(2, extension
->manifest_version());
35 extension
= LoadAndExpectSuccess("incognito_valid_platform_app.json");
36 EXPECT_FALSE(IncognitoInfo::IsSplitMode(extension
.get()));
38 Testcase error_testcases
[] = {
39 Testcase("init_invalid_platform_app_2.json",
40 errors::kBackgroundRequiredForPlatformApps
),
41 Testcase("init_invalid_platform_app_3.json",
42 ErrorUtils::FormatErrorMessage(
43 errors::kInvalidManifestVersionOld
, "2", "apps")),
45 RunTestcases(error_testcases
, arraysize(error_testcases
), EXPECT_TYPE_ERROR
);
47 Testcase warning_testcases
[] = {
49 "init_invalid_platform_app_1.json",
50 "'app.launch' is only allowed for hosted apps and legacy packaged "
51 "apps, but this is a packaged app."),
53 "init_invalid_platform_app_4.json",
54 "'background' is only allowed for extensions, hosted apps, and legacy "
55 "packaged apps, but this is a packaged app."),
57 "init_invalid_platform_app_5.json",
58 "'background' is only allowed for extensions, hosted apps, and legacy "
59 "packaged apps, but this is a packaged app."),
60 Testcase("incognito_invalid_platform_app.json",
61 "'incognito' is only allowed for extensions and legacy packaged apps, "
62 "but this is a packaged app."),
65 warning_testcases
, arraysize(warning_testcases
), EXPECT_TYPE_WARNING
);
68 TEST_F(PlatformAppsManifestTest
, PlatformAppContentSecurityPolicy
) {
69 // Normal platform apps can't specify a CSP value.
70 Testcase warning_testcases
[] = {
72 "init_platform_app_csp_warning_1.json",
73 "'content_security_policy' is only allowed for extensions and legacy "
74 "packaged apps, but this is a packaged app."),
76 "init_platform_app_csp_warning_2.json",
77 "'app.content_security_policy' is not allowed for specified extension "
81 warning_testcases
, arraysize(warning_testcases
), EXPECT_TYPE_WARNING
);
83 // Whitelisted ones can (this is the ID corresponding to the base 64 encoded
84 // key in the init_platform_app_csp.json manifest.)
85 extensions::SimpleFeature::ScopedWhitelistForTest
whitelist(
86 "ahplfneplbnjcflhdgkkjeiglkkfeelb");
87 scoped_refptr
<Extension
> extension
=
88 LoadAndExpectSuccess("init_platform_app_csp.json");
89 EXPECT_EQ(0U, extension
->install_warnings().size())
90 << "Unexpected warning " << extension
->install_warnings()[0].message
;
91 EXPECT_TRUE(extension
->is_platform_app());
92 EXPECT_EQ("default-src 'self' https://www.google.com;",
93 CSPInfo::GetResourceContentSecurityPolicy(extension
.get(),
96 // But even whitelisted ones must specify a secure policy.
98 "init_platform_app_csp_insecure.json",
99 ErrorUtils::FormatErrorMessage(errors::kInvalidCSPInsecureValue
,
100 "http://www.google.com", "default-src"));
103 TEST_F(PlatformAppsManifestTest
, CertainApisRequirePlatformApps
) {
104 // Put APIs here that should be restricted to platform apps, but that haven't
105 // yet graduated from experimental.
106 const char* const kPlatformAppExperimentalApis
[] = {
110 // TODO(miket): When the first platform-app API leaves experimental, write
111 // similar code that tests without the experimental flag.
113 // This manifest is a skeleton used to build more specific manifests for
114 // testing. The requirements are that (1) it be a valid platform app, and (2)
115 // it contain no permissions dictionary.
117 scoped_ptr
<base::DictionaryValue
> manifest(
118 LoadManifest("init_valid_platform_app.json", &error
));
120 std::vector
<linked_ptr
<base::DictionaryValue
> > manifests
;
121 // Create each manifest.
122 for (size_t i
= 0; i
< arraysize(kPlatformAppExperimentalApis
); ++i
) {
123 const char* api_name
= kPlatformAppExperimentalApis
[i
];
125 // DictionaryValue will take ownership of this ListValue.
126 base::ListValue
*permissions
= new base::ListValue();
127 permissions
->Append(new base::StringValue("experimental"));
128 permissions
->Append(new base::StringValue(api_name
));
129 manifest
->Set("permissions", permissions
);
130 manifests
.push_back(make_linked_ptr(manifest
->DeepCopy()));
133 // First try to load without any flags. This should warn for every API.
134 for (size_t i
= 0; i
< arraysize(kPlatformAppExperimentalApis
); ++i
) {
135 LoadAndExpectWarning(
136 ManifestData(manifests
[i
].get(), ""),
137 "'experimental' requires the 'experimental-extension-apis' "
138 "command line switch to be enabled.");
141 // Now try again with the experimental flag set.
142 base::CommandLine::ForCurrentProcess()->AppendSwitch(
143 switches::kEnableExperimentalExtensionApis
);
144 for (size_t i
= 0; i
< arraysize(kPlatformAppExperimentalApis
); ++i
) {
145 LoadAndExpectSuccess(ManifestData(manifests
[i
].get(), ""));
149 } // namespace extensions