| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 <memory> |
| 6 #include <string> |
| 7 |
| 8 #include "base/command_line.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/chromeos/arc/arc_auth_service.h" |
| 15 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| 16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 17 #include "chrome/browser/policy/profile_policy_connector.h" |
| 18 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 19 #include "chrome/browser/policy/test/local_policy_test_server.h" |
| 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| 22 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/test/base/in_process_browser_test.h" |
| 24 #include "chrome/test/base/testing_profile.h" |
| 25 #include "chromeos/dbus/dbus_thread_manager.h" |
| 26 #include "chromeos/dbus/fake_session_manager_client.h" |
| 27 #include "components/arc/arc_bridge_service_impl.h" |
| 28 #include "components/arc/arc_service_manager.h" |
| 29 #include "components/arc/test/fake_arc_bridge_bootstrap.h" |
| 30 #include "components/arc/test/fake_arc_bridge_instance.h" |
| 31 #include "components/policy/core/common/policy_switches.h" |
| 32 #include "components/prefs/pref_service.h" |
| 33 #include "components/signin/core/account_id/account_id.h" |
| 34 #include "components/user_manager/user_manager.h" |
| 35 #include "url/gurl.h" |
| 36 |
| 37 namespace arc { |
| 38 |
| 39 // Set fake auth token for Chrome managed and well-known consumer accounts. |
| 40 const char kFakeAuthToken[] = "fake-auth-token"; |
| 41 // Set managed auth token for Android managed accounts. |
| 42 const char kManagedAuthToken[] = "managed-auth-token"; |
| 43 // Set unmanaged auth token for other Android unmanaged accounts. |
| 44 const char kUnmanagedAuthToken[] = "unmanaged-auth-token"; |
| 45 |
| 46 // Waits for the next ArcAuthService state change. |
| 47 class ArcAuthServiceStateObserver : public ArcAuthService::Observer { |
| 48 public: |
| 49 ArcAuthServiceStateObserver() { ArcAuthService::Get()->AddObserver(this); } |
| 50 |
| 51 // ArcAuthService::Observer: |
| 52 ~ArcAuthServiceStateObserver() override { |
| 53 run_loop_.reset(); |
| 54 ArcAuthService::Get()->RemoveObserver(this); |
| 55 } |
| 56 |
| 57 void OnOptInChanged(ArcAuthService::State state) override { |
| 58 if (!run_loop_) |
| 59 return; |
| 60 run_loop_->Quit(); |
| 61 } |
| 62 |
| 63 void Wait() { |
| 64 run_loop_.reset(new base::RunLoop); |
| 65 run_loop_->Run(); |
| 66 run_loop_.reset(); |
| 67 } |
| 68 |
| 69 private: |
| 70 std::unique_ptr<base::RunLoop> run_loop_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceStateObserver); |
| 73 }; |
| 74 |
| 75 class ArcAuthServiceTest : public InProcessBrowserTest { |
| 76 protected: |
| 77 ArcAuthServiceTest() {} |
| 78 |
| 79 // InProcessBrowserTest: |
| 80 ~ArcAuthServiceTest() override {} |
| 81 |
| 82 void SetUpInProcessBrowserTestFixture() override { |
| 83 // Start test device management server. |
| 84 test_server_.reset(new policy::LocalPolicyTestServer()); |
| 85 ASSERT_TRUE(test_server_->Start()); |
| 86 |
| 87 // Specify device management server URL. |
| 88 std::string url = test_server_->GetServiceURL().spec(); |
| 89 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 90 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, |
| 91 url); |
| 92 |
| 93 // Enable ARC. |
| 94 fake_session_manager_client_ = new chromeos::FakeSessionManagerClient; |
| 95 fake_session_manager_client_->set_arc_available(true); |
| 96 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( |
| 97 std::unique_ptr<chromeos::SessionManagerClient>( |
| 98 fake_session_manager_client_)); |
| 99 |
| 100 // Mock out ARC bridge. |
| 101 fake_arc_bridge_instance_.reset(new FakeArcBridgeInstance); |
| 102 ArcServiceManager::SetArcBridgeServiceForTesting( |
| 103 base::WrapUnique(new ArcBridgeServiceImpl(base::WrapUnique( |
| 104 new FakeArcBridgeBootstrap(fake_arc_bridge_instance_.get()))))); |
| 105 } |
| 106 |
| 107 void SetUpOnMainThread() override { |
| 108 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler( |
| 109 new chromeos::FakeChromeUserManager)); |
| 110 } |
| 111 |
| 112 void TearDownOnMainThread() override { user_manager_enabler_.reset(); } |
| 113 |
| 114 void SetUpTest(const std::string& username, const char* access_token) { |
| 115 // Init ArcAuthService for testing. |
| 116 ArcAuthService::DisableUIForTesting(); |
| 117 ArcAuthService::EnableCheckAndroidManagementForTesting(); |
| 118 policy::AndroidManagementClient::SetAccessTokenForTesting(access_token); |
| 119 |
| 120 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 121 |
| 122 // Create test profile. |
| 123 TestingProfile::Builder profile_builder; |
| 124 profile_builder.SetPath(temp_dir_.path().AppendASCII("TestArcProfile")); |
| 125 profile_builder.SetProfileName(username); |
| 126 profile_ = profile_builder.Build(); |
| 127 |
| 128 const AccountId account_id( |
| 129 AccountId::FromUserEmailGaiaId(username, "1234567890")); |
| 130 GetFakeUserManager()->AddUser(account_id); |
| 131 GetFakeUserManager()->LoginUser(account_id); |
| 132 |
| 133 profile()->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); |
| 134 |
| 135 // Setup ARC for test profile. |
| 136 ArcServiceManager::Get()->OnPrimaryUserProfilePrepared( |
| 137 multi_user_util::GetAccountIdFromProfile(profile())); |
| 138 ArcAuthService::Get()->OnPrimaryUserProfilePrepared(profile()); |
| 139 } |
| 140 |
| 141 void TearDownTest() { |
| 142 ArcAuthService::Get()->Shutdown(); |
| 143 profile_.reset(); |
| 144 } |
| 145 |
| 146 chromeos::FakeChromeUserManager* GetFakeUserManager() const { |
| 147 return static_cast<chromeos::FakeChromeUserManager*>( |
| 148 user_manager::UserManager::Get()); |
| 149 } |
| 150 |
| 151 Profile* profile() { return profile_.get(); } |
| 152 |
| 153 private: |
| 154 std::unique_ptr<policy::LocalPolicyTestServer> test_server_; |
| 155 chromeos::FakeSessionManagerClient* fake_session_manager_client_; |
| 156 std::unique_ptr<FakeArcBridgeInstance> fake_arc_bridge_instance_; |
| 157 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
| 158 base::ScopedTempDir temp_dir_; |
| 159 std::unique_ptr<TestingProfile> profile_; |
| 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest); |
| 162 }; |
| 163 |
| 164 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ConsumerAccount) { |
| 165 SetUpTest("test@example.com", kUnmanagedAuthToken); |
| 166 |
| 167 PrefService* pref = profile()->GetPrefs(); |
| 168 |
| 169 pref->SetBoolean(prefs::kArcEnabled, true); |
| 170 ArcAuthServiceStateObserver observer; |
| 171 observer.Wait(); |
| 172 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); |
| 173 TearDownTest(); |
| 174 } |
| 175 |
| 176 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, WellKnownConsumerAccount) { |
| 177 SetUpTest("test@gmail.com", kFakeAuthToken); |
| 178 |
| 179 PrefService* pref = profile()->GetPrefs(); |
| 180 |
| 181 pref->SetBoolean(prefs::kArcEnabled, true); |
| 182 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); |
| 183 TearDownTest(); |
| 184 } |
| 185 |
| 186 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedChromeAccount) { |
| 187 SetUpTest("test@managedchrome.com", kFakeAuthToken); |
| 188 policy::ProfilePolicyConnector* const connector = |
| 189 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile()); |
| 190 connector->OverrideIsManagedForTesting(true); |
| 191 |
| 192 PrefService* pref = profile()->GetPrefs(); |
| 193 |
| 194 pref->SetBoolean(prefs::kArcEnabled, true); |
| 195 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); |
| 196 TearDownTest(); |
| 197 } |
| 198 |
| 199 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedAndroidAccount) { |
| 200 SetUpTest("test@example.com", kManagedAuthToken); |
| 201 |
| 202 PrefService* pref = profile()->GetPrefs(); |
| 203 |
| 204 pref->SetBoolean(prefs::kArcEnabled, true); |
| 205 ArcAuthServiceStateObserver observer; |
| 206 observer.Wait(); |
| 207 ASSERT_EQ(ArcAuthService::State::STOPPED, ArcAuthService::Get()->state()); |
| 208 TearDownTest(); |
| 209 } |
| 210 |
| 211 } // namespace arc |
| OLD | NEW |