1// Copyright 2014 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 "components/password_manager/core/browser/test_password_store.h"
6
7#include "components/autofill/core/common/password_form.h"
8
9namespace password_manager {
10
11TestPasswordStore::TestPasswordStore()
12    : PasswordStore(base::MessageLoopProxy::current(),
13                    base::MessageLoopProxy::current()) {
14}
15
16TestPasswordStore::~TestPasswordStore() {}
17
18const TestPasswordStore::PasswordMap& TestPasswordStore::stored_passwords()
19    const {
20  return stored_passwords_;
21}
22
23void TestPasswordStore::Clear() {
24  stored_passwords_.clear();
25}
26
27bool TestPasswordStore::IsEmpty() const {
28  // The store is empty, if the sum of all stored passwords across all entries
29  // in |stored_passwords_| is 0.
30  size_t number_of_passwords = 0u;
31  for (PasswordMap::const_iterator it = stored_passwords_.begin();
32       !number_of_passwords && it != stored_passwords_.end();
33       ++it) {
34    number_of_passwords += it->second.size();
35  }
36  return number_of_passwords == 0u;
37}
38
39bool TestPasswordStore::FormsAreEquivalent(const autofill::PasswordForm& lhs,
40                                           const autofill::PasswordForm& rhs) {
41  return lhs.origin == rhs.origin &&
42      lhs.username_element == rhs.username_element &&
43      lhs.username_value == rhs.username_value &&
44      lhs.password_element == rhs.password_element &&
45      lhs.signon_realm == rhs.signon_realm;
46}
47
48void TestPasswordStore::WrapModificationTask(ModificationTask task) {
49  task.Run();
50}
51
52PasswordStoreChangeList TestPasswordStore::AddLoginImpl(
53    const autofill::PasswordForm& form) {
54  PasswordStoreChangeList changes;
55  stored_passwords_[form.signon_realm].push_back(form);
56  changes.push_back(PasswordStoreChange(PasswordStoreChange::ADD, form));
57  return changes;
58}
59
60PasswordStoreChangeList TestPasswordStore::UpdateLoginImpl(
61    const autofill::PasswordForm& form) {
62  PasswordStoreChangeList changes;
63  std::vector<autofill::PasswordForm>& forms =
64      stored_passwords_[form.signon_realm];
65  for (std::vector<autofill::PasswordForm>::iterator it = forms.begin();
66         it != forms.end(); ++it) {
67    if (FormsAreEquivalent(form, *it)) {
68      *it = form;
69      changes.push_back(
70          PasswordStoreChange(PasswordStoreChange::UPDATE, form));
71    }
72  }
73  return changes;
74}
75
76PasswordStoreChangeList TestPasswordStore::RemoveLoginImpl(
77    const autofill::PasswordForm& form) {
78  PasswordStoreChangeList changes;
79  std::vector<autofill::PasswordForm>& forms =
80      stored_passwords_[form.signon_realm];
81  std::vector<autofill::PasswordForm>::iterator it = forms.begin();
82  while (it != forms.end()) {
83    if (FormsAreEquivalent(form, *it)) {
84      it = forms.erase(it);
85      changes.push_back(
86          PasswordStoreChange(PasswordStoreChange::REMOVE, form));
87    } else {
88      ++it;
89    }
90  }
91  return changes;
92}
93
94void TestPasswordStore::GetLoginsImpl(
95    const autofill::PasswordForm& form,
96    PasswordStore::AuthorizationPromptPolicy prompt_policy,
97    const PasswordStore::ConsumerCallbackRunner& runner) {
98  std::vector<autofill::PasswordForm*> matched_forms;
99  std::vector<autofill::PasswordForm> forms =
100      stored_passwords_[form.signon_realm];
101  for (std::vector<autofill::PasswordForm>::iterator it = forms.begin();
102       it != forms.end(); ++it) {
103    matched_forms.push_back(new autofill::PasswordForm(*it));
104  }
105  runner.Run(matched_forms);
106}
107
108PasswordStoreChangeList TestPasswordStore::RemoveLoginsCreatedBetweenImpl(
109    base::Time begin,
110    base::Time end) {
111  PasswordStoreChangeList changes;
112  return changes;
113}
114
115PasswordStoreChangeList TestPasswordStore::RemoveLoginsSyncedBetweenImpl(
116    base::Time begin,
117    base::Time end) {
118  PasswordStoreChangeList changes;
119  return changes;
120}
121
122bool TestPasswordStore::FillAutofillableLogins(
123    std::vector<autofill::PasswordForm*>* forms) {
124  return true;
125}
126
127bool TestPasswordStore::FillBlacklistLogins(
128    std::vector<autofill::PasswordForm*>* forms) {
129  return true;
130}
131
132}  // namespace password_manager
133