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" |
(...skipping 23 matching lines...) Expand all Loading... |
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() override { |
48 const std::vector<PasswordForm*>& result) override { | |
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. | 47 // Quit the message loop to wake up passwords_helper::GetLogins. |
58 base::MessageLoopForUI::current()->Quit(); | 48 base::MessageLoopForUI::current()->Quit(); |
59 } | 49 } |
60 | 50 |
61 private: | 51 private: |
62 std::vector<PasswordForm>* result_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper); | 52 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper); |
65 }; | 53 }; |
66 | 54 |
67 // PasswordForm::date_synced is a local field. Therefore it may be different | 55 // PasswordForm::date_synced is a local field. Therefore it may be different |
68 // across clients. | 56 // across clients. |
69 void ClearSyncDateField(std::vector<PasswordForm>* forms) { | 57 void ClearSyncDateField(std::vector<PasswordForm*>* forms) { |
70 for (std::vector<PasswordForm>::iterator it = forms->begin(); | 58 for (PasswordForm* form : *forms) { |
71 it != forms->end(); | 59 form->date_synced = base::Time(); |
72 ++it) { | |
73 it->date_synced = base::Time(); | |
74 } | 60 } |
75 } | 61 } |
76 | 62 |
77 } // namespace | 63 } // namespace |
78 | 64 |
79 namespace passwords_helper { | 65 namespace passwords_helper { |
80 | 66 |
81 void AddLogin(PasswordStore* store, const PasswordForm& form) { | 67 void AddLogin(PasswordStore* store, const PasswordForm& form) { |
82 ASSERT_TRUE(store); | 68 ASSERT_TRUE(store); |
83 base::WaitableEvent wait_event(true, false); | 69 base::WaitableEvent wait_event(true, false); |
84 store->AddLogin(form); | 70 store->AddLogin(form); |
85 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); | 71 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); |
86 wait_event.Wait(); | 72 wait_event.Wait(); |
87 } | 73 } |
88 | 74 |
89 void UpdateLogin(PasswordStore* store, const PasswordForm& form) { | 75 void UpdateLogin(PasswordStore* store, const PasswordForm& form) { |
90 ASSERT_TRUE(store); | 76 ASSERT_TRUE(store); |
91 base::WaitableEvent wait_event(true, false); | 77 base::WaitableEvent wait_event(true, false); |
92 store->UpdateLogin(form); | 78 store->UpdateLogin(form); |
93 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); | 79 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); |
94 wait_event.Wait(); | 80 wait_event.Wait(); |
95 } | 81 } |
96 | 82 |
97 void GetLogins(PasswordStore* store, std::vector<PasswordForm>& matches) { | 83 ScopedVector<PasswordForm> GetLogins(PasswordStore* store) { |
98 ASSERT_TRUE(store); | 84 EXPECT_TRUE(store); |
99 PasswordForm matcher_form; | 85 PasswordForm matcher_form; |
100 matcher_form.signon_realm = kFakeSignonRealm; | 86 matcher_form.signon_realm = kFakeSignonRealm; |
101 PasswordStoreConsumerHelper consumer(&matches); | 87 PasswordStoreConsumerHelper consumer; |
102 store->GetLogins(matcher_form, PasswordStore::DISALLOW_PROMPT, &consumer); | 88 store->GetLogins(matcher_form, PasswordStore::DISALLOW_PROMPT, &consumer); |
103 content::RunMessageLoop(); | 89 content::RunMessageLoop(); |
| 90 return consumer.results()->Pass(); |
104 } | 91 } |
105 | 92 |
106 void RemoveLogin(PasswordStore* store, const PasswordForm& form) { | 93 void RemoveLogin(PasswordStore* store, const PasswordForm& form) { |
107 ASSERT_TRUE(store); | 94 ASSERT_TRUE(store); |
108 base::WaitableEvent wait_event(true, false); | 95 base::WaitableEvent wait_event(true, false); |
109 store->RemoveLogin(form); | 96 store->RemoveLogin(form); |
110 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); | 97 store->ScheduleTask(base::Bind(&PasswordStoreCallback, &wait_event)); |
111 wait_event.Wait(); | 98 wait_event.Wait(); |
112 } | 99 } |
113 | 100 |
114 void RemoveLogins(PasswordStore* store) { | 101 void RemoveLogins(PasswordStore* store) { |
115 std::vector<PasswordForm> forms; | 102 ScopedVector<PasswordForm> forms = GetLogins(store); |
116 GetLogins(store, forms); | 103 for (const PasswordForm* form : forms) { |
117 for (std::vector<PasswordForm>::iterator it = forms.begin(); | 104 RemoveLogin(store, *form); |
118 it != forms.end(); ++it) { | |
119 RemoveLogin(store, *it); | |
120 } | 105 } |
121 } | 106 } |
122 | 107 |
123 void SetEncryptionPassphrase(int index, | 108 void SetEncryptionPassphrase(int index, |
124 const std::string& passphrase, | 109 const std::string& passphrase, |
125 ProfileSyncService::PassphraseType type) { | 110 ProfileSyncService::PassphraseType type) { |
126 ProfileSyncServiceFactory::GetForProfile( | 111 ProfileSyncServiceFactory::GetForProfile( |
127 test()->GetProfile(index))->SetEncryptionPassphrase(passphrase, type); | 112 test()->GetProfile(index))->SetEncryptionPassphrase(passphrase, type); |
128 } | 113 } |
129 | 114 |
130 bool SetDecryptionPassphrase(int index, const std::string& passphrase) { | 115 bool SetDecryptionPassphrase(int index, const std::string& passphrase) { |
131 return ProfileSyncServiceFactory::GetForProfile( | 116 return ProfileSyncServiceFactory::GetForProfile( |
132 test()->GetProfile(index))->SetDecryptionPassphrase(passphrase); | 117 test()->GetProfile(index))->SetDecryptionPassphrase(passphrase); |
133 } | 118 } |
134 | 119 |
135 PasswordStore* GetPasswordStore(int index) { | 120 PasswordStore* GetPasswordStore(int index) { |
136 return PasswordStoreFactory::GetForProfile(test()->GetProfile(index), | 121 return PasswordStoreFactory::GetForProfile(test()->GetProfile(index), |
137 ServiceAccessType::IMPLICIT_ACCESS) | 122 ServiceAccessType::IMPLICIT_ACCESS) |
138 .get(); | 123 .get(); |
139 } | 124 } |
140 | 125 |
141 PasswordStore* GetVerifierPasswordStore() { | 126 PasswordStore* GetVerifierPasswordStore() { |
142 return PasswordStoreFactory::GetForProfile( | 127 return PasswordStoreFactory::GetForProfile( |
143 test()->verifier(), ServiceAccessType::IMPLICIT_ACCESS).get(); | 128 test()->verifier(), ServiceAccessType::IMPLICIT_ACCESS).get(); |
144 } | 129 } |
145 | 130 |
146 bool ProfileContainsSamePasswordFormsAsVerifier(int index) { | 131 bool ProfileContainsSamePasswordFormsAsVerifier(int index) { |
147 std::vector<PasswordForm> verifier_forms; | 132 ScopedVector<PasswordForm> verifier_forms = |
148 std::vector<PasswordForm> forms; | 133 GetLogins(GetVerifierPasswordStore()); |
149 GetLogins(GetVerifierPasswordStore(), verifier_forms); | 134 ScopedVector<PasswordForm> forms = GetLogins(GetPasswordStore(index)); |
150 GetLogins(GetPasswordStore(index), forms); | 135 ClearSyncDateField(&forms.get()); |
151 ClearSyncDateField(&forms); | 136 bool result = password_manager::ContainsSamePasswordFormsPtr( |
152 bool result = | 137 verifier_forms.get(), forms.get()); |
153 password_manager::ContainsSamePasswordForms(verifier_forms, forms); | |
154 if (!result) { | 138 if (!result) { |
155 LOG(ERROR) << "Password forms in Verifier Profile:"; | 139 VLOG(1) << "Password forms in Verifier Profile:"; |
156 for (std::vector<PasswordForm>::iterator it = verifier_forms.begin(); | 140 for (const PasswordForm* form : verifier_forms) { |
157 it != verifier_forms.end(); ++it) { | 141 VLOG(1) << *form; |
158 LOG(ERROR) << *it << std::endl; | |
159 } | 142 } |
160 LOG(ERROR) << "Password forms in Profile" << index << ":"; | 143 VLOG(1) << "Password forms in Profile" << index << ":"; |
161 for (std::vector<PasswordForm>::iterator it = forms.begin(); | 144 for (const PasswordForm* form : forms) { |
162 it != forms.end(); ++it) { | 145 VLOG(1) << *form; |
163 LOG(ERROR) << *it << std::endl; | |
164 } | 146 } |
165 } | 147 } |
166 return result; | 148 return result; |
167 } | 149 } |
168 | 150 |
169 bool ProfilesContainSamePasswordForms(int index_a, int index_b) { | 151 bool ProfilesContainSamePasswordForms(int index_a, int index_b) { |
170 std::vector<PasswordForm> forms_a; | 152 ScopedVector<PasswordForm> forms_a = GetLogins(GetPasswordStore(index_a)); |
171 std::vector<PasswordForm> forms_b; | 153 ScopedVector<PasswordForm> forms_b = GetLogins(GetPasswordStore(index_b)); |
172 GetLogins(GetPasswordStore(index_a), forms_a); | 154 ClearSyncDateField(&forms_a.get()); |
173 GetLogins(GetPasswordStore(index_b), forms_b); | 155 ClearSyncDateField(&forms_b.get()); |
174 ClearSyncDateField(&forms_a); | 156 bool result = password_manager::ContainsSamePasswordFormsPtr(forms_a.get(), |
175 ClearSyncDateField(&forms_b); | 157 forms_b.get()); |
176 bool result = password_manager::ContainsSamePasswordForms(forms_a, forms_b); | |
177 if (!result) { | 158 if (!result) { |
178 LOG(ERROR) << "Password forms in Profile" << index_a << ":"; | 159 VLOG(1) << "Password forms in Profile" << index_a << ":"; |
179 for (std::vector<PasswordForm>::iterator it = forms_a.begin(); | 160 for (const PasswordForm* form : forms_a) { |
180 it != forms_a.end(); ++it) { | 161 VLOG(1) << *form; |
181 LOG(ERROR) << *it << std::endl; | |
182 } | 162 } |
183 LOG(ERROR) << "Password forms in Profile" << index_b << ":"; | 163 VLOG(1) << "Password forms in Profile" << index_b << ":"; |
184 for (std::vector<PasswordForm>::iterator it = forms_b.begin(); | 164 for (const PasswordForm* form : forms_b) { |
185 it != forms_b.end(); ++it) { | 165 VLOG(1) << *form; |
186 LOG(ERROR) << *it << std::endl; | |
187 } | 166 } |
188 } | 167 } |
189 return result; | 168 return result; |
190 } | 169 } |
191 | 170 |
192 bool AllProfilesContainSamePasswordFormsAsVerifier() { | 171 bool AllProfilesContainSamePasswordFormsAsVerifier() { |
193 for (int i = 0; i < test()->num_clients(); ++i) { | 172 for (int i = 0; i < test()->num_clients(); ++i) { |
194 if (!ProfileContainsSamePasswordFormsAsVerifier(i)) { | 173 if (!ProfileContainsSamePasswordFormsAsVerifier(i)) { |
195 DVLOG(1) << "Profile " << i << " does not contain the same password" | 174 DVLOG(1) << "Profile " << i << " does not contain the same password" |
196 " forms as the verifier."; | 175 " forms as the verifier."; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 316 |
338 } // namespace | 317 } // namespace |
339 | 318 |
340 bool AwaitProfileContainsSamePasswordFormsAsVerifier(int index) { | 319 bool AwaitProfileContainsSamePasswordFormsAsVerifier(int index) { |
341 SamePasswordFormsAsVerifierChecker checker(index); | 320 SamePasswordFormsAsVerifierChecker checker(index); |
342 checker.Wait(); | 321 checker.Wait(); |
343 return !checker.TimedOut(); | 322 return !checker.TimedOut(); |
344 } | 323 } |
345 | 324 |
346 int GetPasswordCount(int index) { | 325 int GetPasswordCount(int index) { |
347 std::vector<PasswordForm> forms; | 326 ScopedVector<PasswordForm> forms = GetLogins(GetPasswordStore(index)); |
348 GetLogins(GetPasswordStore(index), forms); | |
349 return forms.size(); | 327 return forms.size(); |
350 } | 328 } |
351 | 329 |
352 int GetVerifierPasswordCount() { | 330 int GetVerifierPasswordCount() { |
353 std::vector<PasswordForm> verifier_forms; | 331 ScopedVector<PasswordForm> verifier_forms = |
354 GetLogins(GetVerifierPasswordStore(), verifier_forms); | 332 GetLogins(GetVerifierPasswordStore()); |
355 return verifier_forms.size(); | 333 return verifier_forms.size(); |
356 } | 334 } |
357 | 335 |
358 PasswordForm CreateTestPasswordForm(int index) { | 336 PasswordForm CreateTestPasswordForm(int index) { |
359 PasswordForm form; | 337 PasswordForm form; |
360 form.signon_realm = kFakeSignonRealm; | 338 form.signon_realm = kFakeSignonRealm; |
361 form.origin = GURL(base::StringPrintf(kIndexedFakeOrigin, index)); | 339 form.origin = GURL(base::StringPrintf(kIndexedFakeOrigin, index)); |
362 form.username_value = | 340 form.username_value = |
363 base::ASCIIToUTF16(base::StringPrintf("username%d", index)); | 341 base::ASCIIToUTF16(base::StringPrintf("username%d", index)); |
364 form.password_value = | 342 form.password_value = |
365 base::ASCIIToUTF16(base::StringPrintf("password%d", index)); | 343 base::ASCIIToUTF16(base::StringPrintf("password%d", index)); |
366 form.date_created = base::Time::Now(); | 344 form.date_created = base::Time::Now(); |
367 return form; | 345 return form; |
368 } | 346 } |
369 | 347 |
370 } // namespace passwords_helper | 348 } // namespace passwords_helper |
OLD | NEW |