1 // Copyright 2014 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.
7 #include "base/compiler_specific.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/chromeos/login/login_manager_test.h"
13 #include "chrome/browser/chromeos/login/startup_utils.h"
14 #include "chrome/browser/chromeos/login/supervised/supervised_user_authentication.h"
15 #include "chrome/browser/chromeos/login/supervised/supervised_user_test_base.h"
16 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
17 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
18 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
19 #include "chrome/browser/chromeos/login/users/supervised_user_manager.h"
20 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
21 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
22 #include "chrome/browser/supervised_user/supervised_user_constants.h"
23 #include "chromeos/cryptohome/mock_async_method_caller.h"
24 #include "chromeos/cryptohome/mock_homedir_methods.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/test/browser_test_utils.h"
27 #include "content/public/test/test_utils.h"
28 #include "sync/api/fake_sync_change_processor.h"
29 #include "sync/api/sync_change.h"
30 #include "sync/api/sync_error_factory_mock.h"
31 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
32 #include "sync/protocol/sync.pb.h"
35 using chromeos::SupervisedUserTestBase
;
36 using chromeos::kTestSupervisedUserDisplayName
;
37 using chromeos::kTestManager
;
41 class SupervisedUserPasswordTest
: public SupervisedUserTestBase
{
43 SupervisedUserPasswordTest() : SupervisedUserTestBase() {}
46 DISALLOW_COPY_AND_ASSIGN(SupervisedUserPasswordTest
);
49 class SupervisedUserPasswordManagerTest
: public SupervisedUserTestBase
{
51 SupervisedUserPasswordManagerTest() : SupervisedUserTestBase() {}
54 DISALLOW_COPY_AND_ASSIGN(SupervisedUserPasswordManagerTest
);
57 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
58 PRE_PRE_PRE_PasswordChangeFromUserTest
) {
62 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
63 PRE_PRE_PasswordChangeFromUserTest
) {
64 StartFlowLoginAsManager();
65 FillNewUserData(kTestSupervisedUserDisplayName
);
66 StartUserCreation("supervised-user-creation-next-button",
67 kTestSupervisedUserDisplayName
);
70 // Supervised user signs in, get sync notification about password update, and
71 // schedules password migration.
72 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
73 PRE_PasswordChangeFromUserTest
) {
74 SigninAsSupervisedUser(true, 0, kTestSupervisedUserDisplayName
);
76 const user_manager::User
* user
=
77 user_manager::UserManager::Get()->GetUsers().at(0);
79 ChromeUserManager::Get()->GetSupervisedUserManager()->GetUserSyncId(
81 base::DictionaryValue password
;
82 password
.SetIntegerWithoutPathExpansion(
83 kSchemaVersion
, SupervisedUserAuthentication::SCHEMA_SALT_HASHED
);
84 password
.SetIntegerWithoutPathExpansion(kPasswordRevision
, 2);
86 password
.SetStringWithoutPathExpansion(kPasswordSignature
, "signature");
87 password
.SetStringWithoutPathExpansion(kEncryptedPassword
,
88 "new-encrypted-password");
90 shared_settings_adapter_
->AddChange(
91 sync_id
, supervised_users::kChromeOSPasswordData
, password
, true, false);
92 content::RunAllPendingInMessageLoop();
95 // Supervised user signs in for second time, and actual password migration takes
97 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
98 PasswordChangeFromUserTest
) {
99 EXPECT_CALL(*mock_homedir_methods_
, MountEx(_
, _
, _
, _
)).Times(1);
100 EXPECT_CALL(*mock_homedir_methods_
, UpdateKeyEx(_
, _
, _
, _
, _
)).Times(1);
101 SigninAsSupervisedUser(false, 0, kTestSupervisedUserDisplayName
);
102 testing::Mock::VerifyAndClearExpectations(mock_homedir_methods_
);
105 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
106 PRE_PRE_PRE_PasswordChangeFromManagerTest
) {
110 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
111 PRE_PRE_PasswordChangeFromManagerTest
) {
112 StartFlowLoginAsManager();
113 FillNewUserData(kTestSupervisedUserDisplayName
);
114 StartUserCreation("supervised-user-creation-next-button",
115 kTestSupervisedUserDisplayName
);
118 // Manager signs in, gets sync notification about supervised user password
119 // update, and performs migration.
120 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
121 PRE_PasswordChangeFromManagerTest
) {
122 const user_manager::User
* supervised_user
=
123 user_manager::UserManager::Get()->GetUsers().at(0);
127 EXPECT_CALL(*mock_homedir_methods_
, AddKeyEx(_
, _
, _
, _
, _
)).Times(1);
129 std::string sync_id
=
130 ChromeUserManager::Get()->GetSupervisedUserManager()->GetUserSyncId(
131 supervised_user
->email());
133 ::sync_pb::ManagedUserSpecifics managed_user_proto
;
135 managed_user_proto
.set_id(sync_id
);
136 managed_user_proto
.set_name(kTestSupervisedUserDisplayName
);
137 managed_user_proto
.set_acknowledged(true);
138 managed_user_proto
.set_master_key("master key");
139 managed_user_proto
.set_password_signature_key("signature_key");
140 managed_user_proto
.set_password_encryption_key("encryption_key");
142 supervised_users_adapter_
->AddChange(managed_user_proto
, false);
143 content::RunAllPendingInMessageLoop();
145 base::DictionaryValue password
;
146 password
.SetIntegerWithoutPathExpansion(
147 kSchemaVersion
, SupervisedUserAuthentication::SCHEMA_SALT_HASHED
);
148 password
.SetIntegerWithoutPathExpansion(kPasswordRevision
, 2);
150 password
.SetStringWithoutPathExpansion(kPasswordSignature
, "signature");
151 password
.SetStringWithoutPathExpansion(kEncryptedPassword
,
152 "new-encrypted-password");
153 shared_settings_adapter_
->AddChange(
154 sync_id
, supervised_users::kChromeOSPasswordData
, password
, true, false);
155 content::RunAllPendingInMessageLoop();
157 testing::Mock::VerifyAndClearExpectations(mock_homedir_methods_
);
160 // After that supervised user signs in, and no password change happens.
161 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
162 PasswordChangeFromManagerTest
) {
163 EXPECT_CALL(*mock_homedir_methods_
, MountEx(_
, _
, _
, _
)).Times(1);
164 EXPECT_CALL(*mock_homedir_methods_
, UpdateKeyEx(_
, _
, _
, _
, _
)).Times(0);
165 SigninAsSupervisedUser(false, 1, kTestSupervisedUserDisplayName
);
166 testing::Mock::VerifyAndClearExpectations(mock_homedir_methods_
);
169 IN_PROC_BROWSER_TEST_F(
170 SupervisedUserPasswordTest
,
171 PRE_PRE_PRE_PRE_PasswordChangeUserAndManagerTest
) {
175 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
176 PRE_PRE_PRE_PasswordChangeUserAndManagerTest
) {
177 StartFlowLoginAsManager();
178 FillNewUserData(kTestSupervisedUserDisplayName
);
179 StartUserCreation("supervised-user-creation-next-button",
180 kTestSupervisedUserDisplayName
);
183 // Supervised user signs in, get sync notification about password update, and
184 // schedules password migration.
185 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
186 PRE_PRE_PasswordChangeUserAndManagerTest
) {
187 SigninAsSupervisedUser(true, 0, kTestSupervisedUserDisplayName
);
189 const user_manager::User
* user
=
190 user_manager::UserManager::Get()->GetUsers().at(0);
191 std::string sync_id
=
192 ChromeUserManager::Get()->GetSupervisedUserManager()->GetUserSyncId(
194 base::DictionaryValue password
;
195 password
.SetIntegerWithoutPathExpansion(
196 kSchemaVersion
, SupervisedUserAuthentication::SCHEMA_SALT_HASHED
);
197 password
.SetIntegerWithoutPathExpansion(kPasswordRevision
, 2);
199 password
.SetStringWithoutPathExpansion(kPasswordSignature
, "signature");
200 password
.SetStringWithoutPathExpansion(kEncryptedPassword
,
201 "new-encrypted-password");
203 shared_settings_adapter_
->AddChange(
204 sync_id
, supervised_users::kChromeOSPasswordData
, password
, true, false);
205 content::RunAllPendingInMessageLoop();
208 // After that manager signs in, and also detects password change. Manager
209 // performs the migration.
210 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
211 PRE_PasswordChangeUserAndManagerTest
) {
212 const user_manager::User
* supervised_user
=
213 user_manager::UserManager::Get()->GetUsers().at(0);
217 EXPECT_CALL(*mock_homedir_methods_
, AddKeyEx(_
, _
, _
, _
, _
)).Times(1);
219 std::string sync_id
=
220 ChromeUserManager::Get()->GetSupervisedUserManager()->GetUserSyncId(
221 supervised_user
->email());
223 ::sync_pb::ManagedUserSpecifics managed_user_proto
;
225 managed_user_proto
.set_id(sync_id
);
226 managed_user_proto
.set_name(kTestSupervisedUserDisplayName
);
227 managed_user_proto
.set_acknowledged(true);
228 managed_user_proto
.set_master_key("master key");
229 managed_user_proto
.set_password_signature_key("signature_key");
230 managed_user_proto
.set_password_encryption_key("encryption_key");
232 supervised_users_adapter_
->AddChange(managed_user_proto
, false);
233 content::RunAllPendingInMessageLoop();
235 base::DictionaryValue password
;
236 password
.SetIntegerWithoutPathExpansion(
237 kSchemaVersion
, SupervisedUserAuthentication::SCHEMA_SALT_HASHED
);
238 password
.SetIntegerWithoutPathExpansion(kPasswordRevision
, 2);
240 password
.SetStringWithoutPathExpansion(kPasswordSignature
, "signature");
241 password
.SetStringWithoutPathExpansion(kEncryptedPassword
,
242 "new-encrypted-password");
243 shared_settings_adapter_
->AddChange(
244 sync_id
, supervised_users::kChromeOSPasswordData
, password
, true, false);
245 content::RunAllPendingInMessageLoop();
247 testing::Mock::VerifyAndClearExpectations(mock_homedir_methods_
);
250 // When supervised user signs in, password is already migrated, so no migration
251 // should be attempted.
252 IN_PROC_BROWSER_TEST_F(SupervisedUserPasswordTest
,
253 PasswordChangeUserAndManagerTest
) {
254 EXPECT_CALL(*mock_homedir_methods_
, MountEx(_
, _
, _
, _
)).Times(1);
255 EXPECT_CALL(*mock_homedir_methods_
, UpdateKeyEx(_
, _
, _
, _
, _
)).Times(0);
256 SigninAsSupervisedUser(false, 1, kTestSupervisedUserDisplayName
);
257 testing::Mock::VerifyAndClearExpectations(mock_homedir_methods_
);
260 } // namespace chromeos