| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/password_manager/core/browser/password_generation_manager.h
" | 5 #include "components/password_manager/core/browser/password_generation_manager.h
" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/autofill/core/browser/autofill_field.h" | 14 #include "components/autofill/core/browser/autofill_field.h" |
| 15 #include "components/autofill/core/browser/autofill_metrics.h" | 15 #include "components/autofill/core/browser/autofill_metrics.h" |
| 16 #include "components/autofill/core/browser/form_structure.h" | 16 #include "components/autofill/core/browser/form_structure.h" |
| 17 #include "components/autofill/core/common/form_data.h" | 17 #include "components/autofill/core/common/form_data.h" |
| 18 #include "components/autofill/core/common/form_field_data.h" | 18 #include "components/autofill/core/common/form_field_data.h" |
| 19 #include "components/autofill/core/common/password_form_generation_data.h" | 19 #include "components/autofill/core/common/password_form_generation_data.h" |
| 20 #include "components/autofill/core/common/signatures_util.h" |
| 20 #include "components/password_manager/core/browser/password_autofill_manager.h" | 21 #include "components/password_manager/core/browser/password_autofill_manager.h" |
| 21 #include "components/password_manager/core/browser/password_manager.h" | 22 #include "components/password_manager/core/browser/password_manager.h" |
| 22 #include "components/password_manager/core/browser/stub_password_manager_client.
h" | 23 #include "components/password_manager/core/browser/stub_password_manager_client.
h" |
| 23 #include "components/password_manager/core/browser/stub_password_manager_driver.
h" | 24 #include "components/password_manager/core/browser/stub_password_manager_driver.
h" |
| 24 #include "components/password_manager/core/browser/test_password_store.h" | 25 #include "components/password_manager/core/browser/test_password_store.h" |
| 25 #include "components/password_manager/core/common/password_manager_pref_names.h" | 26 #include "components/password_manager/core/common/password_manager_pref_names.h" |
| 26 #include "components/prefs/pref_registry_simple.h" | 27 #include "components/prefs/pref_registry_simple.h" |
| 27 #include "components/prefs/pref_service.h" | 28 #include "components/prefs/pref_service.h" |
| 28 #include "components/prefs/testing_pref_service.h" | 29 #include "components/prefs/testing_pref_service.h" |
| 29 #include "components/variations/entropy_provider.h" | 30 #include "components/variations/entropy_provider.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 33 | 34 |
| 35 using autofill::FormStructure; |
| 34 using base::ASCIIToUTF16; | 36 using base::ASCIIToUTF16; |
| 35 using testing::_; | 37 using testing::_; |
| 36 | 38 |
| 37 namespace password_manager { | 39 namespace password_manager { |
| 38 | 40 |
| 39 namespace { | 41 namespace { |
| 40 | 42 |
| 41 class TestPasswordManagerDriver : public StubPasswordManagerDriver { | 43 class TestPasswordManagerDriver : public StubPasswordManagerDriver { |
| 42 public: | 44 public: |
| 43 explicit TestPasswordManagerDriver(PasswordManagerClient* client) | 45 explicit TestPasswordManagerDriver(PasswordManagerClient* client) |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 username.form_control_type = "text"; | 184 username.form_control_type = "text"; |
| 183 login_form.fields.push_back(username); | 185 login_form.fields.push_back(username); |
| 184 autofill::FormFieldData password; | 186 autofill::FormFieldData password; |
| 185 password.label = ASCIIToUTF16("password"); | 187 password.label = ASCIIToUTF16("password"); |
| 186 password.name = ASCIIToUTF16("password"); | 188 password.name = ASCIIToUTF16("password"); |
| 187 password.form_control_type = "password"; | 189 password.form_control_type = "password"; |
| 188 login_form.fields.push_back(password); | 190 login_form.fields.push_back(password); |
| 189 autofill::FormStructure form1(login_form); | 191 autofill::FormStructure form1(login_form); |
| 190 std::vector<autofill::FormStructure*> forms; | 192 std::vector<autofill::FormStructure*> forms; |
| 191 forms.push_back(&form1); | 193 forms.push_back(&form1); |
| 194 |
| 192 autofill::FormData account_creation_form; | 195 autofill::FormData account_creation_form; |
| 193 account_creation_form.origin = GURL("http://accounts.yahoo.com/"); | 196 account_creation_form.origin = GURL("http://accounts.yahoo.com/"); |
| 194 account_creation_form.action = GURL("http://accounts.yahoo.com/signup"); | 197 account_creation_form.action = GURL("http://accounts.yahoo.com/signup"); |
| 195 account_creation_form.name = ASCIIToUTF16("account_creation_form"); | 198 account_creation_form.name = ASCIIToUTF16("account_creation_form"); |
| 196 account_creation_form.fields.push_back(username); | 199 account_creation_form.fields.push_back(username); |
| 197 account_creation_form.fields.push_back(password); | 200 account_creation_form.fields.push_back(password); |
| 198 autofill::FormFieldData confirm_password; | 201 autofill::FormFieldData confirm_password; |
| 199 confirm_password.label = ASCIIToUTF16("confirm_password"); | 202 confirm_password.label = ASCIIToUTF16("confirm_password"); |
| 200 confirm_password.name = ASCIIToUTF16("password"); | 203 confirm_password.name = ASCIIToUTF16("password"); |
| 201 confirm_password.form_control_type = "password"; | 204 confirm_password.form_control_type = "password"; |
| 202 account_creation_form.fields.push_back(confirm_password); | 205 account_creation_form.fields.push_back(confirm_password); |
| 206 autofill::FormSignature account_creation_form_signature = |
| 207 autofill::CalculateFormSignature(account_creation_form); |
| 208 autofill::FieldSignature account_creation_field_signature = |
| 209 autofill::CalculateFieldSignatureForField(password); |
| 203 autofill::FormStructure form2(account_creation_form); | 210 autofill::FormStructure form2(account_creation_form); |
| 204 forms.push_back(&form2); | 211 forms.push_back(&form2); |
| 212 |
| 205 autofill::FormData change_password_form; | 213 autofill::FormData change_password_form; |
| 206 change_password_form.origin = GURL("http://accounts.yahoo.com/"); | 214 change_password_form.origin = GURL("http://accounts.yahoo.com/"); |
| 207 change_password_form.action = GURL("http://accounts.yahoo.com/change"); | 215 change_password_form.action = GURL("http://accounts.yahoo.com/change"); |
| 208 change_password_form.name = ASCIIToUTF16("change_password_form"); | 216 change_password_form.name = ASCIIToUTF16("change_password_form"); |
| 209 change_password_form.fields.push_back(password); | 217 change_password_form.fields.push_back(password); |
| 210 change_password_form.fields[0].name = ASCIIToUTF16("new_password"); | 218 change_password_form.fields[0].name = ASCIIToUTF16("new_password"); |
| 211 change_password_form.fields.push_back(confirm_password); | 219 change_password_form.fields.push_back(confirm_password); |
| 212 autofill::FormStructure form3(change_password_form); | 220 autofill::FormStructure form3(change_password_form); |
| 221 autofill::FormSignature change_password_form_signature = |
| 222 autofill::CalculateFormSignature(change_password_form); |
| 223 autofill::FieldSignature change_password_field_signature = |
| 224 autofill::CalculateFieldSignatureForField(change_password_form.fields[0]); |
| 213 forms.push_back(&form3); | 225 forms.push_back(&form3); |
| 214 | 226 |
| 215 // Simulate the server response to set the field types. | 227 // Simulate the server response to set the field types. |
| 216 // The server response numbers mean: | 228 // The server response numbers mean: |
| 217 // EMAIL_ADDRESS = 9 | 229 // EMAIL_ADDRESS = 9 |
| 218 // PASSWORD = 75 | 230 // PASSWORD = 75 |
| 219 // ACCOUNT_CREATION_PASSWORD = 76 | 231 // ACCOUNT_CREATION_PASSWORD = 76 |
| 220 // NEW_PASSWORD = 88 | 232 // NEW_PASSWORD = 88 |
| 221 autofill::AutofillQueryResponseContents response; | 233 autofill::AutofillQueryResponseContents response; |
| 222 response.add_field()->set_autofill_type(9); | 234 response.add_field()->set_autofill_type(9); |
| 223 response.add_field()->set_autofill_type(75); | 235 response.add_field()->set_autofill_type(75); |
| 224 response.add_field()->set_autofill_type(9); | 236 response.add_field()->set_autofill_type(9); |
| 225 response.add_field()->set_autofill_type(76); | 237 response.add_field()->set_autofill_type(76); |
| 226 response.add_field()->set_autofill_type(75); | 238 response.add_field()->set_autofill_type(75); |
| 227 response.add_field()->set_autofill_type(88); | 239 response.add_field()->set_autofill_type(88); |
| 228 response.add_field()->set_autofill_type(88); | 240 response.add_field()->set_autofill_type(88); |
| 229 | 241 |
| 230 std::string response_string; | 242 std::string response_string; |
| 231 ASSERT_TRUE(response.SerializeToString(&response_string)); | 243 ASSERT_TRUE(response.SerializeToString(&response_string)); |
| 232 autofill::FormStructure::ParseQueryResponse(response_string, forms, NULL); | 244 autofill::FormStructure::ParseQueryResponse(response_string, forms, NULL); |
| 233 | 245 |
| 234 DetectFormsEligibleForGeneration(forms); | 246 DetectFormsEligibleForGeneration(forms); |
| 235 EXPECT_EQ(2u, GetTestDriver()->GetFoundEligibleForGenerationForms().size()); | 247 EXPECT_EQ(2u, GetTestDriver()->GetFoundEligibleForGenerationForms().size()); |
| 236 EXPECT_EQ(GURL("http://accounts.yahoo.com/signup"), | 248 EXPECT_EQ( |
| 237 GetTestDriver()->GetFoundEligibleForGenerationForms()[0].action); | 249 account_creation_form_signature, |
| 238 EXPECT_EQ(account_creation_form.name, | 250 GetTestDriver()->GetFoundEligibleForGenerationForms()[0].form_signature); |
| 239 GetTestDriver()->GetFoundEligibleForGenerationForms()[0].name); | 251 EXPECT_EQ( |
| 240 EXPECT_EQ(password.name, GetTestDriver() | 252 account_creation_field_signature, |
| 241 ->GetFoundEligibleForGenerationForms()[0] | 253 GetTestDriver()->GetFoundEligibleForGenerationForms()[0].field_signature); |
| 242 .generation_field.name); | 254 |
| 243 EXPECT_EQ(GURL("http://accounts.yahoo.com/change"), | 255 EXPECT_EQ( |
| 244 GetTestDriver()->GetFoundEligibleForGenerationForms()[1].action); | 256 change_password_form_signature, |
| 245 EXPECT_EQ(ASCIIToUTF16("new_password"), | 257 GetTestDriver()->GetFoundEligibleForGenerationForms()[1].form_signature); |
| 246 GetTestDriver() | 258 EXPECT_EQ( |
| 247 ->GetFoundEligibleForGenerationForms()[1] | 259 change_password_field_signature, |
| 248 .generation_field.name); | 260 GetTestDriver()->GetFoundEligibleForGenerationForms()[1].field_signature); |
| 249 EXPECT_EQ(change_password_form.name, | |
| 250 GetTestDriver()->GetFoundEligibleForGenerationForms()[1].name); | |
| 251 } | 261 } |
| 252 | 262 |
| 253 TEST_F(PasswordGenerationManagerTest, UpdatePasswordSyncStateIncognito) { | 263 TEST_F(PasswordGenerationManagerTest, UpdatePasswordSyncStateIncognito) { |
| 254 // Disable password manager by going incognito. Even though password | 264 // Disable password manager by going incognito. Even though password |
| 255 // syncing is enabled, generation should still | 265 // syncing is enabled, generation should still |
| 256 // be disabled. | 266 // be disabled. |
| 257 EXPECT_CALL(*client_, IsOffTheRecord()).WillRepeatedly(testing::Return(true)); | 267 EXPECT_CALL(*client_, IsOffTheRecord()).WillRepeatedly(testing::Return(true)); |
| 258 PrefService* prefs = client_->GetPrefs(); | 268 PrefService* prefs = client_->GetPrefs(); |
| 259 prefs->SetBoolean(prefs::kPasswordManagerSavingEnabled, true); | 269 prefs->SetBoolean(prefs::kPasswordManagerSavingEnabled, true); |
| 260 EXPECT_CALL(*client_, GetPasswordSyncState()) | 270 EXPECT_CALL(*client_, GetPasswordSyncState()) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 276 field_trial = base::FieldTrialList::CreateFieldTrial( | 286 field_trial = base::FieldTrialList::CreateFieldTrial( |
| 277 "AutofillFieldMetadata", "Enabled"); | 287 "AutofillFieldMetadata", "Enabled"); |
| 278 EXPECT_CALL(*GetTestDriver(), AllowToRunFormClassifier()) | 288 EXPECT_CALL(*GetTestDriver(), AllowToRunFormClassifier()) |
| 279 .WillOnce(testing::Return()); | 289 .WillOnce(testing::Return()); |
| 280 } | 290 } |
| 281 GetGenerationManager()->CheckIfFormClassifierShouldRun(); | 291 GetGenerationManager()->CheckIfFormClassifierShouldRun(); |
| 282 } | 292 } |
| 283 } | 293 } |
| 284 | 294 |
| 285 } // namespace password_manager | 295 } // namespace password_manager |
| OLD | NEW |