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.
4
5 #include "base/bind.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/run_loop.h"
8 #include "chrome/browser/extensions/extension_install_prompt.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "extensions/common/extension.h"
11 #include "extensions/common/extension_builder.h"
12 #include "extensions/common/feature_switch.h"
13 #include "extensions/common/permissions/api_permission.h"
14 #include "extensions/common/permissions/api_permission_set.h"
15 #include "extensions/common/permissions/manifest_permission_set.h"
16 #include "extensions/common/permissions/permission_set.h"
17 #include "extensions/common/url_pattern_set.h"
18 #include "extensions/common/value_builder.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace extensions {
22
VerifyPromptPermissionsCallback(const base::Closure & quit_closure,size_t regular_permissions_count,size_t withheld_permissions_count,const ExtensionInstallPrompt::ShowParams & params,ExtensionInstallPrompt::Delegate * delegate,scoped_refptr<ExtensionInstallPrompt::Prompt> install_prompt)23 void VerifyPromptPermissionsCallback(
24 const base::Closure& quit_closure,
25 size_t regular_permissions_count,
26 size_t withheld_permissions_count,
27 const ExtensionInstallPrompt::ShowParams& params,
28 ExtensionInstallPrompt::Delegate* delegate,
29 scoped_refptr<ExtensionInstallPrompt::Prompt> install_prompt) {
30 ASSERT_TRUE(install_prompt.get());
31 EXPECT_EQ(regular_permissions_count,
32 install_prompt->GetPermissionCount(
33 ExtensionInstallPrompt::REGULAR_PERMISSIONS));
34 EXPECT_EQ(withheld_permissions_count,
35 install_prompt->GetPermissionCount(
36 ExtensionInstallPrompt::WITHHELD_PERMISSIONS));
37 quit_closure.Run();
38 }
39
TEST(ExtensionInstallPromptUnittest,PromptShowsPermissionWarnings)40 TEST(ExtensionInstallPromptUnittest, PromptShowsPermissionWarnings) {
41 content::TestBrowserThreadBundle thread_bundle;
42 APIPermissionSet api_permissions;
43 api_permissions.insert(APIPermission::kTab);
44 scoped_refptr<PermissionSet> permission_set =
45 new PermissionSet(api_permissions,
46 ManifestPermissionSet(),
47 URLPatternSet(),
48 URLPatternSet());
49 scoped_refptr<const Extension> extension =
50 ExtensionBuilder().SetManifest(
51 DictionaryBuilder().Set("name", "foo")
52 .Set("version", "1.0")
53 .Set("manifest_version", 2)
54 .Set("description", "Random Ext")).Build();
55 ExtensionInstallPrompt prompt(NULL /* no web contents in this test */);
56 base::RunLoop run_loop;
57 prompt.set_callback_for_test(
58 base::Bind(&VerifyPromptPermissionsCallback,
59 run_loop.QuitClosure(),
60 1u, // |regular_permissions_count|.
61 0u)); // |withheld_permissions_count|.
62 prompt.ConfirmPermissions(NULL, // no delegate
63 extension.get(),
64 permission_set.get());
65 run_loop.Run();
66 }
67
TEST(ExtensionInstallPromptUnittest,PromptShowsWithheldPermissions)68 TEST(ExtensionInstallPromptUnittest, PromptShowsWithheldPermissions) {
69 content::TestBrowserThreadBundle thread_bundle;
70
71 // Enable consent flag so that <all_hosts> permissions get withheld.
72 FeatureSwitch::ScopedOverride enable_scripts_switch(
73 FeatureSwitch::scripts_require_action(), true);
74
75 ListBuilder permissions;
76 permissions.Append("http://*/*");
77 permissions.Append("http://www.google.com/");
78 permissions.Append("tabs");
79 scoped_refptr<const Extension> extension =
80 ExtensionBuilder().SetManifest(
81 DictionaryBuilder().Set("name", "foo")
82 .Set("version", "1.0")
83 .Set("manifest_version", 2)
84 .Set("description", "Random Ext")
85 .Set("permissions", permissions)).Build();
86 ExtensionInstallPrompt prompt(NULL /* no web contents in this test */);
87 base::RunLoop run_loop;
88
89 // We expect <all_hosts> to be withheld, but http://www.google.com/ and tabs
90 // permissions should be granted as regular permissions.
91 prompt.ConfirmInstall(
92 NULL,
93 extension.get(),
94 base::Bind(&VerifyPromptPermissionsCallback,
95 run_loop.QuitClosure(),
96 2u, // |regular_permissions_count|.
97 1u)); // |withheld_permissions_count|.
98 run_loop.Run();
99 }
100
101 } // namespace extensions
102