password_generation_manager_unittest.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 <vector> 6 7#include "base/prefs/pref_registry_simple.h" 8#include "base/prefs/pref_service.h" 9#include "base/prefs/testing_pref_service.h" 10#include "base/strings/utf_string_conversions.h" 11#include "components/autofill/core/browser/autofill_field.h" 12#include "components/autofill/core/browser/autofill_metrics.h" 13#include "components/autofill/core/browser/form_structure.h" 14#include "components/autofill/core/common/form_data.h" 15#include "components/autofill/core/common/form_field_data.h" 16#include "components/password_manager/core/browser/password_generation_manager.h" 17#include "components/password_manager/core/browser/password_manager.h" 18#include "components/password_manager/core/browser/password_manager_client.h" 19#include "components/password_manager/core/common/password_manager_pref_names.h" 20#include "testing/gtest/include/gtest/gtest.h" 21#include "url/gurl.h" 22 23using base::ASCIIToUTF16; 24 25namespace password_manager { 26 27namespace { 28 29class TestPasswordManagerDriver : public PasswordManagerDriver { 30 public: 31 TestPasswordManagerDriver(PasswordManagerClient* client) 32 : password_manager_(client), 33 password_generation_manager_(client), 34 is_off_the_record_(false) {} 35 virtual ~TestPasswordManagerDriver() {} 36 37 // PasswordManagerDriver implementation. 38 virtual void FillPasswordForm(const autofill::PasswordFormFillData& form_data) 39 OVERRIDE {} 40 virtual bool DidLastPageLoadEncounterSSLErrors() OVERRIDE { return false; } 41 virtual bool IsOffTheRecord() OVERRIDE { return is_off_the_record_; } 42 virtual PasswordGenerationManager* GetPasswordGenerationManager() OVERRIDE { 43 return &password_generation_manager_; 44 } 45 virtual PasswordManager* GetPasswordManager() OVERRIDE { 46 return &password_manager_; 47 } 48 virtual autofill::AutofillManager* GetAutofillManager() OVERRIDE { 49 return NULL; 50 } 51 virtual void AllowPasswordGenerationForForm(autofill::PasswordForm* form) 52 OVERRIDE {} 53 virtual void AccountCreationFormsFound( 54 const std::vector<autofill::FormData>& forms) OVERRIDE { 55 found_account_creation_forms_.insert( 56 found_account_creation_forms_.begin(), forms.begin(), forms.end()); 57 } 58 59 const std::vector<autofill::FormData>& GetFoundAccountCreationForms() { 60 return found_account_creation_forms_; 61 } 62 void set_is_off_the_record(bool is_off_the_record) { 63 is_off_the_record_ = is_off_the_record; 64 } 65 66 private: 67 PasswordManager password_manager_; 68 PasswordGenerationManager password_generation_manager_; 69 std::vector<autofill::FormData> found_account_creation_forms_; 70 bool is_off_the_record_; 71}; 72 73class TestPasswordManagerClient : public PasswordManagerClient { 74 public: 75 TestPasswordManagerClient(scoped_ptr<PrefService> prefs) 76 : prefs_(prefs.Pass()), driver_(this), is_sync_enabled_(false) {} 77 78 virtual void PromptUserToSavePassword(PasswordFormManager* form_to_save) 79 OVERRIDE {} 80 virtual PasswordStore* GetPasswordStore() OVERRIDE { return NULL; } 81 virtual PrefService* GetPrefs() OVERRIDE { return prefs_.get(); } 82 virtual PasswordManagerDriver* GetDriver() OVERRIDE { return &driver_; } 83 virtual void AuthenticateAutofillAndFillForm( 84 scoped_ptr<autofill::PasswordFormFillData> fill_data) OVERRIDE {} 85 virtual bool IsPasswordSyncEnabled() OVERRIDE { return is_sync_enabled_; } 86 87 void set_is_password_sync_enabled(bool enabled) { 88 is_sync_enabled_ = enabled; 89 } 90 91 private: 92 scoped_ptr<PrefService> prefs_; 93 TestPasswordManagerDriver driver_; 94 bool is_sync_enabled_; 95}; 96 97// Unlike the base AutofillMetrics, exposes copy and assignment constructors, 98// which are handy for briefer test code. The AutofillMetrics class is 99// stateless, so this is safe. 100class TestAutofillMetrics : public autofill::AutofillMetrics { 101 public: 102 TestAutofillMetrics() {} 103 virtual ~TestAutofillMetrics() {} 104}; 105 106} // anonymous namespace 107 108class PasswordGenerationManagerTest : public testing::Test { 109 protected: 110 virtual void SetUp() OVERRIDE { 111 // Construct a PrefService and register all necessary prefs before handing 112 // it off to |client_|, as the initialization flow of |client_| will 113 // indirectly cause those prefs to be immediately accessed. 114 scoped_ptr<TestingPrefServiceSimple> prefs(new TestingPrefServiceSimple()); 115 prefs->registry()->RegisterBooleanPref(prefs::kPasswordManagerEnabled, 116 true); 117 client_.reset(new TestPasswordManagerClient(prefs.PassAs<PrefService>())); 118 } 119 120 virtual void TearDown() OVERRIDE { client_.reset(); } 121 122 PasswordGenerationManager* GetGenerationManager() { 123 return client_->GetDriver()->GetPasswordGenerationManager(); 124 } 125 126 TestPasswordManagerDriver* GetTestDriver() { 127 return static_cast<TestPasswordManagerDriver*>(client_->GetDriver()); 128 } 129 130 bool IsGenerationEnabled() { 131 return GetGenerationManager()->IsGenerationEnabled(); 132 } 133 134 void DetectAccountCreationForms( 135 const std::vector<autofill::FormStructure*>& forms) { 136 GetGenerationManager()->DetectAccountCreationForms(forms); 137 } 138 139 scoped_ptr<TestPasswordManagerClient> client_; 140}; 141 142TEST_F(PasswordGenerationManagerTest, IsGenerationEnabled) { 143 // Enabling the PasswordManager and password sync should cause generation to 144 // be enabled. 145 PrefService* prefs = client_->GetPrefs(); 146 prefs->SetBoolean(prefs::kPasswordManagerEnabled, true); 147 client_->set_is_password_sync_enabled(true); 148 EXPECT_TRUE(IsGenerationEnabled()); 149 150 // Disabling password syncing should cause generation to be disabled. 151 client_->set_is_password_sync_enabled(false); 152 EXPECT_FALSE(IsGenerationEnabled()); 153 154 // Disabling the PasswordManager should cause generation to be disabled even 155 // if syncing is enabled. 156 prefs->SetBoolean(prefs::kPasswordManagerEnabled, false); 157 client_->set_is_password_sync_enabled(true); 158 EXPECT_FALSE(IsGenerationEnabled()); 159} 160 161TEST_F(PasswordGenerationManagerTest, DetectAccountCreationForms) { 162 // Setup so that IsGenerationEnabled() returns true. 163 PrefService* prefs = client_->GetPrefs(); 164 prefs->SetBoolean(prefs::kPasswordManagerEnabled, true); 165 client_->set_is_password_sync_enabled(true); 166 167 autofill::FormData login_form; 168 login_form.origin = GURL("http://www.yahoo.com/login/"); 169 autofill::FormFieldData username; 170 username.label = ASCIIToUTF16("username"); 171 username.name = ASCIIToUTF16("login"); 172 username.form_control_type = "text"; 173 login_form.fields.push_back(username); 174 autofill::FormFieldData password; 175 password.label = ASCIIToUTF16("password"); 176 password.name = ASCIIToUTF16("password"); 177 password.form_control_type = "password"; 178 login_form.fields.push_back(password); 179 autofill::FormStructure form1(login_form); 180 std::vector<autofill::FormStructure*> forms; 181 forms.push_back(&form1); 182 autofill::FormData account_creation_form; 183 account_creation_form.origin = GURL("http://accounts.yahoo.com/"); 184 account_creation_form.fields.push_back(username); 185 account_creation_form.fields.push_back(password); 186 autofill::FormFieldData confirm_password; 187 confirm_password.label = ASCIIToUTF16("confirm_password"); 188 confirm_password.name = ASCIIToUTF16("password"); 189 confirm_password.form_control_type = "password"; 190 account_creation_form.fields.push_back(confirm_password); 191 autofill::FormStructure form2(account_creation_form); 192 forms.push_back(&form2); 193 194 // Simulate the server response to set the field types. 195 const char* const kServerResponse = 196 "<autofillqueryresponse>" 197 "<field autofilltype=\"9\" />" 198 "<field autofilltype=\"75\" />" 199 "<field autofilltype=\"9\" />" 200 "<field autofilltype=\"76\" />" 201 "<field autofilltype=\"75\" />" 202 "</autofillqueryresponse>"; 203 autofill::FormStructure::ParseQueryResponse( 204 kServerResponse, forms, TestAutofillMetrics()); 205 206 DetectAccountCreationForms(forms); 207 EXPECT_EQ(1u, GetTestDriver()->GetFoundAccountCreationForms().size()); 208 EXPECT_EQ(GURL("http://accounts.yahoo.com/"), 209 GetTestDriver()->GetFoundAccountCreationForms()[0].origin); 210} 211 212TEST_F(PasswordGenerationManagerTest, UpdatePasswordSyncStateIncognito) { 213 // Disable password manager by going incognito. Even though password 214 // syncing is enabled, generation should still 215 // be disabled. 216 GetTestDriver()->set_is_off_the_record(true); 217 PrefService* prefs = client_->GetPrefs(); 218 prefs->SetBoolean(prefs::kPasswordManagerEnabled, true); 219 client_->set_is_password_sync_enabled(true); 220 221 EXPECT_FALSE(IsGenerationEnabled()); 222} 223 224} // namespace password_manager 225