File: signatures_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (208 lines) | stat: -rw-r--r-- 7,208 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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());
}

TEST(SignaturesTest, StructuralFormSignatureSmall) {
  // Test with a small form (<= 2 fields).
  FormData small_form;
  small_form.set_url(GURL("http://foo.com/login?q=a#ref"));

  FormFieldData field1;
  field1.set_form_control_type(FormControlType::kInputText);
  test_api(small_form).Append(field1);

  FormFieldData field2;
  field2.set_form_control_type(FormControlType::kInputEmail);
  test_api(small_form).Append(field2);

  // Structural form signature string of a form with 2 fields or less should
  // only concatenate scheme, host, and field types. It should not include
  // path, ref or query.
  EXPECT_EQ(StrToHash64Bit("http://foo.com&text&email"),
            CalculateStructuralFormSignature(small_form).value());
}

TEST(SignaturesTest, StructuralFormSignatureLarge) {
  // Test with a large form (> 2 fields).
  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::kInputEmail);
  test_api(large_form).Append(field2);

  FormFieldData field3;
  field3.set_form_control_type(FormControlType::kInputTelephone);
  test_api(large_form).Append(field3);

  // Structural 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&email&tel"),
            CalculateStructuralFormSignature(large_form).value());
}

}  // namespace autofill