1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/common/signatures.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_data_test_api.h"
#include "components/autofill/core/common/form_field_data.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace autofill {
TEST(SignaturesTest, StripDigits) {
FormData actual_form;
actual_form.set_url(GURL("http://foo.com"));
actual_form.set_name(u"form_name_12345");
FormFieldData field1;
field1.set_form_control_type(FormControlType::kInputText);
field1.set_name(u"field_name_12345");
test_api(actual_form).Append(field1);
FormFieldData field2;
field2.set_form_control_type(FormControlType::kInputText);
field2.set_name(u"field_name_1234");
test_api(actual_form).Append(field2);
// Sequences of 5 digits or longer should be stripped.
FormData expected_form(actual_form);
expected_form.set_name(u"form_name_");
test_api(expected_form).field(0).set_name(u"field_name_");
EXPECT_EQ(CalculateFormSignature(expected_form).value(),
CalculateFormSignature(actual_form).value());
EXPECT_EQ(
StrToHash64Bit("http://foo.com&form_name_&field_name_&field_name_1234"),
CalculateFormSignature(actual_form).value());
}
// Tests that <input type={checkbox,radio,date}> do not count towards
// FormSignatures.
TEST(SignaturesTest, IgnoreCheckboxRadioDate) {
FormData form;
form.set_url(GURL("http://foo.com"));
form.set_name(u"form_name");
{
FormFieldData field;
field.set_form_control_type(FormControlType::kInputText);
field.set_name(u"field1");
test_api(form).Append(field);
}
{
FormFieldData field;
field.set_form_control_type(FormControlType::kInputCheckbox);
field.set_name(u"field2");
field.set_check_status(FormFieldData::CheckStatus::kCheckableButUnchecked);
test_api(form).Append(field);
}
{
FormFieldData field;
field.set_form_control_type(FormControlType::kInputRadio);
field.set_name(u"field1");
field.set_check_status(FormFieldData::CheckStatus::kChecked);
test_api(form).Append(field);
}
{
FormFieldData field;
field.set_form_control_type(FormControlType::kInputDate);
field.set_name(u"field2");
test_api(form).Append(field);
}
EXPECT_EQ(StrToHash64Bit("http://foo.com&form_name&field1"),
CalculateFormSignature(form).value());
}
TEST(SignaturesTest, AlternativeFormSignatureLarge) {
FormData large_form;
large_form.set_url(GURL("http://foo.com/login?q=a#ref"));
FormFieldData field1;
field1.set_form_control_type(FormControlType::kInputText);
test_api(large_form).Append(field1);
FormFieldData field2;
field2.set_form_control_type(FormControlType::kInputText);
test_api(large_form).Append(field2);
FormFieldData field3;
field3.set_form_control_type(FormControlType::kInputEmail);
test_api(large_form).Append(field3);
FormFieldData field4;
field4.set_form_control_type(FormControlType::kInputTelephone);
test_api(large_form).Append(field4);
// Alternative form signature string of a form with more than two fields
// should only concatenate scheme, host, and field types.
EXPECT_EQ(StrToHash64Bit("http://foo.com&text&text&email&tel"),
CalculateAlternativeFormSignature(large_form).value());
}
TEST(SignaturesTest, AlternativeFormSignatureSmallPath) {
FormData small_form_path;
small_form_path.set_url(GURL("http://foo.com/login?q=a#ref"));
FormFieldData field1;
field1.set_form_control_type(FormControlType::kInputText);
test_api(small_form_path).Append(field1);
FormFieldData field2;
field2.set_form_control_type(FormControlType::kInputText);
test_api(small_form_path).Append(field2);
// Alternative form signature string of a form with 2 fields or less should
// concatenate scheme, host, field types, and path if it is non-empty.
EXPECT_EQ(StrToHash64Bit("http://foo.com&text&text/login"),
CalculateAlternativeFormSignature(small_form_path).value());
}
TEST(SignaturesTest, AlternativeFormSignatureSmallRef) {
FormData small_form_ref;
small_form_ref.set_url(GURL("http://foo.com?q=a#ref"));
FormFieldData field1;
field1.set_form_control_type(FormControlType::kInputText);
test_api(small_form_ref).Append(field1);
FormFieldData field2;
field2.set_form_control_type(FormControlType::kInputText);
test_api(small_form_ref).Append(field2);
// Alternative form signature string of a form with 2 fields or less and
// without a path should concatenate scheme, host, field types, and reference
// if it is non-empty.
EXPECT_EQ(StrToHash64Bit("http://foo.com&text&text#ref"),
CalculateAlternativeFormSignature(small_form_ref).value());
}
TEST(SignaturesTest, AlternativeFormSignatureSmallQuery) {
FormData small_form_query;
small_form_query.set_url(GURL("http://foo.com?q=a"));
FormFieldData field1;
field1.set_form_control_type(FormControlType::kInputText);
test_api(small_form_query).Append(field1);
FormFieldData field2;
field2.set_form_control_type(FormControlType::kInputText);
test_api(small_form_query).Append(field2);
// Alternative form signature string of a form with 2 fields or less and
// without a path or reference should concatenate scheme, host, field types,
// and query if it is non-empty.
EXPECT_EQ(StrToHash64Bit("http://foo.com&text&text?q=a"),
CalculateAlternativeFormSignature(small_form_query).value());
}
} // namespace autofill
|