1 // Copyright (c) 2012 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.
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/sync/test/integration/passwords_helper.h"
7 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
8 #include "chrome/browser/sync/test/integration/sync_test.h"
9 #include "sync/internal_api/public/engine/model_safe_worker.h"
10 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
12 using passwords_helper::AddLogin
;
13 using passwords_helper::AllProfilesContainSamePasswordForms
;
14 using passwords_helper::AllProfilesContainSamePasswordFormsAsVerifier
;
15 using passwords_helper::CreateTestPasswordForm
;
16 using passwords_helper::GetPasswordCount
;
17 using passwords_helper::GetPasswordStore
;
18 using passwords_helper::GetVerifierPasswordCount
;
19 using passwords_helper::GetVerifierPasswordStore
;
20 using passwords_helper::ProfileContainsSamePasswordFormsAsVerifier
;
21 using passwords_helper::RemoveLogin
;
22 using passwords_helper::RemoveLogins
;
23 using passwords_helper::SetDecryptionPassphrase
;
24 using passwords_helper::SetEncryptionPassphrase
;
25 using passwords_helper::UpdateLogin
;
27 using autofill::PasswordForm
;
29 static const char* kValidPassphrase
= "passphrase!";
30 static const char* kAnotherValidPassphrase
= "another passphrase!";
32 class TwoClientPasswordsSyncTest
: public SyncTest
{
34 TwoClientPasswordsSyncTest() : SyncTest(TWO_CLIENT
) {}
35 virtual ~TwoClientPasswordsSyncTest() {}
38 DISALLOW_COPY_AND_ASSIGN(TwoClientPasswordsSyncTest
);
42 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
, Add
) {
43 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
44 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
46 PasswordForm form
= CreateTestPasswordForm(0);
47 AddLogin(GetVerifierPasswordStore(), form
);
48 ASSERT_EQ(1, GetVerifierPasswordCount());
49 AddLogin(GetPasswordStore(0), form
);
50 ASSERT_EQ(1, GetPasswordCount(0));
52 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
53 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
56 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
, Race
) {
57 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
58 ASSERT_TRUE(AllProfilesContainSamePasswordForms());
60 PasswordForm form0
= CreateTestPasswordForm(0);
61 AddLogin(GetPasswordStore(0), form0
);
63 PasswordForm form1
= form0
;
64 form1
.password_value
= base::ASCIIToUTF16("new_password");
65 AddLogin(GetPasswordStore(1), form1
);
67 ASSERT_TRUE(AwaitQuiescence());
68 ASSERT_TRUE(AllProfilesContainSamePasswordForms());
72 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
, DisablePasswords
) {
73 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
74 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
76 ASSERT_TRUE(GetClient(1)->DisableSyncForDatatype(syncer::PASSWORDS
));
77 PasswordForm form
= CreateTestPasswordForm(0);
78 AddLogin(GetVerifierPasswordStore(), form
);
79 ASSERT_EQ(1, GetVerifierPasswordCount());
80 AddLogin(GetPasswordStore(0), form
);
81 ASSERT_EQ(1, GetPasswordCount(0));
83 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
84 ASSERT_TRUE(ProfileContainsSamePasswordFormsAsVerifier(0));
85 ASSERT_FALSE(ProfileContainsSamePasswordFormsAsVerifier(1));
87 ASSERT_TRUE(GetClient(1)->EnableSyncForDatatype(syncer::PASSWORDS
));
88 ASSERT_TRUE(AwaitQuiescence());
89 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
90 ASSERT_EQ(1, GetPasswordCount(1));
94 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
, DisableSync
) {
95 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
96 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
98 ASSERT_TRUE(GetClient(1)->DisableSyncForAllDatatypes());
99 PasswordForm form
= CreateTestPasswordForm(0);
100 AddLogin(GetVerifierPasswordStore(), form
);
101 ASSERT_EQ(1, GetVerifierPasswordCount());
102 AddLogin(GetPasswordStore(0), form
);
103 ASSERT_EQ(1, GetPasswordCount(0));
105 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
106 ASSERT_TRUE(ProfileContainsSamePasswordFormsAsVerifier(0));
107 ASSERT_FALSE(ProfileContainsSamePasswordFormsAsVerifier(1));
109 ASSERT_TRUE(GetClient(1)->EnableSyncForAllDatatypes());
110 ASSERT_TRUE(AwaitQuiescence());
111 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
112 ASSERT_EQ(1, GetPasswordCount(1));
115 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
, SetPassphrase
) {
116 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
118 SetEncryptionPassphrase(0, kValidPassphrase
, ProfileSyncService::EXPLICIT
);
119 ASSERT_TRUE(GetClient(0)->AwaitPassphraseAccepted());
120 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
122 ASSERT_TRUE(GetClient(1)->AwaitPassphraseRequired());
123 ASSERT_TRUE(SetDecryptionPassphrase(1, kValidPassphrase
));
124 ASSERT_TRUE(GetClient(1)->AwaitPassphraseAccepted());
125 ASSERT_TRUE(GetClient(1)->AwaitFullSyncCompletion());
128 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
,
129 SetPassphraseAndAddPassword
) {
130 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
132 SetEncryptionPassphrase(0, kValidPassphrase
, ProfileSyncService::EXPLICIT
);
133 ASSERT_TRUE(GetClient(0)->AwaitPassphraseAccepted());
134 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
136 ASSERT_TRUE(GetClient(1)->AwaitPassphraseRequired());
137 ASSERT_TRUE(SetDecryptionPassphrase(1, kValidPassphrase
));
138 ASSERT_TRUE(GetClient(1)->AwaitPassphraseAccepted());
140 PasswordForm form
= CreateTestPasswordForm(0);
141 AddLogin(GetPasswordStore(0), form
);
142 ASSERT_EQ(1, GetPasswordCount(0));
144 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
145 ASSERT_EQ(1, GetPasswordCount(1));
149 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
, Update
) {
150 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
151 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
153 PasswordForm form
= CreateTestPasswordForm(0);
154 AddLogin(GetVerifierPasswordStore(), form
);
155 AddLogin(GetPasswordStore(0), form
);
156 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
158 form
.password_value
= base::ASCIIToUTF16("new_password");
159 UpdateLogin(GetVerifierPasswordStore(), form
);
160 UpdateLogin(GetPasswordStore(1), form
);
161 ASSERT_TRUE(AwaitQuiescence());
163 ASSERT_EQ(1, GetVerifierPasswordCount());
164 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
168 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
, Delete
) {
169 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
170 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
172 PasswordForm form0
= CreateTestPasswordForm(0);
173 AddLogin(GetVerifierPasswordStore(), form0
);
174 AddLogin(GetPasswordStore(0), form0
);
175 PasswordForm form1
= CreateTestPasswordForm(1);
176 AddLogin(GetVerifierPasswordStore(), form1
);
177 AddLogin(GetPasswordStore(0), form1
);
178 ASSERT_TRUE(AwaitQuiescence());
180 RemoveLogin(GetPasswordStore(1), form0
);
181 RemoveLogin(GetVerifierPasswordStore(), form0
);
182 ASSERT_TRUE(AwaitQuiescence());
184 ASSERT_EQ(1, GetVerifierPasswordCount());
185 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
189 // Flaky on Mac and Windows: http://crbug.com/111399
190 #if defined(OS_WIN) || defined(OS_MACOSX)
191 #define MAYBE_DeleteAll DISABLED_DeleteAll
193 #define MAYBE_DeleteAll DeleteAll
195 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
, MAYBE_DeleteAll
) {
196 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
197 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
199 PasswordForm form0
= CreateTestPasswordForm(0);
200 AddLogin(GetVerifierPasswordStore(), form0
);
201 AddLogin(GetPasswordStore(0), form0
);
202 PasswordForm form1
= CreateTestPasswordForm(1);
203 AddLogin(GetVerifierPasswordStore(), form1
);
204 AddLogin(GetPasswordStore(0), form1
);
205 ASSERT_TRUE(AwaitQuiescence());
207 RemoveLogins(GetPasswordStore(1));
208 RemoveLogins(GetVerifierPasswordStore());
209 ASSERT_TRUE(AwaitQuiescence());
211 ASSERT_EQ(0, GetVerifierPasswordCount());
212 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
216 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
, Merge
) {
217 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
218 ASSERT_TRUE(AllProfilesContainSamePasswordForms());
220 PasswordForm form0
= CreateTestPasswordForm(0);
221 AddLogin(GetPasswordStore(0), form0
);
222 PasswordForm form1
= CreateTestPasswordForm(1);
223 AddLogin(GetPasswordStore(1), form1
);
224 PasswordForm form2
= CreateTestPasswordForm(2);
225 AddLogin(GetPasswordStore(1), form2
);
226 ASSERT_TRUE(AwaitQuiescence());
228 ASSERT_EQ(3, GetPasswordCount(0));
229 ASSERT_TRUE(AllProfilesContainSamePasswordForms());
232 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
,
233 SetPassphraseAndThenSetupSync
) {
234 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
236 ASSERT_TRUE(GetClient(0)->SetupSync());
237 SetEncryptionPassphrase(0, kValidPassphrase
, ProfileSyncService::EXPLICIT
);
238 ASSERT_TRUE(GetClient(0)->AwaitPassphraseAccepted());
239 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
241 ASSERT_FALSE(GetClient(1)->SetupSync());
242 ASSERT_TRUE(GetClient(1)->AwaitPassphraseRequired());
243 ASSERT_TRUE(SetDecryptionPassphrase(1, kValidPassphrase
));
244 ASSERT_TRUE(GetClient(1)->AwaitPassphraseAccepted());
245 ASSERT_TRUE(GetClient(1)->AwaitFullSyncCompletion());
247 // Following ensures types are enabled and active (see bug 87572).
248 syncer::ModelSafeRoutingInfo routes
;
249 GetClient(0)->service()->GetModelSafeRoutingInfo(&routes
);
250 ASSERT_EQ(syncer::GROUP_PASSWORD
, routes
[syncer::PASSWORDS
]);
252 GetClient(1)->service()->GetModelSafeRoutingInfo(&routes
);
253 ASSERT_EQ(syncer::GROUP_PASSWORD
, routes
[syncer::PASSWORDS
]);
256 IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest
,
257 SetDifferentPassphraseAndThenSetupSync
) {
258 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
260 ASSERT_TRUE(GetClient(0)->SetupSync());
261 SetEncryptionPassphrase(0, kValidPassphrase
, ProfileSyncService::EXPLICIT
);
262 ASSERT_TRUE(GetClient(0)->AwaitPassphraseAccepted());
263 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
265 // Setup 1 with a different passphrase, so that it fails to sync.
266 ASSERT_FALSE(GetClient(1)->SetupSync());
267 ASSERT_TRUE(GetClient(1)->AwaitPassphraseRequired());
268 ASSERT_FALSE(SetDecryptionPassphrase(1, kAnotherValidPassphrase
));
269 ASSERT_TRUE(GetClient(1)->AwaitPassphraseRequired());
271 // Add a password on 0 while clients have different passphrases.
272 PasswordForm form0
= CreateTestPasswordForm(0);
273 AddLogin(GetVerifierPasswordStore(), form0
);
274 AddLogin(GetPasswordStore(0), form0
);
276 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
278 // Password hasn't been synced to 1 yet.
279 ASSERT_FALSE(AllProfilesContainSamePasswordFormsAsVerifier());
281 // Update 1 with the correct passphrase, the password should now sync over.
282 ASSERT_TRUE(GetClient(1)->AwaitPassphraseRequired());
283 ASSERT_TRUE(SetDecryptionPassphrase(1, kValidPassphrase
));
284 ASSERT_TRUE(GetClient(1)->AwaitPassphraseAccepted());
286 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
287 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());