two_client_passwords_sync_test.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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.
4
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_integration_test_util.h"
9#include "chrome/browser/sync/test/integration/sync_test.h"
10#include "sync/internal_api/public/engine/model_safe_worker.h"
11#include "sync/internal_api/public/sessions/sync_session_snapshot.h"
12
13using passwords_helper::AddLogin;
14using passwords_helper::AllProfilesContainSamePasswordForms;
15using passwords_helper::AllProfilesContainSamePasswordFormsAsVerifier;
16using passwords_helper::CreateTestPasswordForm;
17using passwords_helper::GetPasswordCount;
18using passwords_helper::GetPasswordStore;
19using passwords_helper::GetVerifierPasswordCount;
20using passwords_helper::GetVerifierPasswordStore;
21using passwords_helper::ProfileContainsSamePasswordFormsAsVerifier;
22using passwords_helper::RemoveLogin;
23using passwords_helper::RemoveLogins;
24using passwords_helper::SetDecryptionPassphrase;
25using passwords_helper::SetEncryptionPassphrase;
26using passwords_helper::UpdateLogin;
27using sync_integration_test_util::AwaitCommitActivityCompletion;
28using sync_integration_test_util::AwaitPassphraseAccepted;
29using sync_integration_test_util::AwaitPassphraseRequired;
30
31using autofill::PasswordForm;
32
33static const char* kValidPassphrase = "passphrase!";
34static const char* kAnotherValidPassphrase = "another passphrase!";
35
36class TwoClientPasswordsSyncTest : public SyncTest {
37 public:
38  TwoClientPasswordsSyncTest() : SyncTest(TWO_CLIENT) {}
39  virtual ~TwoClientPasswordsSyncTest() {}
40
41 private:
42  DISALLOW_COPY_AND_ASSIGN(TwoClientPasswordsSyncTest);
43};
44
45class LegacyTwoClientPasswordsSyncTest : public SyncTest {
46 public:
47  LegacyTwoClientPasswordsSyncTest() : SyncTest(TWO_CLIENT_LEGACY) {}
48  virtual ~LegacyTwoClientPasswordsSyncTest() {}
49
50 private:
51  DISALLOW_COPY_AND_ASSIGN(LegacyTwoClientPasswordsSyncTest);
52};
53
54// TCM ID - 3732277
55IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, Add) {
56  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
57  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
58
59  PasswordForm form = CreateTestPasswordForm(0);
60  AddLogin(GetVerifierPasswordStore(), form);
61  ASSERT_EQ(1, GetVerifierPasswordCount());
62  AddLogin(GetPasswordStore(0), form);
63  ASSERT_EQ(1, GetPasswordCount(0));
64
65  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
66  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
67}
68
69IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, Race) {
70  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
71  ASSERT_TRUE(AllProfilesContainSamePasswordForms());
72
73  PasswordForm form0 = CreateTestPasswordForm(0);
74  AddLogin(GetPasswordStore(0), form0);
75
76  PasswordForm form1 = form0;
77  form1.password_value = base::ASCIIToUTF16("new_password");
78  AddLogin(GetPasswordStore(1), form1);
79
80  ASSERT_TRUE(AwaitQuiescence());
81  ASSERT_TRUE(AllProfilesContainSamePasswordForms());
82}
83
84// TCM ID - 4577932.
85IN_PROC_BROWSER_TEST_F(LegacyTwoClientPasswordsSyncTest, DisablePasswords) {
86  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
87  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
88
89  ASSERT_TRUE(GetClient(1)->DisableSyncForDatatype(syncer::PASSWORDS));
90  PasswordForm form = CreateTestPasswordForm(0);
91  AddLogin(GetVerifierPasswordStore(), form);
92  ASSERT_EQ(1, GetVerifierPasswordCount());
93  AddLogin(GetPasswordStore(0), form);
94  ASSERT_EQ(1, GetPasswordCount(0));
95
96  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
97  ASSERT_TRUE(ProfileContainsSamePasswordFormsAsVerifier(0));
98  ASSERT_FALSE(ProfileContainsSamePasswordFormsAsVerifier(1));
99
100  ASSERT_TRUE(GetClient(1)->EnableSyncForDatatype(syncer::PASSWORDS));
101  ASSERT_TRUE(AwaitQuiescence());
102  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
103  ASSERT_EQ(1, GetPasswordCount(1));
104}
105
106// TCM ID - 4649281.
107IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, DisableSync) {
108  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
109  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
110
111  ASSERT_TRUE(GetClient(1)->DisableSyncForAllDatatypes());
112  PasswordForm form = CreateTestPasswordForm(0);
113  AddLogin(GetVerifierPasswordStore(), form);
114  ASSERT_EQ(1, GetVerifierPasswordCount());
115  AddLogin(GetPasswordStore(0), form);
116  ASSERT_EQ(1, GetPasswordCount(0));
117
118  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
119  ASSERT_TRUE(ProfileContainsSamePasswordFormsAsVerifier(0));
120  ASSERT_FALSE(ProfileContainsSamePasswordFormsAsVerifier(1));
121
122  ASSERT_TRUE(GetClient(1)->EnableSyncForAllDatatypes());
123  ASSERT_TRUE(AwaitQuiescence());
124  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
125  ASSERT_EQ(1, GetPasswordCount(1));
126}
127
128IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, SetPassphrase) {
129  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
130
131  SetEncryptionPassphrase(0, kValidPassphrase, ProfileSyncService::EXPLICIT);
132  ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((0))));
133  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
134
135  ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService((1))));
136  ASSERT_TRUE(SetDecryptionPassphrase(1, kValidPassphrase));
137  ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((1))));
138  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((1))));
139}
140
141IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
142                       SetPassphraseAndAddPassword) {
143  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
144
145  SetEncryptionPassphrase(0, kValidPassphrase, ProfileSyncService::EXPLICIT);
146  ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((0))));
147  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
148
149  ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService((1))));
150  ASSERT_TRUE(SetDecryptionPassphrase(1, kValidPassphrase));
151  ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((1))));
152
153  PasswordForm form = CreateTestPasswordForm(0);
154  AddLogin(GetPasswordStore(0), form);
155  ASSERT_EQ(1, GetPasswordCount(0));
156
157  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
158  ASSERT_EQ(1, GetPasswordCount(1));
159}
160
161// TCM ID - 4603879
162IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, Update) {
163  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
164  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
165
166  PasswordForm form = CreateTestPasswordForm(0);
167  AddLogin(GetVerifierPasswordStore(), form);
168  AddLogin(GetPasswordStore(0), form);
169  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
170
171  form.password_value = base::ASCIIToUTF16("new_password");
172  UpdateLogin(GetVerifierPasswordStore(), form);
173  UpdateLogin(GetPasswordStore(1), form);
174  ASSERT_TRUE(AwaitQuiescence());
175
176  ASSERT_EQ(1, GetVerifierPasswordCount());
177  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
178}
179
180// TCM ID - 3719309
181IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, Delete) {
182  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
183  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
184
185  PasswordForm form0 = CreateTestPasswordForm(0);
186  AddLogin(GetVerifierPasswordStore(), form0);
187  AddLogin(GetPasswordStore(0), form0);
188  PasswordForm form1 = CreateTestPasswordForm(1);
189  AddLogin(GetVerifierPasswordStore(), form1);
190  AddLogin(GetPasswordStore(0), form1);
191  ASSERT_TRUE(AwaitQuiescence());
192
193  RemoveLogin(GetPasswordStore(1), form0);
194  RemoveLogin(GetVerifierPasswordStore(), form0);
195  ASSERT_TRUE(AwaitQuiescence());
196
197  ASSERT_EQ(1, GetVerifierPasswordCount());
198  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
199}
200
201// TCM ID - 7573511
202// Flaky on Mac and Windows: http://crbug.com/111399
203#if defined(OS_WIN) || defined(OS_MACOSX)
204#define MAYBE_DeleteAll DISABLED_DeleteAll
205#else
206#define MAYBE_DeleteAll DeleteAll
207#endif
208IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, MAYBE_DeleteAll) {
209  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
210  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
211
212  PasswordForm form0 = CreateTestPasswordForm(0);
213  AddLogin(GetVerifierPasswordStore(), form0);
214  AddLogin(GetPasswordStore(0), form0);
215  PasswordForm form1 = CreateTestPasswordForm(1);
216  AddLogin(GetVerifierPasswordStore(), form1);
217  AddLogin(GetPasswordStore(0), form1);
218  ASSERT_TRUE(AwaitQuiescence());
219
220  RemoveLogins(GetPasswordStore(1));
221  RemoveLogins(GetVerifierPasswordStore());
222  ASSERT_TRUE(AwaitQuiescence());
223
224  ASSERT_EQ(0, GetVerifierPasswordCount());
225  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
226}
227
228// TCM ID - 3694311
229IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, Merge) {
230  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
231  ASSERT_TRUE(AllProfilesContainSamePasswordForms());
232
233  PasswordForm form0 = CreateTestPasswordForm(0);
234  AddLogin(GetPasswordStore(0), form0);
235  PasswordForm form1 = CreateTestPasswordForm(1);
236  AddLogin(GetPasswordStore(1), form1);
237  PasswordForm form2 = CreateTestPasswordForm(2);
238  AddLogin(GetPasswordStore(1), form2);
239  ASSERT_TRUE(AwaitQuiescence());
240
241  ASSERT_EQ(3, GetPasswordCount(0));
242  ASSERT_TRUE(AllProfilesContainSamePasswordForms());
243}
244
245IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
246                       SetPassphraseAndThenSetupSync) {
247  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
248
249  ASSERT_TRUE(GetClient(0)->SetupSync());
250  SetEncryptionPassphrase(0, kValidPassphrase, ProfileSyncService::EXPLICIT);
251  ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((0))));
252  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
253
254  ASSERT_FALSE(GetClient(1)->SetupSync());
255  ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService((1))));
256  ASSERT_TRUE(SetDecryptionPassphrase(1, kValidPassphrase));
257  ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((1))));
258
259  // For some reason, the tests won't pass unless these flags are set.
260  GetSyncService((1))->SetSetupInProgress(false);
261  GetSyncService((1))->SetSyncSetupCompleted();
262
263  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((1))));
264
265  // Following ensures types are enabled and active (see bug 87572).
266  syncer::ModelSafeRoutingInfo routes;
267  GetSyncService((0))->GetModelSafeRoutingInfo(&routes);
268  ASSERT_EQ(syncer::GROUP_PASSWORD, routes[syncer::PASSWORDS]);
269  routes.clear();
270  GetSyncService((1))->GetModelSafeRoutingInfo(&routes);
271  ASSERT_EQ(syncer::GROUP_PASSWORD, routes[syncer::PASSWORDS]);
272}
273
274IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
275                       SetDifferentPassphraseAndThenSetupSync) {
276  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
277
278  ASSERT_TRUE(GetClient(0)->SetupSync());
279  SetEncryptionPassphrase(0, kValidPassphrase, ProfileSyncService::EXPLICIT);
280  ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((0))));
281  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
282
283  // Setup 1 with a different passphrase, so that it fails to sync.
284  ASSERT_FALSE(GetClient(1)->SetupSync());
285  ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService((1))));
286  ASSERT_FALSE(SetDecryptionPassphrase(1, kAnotherValidPassphrase));
287  ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService((1))));
288
289  // Add a password on 0 while clients have different passphrases.
290  PasswordForm form0 = CreateTestPasswordForm(0);
291  AddLogin(GetVerifierPasswordStore(), form0);
292  AddLogin(GetPasswordStore(0), form0);
293
294  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
295
296  // Password hasn't been synced to 1 yet.
297  ASSERT_FALSE(AllProfilesContainSamePasswordFormsAsVerifier());
298
299  // Update 1 with the correct passphrase, the password should now sync over.
300  ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService((1))));
301  ASSERT_TRUE(SetDecryptionPassphrase(1, kValidPassphrase));
302  ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((1))));
303
304  // For some reason, the tests won't pass unless these flags are set.
305  GetSyncService((1))->SetSetupInProgress(false);
306  GetSyncService((1))->SetSyncSetupCompleted();
307
308  ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
309  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
310}
311