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/extensions/extension_constants.h"
7 #include "chrome/renderer/extensions/chrome_extensions_dispatcher_delegate.h"
8 #include "chrome/renderer/extensions/renderer_permissions_policy_delegate.h"
9 #include "content/public/test/mock_render_process_host.h"
10 #include "content/public/test/mock_render_thread.h"
11 #include "extensions/common/constants.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_builder.h"
14 #include "extensions/common/permissions/permissions_data.h"
15 #include "extensions/renderer/dispatcher.h"
16 #include "extensions/renderer/test_extensions_renderer_client.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace extensions
{
23 class RendererPermissionsPolicyDelegateTest
: public testing::Test
{
25 RendererPermissionsPolicyDelegateTest() {
28 void SetUp() override
{
29 testing::Test::SetUp();
30 render_thread_
.reset(new content::MockRenderThread());
31 renderer_client_
.reset(new TestExtensionsRendererClient
);
32 ExtensionsRendererClient::Set(renderer_client_
.get());
33 extension_dispatcher_delegate_
.reset(
34 new ChromeExtensionsDispatcherDelegate());
35 extension_dispatcher_
.reset(
36 new Dispatcher(extension_dispatcher_delegate_
.get()));
37 policy_delegate_
.reset(
38 new RendererPermissionsPolicyDelegate(extension_dispatcher_
.get()));
42 scoped_ptr
<content::MockRenderThread
> render_thread_
;
43 scoped_ptr
<ExtensionsRendererClient
> renderer_client_
;
44 scoped_ptr
<DispatcherDelegate
> extension_dispatcher_delegate_
;
45 scoped_ptr
<Dispatcher
> extension_dispatcher_
;
46 scoped_ptr
<RendererPermissionsPolicyDelegate
> policy_delegate_
;
49 scoped_refptr
<const Extension
> CreateTestExtension(const std::string
& id
) {
50 return ExtensionBuilder()
51 .SetManifest(DictionaryBuilder()
52 .Set("name", "Extension with ID " + id
)
53 .Set("version", "1.0")
54 .Set("manifest_version", 2)
55 .Set("permissions", ListBuilder().Append("<all_urls>")))
62 // Tests that CanAccessPage returns false for the any process
63 // which hosts the webstore.
64 TEST_F(RendererPermissionsPolicyDelegateTest
, CannotScriptWebstore
) {
65 GURL
kAnyUrl("http://example.com/");
66 scoped_refptr
<const Extension
> extension(CreateTestExtension("a"));
69 EXPECT_TRUE(extension
->permissions_data()->CanAccessPage(
70 extension
.get(), kAnyUrl
, -1, -1, &error
)) << error
;
72 // Pretend we are in the webstore process. We should not be able to execute
74 scoped_refptr
<const Extension
> webstore_extension(
75 CreateTestExtension(extensions::kWebStoreAppId
));
76 extension_dispatcher_
->LoadExtensionForTest(webstore_extension
.get());
77 extension_dispatcher_
->OnActivateExtension(extensions::kWebStoreAppId
);
78 EXPECT_FALSE(extension
->permissions_data()->CanAccessPage(
79 extension
.get(), kAnyUrl
, -1, -1, &error
))
83 } // namespace extensions