1// Copyright 2013 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 "components/autofill/core/common/password_form_fill_data.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "content/public/common/password_form.h"
9#include "testing/gmock/include/gmock/gmock.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12using content::PasswordForm;
13using content::PasswordFormMap;
14
15namespace autofill {
16
17// Tests that the when there is a single preferred match, and no extra
18// matches, the PasswordFormFillData is filled in correctly.
19TEST(PasswordFormFillDataTest, TestSinglePreferredMatch) {
20  // Create the current form on the page.
21  PasswordForm form_on_page;
22  form_on_page.origin = GURL("https://foo.com/");
23  form_on_page.action = GURL("https://foo.com/login");
24  form_on_page.username_element = ASCIIToUTF16("username");
25  form_on_page.username_value = ASCIIToUTF16("test@gmail.com");
26  form_on_page.password_element = ASCIIToUTF16("password");
27  form_on_page.password_value = ASCIIToUTF16("test");
28  form_on_page.submit_element = ASCIIToUTF16("");
29  form_on_page.signon_realm = "https://foo.com/";
30  form_on_page.ssl_valid = true;
31  form_on_page.preferred = false;
32  form_on_page.scheme = PasswordForm::SCHEME_HTML;
33
34  // Create an exact match in the database.
35  PasswordForm preferred_match;
36  preferred_match.origin = GURL("https://foo.com/");
37  preferred_match.action = GURL("https://foo.com/login");
38  preferred_match.username_element = ASCIIToUTF16("username");
39  preferred_match.username_value = ASCIIToUTF16("test@gmail.com");
40  preferred_match.password_element = ASCIIToUTF16("password");
41  preferred_match.password_value = ASCIIToUTF16("test");
42  preferred_match.submit_element = ASCIIToUTF16("");
43  preferred_match.signon_realm = "https://foo.com/";
44  preferred_match.ssl_valid = true;
45  preferred_match.preferred = true;
46  preferred_match.scheme = PasswordForm::SCHEME_HTML;
47
48  PasswordFormMap matches;
49
50  PasswordFormFillData result;
51  InitPasswordFormFillData(form_on_page,
52                           matches,
53                           &preferred_match,
54                           true,
55                           false,
56                           &result);
57
58  // |wait_for_username| should reflect the |wait_for_username_before_autofill|
59  // argument of InitPasswordFormFillData which in this case is true.
60  EXPECT_TRUE(result.wait_for_username);
61  // The preferred realm should be empty since it's the same as the realm of
62  // the form.
63  EXPECT_EQ(result.preferred_realm, "");
64
65  PasswordFormFillData result2;
66  InitPasswordFormFillData(form_on_page,
67                           matches,
68                           &preferred_match,
69                           false,
70                           false,
71                           &result2);
72
73  // |wait_for_username| should reflect the |wait_for_username_before_autofill|
74  // argument of InitPasswordFormFillData which in this case is false.
75  EXPECT_FALSE(result2.wait_for_username);
76}
77
78// Tests that the InitPasswordFormFillData behaves correctly when there is a
79// preferred match that was found using public suffix matching, an additional
80// result that also used public suffix matching, and a third result that was
81// found without using public suffix matching.
82TEST(PasswordFormFillDataTest, TestPublicSuffixDomainMatching) {
83  // Create the current form on the page.
84  PasswordForm form_on_page;
85  form_on_page.origin = GURL("https://foo.com/");
86  form_on_page.action = GURL("https://foo.com/login");
87  form_on_page.username_element = ASCIIToUTF16("username");
88  form_on_page.username_value = ASCIIToUTF16("test@gmail.com");
89  form_on_page.password_element = ASCIIToUTF16("password");
90  form_on_page.password_value = ASCIIToUTF16("test");
91  form_on_page.submit_element = ASCIIToUTF16("");
92  form_on_page.signon_realm = "https://foo.com/";
93  form_on_page.ssl_valid = true;
94  form_on_page.preferred = false;
95  form_on_page.scheme = PasswordForm::SCHEME_HTML;
96
97  // Create a match from the database that matches using public suffix.
98  PasswordForm preferred_match;
99  preferred_match.origin = GURL("https://mobile.foo.com/");
100  preferred_match.action = GURL("https://mobile.foo.com/login");
101  preferred_match.username_element = ASCIIToUTF16("username");
102  preferred_match.username_value = ASCIIToUTF16("test@gmail.com");
103  preferred_match.password_element = ASCIIToUTF16("password");
104  preferred_match.password_value = ASCIIToUTF16("test");
105  preferred_match.submit_element = ASCIIToUTF16("");
106  preferred_match.signon_realm = "https://mobile.foo.com/";
107  preferred_match.original_signon_realm = "https://foo.com/";
108  preferred_match.ssl_valid = true;
109  preferred_match.preferred = true;
110  preferred_match.scheme = PasswordForm::SCHEME_HTML;
111
112  // Create a match that matches exactly, so |original_signon_realm| is not set.
113  PasswordForm exact_match;
114  exact_match.origin = GURL("https://foo.com/");
115  exact_match.action = GURL("https://foo.com/login");
116  exact_match.username_element = ASCIIToUTF16("username");
117  exact_match.username_value = ASCIIToUTF16("test1@gmail.com");
118  exact_match.password_element = ASCIIToUTF16("password");
119  exact_match.password_value = ASCIIToUTF16("test");
120  exact_match.submit_element = ASCIIToUTF16("");
121  exact_match.signon_realm = "https://foo.com/";
122  exact_match.ssl_valid = true;
123  exact_match.preferred = false;
124  exact_match.scheme = PasswordForm::SCHEME_HTML;
125
126  // Create a match that was matched using public suffix, so
127  // |original_signon_realm| is set to where the result came from.
128  PasswordForm public_suffix_match;
129  public_suffix_match.origin = GURL("https://foo.com/");
130  public_suffix_match.action = GURL("https://foo.com/login");
131  public_suffix_match.username_element = ASCIIToUTF16("username");
132  public_suffix_match.username_value = ASCIIToUTF16("test2@gmail.com");
133  public_suffix_match.password_element = ASCIIToUTF16("password");
134  public_suffix_match.password_value = ASCIIToUTF16("test");
135  public_suffix_match.submit_element = ASCIIToUTF16("");
136  public_suffix_match.original_signon_realm = "https://subdomain.foo.com/";
137  public_suffix_match.signon_realm = "https://foo.com/";
138  public_suffix_match.ssl_valid = true;
139  public_suffix_match.preferred = false;
140  public_suffix_match.scheme = PasswordForm::SCHEME_HTML;
141
142  // Add one exact match and one public suffix match.
143  PasswordFormMap matches;
144  matches[exact_match.username_value] = &exact_match;
145  matches[public_suffix_match.username_value] = &public_suffix_match;
146
147  PasswordFormFillData result;
148  InitPasswordFormFillData(form_on_page,
149                           matches,
150                           &preferred_match,
151                           true,
152                           false,
153                           &result);
154  EXPECT_TRUE(result.wait_for_username);
155  // The preferred realm should match the original signon realm from the
156  // preferred match so the user can see where the result came from.
157  EXPECT_EQ(result.preferred_realm,
158            preferred_match.original_signon_realm);
159
160  // The realm of the exact match should be empty.
161  PasswordFormFillData::LoginCollection::const_iterator iter =
162      result.additional_logins.find(exact_match.username_value);
163  EXPECT_EQ(iter->second.realm, "");
164
165  // The realm of the public suffix match should be set to the original signon
166  // realm so the user can see where the result came from.
167  iter = result.additional_logins.find(public_suffix_match.username_value);
168  EXPECT_EQ(iter->second.realm, public_suffix_match.original_signon_realm);
169}
170
171}  // namespace autofill
172