1 // Copyright (c) 2011 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/stringprintf.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/sync/test/integration/passwords_helper.h"
8 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
9 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
10 #include "chrome/browser/sync/test/integration/sync_test.h"
11 #include "components/password_manager/core/browser/password_form_data.h"
12
13 using passwords_helper::AddLogin;
14 using passwords_helper::AllProfilesContainSamePasswordFormsAsVerifier;
15 using passwords_helper::AwaitAllProfilesContainSamePasswordForms;
16 using passwords_helper::AwaitProfileContainsSamePasswordFormsAsVerifier;
17 using passwords_helper::CreateTestPasswordForm;
18 using passwords_helper::GetPasswordCount;
19 using passwords_helper::GetPasswordStore;
20 using passwords_helper::GetVerifierPasswordCount;
21 using passwords_helper::GetVerifierPasswordStore;
22 using passwords_helper::ProfileContainsSamePasswordFormsAsVerifier;
23 using passwords_helper::SetDecryptionPassphrase;
24 using passwords_helper::SetEncryptionPassphrase;
25 using sync_integration_test_util::AwaitPassphraseAccepted;
26 using sync_integration_test_util::AwaitPassphraseRequired;
27
28 using autofill::PasswordForm;
29
30 static const char* kValidPassphrase = "passphrase!";
31 static const char* kAnotherValidPassphrase = "Mot de passe!";
32
33 class MultipleClientPasswordsSyncTest : public SyncTest {
34 public:
MultipleClientPasswordsSyncTest()35 MultipleClientPasswordsSyncTest() : SyncTest(MULTIPLE_CLIENT) {}
~MultipleClientPasswordsSyncTest()36 virtual ~MultipleClientPasswordsSyncTest() {}
37
TestUsesSelfNotifications()38 virtual bool TestUsesSelfNotifications() OVERRIDE {
39 return false;
40 }
41
42 private:
43 DISALLOW_COPY_AND_ASSIGN(MultipleClientPasswordsSyncTest);
44 };
45
IN_PROC_BROWSER_TEST_F(MultipleClientPasswordsSyncTest,Sanity)46 IN_PROC_BROWSER_TEST_F(MultipleClientPasswordsSyncTest, Sanity) {
47 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
48
49 for (int i = 0; i < num_clients(); ++i) {
50 PasswordForm form = CreateTestPasswordForm(i);
51 AddLogin(GetPasswordStore(i), form);
52 }
53
54 ASSERT_TRUE(AwaitAllProfilesContainSamePasswordForms());
55 ASSERT_EQ(num_clients(), GetPasswordCount(0));
56 }
57
IN_PROC_BROWSER_TEST_F(MultipleClientPasswordsSyncTest,SetPassphraseAndThenSetupSync)58 IN_PROC_BROWSER_TEST_F(MultipleClientPasswordsSyncTest,
59 SetPassphraseAndThenSetupSync) {
60 ASSERT_TRUE(SetupClients());
61
62 ASSERT_TRUE(GetClient(0)->SetupSync());
63 SetEncryptionPassphrase(0, kValidPassphrase, ProfileSyncService::EXPLICIT);
64 ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService(0)));
65
66 // When client 1 hits a passphrase required state, we can infer that
67 // client 0's passphrase has been committed. to the server.
68 GetClient(1)->SetupSync();
69 ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService(1)));
70
71 // Setup client 2 *after* the passphrase has been committed.
72 ASSERT_FALSE(GetClient(2)->SetupSync());
73 ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService(2)));
74
75 // Get clients 1 and 2 out of the passphrase required state.
76 ASSERT_TRUE(SetDecryptionPassphrase(1, kValidPassphrase));
77 ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService(1)));
78 ASSERT_TRUE(SetDecryptionPassphrase(2, kValidPassphrase));
79 ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService(2)));
80
81 // For some reason, the tests won't pass unless these flags are set.
82 GetSyncService(1)->SetSyncSetupCompleted();
83 GetSyncService(1)->SetSetupInProgress(false);
84 GetSyncService(2)->SetSyncSetupCompleted();
85 GetSyncService(2)->SetSetupInProgress(false);
86
87 // Move around some passwords to make sure it's all working.
88 PasswordForm form0 = CreateTestPasswordForm(0);
89 AddLogin(GetPasswordStore(0), form0);
90
91 ASSERT_TRUE(AwaitAllProfilesContainSamePasswordForms());
92 }
93
IN_PROC_BROWSER_TEST_F(MultipleClientPasswordsSyncTest,SetDifferentPassphraseAndThenSetupSync)94 IN_PROC_BROWSER_TEST_F(MultipleClientPasswordsSyncTest,
95 SetDifferentPassphraseAndThenSetupSync) {
96 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
97
98 ASSERT_TRUE(GetClient(0)->SetupSync());
99 SetEncryptionPassphrase(0, kValidPassphrase, ProfileSyncService::EXPLICIT);
100 ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((0))));
101
102 // When client 1 hits a passphrase required state, we can infer that
103 // client 0's passphrase has been committed. to the server.
104 GetClient(1)->SetupSync();
105 ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService(1)));
106
107 // Give client 1 the correct passphrase.
108 SetDecryptionPassphrase(1, kValidPassphrase);
109 ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService((1))));
110
111 // For some reason, the tests won't pass unless these flags are set.
112 GetSyncService(1)->SetSetupInProgress(false);
113 GetSyncService(1)->SetSyncSetupCompleted();
114
115 // Give client 2 a different passphrase so it fails to sync.
116 ASSERT_FALSE(GetClient(2)->SetupSync());
117 ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService((2))));
118 SetDecryptionPassphrase(2, kAnotherValidPassphrase);
119 ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService((2))));
120
121 // Add a password on 0 while client 2 has different passphrases.
122 PasswordForm form0 = CreateTestPasswordForm(0);
123 AddLogin(GetVerifierPasswordStore(), form0);
124 AddLogin(GetPasswordStore(0), form0);
125
126 // It should sync to client 1.
127 ASSERT_TRUE(AwaitProfileContainsSamePasswordFormsAsVerifier(1));
128
129 // But it won't get synced to 2.
130 ASSERT_FALSE(ProfileContainsSamePasswordFormsAsVerifier(2));
131
132 // Update 2 with the correct passphrase, the password should now sync over.
133 ASSERT_TRUE(AwaitPassphraseRequired(GetSyncService(2)));
134 ASSERT_TRUE(SetDecryptionPassphrase(2, kValidPassphrase));
135 ASSERT_TRUE(AwaitPassphraseAccepted(GetSyncService(2)));
136
137 // For some reason, the tests won't pass unless these flags are set.
138 GetSyncService(2)->SetSetupInProgress(false);
139 GetSyncService(2)->SetSyncSetupCompleted();
140
141 ASSERT_TRUE(AwaitProfileContainsSamePasswordFormsAsVerifier(2));
142 ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
143 }
144