OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/test/integration/passwords_helper.h" | 5 #include "chrome/browser/sync/test/integration/passwords_helper.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "chrome/browser/password_manager/password_store_factory.h" | 12 #include "chrome/browser/password_manager/password_store_factory.h" |
13 #include "chrome/browser/sync/profile_sync_service.h" | 13 #include "chrome/browser/sync/profile_sync_service.h" |
14 #include "chrome/browser/sync/profile_sync_service_factory.h" | 14 #include "chrome/browser/sync/profile_sync_service_factory.h" |
15 #include "chrome/browser/sync/test/integration/multi_client_status_change_checke
r.h" | 15 #include "chrome/browser/sync/test/integration/multi_client_status_change_checke
r.h" |
16 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" | 16 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" |
17 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" | 17 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" |
18 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 18 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
19 #include "components/password_manager/core/browser/password_form_data.h" | 19 #include "components/password_manager/core/browser/password_form_data.h" |
20 #include "components/password_manager/core/browser/password_store.h" | 20 #include "components/password_manager/core/browser/password_store.h" |
21 #include "components/password_manager/core/browser/password_store_consumer.h" | 21 #include "components/password_manager/core/browser/password_store_consumer.h" |
22 #include "content/public/test/test_utils.h" | 22 #include "content/public/test/test_utils.h" |
23 | 23 |
24 using autofill::PasswordForm; | 24 using autofill::PasswordForm; |
25 using password_manager::PasswordStore; | 25 using password_manager::PasswordStore; |
26 using sync_datatype_helper::test; | 26 using sync_datatype_helper::test; |
27 | 27 |
28 const std::string kFakeSignonRealm = "http://fake-signon-realm.google.com/"; | 28 namespace { |
29 const char* kIndexedFakeOrigin = "http://fake-signon-realm.google.com/%d"; | |
30 | 29 |
31 namespace { | 30 const char kFakeSignonRealm[] = "http://fake-signon-realm.google.com/"; |
| 31 const char kIndexedFakeOrigin[] = "http://fake-signon-realm.google.com/%d"; |
32 | 32 |
33 // We use a WaitableEvent to wait when logins are added, removed, or updated | 33 // We use a WaitableEvent to wait when logins are added, removed, or updated |
34 // instead of running the UI message loop because of a restriction that | 34 // instead of running the UI message loop because of a restriction that |
35 // prevents a DB thread from initiating a quit of the UI message loop. | 35 // prevents a DB thread from initiating a quit of the UI message loop. |
36 void PasswordStoreCallback(base::WaitableEvent* wait_event) { | 36 void PasswordStoreCallback(base::WaitableEvent* wait_event) { |
37 // Wake up passwords_helper::AddLogin. | 37 // Wake up passwords_helper::AddLogin. |
38 wait_event->Signal(); | 38 wait_event->Signal(); |
39 } | 39 } |
40 | 40 |
41 class PasswordStoreConsumerHelper | 41 class PasswordStoreConsumerHelper |
42 : public password_manager::PasswordStoreConsumer { | 42 : public password_manager::PasswordStoreConsumer { |
43 public: | 43 public: |
44 explicit PasswordStoreConsumerHelper(std::vector<PasswordForm>* result) | 44 PasswordStoreConsumerHelper() {} |
45 : password_manager::PasswordStoreConsumer(), result_(result) {} | |
46 | 45 |
47 void OnGetPasswordStoreResults( | 46 void OnGetPasswordStoreResults(ScopedVector<PasswordForm> results) override { |
48 const std::vector<PasswordForm*>& result) override { | 47 result_.swap(results); |
49 result_->clear(); | |
50 for (std::vector<PasswordForm*>::const_iterator it = result.begin(); | |
51 it != result.end(); | |
52 ++it) { | |
53 result_->push_back(**it); | |
54 delete *it; | |
55 } | |
56 | |
57 // Quit the message loop to wake up passwords_helper::GetLogins. | 48 // Quit the message loop to wake up passwords_helper::GetLogins. |
58 base::MessageLoopForUI::current()->Quit(); | 49 base::MessageLoopForUI::current()->Quit(); |
59 } | 50 } |
60 | 51 |
| 52 ScopedVector<PasswordForm> result() { return result_.Pass(); } |
| 53 |
61 private: | 54 private: |
62 std::vector<PasswordForm>* result_; | 55 ScopedVector<PasswordForm> result_; |
63 | 56 |
64 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper); | 57 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper); |
65 }; | 58 }; |
66 | 59 |
67 // PasswordForm::date_synced is a local field. Therefore it may be different | 60 // PasswordForm::date_synced is a local field. Therefore it may be different |
68 // across clients. | 61 // across clients. |
69 void ClearSyncDateField(std::vector<PasswordForm>* forms) { | 62 void ClearSyncDateField(std::vector<PasswordForm*>* forms) { |
70 for (std::vector<PasswordForm>::iterator it = forms->begin(); | 63 for (PasswordForm* form : *forms) { |
71 it != forms->end(); | 64 form->date_synced = base::Time(); |
72 ++it) { | |
73 it->date_synced = base::Time(); | |
74 } | 65 } |
75 } | 66 } |
76 | 67 |
77 } // namespace | 68 } // namespace |
78 | 69 |
79 namespace passwords_helper { | 70 namespace passwords_helper { |
80 | 71 |
81 void AddLogin(PasswordStore* store, const PasswordForm& form) { | 72 void AddLogin(PasswordStore* store, const PasswordForm& form) { |
82 ASSERT_TRUE(store); | 73 ASSERT_TRUE(store); |
83 base::WaitableEvent wait_event(true, false); | 74 base::WaitableEvent wait_event(true, false); |
84 store->AddLogin(form); | 75 store->AddLogin(form); |
85 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); | 76 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); |
86 wait_event.Wait(); | 77 wait_event.Wait(); |
87 } | 78 } |
88 | 79 |
89 void UpdateLogin(PasswordStore* store, const PasswordForm& form) { | 80 void UpdateLogin(PasswordStore* store, const PasswordForm& form) { |
90 ASSERT_TRUE(store); | 81 ASSERT_TRUE(store); |
91 base::WaitableEvent wait_event(true, false); | 82 base::WaitableEvent wait_event(true, false); |
92 store->UpdateLogin(form); | 83 store->UpdateLogin(form); |
93 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); | 84 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); |
94 wait_event.Wait(); | 85 wait_event.Wait(); |
95 } | 86 } |
96 | 87 |
97 void GetLogins(PasswordStore* store, std::vector<PasswordForm>& matches) { | 88 ScopedVector<PasswordForm> GetLogins(PasswordStore* store) { |
98 ASSERT_TRUE(store); | 89 EXPECT_TRUE(store); |
99 PasswordForm matcher_form; | 90 PasswordForm matcher_form; |
100 matcher_form.signon_realm = kFakeSignonRealm; | 91 matcher_form.signon_realm = kFakeSignonRealm; |
101 PasswordStoreConsumerHelper consumer(&matches); | 92 PasswordStoreConsumerHelper consumer; |
102 store->GetLogins(matcher_form, PasswordStore::DISALLOW_PROMPT, &consumer); | 93 store->GetLogins(matcher_form, PasswordStore::DISALLOW_PROMPT, &consumer); |
103 content::RunMessageLoop(); | 94 content::RunMessageLoop(); |
| 95 return consumer.result(); |
104 } | 96 } |
105 | 97 |
106 void RemoveLogin(PasswordStore* store, const PasswordForm& form) { | 98 void RemoveLogin(PasswordStore* store, const PasswordForm& form) { |
107 ASSERT_TRUE(store); | 99 ASSERT_TRUE(store); |
108 base::WaitableEvent wait_event(true, false); | 100 base::WaitableEvent wait_event(true, false); |
109 store->RemoveLogin(form); | 101 store->RemoveLogin(form); |
110 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); | 102 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); |
111 wait_event.Wait(); | 103 wait_event.Wait(); |
112 } | 104 } |
113 | 105 |
114 void RemoveLogins(PasswordStore* store) { | 106 void RemoveLogins(PasswordStore* store) { |
115 std::vector<PasswordForm> forms; | 107 ScopedVector<PasswordForm> forms = GetLogins(store); |
116 GetLogins(store, forms); | 108 for (const PasswordForm* form : forms) { |
117 for (std::vector<PasswordForm>::iterator it = forms.begin(); | 109 RemoveLogin(store, *form); |
118 it != forms.end(); ++it) { | |
119 RemoveLogin(store, *it); | |
120 } | 110 } |
121 } | 111 } |
122 | 112 |
123 void SetEncryptionPassphrase(int index, | 113 void SetEncryptionPassphrase(int index, |
124 const std::string& passphrase, | 114 const std::string& passphrase, |
125 ProfileSyncService::PassphraseType type) { | 115 ProfileSyncService::PassphraseType type) { |
126 ProfileSyncServiceFactory::GetForProfile( | 116 ProfileSyncServiceFactory::GetForProfile( |
127 test()->GetProfile(index))->SetEncryptionPassphrase(passphrase, type); | 117 test()->GetProfile(index))->SetEncryptionPassphrase(passphrase, type); |
128 } | 118 } |
129 | 119 |
130 bool SetDecryptionPassphrase(int index, const std::string& passphrase) { | 120 bool SetDecryptionPassphrase(int index, const std::string& passphrase) { |
131 return ProfileSyncServiceFactory::GetForProfile( | 121 return ProfileSyncServiceFactory::GetForProfile( |
132 test()->GetProfile(index))->SetDecryptionPassphrase(passphrase); | 122 test()->GetProfile(index))->SetDecryptionPassphrase(passphrase); |
133 } | 123 } |
134 | 124 |
135 PasswordStore* GetPasswordStore(int index) { | 125 PasswordStore* GetPasswordStore(int index) { |
136 return PasswordStoreFactory::GetForProfile(test()->GetProfile(index), | 126 return PasswordStoreFactory::GetForProfile(test()->GetProfile(index), |
137 ServiceAccessType::IMPLICIT_ACCESS) | 127 ServiceAccessType::IMPLICIT_ACCESS) |
138 .get(); | 128 .get(); |
139 } | 129 } |
140 | 130 |
141 PasswordStore* GetVerifierPasswordStore() { | 131 PasswordStore* GetVerifierPasswordStore() { |
142 return PasswordStoreFactory::GetForProfile( | 132 return PasswordStoreFactory::GetForProfile( |
143 test()->verifier(), ServiceAccessType::IMPLICIT_ACCESS).get(); | 133 test()->verifier(), ServiceAccessType::IMPLICIT_ACCESS).get(); |
144 } | 134 } |
145 | 135 |
146 bool ProfileContainsSamePasswordFormsAsVerifier(int index) { | 136 bool ProfileContainsSamePasswordFormsAsVerifier(int index) { |
147 std::vector<PasswordForm> verifier_forms; | 137 ScopedVector<PasswordForm> verifier_forms = |
148 std::vector<PasswordForm> forms; | 138 GetLogins(GetVerifierPasswordStore()); |
149 GetLogins(GetVerifierPasswordStore(), verifier_forms); | 139 ScopedVector<PasswordForm> forms = GetLogins(GetPasswordStore(index)); |
150 GetLogins(GetPasswordStore(index), forms); | 140 ClearSyncDateField(&forms.get()); |
151 ClearSyncDateField(&forms); | 141 bool result = password_manager::ContainsSamePasswordFormsPtr( |
152 bool result = | 142 verifier_forms.get(), forms.get()); |
153 password_manager::ContainsSamePasswordForms(verifier_forms, forms); | |
154 if (!result) { | 143 if (!result) { |
155 LOG(ERROR) << "Password forms in Verifier Profile:"; | 144 VLOG(1) << "Password forms in Verifier Profile:"; |
156 for (std::vector<PasswordForm>::iterator it = verifier_forms.begin(); | 145 for (const PasswordForm* form : verifier_forms) { |
157 it != verifier_forms.end(); ++it) { | 146 VLOG(1) << *form; |
158 LOG(ERROR) << *it << std::endl; | |
159 } | 147 } |
160 LOG(ERROR) << "Password forms in Profile" << index << ":"; | 148 VLOG(1) << "Password forms in Profile" << index << ":"; |
161 for (std::vector<PasswordForm>::iterator it = forms.begin(); | 149 for (const PasswordForm* form : forms) { |
162 it != forms.end(); ++it) { | 150 VLOG(1) << *form; |
163 LOG(ERROR) << *it << std::endl; | |
164 } | 151 } |
165 } | 152 } |
166 return result; | 153 return result; |
167 } | 154 } |
168 | 155 |
169 bool ProfilesContainSamePasswordForms(int index_a, int index_b) { | 156 bool ProfilesContainSamePasswordForms(int index_a, int index_b) { |
170 std::vector<PasswordForm> forms_a; | 157 ScopedVector<PasswordForm> forms_a = GetLogins(GetPasswordStore(index_a)); |
171 std::vector<PasswordForm> forms_b; | 158 ScopedVector<PasswordForm> forms_b = GetLogins(GetPasswordStore(index_b)); |
172 GetLogins(GetPasswordStore(index_a), forms_a); | 159 ClearSyncDateField(&forms_a.get()); |
173 GetLogins(GetPasswordStore(index_b), forms_b); | 160 ClearSyncDateField(&forms_b.get()); |
174 ClearSyncDateField(&forms_a); | 161 bool result = password_manager::ContainsSamePasswordFormsPtr(forms_a.get(), |
175 ClearSyncDateField(&forms_b); | 162 forms_b.get()); |
176 bool result = password_manager::ContainsSamePasswordForms(forms_a, forms_b); | |
177 if (!result) { | 163 if (!result) { |
178 LOG(ERROR) << "Password forms in Profile" << index_a << ":"; | 164 VLOG(1) << "Password forms in Profile" << index_a << ":"; |
179 for (std::vector<PasswordForm>::iterator it = forms_a.begin(); | 165 for (const PasswordForm* form : forms_a) { |
180 it != forms_a.end(); ++it) { | 166 VLOG(1) << *form; |
181 LOG(ERROR) << *it << std::endl; | |
182 } | 167 } |
183 LOG(ERROR) << "Password forms in Profile" << index_b << ":"; | 168 VLOG(1) << "Password forms in Profile" << index_b << ":"; |
184 for (std::vector<PasswordForm>::iterator it = forms_b.begin(); | 169 for (const PasswordForm* form : forms_b) { |
185 it != forms_b.end(); ++it) { | 170 VLOG(1) << *form; |
186 LOG(ERROR) << *it << std::endl; | |
187 } | 171 } |
188 } | 172 } |
189 return result; | 173 return result; |
190 } | 174 } |
191 | 175 |
192 bool AllProfilesContainSamePasswordFormsAsVerifier() { | 176 bool AllProfilesContainSamePasswordFormsAsVerifier() { |
193 for (int i = 0; i < test()->num_clients(); ++i) { | 177 for (int i = 0; i < test()->num_clients(); ++i) { |
194 if (!ProfileContainsSamePasswordFormsAsVerifier(i)) { | 178 if (!ProfileContainsSamePasswordFormsAsVerifier(i)) { |
195 DVLOG(1) << "Profile " << i << " does not contain the same password" | 179 DVLOG(1) << "Profile " << i << " does not contain the same password" |
196 " forms as the verifier."; | 180 " forms as the verifier."; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 321 |
338 } // namespace | 322 } // namespace |
339 | 323 |
340 bool AwaitProfileContainsSamePasswordFormsAsVerifier(int index) { | 324 bool AwaitProfileContainsSamePasswordFormsAsVerifier(int index) { |
341 SamePasswordFormsAsVerifierChecker checker(index); | 325 SamePasswordFormsAsVerifierChecker checker(index); |
342 checker.Wait(); | 326 checker.Wait(); |
343 return !checker.TimedOut(); | 327 return !checker.TimedOut(); |
344 } | 328 } |
345 | 329 |
346 int GetPasswordCount(int index) { | 330 int GetPasswordCount(int index) { |
347 std::vector<PasswordForm> forms; | 331 ScopedVector<PasswordForm> forms = GetLogins(GetPasswordStore(index)); |
348 GetLogins(GetPasswordStore(index), forms); | |
349 return forms.size(); | 332 return forms.size(); |
350 } | 333 } |
351 | 334 |
352 int GetVerifierPasswordCount() { | 335 int GetVerifierPasswordCount() { |
353 std::vector<PasswordForm> verifier_forms; | 336 ScopedVector<PasswordForm> verifier_forms = |
354 GetLogins(GetVerifierPasswordStore(), verifier_forms); | 337 GetLogins(GetVerifierPasswordStore()); |
355 return verifier_forms.size(); | 338 return verifier_forms.size(); |
356 } | 339 } |
357 | 340 |
358 PasswordForm CreateTestPasswordForm(int index) { | 341 PasswordForm CreateTestPasswordForm(int index) { |
359 PasswordForm form; | 342 PasswordForm form; |
360 form.signon_realm = kFakeSignonRealm; | 343 form.signon_realm = kFakeSignonRealm; |
361 form.origin = GURL(base::StringPrintf(kIndexedFakeOrigin, index)); | 344 form.origin = GURL(base::StringPrintf(kIndexedFakeOrigin, index)); |
362 form.username_value = | 345 form.username_value = |
363 base::ASCIIToUTF16(base::StringPrintf("username%d", index)); | 346 base::ASCIIToUTF16(base::StringPrintf("username%d", index)); |
364 form.password_value = | 347 form.password_value = |
365 base::ASCIIToUTF16(base::StringPrintf("password%d", index)); | 348 base::ASCIIToUTF16(base::StringPrintf("password%d", index)); |
366 form.date_created = base::Time::Now(); | 349 form.date_created = base::Time::Now(); |
367 return form; | 350 return form; |
368 } | 351 } |
369 | 352 |
370 } // namespace passwords_helper | 353 } // namespace passwords_helper |
OLD | NEW |