1 // Copyright 2014 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/memory/scoped_ptr.h"
6 #include "chrome/browser/extensions/extension_service.h"
7 #include "chrome/browser/extensions/permissions_updater.h"
8 #include "chrome/browser/extensions/test_extension_environment.h"
9 #include "chrome/common/extensions/permissions/chrome_permission_message_provider.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/crx_file/id_util.h"
13 #include "extensions/browser/extension_prefs.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/extension_builder.h"
16 #include "extensions/common/manifest.h"
17 #include "extensions/common/manifest_handlers/permissions_parser.h"
18 #include "extensions/common/permissions/permission_set.h"
19 #include "extensions/common/permissions/permissions_data.h"
20 #include "extensions/common/test_util.h"
21 #include "extensions/common/value_builder.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/base/l10n/l10n_util.h"
25 namespace extensions
{
27 // Tests that ChromePermissionMessageProvider provides not only correct, but
28 // meaningful permission messages that coalesce correctly where appropriate.
29 // There are 3 types of permission messages that need to be tested:
30 // 1. The combined list of active permissions, displayed at install time (or
31 // when the app has been disabled automatically and needs to be re-enabled)
32 // 2. The split list of active permissions, displayed in the App Info dialog,
33 // where the optional permissions are individually revokable
34 // 3. The list of requested optional permissions, displayed in a prompt to the
35 // user when the app requests these during runtime
36 // Some of these tests are prefixed AntiTest_, since they demonstrate existing
37 // problematic functionality. These tests are prefixed with AntiTest_ and will
38 // be changed as the correct behaviour is implemented. TODOs in the test explain
39 // the currently problematic behaviour.
40 class PermissionMessagesUnittest
: public testing::Test
{
42 PermissionMessagesUnittest()
43 : message_provider_(new ChromePermissionMessageProvider()) {}
44 ~PermissionMessagesUnittest() override
{}
46 // Overridden from testing::Test:
47 void SetUp() override
{
48 testing::Test::SetUp();
52 void CreateAndInstallAppWithPermissions(ListBuilder
& required_permissions
,
53 ListBuilder
& optional_permissions
) {
54 app_
= test_util::BuildApp(ExtensionBuilder().Pass())
57 .Set("permissions", required_permissions
)
58 .Set("optional_permissions", optional_permissions
))
59 .SetID(crx_file::id_util::GenerateId("app"))
60 .SetLocation(Manifest::INTERNAL
)
62 env_
.GetExtensionService()->AddExtension(app_
.get());
65 void CreateAndInstallExtensionWithPermissions(
66 ListBuilder
& required_permissions
,
67 ListBuilder
& optional_permissions
) {
68 app_
= test_util::BuildExtension(ExtensionBuilder().Pass())
71 .Set("permissions", required_permissions
)
72 .Set("optional_permissions", optional_permissions
))
73 .SetID(crx_file::id_util::GenerateId("extension"))
74 .SetLocation(Manifest::INTERNAL
)
76 env_
.GetExtensionService()->AddExtension(app_
.get());
79 // Returns the permission messages that would display in the prompt that
80 // requests all the optional permissions for the current |app_|.
81 std::vector
<base::string16
> GetOptionalPermissionMessages() {
82 scoped_refptr
<const PermissionSet
> granted_permissions
=
83 env_
.GetExtensionPrefs()->GetGrantedPermissions(app_
->id());
84 scoped_refptr
<const PermissionSet
> optional_permissions
=
85 PermissionsParser::GetOptionalPermissions(app_
.get());
86 scoped_refptr
<const PermissionSet
> requested_permissions
=
87 PermissionSet::CreateDifference(optional_permissions
.get(),
88 granted_permissions
.get());
89 return GetMessages(requested_permissions
);
92 void GrantOptionalPermissions() {
93 PermissionsUpdater
perms_updater(env_
.profile());
94 perms_updater
.AddPermissions(
96 PermissionsParser::GetOptionalPermissions(app_
.get()).get());
99 std::vector
<base::string16
> active_permissions() {
100 return GetMessages(app_
->permissions_data()->active_permissions());
103 std::vector
<base::string16
> required_permissions() {
104 return GetMessages(PermissionsParser::GetRequiredPermissions(app_
.get()));
107 std::vector
<base::string16
> optional_permissions() {
108 return GetMessages(PermissionsParser::GetOptionalPermissions(app_
.get()));
112 std::vector
<base::string16
> GetMessages(
113 scoped_refptr
<const PermissionSet
> permissions
) {
114 std::vector
<base::string16
> messages
;
115 for (const PermissionMessageString
& str
:
116 message_provider_
->GetPermissionMessageStrings(permissions
.get(),
118 messages
.push_back(str
.message
);
123 extensions::TestExtensionEnvironment env_
;
124 scoped_ptr
<ChromePermissionMessageProvider
> message_provider_
;
125 scoped_refptr
<const Extension
> app_
;
127 DISALLOW_COPY_AND_ASSIGN(PermissionMessagesUnittest
);
130 // If an app has both the 'history' and 'tabs' permission, one should hide the
131 // other (the 'history' permission has superset permissions).
132 TEST_F(PermissionMessagesUnittest
, HistoryHidesTabsMessage
) {
133 CreateAndInstallExtensionWithPermissions(
134 ListBuilder().Append("tabs").Append("history").Pass(),
135 ListBuilder().Pass());
137 ASSERT_EQ(1U, required_permissions().size());
139 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_WRITE
),
140 required_permissions()[0]);
142 ASSERT_EQ(0U, optional_permissions().size());
145 // If an app requests the 'history' permission, but already has the 'tabs'
146 // permission, only the new coalesced message is displayed.
147 TEST_F(PermissionMessagesUnittest
, MixedPermissionMessagesCoalesceOnceGranted
) {
148 CreateAndInstallExtensionWithPermissions(
149 ListBuilder().Append("tabs").Pass(),
150 ListBuilder().Append("history").Pass());
152 ASSERT_EQ(1U, required_permissions().size());
154 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
155 required_permissions()[0]);
157 ASSERT_EQ(1U, optional_permissions().size());
159 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_WRITE
),
160 optional_permissions()[0]);
162 ASSERT_EQ(1U, active_permissions().size());
164 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
165 active_permissions()[0]);
167 ASSERT_EQ(1U, GetOptionalPermissionMessages().size());
169 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_WRITE
),
170 GetOptionalPermissionMessages()[0]);
172 GrantOptionalPermissions();
174 ASSERT_EQ(1U, active_permissions().size());
176 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_WRITE
),
177 active_permissions()[0]);
180 // AntiTest: This behavior should be changed and improved.
181 // If an app requests the 'tabs' permission but already has the 'history'
182 // permission, a prompt is displayed. However, no prompt should appear at all,
183 // since 'tabs' is a subset of 'history' and the final list of permissions are
184 // not affected by this grant.
185 TEST_F(PermissionMessagesUnittest
,
186 AntiTest_PromptCanRequestSubsetOfAlreadyGrantedPermissions
) {
187 CreateAndInstallExtensionWithPermissions(
188 ListBuilder().Append("history").Pass(),
189 ListBuilder().Append("tabs").Pass());
191 ASSERT_EQ(1U, required_permissions().size());
193 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_WRITE
),
194 required_permissions()[0]);
196 ASSERT_EQ(1U, optional_permissions().size());
198 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
199 optional_permissions()[0]);
201 ASSERT_EQ(1U, active_permissions().size());
203 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_WRITE
),
204 active_permissions()[0]);
206 // TODO(sashab): This prompt should display no permissions, since READ is a
207 // subset permission of WRITE.
208 ASSERT_EQ(1U, GetOptionalPermissionMessages().size());
210 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
211 GetOptionalPermissionMessages()[0]);
213 GrantOptionalPermissions();
215 ASSERT_EQ(1U, active_permissions().size());
217 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_WRITE
),
218 active_permissions()[0]);
221 // AntiTest: This behavior should be changed and improved.
222 // If an app requests the 'sessions' permission, nothing is displayed in the
223 // permission request prompt. However, the required permissions for the app are
224 // actually modified, so the prompt *should* display a message to prevent this
225 // permission from being granted for free.
226 TEST_F(PermissionMessagesUnittest
,
227 AntiTest_PromptCanBeEmptyButCausesChangeInPermissions
) {
228 CreateAndInstallExtensionWithPermissions(
229 ListBuilder().Append("tabs").Pass(),
230 ListBuilder().Append("sessions").Pass());
232 ASSERT_EQ(1U, required_permissions().size());
234 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
235 required_permissions()[0]);
237 ASSERT_EQ(0U, optional_permissions().size());
239 ASSERT_EQ(1U, active_permissions().size());
241 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ
),
242 active_permissions()[0]);
244 // TODO(sashab): This prompt should display the sessions permission message,
245 // as well as warn the user that it can affect the existing 'tab' permission.
246 ASSERT_EQ(0U, GetOptionalPermissionMessages().size());
248 GrantOptionalPermissions();
250 ASSERT_EQ(1U, active_permissions().size());
251 EXPECT_EQ(l10n_util::GetStringUTF16(
252 IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ_AND_SESSIONS
),
253 active_permissions()[0]);
256 } // namespace extensions