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