Commit fac61079 authored by Parastoo Geranmayeh's avatar Parastoo Geranmayeh Committed by Commit Bot

[AF] Stricter Validity Logic

Change the address validation logic according to this doc:
(Solution 6)
go/autofill-validation-dilemma

Bug: 899251
Change-Id: I93435025754106d5c61889097690288d812f5f0c
Reviewed-on: https://chromium-review.googlesource.com/c/1301797
Commit-Queue: Parastoo Geranmayeh <parastoog@google.com>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#603512}
parent 155b96f5
......@@ -4,6 +4,7 @@
#include "components/autofill/core/browser/autofill_profile_validation_util.h"
#include <string>
#include <utility>
#include "base/i18n/case_conversion.h"
......@@ -125,6 +126,41 @@ void SetEmptyValidityIfEmpty(AutofillProfile* profile) {
AutofillProfile::CLIENT);
}
void SetInvalidIfUnvalidated(AutofillProfile* profile) {
if (profile->GetValidityState(ADDRESS_HOME_COUNTRY,
AutofillProfile::CLIENT) ==
AutofillProfile::UNVALIDATED) {
profile->SetValidityState(ADDRESS_HOME_COUNTRY, AutofillProfile::INVALID,
AutofillProfile::CLIENT);
}
if (profile->GetValidityState(ADDRESS_HOME_STATE, AutofillProfile::CLIENT) ==
AutofillProfile::UNVALIDATED) {
profile->SetValidityState(ADDRESS_HOME_STATE, AutofillProfile::INVALID,
AutofillProfile::CLIENT);
}
if (profile->GetValidityState(ADDRESS_HOME_CITY, AutofillProfile::CLIENT) ==
AutofillProfile::UNVALIDATED) {
profile->SetValidityState(ADDRESS_HOME_CITY, AutofillProfile::INVALID,
AutofillProfile::CLIENT);
}
if (profile->GetValidityState(ADDRESS_HOME_DEPENDENT_LOCALITY,
AutofillProfile::CLIENT) ==
AutofillProfile::UNVALIDATED) {
profile->SetValidityState(ADDRESS_HOME_DEPENDENT_LOCALITY,
AutofillProfile::INVALID,
AutofillProfile::CLIENT);
}
if (profile->GetValidityState(ADDRESS_HOME_ZIP, AutofillProfile::CLIENT) ==
AutofillProfile::UNVALIDATED) {
profile->SetValidityState(ADDRESS_HOME_ZIP, AutofillProfile::INVALID,
AutofillProfile::CLIENT);
}
}
void MaybeApplyValidToFields(AutofillProfile* profile) {
// The metadata works from top to bottom. Therefore, a so far UNVALIDATED
// subregion can only be validated if its super-region is VALID. In this
......@@ -162,6 +198,29 @@ void MaybeApplyValidToFields(AutofillProfile* profile) {
}
}
void ApplyValidOnlyIfAllChildrenNotInvalid(AutofillProfile* profile) {
if (profile->GetValidityState(ADDRESS_HOME_STATE, AutofillProfile::CLIENT) ==
AutofillProfile::INVALID &&
profile->GetValidityState(ADDRESS_HOME_ZIP, AutofillProfile::CLIENT) ==
AutofillProfile::INVALID) {
profile->SetValidityState(ADDRESS_HOME_COUNTRY, AutofillProfile::INVALID,
AutofillProfile::CLIENT);
}
if (profile->GetValidityState(ADDRESS_HOME_CITY, AutofillProfile::CLIENT) ==
AutofillProfile::INVALID) {
profile->SetValidityState(ADDRESS_HOME_STATE, AutofillProfile::INVALID,
AutofillProfile::CLIENT);
}
if (profile->GetValidityState(ADDRESS_HOME_DEPENDENT_LOCALITY,
AutofillProfile::CLIENT) ==
AutofillProfile::INVALID) {
profile->SetValidityState(ADDRESS_HOME_CITY, AutofillProfile::INVALID,
AutofillProfile::CLIENT);
}
}
} // namespace
namespace profile_validation_util {
......@@ -170,13 +229,13 @@ void ValidateProfile(AutofillProfile* profile,
AddressValidator* address_validator) {
DCHECK(address_validator);
DCHECK(profile);
ValidateAddress(profile, address_validator);
ValidateAddressStrictly(profile, address_validator);
ValidatePhoneNumber(profile);
ValidateEmailAddress(profile);
}
void ValidateAddress(AutofillProfile* profile,
AddressValidator* address_validator) {
AddressValidator::Status ValidateAddress(AutofillProfile* profile,
AddressValidator* address_validator) {
DCHECK(address_validator);
DCHECK(profile);
......@@ -190,7 +249,7 @@ void ValidateAddress(AutofillProfile* profile,
// unclear which, if any, rule should apply.
SetValidityStateForAddressField(profile, COUNTRY, AutofillProfile::INVALID);
SetEmptyValidityIfEmpty(profile);
return;
return AddressValidator::SUCCESS;
}
// The COUNTRY was already listed in the CountryDataMap, therefore it's valid.
......@@ -212,6 +271,31 @@ void ValidateAddress(AutofillProfile* profile,
// Fields (except COUNTRY) could be VALID, only if the rules were available.
if (status == AddressValidator::SUCCESS)
MaybeApplyValidToFields(profile);
return status;
}
void ValidateAddressStrictly(AutofillProfile* profile,
AddressValidator* address_validator) {
DCHECK(address_validator);
DCHECK(profile);
// If the rules were loaded successfully, add a second layer of validation:
// 1. For a field to stay valid after the first run, all the fields that
// depend on that field for validation need to not be invalid on the first
// run, otherwise there is a chance that the data on that field was also
// invalid (incorrect.)
// Example: 1225 Notre-Dame Ouest, Montreal, Quebec, H3C 2A3, United States.
// A human validator can see that the country is most probably the invalid
// field. The first step helps us validate the rules interdependently.
// 2. All the address fields that could not be validated (UNVALIDATED),
// should be considered as invalid.
if (ValidateAddress(profile, address_validator) ==
AddressValidator::SUCCESS) {
ApplyValidOnlyIfAllChildrenNotInvalid(profile);
SetInvalidIfUnvalidated(profile);
}
}
void ValidateEmailAddress(AutofillProfile* profile) {
......
......@@ -16,8 +16,14 @@ void ValidateProfile(AutofillProfile* profile,
AddressValidator* address_validator);
// Sets the validity state of the address fields of the |profile|.
void ValidateAddress(AutofillProfile* profile,
AddressValidator* address_validator);
AddressValidator::Status ValidateAddress(AutofillProfile* profile,
AddressValidator* address_validator);
// Sets the validity state of the address fields of the |profile| in two passes.
// First runs the ValidateAddress, then adds a second layer of validation based
// on the results.
void ValidateAddressStrictly(AutofillProfile* profile,
AddressValidator* address_validator);
// Sets the validity state of the phone number field of the |profile|.
void ValidatePhoneNumber(AutofillProfile* profile);
......
......@@ -165,8 +165,8 @@ TEST_F(AutofillProfileValidatorTest,
// Set up the test expectations.
expected_validity_ = {{ADDRESS_HOME_COUNTRY, AutofillProfile::INVALID},
{ADDRESS_HOME_STATE, AutofillProfile::UNVALIDATED},
{ADDRESS_HOME_ZIP, AutofillProfile::UNVALIDATED},
{ADDRESS_HOME_STATE, AutofillProfile::INVALID},
{ADDRESS_HOME_ZIP, AutofillProfile::INVALID},
{PHONE_HOME_WHOLE_NUMBER, AutofillProfile::UNVALIDATED},
{EMAIL_ADDRESS, AutofillProfile::VALID}};
......@@ -205,8 +205,8 @@ TEST_F(AutofillProfileValidatorTest, ValidateAddress_EmptyCountryCode) {
// Set up the test expectations.
// The phone is validated for the US.
expected_validity_ = {{ADDRESS_HOME_COUNTRY, AutofillProfile::EMPTY},
{ADDRESS_HOME_STATE, AutofillProfile::UNVALIDATED},
{ADDRESS_HOME_ZIP, AutofillProfile::UNVALIDATED},
{ADDRESS_HOME_STATE, AutofillProfile::INVALID},
{ADDRESS_HOME_ZIP, AutofillProfile::INVALID},
{PHONE_HOME_WHOLE_NUMBER, AutofillProfile::UNVALIDATED},
{EMAIL_ADDRESS, AutofillProfile::VALID}};
......
......@@ -6752,7 +6752,7 @@ TEST_F(PersonalDataManagerTest, UpdateClientValidityStates) {
EXPECT_EQ(
AutofillProfile::VALID,
profiles[2]->GetValidityState(ADDRESS_HOME_ZIP, AutofillProfile::CLIENT));
EXPECT_EQ(AutofillProfile::UNVALIDATED,
EXPECT_EQ(AutofillProfile::INVALID,
profiles[2]->GetValidityState(ADDRESS_HOME_CITY,
AutofillProfile::CLIENT));
EXPECT_EQ(AutofillProfile::EMPTY,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment