Home | History | Annotate | Download | only in extensions
      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.
      4 
      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"
     19 
     20 namespace extensions {
     21 
     22 namespace {
     23 
     24 class RendererPermissionsPolicyDelegateTest : public testing::Test {
     25  public:
     26   RendererPermissionsPolicyDelegateTest() {
     27   }
     28 
     29   virtual 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()));
     40   }
     41 
     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_;
     48 };
     49 
     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();
     59 }
     60 
     61 }  // namespace
     62 
     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;
     70 
     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   CommandLine::ForCurrentProcess()->AppendSwitch(switches::kSigninProcess);
     77   EXPECT_FALSE(extension->permissions_data()->CanAccessPage(
     78       extension.get(), kSigninUrl, kSigninUrl, -1, -1, &error))
     79       << error;
     80 }
     81 
     82 // Tests that CanAccessPage returns false for the any process
     83 // which hosts the webstore.
     84 TEST_F(RendererPermissionsPolicyDelegateTest, CannotScriptWebstore) {
     85   GURL kAnyUrl("http://example.com/");
     86   scoped_refptr<const Extension> extension(CreateTestExtension("a"));
     87   std::string error;
     88 
     89   EXPECT_TRUE(extension->permissions_data()->CanAccessPage(
     90       extension.get(), kAnyUrl, kAnyUrl, -1, -1, &error))
     91       << error;
     92 
     93   // Pretend we are in the webstore process. We should not be able to execute
     94   // script.
     95   scoped_refptr<const Extension> webstore_extension(
     96       CreateTestExtension(extensions::kWebStoreAppId));
     97   extension_dispatcher_->OnLoadedInternal(webstore_extension);
     98   extension_dispatcher_->OnActivateExtension(extensions::kWebStoreAppId);
     99   EXPECT_FALSE(extension->permissions_data()->CanAccessPage(
    100       extension.get(), kAnyUrl, kAnyUrl, -1, -1, &error))
    101       << error;
    102 }
    103 
    104 }  // namespace extensions
    105