| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "chrome/browser/sync/test/live_sync/passwords_helper.h" | |
| 6 | |
| 7 #include "base/stringprintf.h" | |
| 8 #include "base/synchronization/waitable_event.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/password_manager/password_form_data.h" | |
| 11 #include "chrome/browser/password_manager/password_store_consumer.h" | |
| 12 #include "chrome/browser/password_manager/password_store.h" | |
| 13 #include "chrome/browser/sync/profile_sync_service_harness.h" | |
| 14 #include "chrome/browser/sync/test/live_sync/sync_datatype_helper.h" | |
| 15 #include "chrome/test/base/ui_test_utils.h" | |
| 16 #include "content/browser/browser_thread.h" | |
| 17 | |
| 18 using webkit_glue::PasswordForm; | |
| 19 using sync_datatype_helper::test; | |
| 20 | |
| 21 const std::string kFakeSignonRealm = "http://fake-signon-realm.google.com/"; | |
| 22 const char* kIndexedFakeOrigin = "http://fake-signon-realm.google.com/%d"; | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 // We use a WaitableEvent to wait when logins are added, removed, or updated | |
| 27 // instead of running the UI message loop because of a restriction that | |
| 28 // prevents a DB thread from initiating a quit of the UI message loop. | |
| 29 void PasswordStoreCallback(base::WaitableEvent* wait_event) { | |
| 30 // Wake up passwords_helper::AddLogin. | |
| 31 wait_event->Signal(); | |
| 32 } | |
| 33 | |
| 34 class PasswordStoreConsumerHelper : public PasswordStoreConsumer { | |
| 35 public: | |
| 36 explicit PasswordStoreConsumerHelper(std::vector<PasswordForm>* result) | |
| 37 : PasswordStoreConsumer(), | |
| 38 result_(result) {} | |
| 39 | |
| 40 virtual void OnPasswordStoreRequestDone( | |
| 41 CancelableRequestProvider::Handle handle, | |
| 42 const std::vector<PasswordForm*>& result) { | |
| 43 result_->clear(); | |
| 44 for (std::vector<PasswordForm*>::const_iterator it = result.begin(); | |
| 45 it != result.end(); ++it) { | |
| 46 // Make a copy of the form since it gets deallocated after the caller of | |
| 47 // this method returns. | |
| 48 result_->push_back(**it); | |
| 49 } | |
| 50 | |
| 51 // Quit the message loop to wake up passwords_helper::GetLogins. | |
| 52 MessageLoopForUI::current()->Quit(); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 std::vector<PasswordForm>* result_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper); | |
| 59 }; | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 namespace passwords_helper { | |
| 64 | |
| 65 void AddLogin(PasswordStore* store, const PasswordForm& form) { | |
| 66 ASSERT_TRUE(store); | |
| 67 base::WaitableEvent wait_event(true, false); | |
| 68 store->AddLogin(form); | |
| 69 store->ScheduleTask(NewRunnableFunction(&PasswordStoreCallback, &wait_event)); | |
| 70 wait_event.Wait(); | |
| 71 } | |
| 72 | |
| 73 void UpdateLogin(PasswordStore* store, const PasswordForm& form) { | |
| 74 ASSERT_TRUE(store); | |
| 75 base::WaitableEvent wait_event(true, false); | |
| 76 store->UpdateLogin(form); | |
| 77 store->ScheduleTask(NewRunnableFunction(&PasswordStoreCallback, &wait_event)); | |
| 78 wait_event.Wait(); | |
| 79 } | |
| 80 | |
| 81 void GetLogins(PasswordStore* store, std::vector<PasswordForm>& matches) { | |
| 82 ASSERT_TRUE(store); | |
| 83 PasswordForm matcher_form; | |
| 84 matcher_form.signon_realm = kFakeSignonRealm; | |
| 85 PasswordStoreConsumerHelper consumer(&matches); | |
| 86 store->GetLogins(matcher_form, &consumer); | |
| 87 ui_test_utils::RunMessageLoop(); | |
| 88 } | |
| 89 | |
| 90 void RemoveLogin(PasswordStore* store, const PasswordForm& form) { | |
| 91 ASSERT_TRUE(store); | |
| 92 base::WaitableEvent wait_event(true, false); | |
| 93 store->RemoveLogin(form); | |
| 94 store->ScheduleTask(NewRunnableFunction(&PasswordStoreCallback, &wait_event)); | |
| 95 wait_event.Wait(); | |
| 96 } | |
| 97 | |
| 98 void RemoveLogins(PasswordStore* store) { | |
| 99 std::vector<PasswordForm> forms; | |
| 100 GetLogins(store, forms); | |
| 101 for (std::vector<PasswordForm>::iterator it = forms.begin(); | |
| 102 it != forms.end(); ++it) { | |
| 103 RemoveLogin(store, *it); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void SetPassphrase(int index, const std::string& passphrase) { | |
| 108 test()->GetProfile(index)->GetProfileSyncService("")->SetPassphrase( | |
| 109 passphrase, true); | |
| 110 } | |
| 111 | |
| 112 PasswordStore* GetPasswordStore(int index) { | |
| 113 return test()->GetProfile(index)->GetPasswordStore(Profile::IMPLICIT_ACCESS); | |
| 114 } | |
| 115 | |
| 116 PasswordStore* GetVerifierPasswordStore() { | |
| 117 return test()->verifier()->GetPasswordStore(Profile::IMPLICIT_ACCESS); | |
| 118 } | |
| 119 | |
| 120 bool ProfileContainsSamePasswordFormsAsVerifier(int index) { | |
| 121 std::vector<PasswordForm> verifier_forms; | |
| 122 std::vector<PasswordForm> forms; | |
| 123 GetLogins(GetVerifierPasswordStore(), verifier_forms); | |
| 124 GetLogins(GetPasswordStore(index), forms); | |
| 125 bool result = ContainsSamePasswordForms(verifier_forms, forms); | |
| 126 if (!result) { | |
| 127 LOG(ERROR) << "Password forms in Verifier Profile:"; | |
| 128 for (std::vector<PasswordForm>::iterator it = verifier_forms.begin(); | |
| 129 it != verifier_forms.end(); ++it) { | |
| 130 LOG(ERROR) << *it << std::endl; | |
| 131 } | |
| 132 LOG(ERROR) << "Password forms in Profile" << index << ":"; | |
| 133 for (std::vector<PasswordForm>::iterator it = forms.begin(); | |
| 134 it != forms.end(); ++it) { | |
| 135 LOG(ERROR) << *it << std::endl; | |
| 136 } | |
| 137 } | |
| 138 return result; | |
| 139 } | |
| 140 | |
| 141 bool ProfilesContainSamePasswordForms(int index_a, int index_b) { | |
| 142 std::vector<PasswordForm> forms_a; | |
| 143 std::vector<PasswordForm> forms_b; | |
| 144 GetLogins(GetPasswordStore(index_a), forms_a); | |
| 145 GetLogins(GetPasswordStore(index_b), forms_b); | |
| 146 bool result = ContainsSamePasswordForms(forms_a, forms_b); | |
| 147 if (!result) { | |
| 148 LOG(ERROR) << "Password forms in Profile" << index_a << ":"; | |
| 149 for (std::vector<PasswordForm>::iterator it = forms_a.begin(); | |
| 150 it != forms_a.end(); ++it) { | |
| 151 LOG(ERROR) << *it << std::endl; | |
| 152 } | |
| 153 LOG(ERROR) << "Password forms in Profile" << index_b << ":"; | |
| 154 for (std::vector<PasswordForm>::iterator it = forms_b.begin(); | |
| 155 it != forms_b.end(); ++it) { | |
| 156 LOG(ERROR) << *it << std::endl; | |
| 157 } | |
| 158 } | |
| 159 return result; | |
| 160 } | |
| 161 | |
| 162 bool AllProfilesContainSamePasswordFormsAsVerifier() { | |
| 163 for (int i = 0; i < test()->num_clients(); ++i) { | |
| 164 if (!ProfileContainsSamePasswordFormsAsVerifier(i)) { | |
| 165 LOG(ERROR) << "Profile " << i << " does not contain the same password " | |
| 166 " forms as the verifier."; | |
| 167 return false; | |
| 168 } | |
| 169 } | |
| 170 return true; | |
| 171 } | |
| 172 | |
| 173 bool AllProfilesContainSamePasswordForms() { | |
| 174 for (int i = 1; i < test()->num_clients(); ++i) { | |
| 175 if (!ProfilesContainSamePasswordForms(0, i)) { | |
| 176 LOG(ERROR) << "Profile " << i << " does not contain the same password " | |
| 177 " forms as Profile 0."; | |
| 178 return false; | |
| 179 } | |
| 180 } | |
| 181 return true; | |
| 182 } | |
| 183 | |
| 184 int GetPasswordCount(int index) { | |
| 185 std::vector<PasswordForm> forms; | |
| 186 GetLogins(GetPasswordStore(index), forms); | |
| 187 return forms.size(); | |
| 188 } | |
| 189 | |
| 190 int GetVerifierPasswordCount() { | |
| 191 std::vector<PasswordForm> verifier_forms; | |
| 192 GetLogins(GetVerifierPasswordStore(), verifier_forms); | |
| 193 return verifier_forms.size(); | |
| 194 } | |
| 195 | |
| 196 PasswordForm CreateTestPasswordForm(int index) { | |
| 197 PasswordForm form; | |
| 198 form.signon_realm = kFakeSignonRealm; | |
| 199 form.origin = GURL(base::StringPrintf(kIndexedFakeOrigin, index)); | |
| 200 form.username_value = ASCIIToUTF16(base::StringPrintf("username%d", index)); | |
| 201 form.password_value = ASCIIToUTF16(base::StringPrintf("password%d", index)); | |
| 202 return form; | |
| 203 } | |
| 204 | |
| 205 } // namespace passwords_helper | |
| OLD | NEW |