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 <sstream> | 7 #include <sstream> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "chrome/browser/password_manager/password_store_factory.h" | 17 #include "chrome/browser/password_manager/password_store_factory.h" |
18 #include "chrome/browser/sync/profile_sync_service_factory.h" | 18 #include "chrome/browser/sync/profile_sync_service_factory.h" |
19 #include "chrome/browser/sync/test/integration/multi_client_status_change_checke
r.h" | |
20 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" | 19 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" |
21 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" | |
22 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 20 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
23 #include "components/password_manager/core/browser/password_manager_test_utils.h
" | 21 #include "components/password_manager/core/browser/password_manager_test_utils.h
" |
24 #include "components/password_manager/core/browser/password_store.h" | 22 #include "components/password_manager/core/browser/password_store.h" |
25 #include "components/password_manager/core/browser/password_store_consumer.h" | 23 #include "components/password_manager/core/browser/password_store_consumer.h" |
26 #include "content/public/test/test_utils.h" | 24 #include "content/public/test/test_utils.h" |
27 | 25 |
28 using autofill::PasswordForm; | 26 using autofill::PasswordForm; |
29 using password_manager::PasswordStore; | 27 using password_manager::PasswordStore; |
30 using sync_datatype_helper::test; | 28 using sync_datatype_helper::test; |
31 | 29 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 for (int i = 1; i < test()->num_clients(); ++i) { | 186 for (int i = 1; i < test()->num_clients(); ++i) { |
189 if (!ProfilesContainSamePasswordForms(0, i)) { | 187 if (!ProfilesContainSamePasswordForms(0, i)) { |
190 DVLOG(1) << "Profile " << i << " does not contain the same password" | 188 DVLOG(1) << "Profile " << i << " does not contain the same password" |
191 " forms as Profile 0."; | 189 " forms as Profile 0."; |
192 return false; | 190 return false; |
193 } | 191 } |
194 } | 192 } |
195 return true; | 193 return true; |
196 } | 194 } |
197 | 195 |
198 namespace { | 196 int GetPasswordCount(int index) { |
| 197 return GetLogins(GetPasswordStore(index)).size(); |
| 198 } |
199 | 199 |
200 // Helper class used in the implementation of | 200 int GetVerifierPasswordCount() { |
201 // AwaitAllProfilesContainSamePasswordForms. | 201 return GetLogins(GetVerifierPasswordStore()).size(); |
202 class SamePasswordFormsChecker : public MultiClientStatusChangeChecker { | 202 } |
203 public: | |
204 SamePasswordFormsChecker(); | |
205 ~SamePasswordFormsChecker() override; | |
206 | 203 |
207 bool IsExitConditionSatisfied() override; | 204 PasswordForm CreateTestPasswordForm(int index) { |
208 std::string GetDebugMessage() const override; | 205 PasswordForm form; |
| 206 form.signon_realm = kFakeSignonRealm; |
| 207 form.origin = GURL(base::StringPrintf(kIndexedFakeOrigin, index)); |
| 208 form.username_value = |
| 209 base::ASCIIToUTF16(base::StringPrintf("username%d", index)); |
| 210 form.password_value = |
| 211 base::ASCIIToUTF16(base::StringPrintf("password%d", index)); |
| 212 form.date_created = base::Time::Now(); |
| 213 return form; |
| 214 } |
209 | 215 |
210 private: | 216 } // namespace passwords_helper |
211 bool in_progress_; | |
212 bool needs_recheck_; | |
213 }; | |
214 | 217 |
215 SamePasswordFormsChecker::SamePasswordFormsChecker() | 218 SamePasswordFormsChecker::SamePasswordFormsChecker() |
216 : MultiClientStatusChangeChecker( | 219 : MultiClientStatusChangeChecker( |
217 sync_datatype_helper::test()->GetSyncServices()), | 220 sync_datatype_helper::test()->GetSyncServices()), |
218 in_progress_(false), | 221 in_progress_(false), |
219 needs_recheck_(false) {} | 222 needs_recheck_(false) {} |
220 | 223 |
221 SamePasswordFormsChecker::~SamePasswordFormsChecker() {} | |
222 | |
223 // This method needs protection against re-entrancy. | 224 // This method needs protection against re-entrancy. |
224 // | 225 // |
225 // This function indirectly calls GetLogins(), which starts a RunLoop on the UI | 226 // This function indirectly calls GetLogins(), which starts a RunLoop on the UI |
226 // thread. This can be a problem, since the next task to execute could very | 227 // thread. This can be a problem, since the next task to execute could very |
227 // well contain a ProfileSyncService::OnStateChanged() event, which would | 228 // well contain a ProfileSyncService::OnStateChanged() event, which would |
228 // trigger another call to this here function, and start another layer of | 229 // trigger another call to this here function, and start another layer of |
229 // nested RunLoops. That makes the StatusChangeChecker's Quit() method | 230 // nested RunLoops. That makes the StatusChangeChecker's Quit() method |
230 // ineffective. | 231 // ineffective. |
231 // | 232 // |
232 // The work-around is to not allow re-entrancy. But we can't just drop | 233 // The work-around is to not allow re-entrancy. But we can't just drop |
233 // IsExitConditionSatisifed() calls if one is already in progress. Instead, we | 234 // IsExitConditionSatisifed() calls if one is already in progress. Instead, we |
234 // set a flag to ask the current execution of IsExitConditionSatisfied() to be | 235 // set a flag to ask the current execution of IsExitConditionSatisfied() to be |
235 // re-run. This ensures that the return value is always based on the most | 236 // re-run. This ensures that the return value is always based on the most |
236 // up-to-date state. | 237 // up-to-date state. |
237 bool SamePasswordFormsChecker::IsExitConditionSatisfied() { | 238 bool SamePasswordFormsChecker::IsExitConditionSatisfied() { |
238 if (in_progress_) { | 239 if (in_progress_) { |
239 LOG(WARNING) << "Setting flag and returning early to prevent nesting."; | 240 LOG(WARNING) << "Setting flag and returning early to prevent nesting."; |
240 needs_recheck_ = true; | 241 needs_recheck_ = true; |
241 return false; | 242 return false; |
242 } | 243 } |
243 | 244 |
244 // Keep retrying until we get a good reading. | 245 // Keep retrying until we get a good reading. |
245 bool result = false; | 246 bool result = false; |
246 in_progress_ = true; | 247 in_progress_ = true; |
247 do { | 248 do { |
248 needs_recheck_ = false; | 249 needs_recheck_ = false; |
249 result = AllProfilesContainSamePasswordForms(); | 250 result = passwords_helper::AllProfilesContainSamePasswordForms(); |
250 } while (needs_recheck_); | 251 } while (needs_recheck_); |
251 in_progress_ = false; | 252 in_progress_ = false; |
252 return result; | 253 return result; |
253 } | 254 } |
254 | 255 |
255 std::string SamePasswordFormsChecker::GetDebugMessage() const { | 256 std::string SamePasswordFormsChecker::GetDebugMessage() const { |
256 return "Waiting for matching passwords"; | 257 return "Waiting for matching passwords"; |
257 } | 258 } |
258 | 259 |
259 } // namespace | |
260 | |
261 bool AwaitAllProfilesContainSamePasswordForms() { | |
262 SamePasswordFormsChecker checker; | |
263 checker.Wait(); | |
264 return !checker.TimedOut(); | |
265 } | |
266 | |
267 namespace { | |
268 | |
269 // Helper class used in the implementation of | |
270 // AwaitProfileContainSamePasswordFormsAsVerifier. | |
271 class SamePasswordFormsAsVerifierChecker | |
272 : public SingleClientStatusChangeChecker { | |
273 public: | |
274 explicit SamePasswordFormsAsVerifierChecker(int index); | |
275 ~SamePasswordFormsAsVerifierChecker() override; | |
276 | |
277 bool IsExitConditionSatisfied() override; | |
278 std::string GetDebugMessage() const override; | |
279 | |
280 private: | |
281 int index_; | |
282 | |
283 bool in_progress_; | |
284 bool needs_recheck_; | |
285 }; | |
286 | |
287 SamePasswordFormsAsVerifierChecker::SamePasswordFormsAsVerifierChecker(int i) | 260 SamePasswordFormsAsVerifierChecker::SamePasswordFormsAsVerifierChecker(int i) |
288 : SingleClientStatusChangeChecker( | 261 : SingleClientStatusChangeChecker( |
289 sync_datatype_helper::test()->GetSyncService(i)), | 262 sync_datatype_helper::test()->GetSyncService(i)), |
290 index_(i), | 263 index_(i), |
291 in_progress_(false), | 264 in_progress_(false), |
292 needs_recheck_(false) { | 265 needs_recheck_(false) { |
293 } | 266 } |
294 | 267 |
295 SamePasswordFormsAsVerifierChecker::~SamePasswordFormsAsVerifierChecker() { | |
296 } | |
297 | |
298 // This method uses the same re-entrancy prevention trick as | 268 // This method uses the same re-entrancy prevention trick as |
299 // the SamePasswordFormsChecker. | 269 // the SamePasswordFormsChecker. |
300 bool SamePasswordFormsAsVerifierChecker::IsExitConditionSatisfied() { | 270 bool SamePasswordFormsAsVerifierChecker::IsExitConditionSatisfied() { |
301 if (in_progress_) { | 271 if (in_progress_) { |
302 LOG(WARNING) << "Setting flag and returning early to prevent nesting."; | 272 LOG(WARNING) << "Setting flag and returning early to prevent nesting."; |
303 needs_recheck_ = true; | 273 needs_recheck_ = true; |
304 return false; | 274 return false; |
305 } | 275 } |
306 | 276 |
307 // Keep retrying until we get a good reading. | 277 // Keep retrying until we get a good reading. |
308 bool result = false; | 278 bool result = false; |
309 in_progress_ = true; | 279 in_progress_ = true; |
310 do { | 280 do { |
311 needs_recheck_ = false; | 281 needs_recheck_ = false; |
312 result = ProfileContainsSamePasswordFormsAsVerifier(index_); | 282 result = |
| 283 passwords_helper::ProfileContainsSamePasswordFormsAsVerifier(index_); |
313 } while (needs_recheck_); | 284 } while (needs_recheck_); |
314 in_progress_ = false; | 285 in_progress_ = false; |
315 return result; | 286 return result; |
316 } | 287 } |
317 | 288 |
318 std::string SamePasswordFormsAsVerifierChecker::GetDebugMessage() const { | 289 std::string SamePasswordFormsAsVerifierChecker::GetDebugMessage() const { |
319 return "Waiting for passwords to match verifier"; | 290 return "Waiting for passwords to match verifier"; |
320 } | 291 } |
321 | |
322 } // namespace | |
323 | |
324 bool AwaitProfileContainsSamePasswordFormsAsVerifier(int index) { | |
325 SamePasswordFormsAsVerifierChecker checker(index); | |
326 checker.Wait(); | |
327 return !checker.TimedOut(); | |
328 } | |
329 | |
330 int GetPasswordCount(int index) { | |
331 return GetLogins(GetPasswordStore(index)).size(); | |
332 } | |
333 | |
334 int GetVerifierPasswordCount() { | |
335 return GetLogins(GetVerifierPasswordStore()).size(); | |
336 } | |
337 | |
338 PasswordForm CreateTestPasswordForm(int index) { | |
339 PasswordForm form; | |
340 form.signon_realm = kFakeSignonRealm; | |
341 form.origin = GURL(base::StringPrintf(kIndexedFakeOrigin, index)); | |
342 form.username_value = | |
343 base::ASCIIToUTF16(base::StringPrintf("username%d", index)); | |
344 form.password_value = | |
345 base::ASCIIToUTF16(base::StringPrintf("password%d", index)); | |
346 form.date_created = base::Time::Now(); | |
347 return form; | |
348 } | |
349 | |
350 } // namespace passwords_helper | |
OLD | NEW |