Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 <string> | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/files/file_path.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/chromeos/arc/arc_auth_context.h" | |
| 14 #include "chrome/browser/chromeos/arc/arc_auth_notification.h" | |
| 15 #include "chrome/browser/chromeos/arc/arc_auth_service.h" | |
| 16 #include "chrome/browser/chromeos/arc/arc_service_launcher.h" | |
| 17 #include "chrome/browser/chromeos/arc/arc_session_manager.h" | |
| 18 #include "chrome/browser/chromeos/arc/arc_util.h" | |
| 19 #include "chrome/browser/chromeos/arc/auth/arc_background_auth_code_fetcher.h" | |
| 20 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | |
| 21 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | |
| 23 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | |
| 24 #include "chrome/browser/signin/fake_signin_manager_builder.h" | |
| 25 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 26 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 27 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | |
| 28 #include "chrome/common/pref_names.h" | |
| 29 #include "chrome/test/base/in_process_browser_test.h" | |
| 30 #include "chrome/test/base/testing_profile.h" | |
| 31 #include "components/arc/arc_features.h" | |
| 32 #include "components/arc/arc_service_manager.h" | |
| 33 #include "components/arc/arc_session_runner.h" | |
| 34 #include "components/arc/arc_util.h" | |
| 35 #include "components/arc/test/fake_arc_session.h" | |
| 36 #include "components/prefs/pref_member.h" | |
| 37 #include "components/prefs/pref_service.h" | |
| 38 #include "components/signin/core/account_id/account_id.h" | |
| 39 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
| 40 #include "components/user_manager/known_user.h" | |
| 41 #include "components/user_manager/user_manager.h" | |
| 42 #include "net/url_request/test_url_fetcher_factory.h" | |
| 43 #include "testing/gtest/include/gtest/gtest.h" | |
| 44 #include "url/gurl.h" | |
| 45 | |
| 46 namespace { | |
| 47 | |
| 48 constexpr char kRefreshToken[] = "fake-refresh-token"; | |
| 49 constexpr char kFakeUserName[] = "test@example.com"; | |
| 50 constexpr char kFakeGaiaId[] = "1234567890"; | |
| 51 constexpr char kFakeAuthCode[] = "fake-auth-code"; | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 namespace arc { | |
| 56 | |
| 57 class FakeAuthInstance : public mojom::AuthInstance { | |
| 58 public: | |
| 59 // mojom::AuthInstance: | |
| 60 void Init(mojom::AuthHostPtr host) override { host_ = std::move(host); } | |
| 61 | |
| 62 void OnAccountInfoReady(mojom::AccountInfoPtr account_info) override { | |
| 63 account_info_ = std::move(account_info); | |
| 64 done_closure_.Run(); | |
|
Luis Héctor Chávez
2017/05/11 16:57:54
nit: base::ResetAndReturn(&done_closure_).Run();
blundell
2017/05/12 09:09:32
Done.
| |
| 65 } | |
| 66 | |
| 67 void RequestAccountInfo(base::Closure done_closure) { | |
| 68 done_closure_ = done_closure; | |
| 69 host_->RequestAccountInfo(); | |
| 70 } | |
| 71 | |
| 72 mojom::AccountInfo* account_info() { return account_info_.get(); } | |
| 73 | |
| 74 private: | |
| 75 mojom::AuthHostPtr host_; | |
| 76 mojom::AccountInfoPtr account_info_; | |
| 77 base::Closure done_closure_; | |
| 78 }; | |
| 79 | |
| 80 class ArcAuthServiceTest : public InProcessBrowserTest { | |
| 81 protected: | |
| 82 ArcAuthServiceTest() {} | |
|
Luis Héctor Chávez
2017/05/11 16:57:54
nit: = default. Same in L85
blundell
2017/05/12 09:09:32
Done.
| |
| 83 | |
| 84 // InProcessBrowserTest: | |
| 85 ~ArcAuthServiceTest() override {} | |
| 86 | |
| 87 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 88 InProcessBrowserTest::SetUpCommandLine(command_line); | |
| 89 arc::SetArcAvailableCommandLineForTesting(command_line); | |
| 90 } | |
| 91 | |
| 92 void SetUpOnMainThread() override { | |
| 93 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler( | |
|
Luis Héctor Chávez
2017/05/11 16:57:54
nit: prefer = base::MakeUnique<chromeos::ScopedUse
| |
| 94 new chromeos::FakeChromeUserManager)); | |
| 95 // Init ArcSessionManager for testing. | |
| 96 ArcSessionManager::DisableUIForTesting(); | |
| 97 ArcAuthNotification::DisableForTesting(); | |
| 98 ArcSessionManager::EnableCheckAndroidManagementForTesting(); | |
| 99 ArcSessionManager::Get()->SetArcSessionRunnerForTesting( | |
| 100 base::MakeUnique<ArcSessionRunner>(base::Bind(FakeArcSession::Create))); | |
| 101 | |
| 102 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
|
Luis Héctor Chávez
2017/05/11 16:57:54
maybe ASSERT_TRUE? The test cannot continue if thi
blundell
2017/05/12 09:09:32
Done.
| |
| 103 | |
| 104 // Create test profile. | |
| 105 TestingProfile::Builder profile_builder; | |
| 106 profile_builder.SetPath(temp_dir_.GetPath().AppendASCII("TestArcProfile")); | |
| 107 profile_builder.SetProfileName(kFakeUserName); | |
| 108 | |
| 109 profile_builder.AddTestingFactory( | |
| 110 ProfileOAuth2TokenServiceFactory::GetInstance(), | |
| 111 BuildFakeProfileOAuth2TokenService); | |
| 112 profile_builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | |
| 113 BuildFakeSigninManagerBase); | |
| 114 | |
| 115 profile_ = profile_builder.Build(); | |
| 116 | |
| 117 token_service_ = static_cast<FakeProfileOAuth2TokenService*>( | |
| 118 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())); | |
| 119 token_service_->UpdateCredentials(kFakeUserName, kRefreshToken); | |
| 120 token_service_->set_auto_post_fetch_response_on_message_loop(true); | |
| 121 | |
| 122 FakeSigninManagerBase* signin_manager = static_cast<FakeSigninManagerBase*>( | |
| 123 SigninManagerFactory::GetForProfile(profile())); | |
| 124 signin_manager->SetAuthenticatedAccountInfo(kFakeGaiaId, kFakeUserName); | |
| 125 | |
| 126 profile()->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); | |
| 127 profile()->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, true); | |
| 128 | |
| 129 const AccountId account_id( | |
| 130 AccountId::FromUserEmailGaiaId(kFakeUserName, kFakeGaiaId)); | |
| 131 GetFakeUserManager()->AddUser(account_id); | |
| 132 GetFakeUserManager()->LoginUser(account_id); | |
| 133 GetFakeUserManager()->CreateLocalState(); | |
| 134 | |
| 135 // Set up ARC for test profile. | |
| 136 // Currently, ArcSessionManager is singleton and set up with the original | |
| 137 // Profile instance. This re-initializes the ArcServiceLauncher by | |
| 138 // overwriting Profile with profile(). | |
| 139 // TODO(hidehiko): This way several ArcService instances created with | |
| 140 // the original Profile instance on Browser creatuion are kept in the | |
| 141 // ArcServiceManager. For proper overwriting, those should be removed. | |
| 142 ArcServiceLauncher::Get()->OnPrimaryUserProfilePrepared(profile()); | |
| 143 | |
| 144 // It is non-trivial to navigate through the merge session in a testing | |
| 145 // context; currently we just skip it. | |
| 146 // TODO(blundell): Figure out how to enable this flow. | |
| 147 ArcSessionManager::Get()->auth_context()->SkipMergeSessionForTesting(); | |
| 148 | |
| 149 user_manager::known_user::SetDeviceId( | |
| 150 multi_user_util::GetAccountIdFromProfile(profile()), "dummy"); | |
| 151 } | |
| 152 | |
| 153 void TearDownOnMainThread() override { | |
| 154 // Explicitly removing the user is required; otherwise ProfileHelper keeps | |
| 155 // a dangling pointer to the User. | |
| 156 // TODO(nya): Consider removing all users from ProfileHelper in the | |
| 157 // destructor of FakeChromeUserManager. | |
| 158 const AccountId account_id( | |
| 159 AccountId::FromUserEmailGaiaId(kFakeUserName, kFakeGaiaId)); | |
| 160 GetFakeUserManager()->RemoveUserFromList(account_id); | |
| 161 // Since ArcServiceLauncher is (re-)set up with profile() in | |
| 162 // SetUpOnMainThread() it is necessary to Shutdown() before the profile() | |
| 163 // is destroyed. ArcServiceLauncher::Shutdown() will be called again on | |
| 164 // fixture destruction (because it is initialized with the original Profile | |
| 165 // instance in fixture, once), but it should be no op. | |
| 166 // TODO(hidehiko): Think about a way to test the code cleanly. | |
| 167 ArcServiceLauncher::Get()->Shutdown(); | |
| 168 profile_.reset(); | |
| 169 user_manager_enabler_.reset(); | |
| 170 } | |
| 171 | |
| 172 chromeos::FakeChromeUserManager* GetFakeUserManager() const { | |
| 173 return static_cast<chromeos::FakeChromeUserManager*>( | |
| 174 user_manager::UserManager::Get()); | |
| 175 } | |
| 176 | |
| 177 void EnableArc() { | |
| 178 PrefService* const prefs = profile()->GetPrefs(); | |
| 179 prefs->SetBoolean(prefs::kArcEnabled, true); | |
| 180 base::RunLoop().RunUntilIdle(); | |
| 181 } | |
| 182 | |
| 183 void set_profile_name(const std::string& username) { | |
| 184 profile_->set_profile_name(username); | |
| 185 } | |
| 186 | |
| 187 Profile* profile() { return profile_.get(); } | |
| 188 | |
| 189 private: | |
| 190 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | |
| 191 base::ScopedTempDir temp_dir_; | |
| 192 std::unique_ptr<TestingProfile> profile_; | |
| 193 FakeProfileOAuth2TokenService* token_service_; | |
|
Luis Héctor Chávez
2017/05/11 16:57:54
Maybe remove this member variable and turn it into
blundell
2017/05/12 09:09:32
Done.
| |
| 194 | |
| 195 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest); | |
| 196 }; | |
| 197 | |
| 198 // Tests that when ARC++ requests account info for a non-managed account and | |
| 199 // the system is in silent authentication mode, Chrome supplies the info | |
| 200 // configured in SetUpOnMainThread() above. | |
| 201 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, SuccessfulBackgroundFetch) { | |
| 202 ASSERT_TRUE(base::FeatureList::IsEnabled(arc::kArcUseAuthEndpointFeature)); | |
| 203 | |
| 204 net::FakeURLFetcherFactory factory(nullptr); | |
| 205 factory.SetFakeResponse( | |
| 206 GURL(ArcBackgroundAuthCodeFetcher::GetTokenExchangeEndpoint()), | |
| 207 "{ \"token\" : \"" + std::string(kFakeAuthCode) + "\" }", net::HTTP_OK, | |
| 208 net::URLRequestStatus::SUCCESS); | |
| 209 | |
| 210 FakeAuthInstance auth_instance; | |
| 211 ArcAuthService* auth_service = | |
| 212 ArcServiceManager::GetGlobalService<ArcAuthService>(); | |
| 213 ASSERT_TRUE(auth_service); | |
| 214 | |
| 215 ArcBridgeService* arc_bridge_service = | |
| 216 ArcServiceManager::Get()->arc_bridge_service(); | |
| 217 ASSERT_TRUE(arc_bridge_service); | |
| 218 arc_bridge_service->auth()->SetInstance(&auth_instance); | |
| 219 | |
| 220 base::RunLoop run_loop; | |
| 221 auth_instance.RequestAccountInfo(run_loop.QuitClosure()); | |
| 222 run_loop.Run(); | |
| 223 | |
| 224 EXPECT_TRUE(auth_instance.account_info()); | |
| 225 EXPECT_EQ(kFakeUserName, auth_instance.account_info()->account_name.value()); | |
| 226 EXPECT_EQ(kFakeAuthCode, auth_instance.account_info()->auth_code.value()); | |
| 227 EXPECT_EQ(mojom::ChromeAccountType::USER_ACCOUNT, | |
| 228 auth_instance.account_info()->account_type); | |
| 229 EXPECT_FALSE(auth_instance.account_info()->enrollment_token); | |
| 230 EXPECT_FALSE(auth_instance.account_info()->is_managed); | |
| 231 } | |
| 232 | |
| 233 } // namespace arc | |
| OLD | NEW |