1 // Copyright 2013 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 "chrome/common/chrome_switches.h"
7 #include "chrome/common/extensions/extension_constants.h"
8 #include "chrome/renderer/extensions/dispatcher.h"
9 #include "chrome/renderer/extensions/renderer_permissions_policy_delegate.h"
10 #include "content/public/test/mock_render_process_host.h"
11 #include "content/public/test/mock_render_thread.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_builder.h"
14 #include "extensions/common/extension_messages.h"
15 #include "extensions/common/permissions/permissions_data.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace extensions
{
22 class RendererPermissionsPolicyDelegateTest
: public testing::Test
{
24 RendererPermissionsPolicyDelegateTest() {
26 virtual void SetUp() {
27 testing::Test::SetUp();
28 render_thread_
.reset(new content::MockRenderThread());
29 extension_dispatcher_
.reset(new Dispatcher());
30 policy_delegate_
.reset(
31 new RendererPermissionsPolicyDelegate(extension_dispatcher_
.get()));
34 scoped_ptr
<Dispatcher
> extension_dispatcher_
;
35 scoped_ptr
<RendererPermissionsPolicyDelegate
> policy_delegate_
;
36 scoped_ptr
<content::MockRenderThread
> render_thread_
;
39 scoped_refptr
<const Extension
> CreateTestExtension(const std::string
& id
) {
40 return ExtensionBuilder()
41 .SetManifest(DictionaryBuilder()
42 .Set("name", "Extension with ID " + id
)
43 .Set("version", "1.0")
44 .Set("manifest_version", 2)
45 .Set("permissions", ListBuilder().Append("<all_urls>")))
52 // Tests that CanExecuteScriptOnPage returns false for the signin process,
53 // all else being equal.
54 TEST_F(RendererPermissionsPolicyDelegateTest
, CannotScriptSigninProcess
) {
56 "https://accounts.google.com/ServiceLogin?service=chromiumsync");
57 scoped_refptr
<const Extension
> extension(CreateTestExtension("a"));
60 EXPECT_TRUE(PermissionsData::CanExecuteScriptOnPage(
61 extension
.get(), kSigninUrl
, kSigninUrl
, -1, NULL
, -1, &error
)) << error
;
62 // Pretend we are in the signin process. We should not be able to execute
64 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kSigninProcess
);
65 EXPECT_FALSE(PermissionsData::CanExecuteScriptOnPage(
66 extension
.get(), kSigninUrl
, kSigninUrl
, -1, NULL
, -1, &error
)) << error
;
69 // Tests that CanExecuteScriptOnPage returns false for the any process
70 // which hosts the webstore.
71 TEST_F(RendererPermissionsPolicyDelegateTest
, CannotScriptWebstore
) {
72 GURL
kAnyUrl("http://example.com/");
73 scoped_refptr
<const Extension
> extension(CreateTestExtension("a"));
76 EXPECT_TRUE(PermissionsData::CanExecuteScriptOnPage(
77 extension
.get(), kAnyUrl
, kAnyUrl
, -1, NULL
, -1, &error
)) << error
;
79 // Pretend we are in the webstore process. We should not be able to execute
81 scoped_refptr
<const Extension
> webstore_extension(
82 CreateTestExtension(extension_misc::kWebStoreAppId
));
83 extension_dispatcher_
->OnLoadedInternal(webstore_extension
);
84 extension_dispatcher_
->OnActivateExtension(extension_misc::kWebStoreAppId
);
85 EXPECT_FALSE(PermissionsData::CanExecuteScriptOnPage(
86 extension
.get(), kAnyUrl
, kAnyUrl
, -1, NULL
, -1, &error
)) << error
;
89 } // namespace extensions