Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / chrome / renderer / extensions / renderer_permissions_policy_delegate_unittest.cc
blobc71ccb84598211125a53d116ab18a9bd625d0ad1
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/chrome_extensions_dispatcher_delegate.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/constants.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/extension_builder.h"
15 #include "extensions/common/permissions/permissions_data.h"
16 #include "extensions/renderer/dispatcher.h"
17 #include "extensions/renderer/test_extensions_renderer_client.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace extensions {
22 namespace {
24 class RendererPermissionsPolicyDelegateTest : public testing::Test {
25 public:
26 RendererPermissionsPolicyDelegateTest() {
29 void SetUp() override {
30 testing::Test::SetUp();
31 render_thread_.reset(new content::MockRenderThread());
32 renderer_client_.reset(new TestExtensionsRendererClient);
33 ExtensionsRendererClient::Set(renderer_client_.get());
34 extension_dispatcher_delegate_.reset(
35 new ChromeExtensionsDispatcherDelegate());
36 extension_dispatcher_.reset(
37 new Dispatcher(extension_dispatcher_delegate_.get()));
38 policy_delegate_.reset(
39 new RendererPermissionsPolicyDelegate(extension_dispatcher_.get()));
42 protected:
43 scoped_ptr<content::MockRenderThread> render_thread_;
44 scoped_ptr<ExtensionsRendererClient> renderer_client_;
45 scoped_ptr<DispatcherDelegate> extension_dispatcher_delegate_;
46 scoped_ptr<Dispatcher> extension_dispatcher_;
47 scoped_ptr<RendererPermissionsPolicyDelegate> policy_delegate_;
50 scoped_refptr<const Extension> CreateTestExtension(const std::string& id) {
51 return ExtensionBuilder()
52 .SetManifest(DictionaryBuilder()
53 .Set("name", "Extension with ID " + id)
54 .Set("version", "1.0")
55 .Set("manifest_version", 2)
56 .Set("permissions", ListBuilder().Append("<all_urls>")))
57 .SetID(id)
58 .Build();
61 } // namespace
63 // Tests that CanAccessPage returns false for the signin process,
64 // all else being equal.
65 TEST_F(RendererPermissionsPolicyDelegateTest, CannotScriptSigninProcess) {
66 GURL kSigninUrl(
67 "https://accounts.google.com/ServiceLogin?service=chromiumsync");
68 scoped_refptr<const Extension> extension(CreateTestExtension("a"));
69 std::string error;
71 EXPECT_TRUE(extension->permissions_data()->CanAccessPage(
72 extension.get(), kSigninUrl, kSigninUrl, -1, -1, &error))
73 << error;
74 // Pretend we are in the signin process. We should not be able to execute
75 // script.
76 base::CommandLine::ForCurrentProcess()->AppendSwitch(
77 switches::kSigninProcess);
78 EXPECT_FALSE(extension->permissions_data()->CanAccessPage(
79 extension.get(), kSigninUrl, kSigninUrl, -1, -1, &error))
80 << error;
83 // Tests that CanAccessPage returns false for the any process
84 // which hosts the webstore.
85 TEST_F(RendererPermissionsPolicyDelegateTest, CannotScriptWebstore) {
86 GURL kAnyUrl("http://example.com/");
87 scoped_refptr<const Extension> extension(CreateTestExtension("a"));
88 std::string error;
90 EXPECT_TRUE(extension->permissions_data()->CanAccessPage(
91 extension.get(), kAnyUrl, kAnyUrl, -1, -1, &error))
92 << error;
94 // Pretend we are in the webstore process. We should not be able to execute
95 // script.
96 scoped_refptr<const Extension> webstore_extension(
97 CreateTestExtension(extensions::kWebStoreAppId));
98 extension_dispatcher_->OnLoadedInternal(webstore_extension);
99 extension_dispatcher_->OnActivateExtension(extensions::kWebStoreAppId);
100 EXPECT_FALSE(extension->permissions_data()->CanAccessPage(
101 extension.get(), kAnyUrl, kAnyUrl, -1, -1, &error))
102 << error;
105 } // namespace extensions