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 "chrome/browser/sync/test/integration/passwords_helper.h"
7 #include "base/compiler_specific.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/password_manager/password_form_data.h"
13 #include "chrome/browser/password_manager/password_store.h"
14 #include "chrome/browser/password_manager/password_store_consumer.h"
15 #include "chrome/browser/password_manager/password_store_factory.h"
16 #include "chrome/browser/sync/profile_sync_service.h"
17 #include "chrome/browser/sync/profile_sync_service_factory.h"
18 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
19 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
20 #include "chrome/test/base/ui_test_utils.h"
22 using autofill::PasswordForm
;
23 using sync_datatype_helper::test
;
25 const std::string kFakeSignonRealm
= "http://fake-signon-realm.google.com/";
26 const char* kIndexedFakeOrigin
= "http://fake-signon-realm.google.com/%d";
30 // We use a WaitableEvent to wait when logins are added, removed, or updated
31 // instead of running the UI message loop because of a restriction that
32 // prevents a DB thread from initiating a quit of the UI message loop.
33 void PasswordStoreCallback(base::WaitableEvent
* wait_event
) {
34 // Wake up passwords_helper::AddLogin.
38 class PasswordStoreConsumerHelper
: public PasswordStoreConsumer
{
40 explicit PasswordStoreConsumerHelper(std::vector
<PasswordForm
>* result
)
41 : PasswordStoreConsumer(),
44 virtual void OnPasswordStoreRequestDone(
45 CancelableRequestProvider::Handle handle
,
46 const std::vector
<PasswordForm
*>& result
) OVERRIDE
{
47 // TODO(kaiwang): Remove this function.
51 virtual void OnGetPasswordStoreResults(
52 const std::vector
<PasswordForm
*>& result
) OVERRIDE
{
54 for (std::vector
<PasswordForm
*>::const_iterator it
= result
.begin();
57 result_
->push_back(**it
);
61 // Quit the message loop to wake up passwords_helper::GetLogins.
62 base::MessageLoopForUI::current()->Quit();
66 std::vector
<PasswordForm
>* result_
;
68 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper
);
73 namespace passwords_helper
{
75 void AddLogin(PasswordStore
* store
, const PasswordForm
& form
) {
77 base::WaitableEvent
wait_event(true, false);
78 store
->AddLogin(form
);
79 store
->ScheduleTask(base::Bind(&PasswordStoreCallback
, &wait_event
));
83 void UpdateLogin(PasswordStore
* store
, const PasswordForm
& form
) {
85 base::WaitableEvent
wait_event(true, false);
86 store
->UpdateLogin(form
);
87 store
->ScheduleTask(base::Bind(&PasswordStoreCallback
, &wait_event
));
91 void GetLogins(PasswordStore
* store
, std::vector
<PasswordForm
>& matches
) {
93 PasswordForm matcher_form
;
94 matcher_form
.signon_realm
= kFakeSignonRealm
;
95 PasswordStoreConsumerHelper
consumer(&matches
);
96 store
->GetLogins(matcher_form
, PasswordStore::DISALLOW_PROMPT
, &consumer
);
97 content::RunMessageLoop();
100 void RemoveLogin(PasswordStore
* store
, const PasswordForm
& form
) {
102 base::WaitableEvent
wait_event(true, false);
103 store
->RemoveLogin(form
);
104 store
->ScheduleTask(base::Bind(&PasswordStoreCallback
, &wait_event
));
108 void RemoveLogins(PasswordStore
* store
) {
109 std::vector
<PasswordForm
> forms
;
110 GetLogins(store
, forms
);
111 for (std::vector
<PasswordForm
>::iterator it
= forms
.begin();
112 it
!= forms
.end(); ++it
) {
113 RemoveLogin(store
, *it
);
117 void SetEncryptionPassphrase(int index
,
118 const std::string
& passphrase
,
119 ProfileSyncService::PassphraseType type
) {
120 ProfileSyncServiceFactory::GetForProfile(
121 test()->GetProfile(index
))->SetEncryptionPassphrase(passphrase
, type
);
124 bool SetDecryptionPassphrase(int index
, const std::string
& passphrase
) {
125 return ProfileSyncServiceFactory::GetForProfile(
126 test()->GetProfile(index
))->SetDecryptionPassphrase(passphrase
);
129 PasswordStore
* GetPasswordStore(int index
) {
130 return PasswordStoreFactory::GetForProfile(test()->GetProfile(index
),
131 Profile::IMPLICIT_ACCESS
).get();
134 PasswordStore
* GetVerifierPasswordStore() {
135 return PasswordStoreFactory::GetForProfile(test()->verifier(),
136 Profile::IMPLICIT_ACCESS
).get();
139 bool ProfileContainsSamePasswordFormsAsVerifier(int index
) {
140 std::vector
<PasswordForm
> verifier_forms
;
141 std::vector
<PasswordForm
> forms
;
142 GetLogins(GetVerifierPasswordStore(), verifier_forms
);
143 GetLogins(GetPasswordStore(index
), forms
);
144 bool result
= ContainsSamePasswordForms(verifier_forms
, forms
);
146 LOG(ERROR
) << "Password forms in Verifier Profile:";
147 for (std::vector
<PasswordForm
>::iterator it
= verifier_forms
.begin();
148 it
!= verifier_forms
.end(); ++it
) {
149 LOG(ERROR
) << *it
<< std::endl
;
151 LOG(ERROR
) << "Password forms in Profile" << index
<< ":";
152 for (std::vector
<PasswordForm
>::iterator it
= forms
.begin();
153 it
!= forms
.end(); ++it
) {
154 LOG(ERROR
) << *it
<< std::endl
;
160 bool ProfilesContainSamePasswordForms(int index_a
, int index_b
) {
161 std::vector
<PasswordForm
> forms_a
;
162 std::vector
<PasswordForm
> forms_b
;
163 GetLogins(GetPasswordStore(index_a
), forms_a
);
164 GetLogins(GetPasswordStore(index_b
), forms_b
);
165 bool result
= ContainsSamePasswordForms(forms_a
, forms_b
);
167 LOG(ERROR
) << "Password forms in Profile" << index_a
<< ":";
168 for (std::vector
<PasswordForm
>::iterator it
= forms_a
.begin();
169 it
!= forms_a
.end(); ++it
) {
170 LOG(ERROR
) << *it
<< std::endl
;
172 LOG(ERROR
) << "Password forms in Profile" << index_b
<< ":";
173 for (std::vector
<PasswordForm
>::iterator it
= forms_b
.begin();
174 it
!= forms_b
.end(); ++it
) {
175 LOG(ERROR
) << *it
<< std::endl
;
181 bool AllProfilesContainSamePasswordFormsAsVerifier() {
182 for (int i
= 0; i
< test()->num_clients(); ++i
) {
183 if (!ProfileContainsSamePasswordFormsAsVerifier(i
)) {
184 LOG(ERROR
) << "Profile " << i
<< " does not contain the same password"
185 " forms as the verifier.";
192 bool AllProfilesContainSamePasswordForms() {
193 for (int i
= 1; i
< test()->num_clients(); ++i
) {
194 if (!ProfilesContainSamePasswordForms(0, i
)) {
195 LOG(ERROR
) << "Profile " << i
<< " does not contain the same password"
196 " forms as Profile 0.";
203 int GetPasswordCount(int index
) {
204 std::vector
<PasswordForm
> forms
;
205 GetLogins(GetPasswordStore(index
), forms
);
209 int GetVerifierPasswordCount() {
210 std::vector
<PasswordForm
> verifier_forms
;
211 GetLogins(GetVerifierPasswordStore(), verifier_forms
);
212 return verifier_forms
.size();
215 PasswordForm
CreateTestPasswordForm(int index
) {
217 form
.signon_realm
= kFakeSignonRealm
;
218 form
.origin
= GURL(base::StringPrintf(kIndexedFakeOrigin
, index
));
219 form
.username_value
=
220 base::ASCIIToUTF16(base::StringPrintf("username%d", index
));
221 form
.password_value
=
222 base::ASCIIToUTF16(base::StringPrintf("password%d", index
));
223 form
.date_created
= base::Time::Now();
227 } // namespace passwords_helper