| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 void SetUpOnMainThread() override { | 50 void SetUpOnMainThread() override { |
| 51 OobeBaseTest::SetUpOnMainThread(); | 51 OobeBaseTest::SetUpOnMainThread(); |
| 52 LoadRefreshTokenToDeviceIdMap(); | 52 LoadRefreshTokenToDeviceIdMap(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void TearDownOnMainThread() override { | 55 void TearDownOnMainThread() override { |
| 56 SaveRefreshTokenToDeviceIdMap(); | 56 SaveRefreshTokenToDeviceIdMap(); |
| 57 OobeBaseTest::TearDownOnMainThread(); | 57 OobeBaseTest::TearDownOnMainThread(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 std::string GetDeviceId(const AccountId& account_id) { | 60 std::string GetDeviceId(const std::string& user_id) { |
| 61 return user_manager::UserManager::Get()->GetKnownUserDeviceId(account_id); | 61 return user_manager::UserManager::Get()->GetKnownUserDeviceId(user_id); |
| 62 } | 62 } |
| 63 | 63 |
| 64 std::string GetDeviceIdFromSigninClient(const AccountId& account_id) { | 64 std::string GetDeviceIdFromSigninClient(const std::string& user_id) { |
| 65 return ChromeSigninClientFactory::GetForProfile( | 65 return ChromeSigninClientFactory::GetForProfile( |
| 66 ProfileHelper::Get()->GetProfileByUser( | 66 ProfileHelper::Get()->GetProfileByUser( |
| 67 user_manager::UserManager::Get()->FindUser(account_id))) | 67 user_manager::UserManager::Get()->FindUser(user_id))) |
| 68 ->GetSigninScopedDeviceId(); | 68 ->GetSigninScopedDeviceId(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 std::string GetDeviceIdFromGAIA(const std::string& refresh_token) { | 71 std::string GetDeviceIdFromGAIA(const std::string& refresh_token) { |
| 72 return fake_gaia_->GetDeviceIdByRefreshToken(refresh_token); | 72 return fake_gaia_->GetDeviceIdByRefreshToken(refresh_token); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // Checks that user's device ID retrieved from UserManager and SigninClient | 75 // Checks that user's device ID retrieved from UserManager and SigninClient |
| 76 // are the same. | 76 // are the same. |
| 77 // If |refresh_token| is not empty, checks that device ID associated with the | 77 // If |refresh_token| is not empty, checks that device ID associated with the |
| 78 // |refresh_token| in GAIA is the same as ID saved on device. | 78 // |refresh_token| in GAIA is the same as ID saved on device. |
| 79 void CheckDeviceIDIsConsistent(const AccountId& account_id, | 79 void CheckDeviceIDIsConsistent(const std::string& user_id, |
| 80 const std::string& refresh_token) { | 80 const std::string& refresh_token) { |
| 81 const std::string device_id_in_signin_client = | 81 const std::string device_id_in_signin_client = |
| 82 GetDeviceIdFromSigninClient(account_id); | 82 GetDeviceIdFromSigninClient(user_id); |
| 83 const std::string device_id_in_local_state = GetDeviceId(account_id); | 83 const std::string device_id_in_local_state = GetDeviceId(user_id); |
| 84 | 84 |
| 85 EXPECT_FALSE(device_id_in_signin_client.empty()); | 85 EXPECT_FALSE(device_id_in_signin_client.empty()); |
| 86 EXPECT_EQ(device_id_in_signin_client, device_id_in_local_state); | 86 EXPECT_EQ(device_id_in_signin_client, device_id_in_local_state); |
| 87 | 87 |
| 88 if (!refresh_token.empty()) { | 88 if (!refresh_token.empty()) { |
| 89 const std::string device_id_in_gaia = GetDeviceIdFromGAIA(refresh_token); | 89 const std::string device_id_in_gaia = GetDeviceIdFromGAIA(refresh_token); |
| 90 EXPECT_EQ(device_id_in_signin_client, device_id_in_gaia); | 90 EXPECT_EQ(device_id_in_signin_client, device_id_in_gaia); |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 115 | 115 |
| 116 void SignInOffline(const std::string& user_id, const std::string& password) { | 116 void SignInOffline(const std::string& user_id, const std::string& password) { |
| 117 WaitForSigninScreen(); | 117 WaitForSigninScreen(); |
| 118 | 118 |
| 119 JS().ExecuteAsync( | 119 JS().ExecuteAsync( |
| 120 base::StringPrintf("chrome.send('authenticateUser', ['%s', '%s'])", | 120 base::StringPrintf("chrome.send('authenticateUser', ['%s', '%s'])", |
| 121 user_id.c_str(), password.c_str())); | 121 user_id.c_str(), password.c_str())); |
| 122 WaitForSessionStart(); | 122 WaitForSessionStart(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void RemoveUser(const AccountId& account_id) { | 125 void RemoveUser(const std::string& user_id) { |
| 126 user_manager::UserManager::Get()->RemoveUser(account_id, this); | 126 user_manager::UserManager::Get()->RemoveUser(user_id, this); |
| 127 user_removal_loop_.Run(); | 127 user_removal_loop_.Run(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 private: | 130 private: |
| 131 // user_manager::RemoveUserDelegate: | 131 // user_manager::RemoveUserDelegate: |
| 132 void OnBeforeUserRemoved(const std::string& username) override {} | 132 void OnBeforeUserRemoved(const std::string& username) override {} |
| 133 | 133 |
| 134 void OnUserRemoved(const std::string& username) override { | 134 void OnUserRemoved(const std::string& username) override { |
| 135 user_removal_loop_.Quit(); | 135 user_removal_loop_.Quit(); |
| 136 } | 136 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 json.c_str(), json.length())); | 169 json.c_str(), json.length())); |
| 170 } | 170 } |
| 171 | 171 |
| 172 base::RunLoop user_removal_loop_; | 172 base::RunLoop user_removal_loop_; |
| 173 }; | 173 }; |
| 174 | 174 |
| 175 // Add the first user and check that device ID is consistent. | 175 // Add the first user and check that device ID is consistent. |
| 176 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_PRE_PRE_PRE_PRE_NewUsers) { | 176 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_PRE_PRE_PRE_PRE_NewUsers) { |
| 177 SignInOnline(kFakeUserEmail, kFakeUserPassword, kRefreshToken1, | 177 SignInOnline(kFakeUserEmail, kFakeUserPassword, kRefreshToken1, |
| 178 kFakeUserGaiaId); | 178 kFakeUserGaiaId); |
| 179 CheckDeviceIDIsConsistent(AccountId::FromUserEmail(kFakeUserEmail), | 179 CheckDeviceIDIsConsistent(kFakeUserEmail, kRefreshToken1); |
| 180 kRefreshToken1); | |
| 181 } | 180 } |
| 182 | 181 |
| 183 // Authenticate the first user through GAIA and verify that device ID remains | 182 // Authenticate the first user through GAIA and verify that device ID remains |
| 184 // the same. | 183 // the same. |
| 185 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_PRE_PRE_PRE_NewUsers) { | 184 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_PRE_PRE_PRE_NewUsers) { |
| 186 const std::string device_id = | 185 const std::string device_id = GetDeviceId(kFakeUserEmail); |
| 187 GetDeviceId(AccountId::FromUserEmail(kFakeUserEmail)); | |
| 188 EXPECT_FALSE(device_id.empty()); | 186 EXPECT_FALSE(device_id.empty()); |
| 189 EXPECT_EQ(device_id, GetDeviceIdFromGAIA(kRefreshToken1)); | 187 EXPECT_EQ(device_id, GetDeviceIdFromGAIA(kRefreshToken1)); |
| 190 | 188 |
| 191 SignInOnline(kFakeUserEmail, kFakeUserPassword, kRefreshToken2, | 189 SignInOnline(kFakeUserEmail, kFakeUserPassword, kRefreshToken2, |
| 192 kFakeUserGaiaId); | 190 kFakeUserGaiaId); |
| 193 CheckDeviceIDIsConsistent(AccountId::FromUserEmail(kFakeUserEmail), | 191 CheckDeviceIDIsConsistent(kFakeUserEmail, kRefreshToken2); |
| 194 kRefreshToken2); | |
| 195 | 192 |
| 196 CHECK_EQ(device_id, GetDeviceId(AccountId::FromUserEmail(kFakeUserEmail))); | 193 CHECK_EQ(device_id, GetDeviceId(kFakeUserEmail)); |
| 197 } | 194 } |
| 198 | 195 |
| 199 // Authenticate the first user offline and verify that device ID remains | 196 // Authenticate the first user offline and verify that device ID remains |
| 200 // the same. | 197 // the same. |
| 201 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_PRE_PRE_NewUsers) { | 198 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_PRE_PRE_NewUsers) { |
| 202 const std::string device_id = | 199 const std::string device_id = GetDeviceId(kFakeUserEmail); |
| 203 GetDeviceId(AccountId::FromUserEmail(kFakeUserEmail)); | |
| 204 EXPECT_FALSE(device_id.empty()); | 200 EXPECT_FALSE(device_id.empty()); |
| 205 | 201 |
| 206 SignInOffline(kFakeUserEmail, kFakeUserPassword); | 202 SignInOffline(kFakeUserEmail, kFakeUserPassword); |
| 207 CheckDeviceIDIsConsistent(AccountId::FromUserEmail(kFakeUserEmail), | 203 CheckDeviceIDIsConsistent(kFakeUserEmail, kRefreshToken2); |
| 208 kRefreshToken2); | |
| 209 | 204 |
| 210 // Verify that device ID remained the same after offline auth. | 205 // Verify that device ID remained the same after offline auth. |
| 211 CHECK_EQ(device_id, GetDeviceId(AccountId::FromUserEmail(kFakeUserEmail))); | 206 CHECK_EQ(device_id, GetDeviceId(kFakeUserEmail)); |
| 212 } | 207 } |
| 213 | 208 |
| 214 // Add the second user. | 209 // Add the second user. |
| 215 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_PRE_NewUsers) { | 210 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_PRE_NewUsers) { |
| 216 WaitForSigninScreen(); | 211 WaitForSigninScreen(); |
| 217 JS().ExecuteAsync("chrome.send('showAddUser')"); | 212 JS().ExecuteAsync("chrome.send('showAddUser')"); |
| 218 SignInOnline(kSecondUserEmail, kSecondUserPassword, kSecondUserRefreshToken1, | 213 SignInOnline(kSecondUserEmail, kSecondUserPassword, kSecondUserRefreshToken1, |
| 219 kSecondUserGaiaId); | 214 kSecondUserGaiaId); |
| 220 CheckDeviceIDIsConsistent(AccountId::FromUserEmail(kSecondUserEmail), | 215 CheckDeviceIDIsConsistent(kSecondUserEmail, kSecondUserRefreshToken1); |
| 221 kSecondUserRefreshToken1); | |
| 222 } | 216 } |
| 223 | 217 |
| 224 // Remove the second user. | 218 // Remove the second user. |
| 225 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_NewUsers) { | 219 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_NewUsers) { |
| 226 WaitForSigninScreen(); | 220 WaitForSigninScreen(); |
| 227 RemoveUser(AccountId::FromUserEmail(kSecondUserEmail)); | 221 RemoveUser(kSecondUserEmail); |
| 228 } | 222 } |
| 229 | 223 |
| 230 // Add the second user back. Verify that device ID has been changed. | 224 // Add the second user back. Verify that device ID has been changed. |
| 231 IN_PROC_BROWSER_TEST_F(DeviceIDTest, NewUsers) { | 225 IN_PROC_BROWSER_TEST_F(DeviceIDTest, NewUsers) { |
| 232 EXPECT_TRUE(GetDeviceId(AccountId::FromUserEmail(kSecondUserEmail)).empty()); | 226 EXPECT_TRUE(GetDeviceId(kSecondUserEmail).empty()); |
| 233 SignInOnline(kSecondUserEmail, kSecondUserPassword, kSecondUserRefreshToken2, | 227 SignInOnline(kSecondUserEmail, kSecondUserPassword, kSecondUserRefreshToken2, |
| 234 kSecondUserGaiaId); | 228 kSecondUserGaiaId); |
| 235 CheckDeviceIDIsConsistent(AccountId::FromUserEmail(kSecondUserEmail), | 229 CheckDeviceIDIsConsistent(kSecondUserEmail, kSecondUserRefreshToken2); |
| 236 kSecondUserRefreshToken2); | |
| 237 EXPECT_NE(GetDeviceIdFromGAIA(kSecondUserRefreshToken1), | 230 EXPECT_NE(GetDeviceIdFromGAIA(kSecondUserRefreshToken1), |
| 238 GetDeviceId(AccountId::FromUserEmail(kSecondUserEmail))); | 231 GetDeviceId(kSecondUserEmail)); |
| 239 } | 232 } |
| 240 | 233 |
| 241 // Set up a user that has a device ID stored in preference only. | 234 // Set up a user that has a device ID stored in preference only. |
| 242 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_Migration) { | 235 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_Migration) { |
| 243 SignInOnline(kFakeUserEmail, kFakeUserPassword, kRefreshToken1, | 236 SignInOnline(kFakeUserEmail, kFakeUserPassword, kRefreshToken1, |
| 244 kFakeUserGaiaId); | 237 kFakeUserGaiaId); |
| 245 | 238 |
| 246 // Simulate user that has device ID saved only in preferences (pre-M44). | 239 // Simulate user that has device ID saved only in preferences (pre-M44). |
| 247 PrefService* prefs = | 240 PrefService* prefs = |
| 248 ProfileHelper::Get() | 241 ProfileHelper::Get() |
| 249 ->GetProfileByUser(user_manager::UserManager::Get()->GetActiveUser()) | 242 ->GetProfileByUser(user_manager::UserManager::Get()->GetActiveUser()) |
| 250 ->GetPrefs(); | 243 ->GetPrefs(); |
| 251 prefs->SetString(prefs::kGoogleServicesSigninScopedDeviceId, | 244 prefs->SetString(prefs::kGoogleServicesSigninScopedDeviceId, |
| 252 GetDeviceId(AccountId::FromUserEmail(kFakeUserEmail))); | 245 GetDeviceId(kFakeUserEmail)); |
| 253 | 246 |
| 254 // Can't use SetKnownUserDeviceId here, because it forbids changing a device | 247 // Can't use SetKnownUserDeviceId here, because it forbids changing a device |
| 255 // ID. | 248 // ID. |
| 256 user_manager::UserManager::Get()->SetKnownUserStringPref( | 249 user_manager::UserManager::Get()->SetKnownUserStringPref( |
| 257 AccountId::FromUserEmail(kFakeUserEmail), "device_id", std::string()); | 250 kFakeUserEmail, "device_id", std::string()); |
| 258 } | 251 } |
| 259 | 252 |
| 260 // Tests that after the first sign in the device ID has been moved to the Local | 253 // Tests that after the first sign in the device ID has been moved to the Local |
| 261 // state. | 254 // state. |
| 262 IN_PROC_BROWSER_TEST_F(DeviceIDTest, Migration) { | 255 IN_PROC_BROWSER_TEST_F(DeviceIDTest, Migration) { |
| 263 EXPECT_TRUE(GetDeviceId(AccountId::FromUserEmail(kFakeUserEmail)).empty()); | 256 EXPECT_TRUE(GetDeviceId(kFakeUserEmail).empty()); |
| 264 SignInOffline(kFakeUserEmail, kFakeUserPassword); | 257 SignInOffline(kFakeUserEmail, kFakeUserPassword); |
| 265 CheckDeviceIDIsConsistent(AccountId::FromUserEmail(kFakeUserEmail), | 258 CheckDeviceIDIsConsistent(kFakeUserEmail, kRefreshToken1); |
| 266 kRefreshToken1); | |
| 267 } | 259 } |
| 268 | 260 |
| 269 // Set up a user that doesn't have a device ID. | 261 // Set up a user that doesn't have a device ID. |
| 270 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_LegacyUsers) { | 262 IN_PROC_BROWSER_TEST_F(DeviceIDTest, PRE_LegacyUsers) { |
| 271 SignInOnline(kFakeUserEmail, kFakeUserPassword, kRefreshToken1, | 263 SignInOnline(kFakeUserEmail, kFakeUserPassword, kRefreshToken1, |
| 272 kFakeUserGaiaId); | 264 kFakeUserGaiaId); |
| 273 | 265 |
| 274 PrefService* prefs = | 266 PrefService* prefs = |
| 275 ProfileHelper::Get() | 267 ProfileHelper::Get() |
| 276 ->GetProfileByUser(user_manager::UserManager::Get()->GetActiveUser()) | 268 ->GetProfileByUser(user_manager::UserManager::Get()->GetActiveUser()) |
| 277 ->GetPrefs(); | 269 ->GetPrefs(); |
| 278 EXPECT_TRUE( | 270 EXPECT_TRUE( |
| 279 prefs->GetString(prefs::kGoogleServicesSigninScopedDeviceId).empty()); | 271 prefs->GetString(prefs::kGoogleServicesSigninScopedDeviceId).empty()); |
| 280 | 272 |
| 281 // Can't use SetKnownUserDeviceId here, because it forbids changing a device | 273 // Can't use SetKnownUserDeviceId here, because it forbids changing a device |
| 282 // ID. | 274 // ID. |
| 283 user_manager::UserManager::Get()->SetKnownUserStringPref( | 275 user_manager::UserManager::Get()->SetKnownUserStringPref( |
| 284 AccountId::FromUserEmail(kFakeUserEmail), "device_id", std::string()); | 276 kFakeUserEmail, "device_id", std::string()); |
| 285 } | 277 } |
| 286 | 278 |
| 287 // Tests that device ID has been generated after the first sign in. | 279 // Tests that device ID has been generated after the first sign in. |
| 288 IN_PROC_BROWSER_TEST_F(DeviceIDTest, LegacyUsers) { | 280 IN_PROC_BROWSER_TEST_F(DeviceIDTest, LegacyUsers) { |
| 289 EXPECT_TRUE(GetDeviceId(AccountId::FromUserEmail(kFakeUserEmail)).empty()); | 281 EXPECT_TRUE(GetDeviceId(kFakeUserEmail).empty()); |
| 290 SignInOffline(kFakeUserEmail, kFakeUserPassword); | 282 SignInOffline(kFakeUserEmail, kFakeUserPassword); |
| 291 // Last param |auth_code| is empty, because we don't pass a device ID to GAIA | 283 // Last param |auth_code| is empty, because we don't pass a device ID to GAIA |
| 292 // in this case. | 284 // in this case. |
| 293 CheckDeviceIDIsConsistent(AccountId::FromUserEmail(kFakeUserEmail), | 285 CheckDeviceIDIsConsistent(kFakeUserEmail, std::string()); |
| 294 std::string()); | |
| 295 } | 286 } |
| 296 | 287 |
| 297 } // namespace chromeos | 288 } // namespace chromeos |
| OLD | NEW |