| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 #include "components/user_manager/user_names.h" | 37 #include "components/user_manager/user_names.h" |
| 38 #include "content/public/browser/browser_thread.h" | 38 #include "content/public/browser/browser_thread.h" |
| 39 #include "content/public/test/test_browser_thread_bundle.h" | 39 #include "content/public/test/test_browser_thread_bundle.h" |
| 40 #include "google_apis/gaia/gaia_constants.h" | 40 #include "google_apis/gaia/gaia_constants.h" |
| 41 #include "google_apis/gaia/gaia_urls.h" | 41 #include "google_apis/gaia/gaia_urls.h" |
| 42 #include "net/http/http_status_code.h" | 42 #include "net/http/http_status_code.h" |
| 43 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
| 44 | 44 |
| 45 namespace arc { | 45 namespace arc { |
| 46 | 46 |
| 47 namespace { | |
| 48 | |
| 49 const char kTestAuthCode[] = "4/Qa3CPIhh-WcMfWSf9HZaYcGUhEeax-F9sQK9CNRhZWs"; | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 class ArcAuthServiceTest : public testing::Test { | 47 class ArcAuthServiceTest : public testing::Test { |
| 54 public: | 48 public: |
| 55 ArcAuthServiceTest() | 49 ArcAuthServiceTest() |
| 56 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 50 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 57 user_manager_enabler_(new chromeos::FakeChromeUserManager) {} | 51 user_manager_enabler_(new chromeos::FakeChromeUserManager) {} |
| 58 ~ArcAuthServiceTest() override = default; | 52 ~ArcAuthServiceTest() override = default; |
| 59 | 53 |
| 60 void SetUp() override { | 54 void SetUp() override { |
| 61 chromeos::DBusThreadManager::Initialize(); | 55 chromeos::DBusThreadManager::Initialize(); |
| 62 | 56 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 172 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 179 ASSERT_EQ(ArcAuthService::State::NOT_INITIALIZED, auth_service()->state()); | 173 ASSERT_EQ(ArcAuthService::State::NOT_INITIALIZED, auth_service()->state()); |
| 180 | 174 |
| 181 // Correctly stop service. | 175 // Correctly stop service. |
| 182 auth_service()->Shutdown(); | 176 auth_service()->Shutdown(); |
| 183 } | 177 } |
| 184 | 178 |
| 185 TEST_F(ArcAuthServiceTest, BaseWorkflow) { | 179 TEST_F(ArcAuthServiceTest, BaseWorkflow) { |
| 186 ASSERT_FALSE(bridge_service()->ready()); | 180 ASSERT_FALSE(bridge_service()->ready()); |
| 187 ASSERT_EQ(ArcAuthService::State::NOT_INITIALIZED, auth_service()->state()); | 181 ASSERT_EQ(ArcAuthService::State::NOT_INITIALIZED, auth_service()->state()); |
| 188 ASSERT_EQ(std::string(), auth_service()->GetAndResetAuthCode()); | |
| 189 | 182 |
| 190 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 183 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 191 | 184 |
| 192 // By default ARC is not enabled. | 185 // By default ARC is not enabled. |
| 193 ASSERT_EQ(ArcAuthService::State::STOPPED, auth_service()->state()); | 186 ASSERT_EQ(ArcAuthService::State::STOPPED, auth_service()->state()); |
| 194 | 187 |
| 195 profile()->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); | 188 profile()->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); |
| 196 | 189 |
| 197 // Setting profile and pref initiates a code fetching process. | 190 // Setting profile and pref initiates a code fetching process. |
| 198 ASSERT_EQ(ArcAuthService::State::TERMS, auth_service()->state()); | 191 ASSERT_EQ(ArcAuthService::State::TERMS, auth_service()->state()); |
| 199 | 192 |
| 200 // TODO(hidehiko): Verify state transition from TERMS -> | 193 // TODO(hidehiko): Verify state transition from TERMS -> |
| 201 // ANDROID_MANAGEMENT_CHECK -> FETCHING_CODE, when we extract | 194 // ANDROID_MANAGEMENT_CHECK, when we extract ArcSessionManager. |
| 202 // ArcSessionManager. | 195 auth_service()->StartArc(); |
| 203 auth_service()->SetAuthCodeAndStartArc(kTestAuthCode); | |
| 204 | 196 |
| 205 ASSERT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); | 197 ASSERT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); |
| 206 ASSERT_TRUE(bridge_service()->ready()); | 198 ASSERT_TRUE(bridge_service()->ready()); |
| 207 // Auth code valid only for one call. | |
| 208 ASSERT_EQ(kTestAuthCode, auth_service()->GetAndResetAuthCode()); | |
| 209 ASSERT_EQ(std::string(), auth_service()->GetAndResetAuthCode()); | |
| 210 | 199 |
| 211 auth_service()->Shutdown(); | 200 auth_service()->Shutdown(); |
| 212 ASSERT_EQ(ArcAuthService::State::NOT_INITIALIZED, auth_service()->state()); | 201 ASSERT_EQ(ArcAuthService::State::NOT_INITIALIZED, auth_service()->state()); |
| 213 ASSERT_FALSE(bridge_service()->ready()); | 202 ASSERT_FALSE(bridge_service()->ready()); |
| 214 ASSERT_EQ(std::string(), auth_service()->GetAndResetAuthCode()); | |
| 215 | 203 |
| 216 // Send profile and don't provide a code. | 204 // Send profile and don't provide a code. |
| 217 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 205 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 218 | 206 |
| 219 // Setting profile initiates a code fetching process. | 207 // Setting profile initiates a code fetching process. |
| 220 ASSERT_EQ(ArcAuthService::State::TERMS, auth_service()->state()); | 208 ASSERT_EQ(ArcAuthService::State::TERMS, auth_service()->state()); |
| 221 | 209 |
| 222 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 210 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 223 base::RunLoop().RunUntilIdle(); | 211 base::RunLoop().RunUntilIdle(); |
| 224 | 212 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 243 // Correctly stop service. | 231 // Correctly stop service. |
| 244 auth_service()->Shutdown(); | 232 auth_service()->Shutdown(); |
| 245 } | 233 } |
| 246 | 234 |
| 247 TEST_F(ArcAuthServiceTest, CloseUIKeepsArcEnabled) { | 235 TEST_F(ArcAuthServiceTest, CloseUIKeepsArcEnabled) { |
| 248 PrefService* const pref = profile()->GetPrefs(); | 236 PrefService* const pref = profile()->GetPrefs(); |
| 249 | 237 |
| 250 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 238 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 251 pref->SetBoolean(prefs::kArcEnabled, true); | 239 pref->SetBoolean(prefs::kArcEnabled, true); |
| 252 | 240 |
| 253 auth_service()->SetAuthCodeAndStartArc(kTestAuthCode); | 241 auth_service()->StartArc(); |
| 254 | 242 |
| 255 ASSERT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); | 243 ASSERT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); |
| 256 | 244 |
| 257 auth_service()->CancelAuthCode(); | 245 auth_service()->CancelAuthCode(); |
| 258 ASSERT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); | 246 ASSERT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); |
| 259 ASSERT_TRUE(pref->GetBoolean(prefs::kArcEnabled)); | 247 ASSERT_TRUE(pref->GetBoolean(prefs::kArcEnabled)); |
| 260 | 248 |
| 261 // Correctly stop service. | 249 // Correctly stop service. |
| 262 auth_service()->Shutdown(); | 250 auth_service()->Shutdown(); |
| 263 } | 251 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 277 } | 265 } |
| 278 | 266 |
| 279 TEST_F(ArcAuthServiceTest, SignInStatus) { | 267 TEST_F(ArcAuthServiceTest, SignInStatus) { |
| 280 PrefService* const prefs = profile()->GetPrefs(); | 268 PrefService* const prefs = profile()->GetPrefs(); |
| 281 | 269 |
| 282 EXPECT_FALSE(prefs->GetBoolean(prefs::kArcSignedIn)); | 270 EXPECT_FALSE(prefs->GetBoolean(prefs::kArcSignedIn)); |
| 283 prefs->SetBoolean(prefs::kArcEnabled, true); | 271 prefs->SetBoolean(prefs::kArcEnabled, true); |
| 284 | 272 |
| 285 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 273 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 286 EXPECT_EQ(ArcAuthService::State::TERMS, auth_service()->state()); | 274 EXPECT_EQ(ArcAuthService::State::TERMS, auth_service()->state()); |
| 287 auth_service()->SetAuthCodeAndStartArc(kTestAuthCode); | 275 auth_service()->StartArc(); |
| 288 EXPECT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); | 276 EXPECT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); |
| 289 EXPECT_TRUE(bridge_service()->ready()); | 277 EXPECT_TRUE(bridge_service()->ready()); |
| 290 EXPECT_FALSE(prefs->GetBoolean(prefs::kArcSignedIn)); | 278 EXPECT_FALSE(prefs->GetBoolean(prefs::kArcSignedIn)); |
| 291 auth_service()->OnSignInComplete(); | 279 auth_service()->OnSignInComplete(); |
| 292 EXPECT_TRUE(prefs->GetBoolean(prefs::kArcSignedIn)); | 280 EXPECT_TRUE(prefs->GetBoolean(prefs::kArcSignedIn)); |
| 293 EXPECT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); | 281 EXPECT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); |
| 294 EXPECT_TRUE(bridge_service()->ready()); | 282 EXPECT_TRUE(bridge_service()->ready()); |
| 295 | 283 |
| 296 // Second start, no fetching code is expected. | 284 // Second start, no fetching code is expected. |
| 297 auth_service()->Shutdown(); | 285 auth_service()->Shutdown(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 313 | 301 |
| 314 // Correctly stop service. | 302 // Correctly stop service. |
| 315 auth_service()->Shutdown(); | 303 auth_service()->Shutdown(); |
| 316 } | 304 } |
| 317 | 305 |
| 318 TEST_F(ArcAuthServiceTest, DisabledForDeviceLocalAccount) { | 306 TEST_F(ArcAuthServiceTest, DisabledForDeviceLocalAccount) { |
| 319 PrefService* const prefs = profile()->GetPrefs(); | 307 PrefService* const prefs = profile()->GetPrefs(); |
| 320 EXPECT_FALSE(prefs->GetBoolean(prefs::kArcSignedIn)); | 308 EXPECT_FALSE(prefs->GetBoolean(prefs::kArcSignedIn)); |
| 321 prefs->SetBoolean(prefs::kArcEnabled, true); | 309 prefs->SetBoolean(prefs::kArcEnabled, true); |
| 322 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 310 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 323 auth_service()->SetAuthCodeAndStartArc(kTestAuthCode); | 311 auth_service()->StartArc(); |
| 324 EXPECT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); | 312 EXPECT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); |
| 325 | 313 |
| 326 // Create device local account and set it as active. | 314 // Create device local account and set it as active. |
| 327 const std::string email = "device-local-account@fake-email.com"; | 315 const std::string email = "device-local-account@fake-email.com"; |
| 328 TestingProfile::Builder profile_builder; | 316 TestingProfile::Builder profile_builder; |
| 329 profile_builder.SetProfileName(email); | 317 profile_builder.SetProfileName(email); |
| 330 std::unique_ptr<TestingProfile> device_local_profile(profile_builder.Build()); | 318 std::unique_ptr<TestingProfile> device_local_profile(profile_builder.Build()); |
| 331 const AccountId account_id(AccountId::FromUserEmail(email)); | 319 const AccountId account_id(AccountId::FromUserEmail(email)); |
| 332 GetFakeUserManager()->AddPublicAccountUser(account_id); | 320 GetFakeUserManager()->AddPublicAccountUser(account_id); |
| 333 | 321 |
| 334 // Remove |profile_| to set the device local account be the primary account. | 322 // Remove |profile_| to set the device local account be the primary account. |
| 335 GetFakeUserManager()->RemoveUserFromList( | 323 GetFakeUserManager()->RemoveUserFromList( |
| 336 multi_user_util::GetAccountIdFromProfile(profile())); | 324 multi_user_util::GetAccountIdFromProfile(profile())); |
| 337 GetFakeUserManager()->LoginUser(account_id); | 325 GetFakeUserManager()->LoginUser(account_id); |
| 338 | 326 |
| 339 // Check that user without GAIA account can't use ARC. | 327 // Check that user without GAIA account can't use ARC. |
| 340 device_local_profile->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); | 328 device_local_profile->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); |
| 341 auth_service()->OnPrimaryUserProfilePrepared(device_local_profile.get()); | 329 auth_service()->OnPrimaryUserProfilePrepared(device_local_profile.get()); |
| 342 EXPECT_EQ(ArcAuthService::State::NOT_INITIALIZED, auth_service()->state()); | 330 EXPECT_EQ(ArcAuthService::State::NOT_INITIALIZED, auth_service()->state()); |
| 343 | 331 |
| 344 // Correctly stop service. | 332 // Correctly stop service. |
| 345 auth_service()->Shutdown(); | 333 auth_service()->Shutdown(); |
| 346 } | 334 } |
| 347 | 335 |
| 348 TEST_F(ArcAuthServiceTest, DisabledForNonPrimaryProfile) { | 336 TEST_F(ArcAuthServiceTest, DisabledForNonPrimaryProfile) { |
| 349 profile()->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); | 337 profile()->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); |
| 350 auth_service()->OnPrimaryUserProfilePrepared(profile()); | 338 auth_service()->OnPrimaryUserProfilePrepared(profile()); |
| 351 auth_service()->SetAuthCodeAndStartArc(kTestAuthCode); | 339 auth_service()->StartArc(); |
| 352 EXPECT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); | 340 EXPECT_EQ(ArcAuthService::State::ACTIVE, auth_service()->state()); |
| 353 | 341 |
| 354 // Create a second profile and set it as the active profile. | 342 // Create a second profile and set it as the active profile. |
| 355 const std::string email = "test@example.com"; | 343 const std::string email = "test@example.com"; |
| 356 TestingProfile::Builder profile_builder; | 344 TestingProfile::Builder profile_builder; |
| 357 profile_builder.SetProfileName(email); | 345 profile_builder.SetProfileName(email); |
| 358 std::unique_ptr<TestingProfile> second_profile(profile_builder.Build()); | 346 std::unique_ptr<TestingProfile> second_profile(profile_builder.Build()); |
| 359 const AccountId account_id(AccountId::FromUserEmail(email)); | 347 const AccountId account_id(AccountId::FromUserEmail(email)); |
| 360 GetFakeUserManager()->AddUser(account_id); | 348 GetFakeUserManager()->AddUser(account_id); |
| 361 GetFakeUserManager()->SwitchActiveUser(account_id); | 349 GetFakeUserManager()->SwitchActiveUser(account_id); |
| 362 second_profile->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); | 350 second_profile->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); |
| 363 | 351 |
| 364 // Check that non-primary user can't use Arc. | 352 // Check that non-primary user can't use Arc. |
| 365 EXPECT_FALSE(chromeos::ProfileHelper::IsPrimaryProfile(second_profile.get())); | 353 EXPECT_FALSE(chromeos::ProfileHelper::IsPrimaryProfile(second_profile.get())); |
| 366 EXPECT_FALSE(ArcAppListPrefs::Get(second_profile.get())); | 354 EXPECT_FALSE(ArcAppListPrefs::Get(second_profile.get())); |
| 367 | 355 |
| 368 auth_service()->Shutdown(); | 356 auth_service()->Shutdown(); |
| 369 } | 357 } |
| 370 | 358 |
| 371 } // namespace arc | 359 } // namespace arc |
| OLD | NEW |