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 "chrome/browser/signin/chrome_signin_client.h"
6#include "chrome/browser/signin/chrome_signin_client_factory.h"
7#include "chrome/browser/signin/signin_manager_factory.h"
8#include "chrome/test/base/testing_browser_process.h"
9#include "chrome/test/base/testing_profile.h"
10#include "chrome/test/base/testing_profile_manager.h"
11#include "components/signin/core/browser/signin_manager.h"
12#include "content/public/test/mock_render_process_host.h"
13#include "content/public/test/test_browser_thread_bundle.h"
14#include "extensions/common/extension.h"
15#include "extensions/common/extension_builder.h"
16#include "extensions/common/permissions/permissions_data.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace extensions {
20
21namespace {
22
23class BrowserPermissionsPolicyDelegateTest : public testing::Test {
24 protected:
25  virtual void SetUp() {
26    profile_manager_.reset(
27        new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
28    ASSERT_TRUE(profile_manager_->SetUp());
29    profile_ = profile_manager_->CreateTestingProfile("test");
30  }
31  virtual void TearDown() {
32    // Need to delete profile here before the UI thread is destroyed.
33    profile_manager_->DeleteTestingProfile("test");
34    profile_manager_.reset();
35  }
36 protected:
37  content::TestBrowserThreadBundle thread_bundle_;
38  scoped_ptr<TestingProfileManager> profile_manager_;
39  TestingProfile* profile_;
40};
41
42#if !defined(OS_CHROMEOS)
43scoped_refptr<const Extension> CreateTestExtension(const std::string& id) {
44  return ExtensionBuilder()
45      .SetManifest(DictionaryBuilder()
46          .Set("name", "Extension with ID " + id)
47          .Set("version", "1.0")
48          .Set("manifest_version", 2)
49          .Set("permissions", ListBuilder().Append("<all_urls>")))
50      .SetID(id)
51      .Build();
52}
53#endif
54
55}  // namespace
56
57#if !defined(OS_CHROMEOS)
58// Tests that CanExecuteScriptOnPage returns false for the signin process,
59// all else being equal.
60TEST_F(BrowserPermissionsPolicyDelegateTest, CanExecuteScriptOnPage) {
61  GURL kSigninUrl(
62      "https://accounts.google.com/ServiceLogin?service=chromiumsync");
63  ASSERT_TRUE(SigninManager::IsWebBasedSigninFlowURL(kSigninUrl));
64
65  content::MockRenderProcessHost signin_process(profile_);
66  content::MockRenderProcessHost normal_process(profile_);
67  SigninClient* signin_client =
68      ChromeSigninClientFactory::GetForProfile(profile_);
69  ASSERT_TRUE(signin_client);
70  signin_client->SetSigninProcess(signin_process.GetID());
71
72  scoped_refptr<const Extension> extension(CreateTestExtension("a"));
73  std::string error;
74
75  // The same call should succeed with a normal process, but fail with a signin
76  // process.
77  const PermissionsData* permissions_data = extension->permissions_data();
78  EXPECT_TRUE(permissions_data->CanAccessPage(extension.get(),
79                                              kSigninUrl,
80                                              kSigninUrl,
81                                              -1,  // no tab id.
82                                              normal_process.GetID(),
83                                              &error))
84      << error;
85  EXPECT_FALSE(permissions_data->CanAccessPage(extension.get(),
86                                               kSigninUrl,
87                                               kSigninUrl,
88                                               -1,  // no tab id.
89                                               signin_process.GetID(),
90                                               &error))
91      << error;
92}
93#endif
94
95}  // namespace extensions
96