File: manage_passwords_test.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; 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 (338 lines) | stat: -rw-r--r-- 15,707 bytes parent folder | download | duplicates (5)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/passwords/manage_passwords_test.h"

#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/password_manager/chrome_password_change_service.h"
#include "chrome/browser/password_manager/password_change_service_factory.h"
#include "chrome/browser/password_manager/password_manager_test_base.h"
#include "chrome/browser/password_manager/profile_password_store_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
#include "chrome/browser/ui/passwords/passwords_model_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/affiliations/core/browser/mock_affiliation_service.h"
#include "components/autofill/core/common/form_data_test_api.h"
#include "components/password_manager/core/browser/form_saver.h"
#include "components/password_manager/core/browser/form_saver_impl.h"
#include "components/password_manager/core/browser/leak_detection_dialog_utils.h"
#include "components/password_manager/core/browser/mock_password_form_manager_for_ui.h"
#include "components/password_manager/core/browser/password_form.h"
#include "components/password_manager/core/browser/password_form_manager.h"
#include "components/password_manager/core/browser/password_manager_metrics_util.h"
#include "components/password_manager/core/browser/password_manager_test_utils.h"
#include "components/password_manager/core/browser/password_save_manager_impl.h"
#include "components/password_manager/core/browser/password_store/test_password_store.h"
#include "components/password_manager/core/browser/possible_username_data.h"
#include "components/password_manager/core/browser/stub_form_saver.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/sync/test/test_sync_service.h"
#include "content/public/test/test_utils.h"

using base::ASCIIToUTF16;
using password_manager::PasswordFormManager;
using password_manager::PossibleUsernameData;
using password_manager::PossibleUsernameFieldIdentifier;
using testing::Return;
using testing::ReturnRef;

namespace {
constexpr char16_t kTestUsername[] = u"test_username";
}  // namespace

ManagePasswordsTest::ManagePasswordsTest() {
  fetcher_.Fetch();

  // Turn off waiting for server predictions in order to avoid dealing with
  // posted tasks in PasswordFormManager.
  PasswordFormManager::set_wait_for_server_predictions_for_filling(false);
}

ManagePasswordsTest::~ManagePasswordsTest() = default;

void ManagePasswordsTest::SetUpOnMainThread() {
  InteractiveBrowserTest::SetUpOnMainThread();
  mock_optimization_service_ =
      std::make_unique<testing::NiceMock<MockOptimizationGuideKeyedService>>();
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL test_url = embedded_test_server()->GetURL("/empty.html");

  password_form_.signon_realm = test_url.GetWithEmptyPath().spec();
  password_form_.url = test_url;
  password_form_.username_value = kTestUsername;
  password_form_.password_value = u"test_password";
  password_form_.match_type = password_manager::PasswordForm::MatchType::kExact;
  ASSERT_TRUE(AddTabAtIndex(0, test_url, ui::PAGE_TRANSITION_TYPED));
}

void ManagePasswordsTest::TearDownOnMainThread() {
  mock_optimization_service_ = nullptr;
  InteractiveBrowserTest::TearDownOnMainThread();
}

void ManagePasswordsTest::SetUpInProcessBrowserTestFixture() {
  InteractiveBrowserTest::SetUpInProcessBrowserTestFixture();
  create_services_subscription_ =
      BrowserContextDependencyManager::GetInstance()
          ->RegisterCreateServicesCallbackForTesting(
              base::BindRepeating([](content::BrowserContext* context) {
                // Overwrite the password store early before it's accessed by
                // safe browsing.
                ProfilePasswordStoreFactory::GetInstance()->SetTestingFactory(
                    context,
                    base::BindRepeating(&password_manager::BuildPasswordStore<
                                        content::BrowserContext,
                                        password_manager::TestPasswordStore>));

                SyncServiceFactory::GetInstance()->SetTestingFactory(
                    context,
                    base::BindRepeating([](content::BrowserContext*)
                                            -> std::unique_ptr<KeyedService> {
                      return std::make_unique<syncer::TestSyncService>();
                    }));
              }));
}

void ManagePasswordsTest::ExecuteManagePasswordsCommand() {
  // Show the window to ensure that it's active.
  browser()->window()->Show();

  CommandUpdater* updater = browser()->command_controller();
  EXPECT_TRUE(updater->IsCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE));
  EXPECT_TRUE(updater->ExecuteCommand(IDC_MANAGE_PASSWORDS_FOR_PAGE));
}

void ManagePasswordsTest::SetupManagingPasswords(
    const GURL& password_form_url) {
  password_manager::PasswordForm federated_form;
  federated_form.signon_realm = "federation://" +
                                embedded_test_server()->GetOrigin().host() +
                                "/somelongeroriginurl.com";
  federated_form.url = embedded_test_server()->GetURL("/empty.html");
  federated_form.federation_origin =
      url::SchemeHostPort(GURL("https://somelongeroriginurl.com/"));
  federated_form.username_value = u"test_federation_username";
  federated_form.match_type = password_manager::PasswordForm::MatchType::kExact;
  // Overrides url to a defined value to avoid flakiness in pixel tests.
  password_form_.url = !password_form_url.is_empty()
                           ? GURL(password_form_url.spec() + "empty.html")
                           : embedded_test_server()->GetURL("/empty.html");
  std::vector<password_manager::PasswordForm> forms = {password_form_,
                                                       federated_form};
  GetController()->OnPasswordAutofilled(
      forms, embedded_test_server()->GetOrigin(), {});
}

void ManagePasswordsTest::SetupPasswordChange() {
  affiliations::MockAffiliationService mock_affiliation_service;
  PasswordChangeServiceFactory::GetInstance()->SetTestingFactory(
      browser()->profile(),
      base::BindLambdaForTesting([this, &mock_affiliation_service](
                                     content::BrowserContext* context)
                                     -> std::unique_ptr<KeyedService> {
        auto feature_manager =
            std::make_unique<password_manager::MockPasswordFeatureManager>();
        ON_CALL(*feature_manager.get(), IsGenerationEnabled)
            .WillByDefault(testing::Return(true));
        return std::make_unique<ChromePasswordChangeService>(
            &mock_affiliation_service, mock_optimization_service_.get(),
            std::move(feature_manager));
      }));
  mock_optimization_service_.reset();

  const GURL kUrl = GURL("https://example.com/");
  ON_CALL(mock_affiliation_service, GetChangePasswordURL(kUrl))
      .WillByDefault(testing::Return(embedded_test_server()->GetURL(
          "/password/update_form_empty_fields.html")));
  GetController()->OnCredentialLeak(password_manager::LeakedPasswordDetails(
      password_manager::CreateLeakType(
          password_manager::IsSaved(true), password_manager::IsReused(false),
          password_manager::IsSyncing(true),
          password_manager::HasChangePasswordUrl(true)),
      kUrl, u"new_username", u"new_password",
      /*in_account_store=*/true));
  static_cast<PasswordsModelDelegate*>(GetController())
      ->GetPasswordChangeDelegate()
      ->StartPasswordChangeFlow();
}

void ManagePasswordsTest::SetupPendingPassword() {
  GetController()->OnPasswordSubmitted(CreateFormManager());
}

void ManagePasswordsTest::SetupAutomaticPassword() {
  GetController()->OnAutomaticPasswordSave(CreateFormManager(),
                                           /*is_update_confirmation=*/false);
}

void ManagePasswordsTest::SetupAutoSignin(
    std::vector<std::unique_ptr<password_manager::PasswordForm>>
        local_credentials) {
  ASSERT_FALSE(local_credentials.empty());
  url::Origin origin = url::Origin::Create(local_credentials[0]->url);
  GetController()->OnAutoSignin(std::move(local_credentials), origin);
}

void ManagePasswordsTest::SetupSafeState() {
  browser()->profile()->GetPrefs()->SetDouble(
      password_manager::prefs::kLastTimePasswordCheckCompleted,
      (base::Time::Now() - base::Minutes(1)).InSecondsFSinceUnixEpoch());
  SetupPendingPassword();
  scoped_refptr<password_manager::PasswordStoreInterface> password_store =
      ProfilePasswordStoreFactory::GetForProfile(
          browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS);
  password_store->AddLogin(password_form_);
  GetController()->SavePassword(password_form_.username_value,
                                password_form_.password_value);
  GetController()->OnBubbleHidden();
  PasswordManagerBrowserTestBase::WaitForPasswordStore(browser());

  EXPECT_EQ(GetController()->GetState(),
            password_manager::ui::PASSWORD_UPDATED_SAFE_STATE);
}

void ManagePasswordsTest::SetupMoreToFixState() {
  browser()->profile()->GetPrefs()->SetDouble(
      password_manager::prefs::kLastTimePasswordCheckCompleted,
      (base::Time::Now() - base::Minutes(1)).InSecondsFSinceUnixEpoch());
  scoped_refptr<password_manager::PasswordStoreInterface> password_store =
      ProfilePasswordStoreFactory::GetForProfile(
          browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS);
  // This is an unrelated insecure credential that should still be fixed.
  password_manager::PasswordForm to_be_fixed = password_form_;
  to_be_fixed.signon_realm = "https://somesite.com/";
  to_be_fixed.password_issues.insert({password_manager::InsecureType::kLeaked,
                                      password_manager::InsecurityMetadata()});
  password_store->AddLogin(to_be_fixed);
  password_store->AddLogin(password_form_);
  SetupPendingPassword();
  GetController()->SavePassword(password_form_.username_value,
                                password_form_.password_value);
  GetController()->OnBubbleHidden();
  PasswordManagerBrowserTestBase::WaitForPasswordStore(browser());

  EXPECT_EQ(GetController()->GetState(),
            password_manager::ui::PASSWORD_UPDATED_MORE_TO_FIX);
}

void ManagePasswordsTest::SetupMovingPasswords() {
  auto form_manager = std::make_unique<
      testing::NiceMock<password_manager::MockPasswordFormManagerForUI>>();
  password_manager::MockPasswordFormManagerForUI* form_manager_ptr =
      form_manager.get();
  std::vector<password_manager::PasswordForm> best_matches = {*test_form()};
  EXPECT_CALL(*form_manager, GetBestMatches).WillOnce(Return(best_matches));
  ON_CALL(*form_manager, GetPendingCredentials)
      .WillByDefault(ReturnRef(*test_form()));
  ON_CALL(*form_manager, GetFederatedMatches)
      .WillByDefault(Return(std::vector<password_manager::PasswordForm>{}));
  ON_CALL(*form_manager, GetURL).WillByDefault(ReturnRef(test_form()->url));
  GetController()->OnShowMoveToAccountBubble(std::move(form_manager));
  // Clearing the mock here ensures that |GetBestMatches| won't be called with a
  // reference to |best_matches|.
  testing::Mock::VerifyAndClear(form_manager_ptr);
}

void ManagePasswordsTest::ConfigurePasswordSync(
    SyncConfiguration configuration) {
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(browser()->profile());
  syncer::TestSyncService* sync_service = static_cast<syncer::TestSyncService*>(
      SyncServiceFactory::GetForProfile(browser()->profile()));
  switch (configuration) {
    case SyncConfiguration::kNotSyncing: {
      sync_service->SetSignedOut();
      break;
    }
    case SyncConfiguration::kAccountStorageOnly:
    case SyncConfiguration::kSyncing: {
      auto consent_level = configuration == SyncConfiguration::kSyncing
                               ? signin::ConsentLevel::kSync
                               : signin::ConsentLevel::kSignin;
      AccountInfo info = signin::MakePrimaryAccountAvailable(
          identity_manager, "test@email.com", consent_level);
      sync_service->SetSignedIn(consent_level, info);
      break;
    }
  }
}

std::unique_ptr<base::HistogramSamples> ManagePasswordsTest::GetSamples(
    const char* histogram) {
  // Ensure that everything has been properly recorded before pulling samples.
  content::RunAllPendingInMessageLoop();
  return histogram_tester_.GetHistogramSamplesSinceCreation(histogram);
}

ManagePasswordsUIController* ManagePasswordsTest::GetController() {
  return ManagePasswordsUIController::FromWebContents(
      browser()->tab_strip_model()->GetActiveWebContents());
}

std::unique_ptr<PasswordFormManager> ManagePasswordsTest::CreateFormManager(
    password_manager::PasswordStoreInterface* profile_store,
    password_manager::PasswordStoreInterface* account_store) {
  autofill::FormData observed_form;
  observed_form.set_url(password_form_.url);
  autofill::FormFieldData field;
  field.set_form_control_type(autofill::FormControlType::kInputText);
  test_api(observed_form).Append(field);
  field.set_form_control_type(autofill::FormControlType::kInputPassword);
  test_api(observed_form).Append(field);

  std::unique_ptr<password_manager::FormSaver> form_saver;
  if (profile_store) {
    form_saver =
        std::make_unique<password_manager::FormSaverImpl>(profile_store);
  } else {
    form_saver = std::make_unique<password_manager::StubFormSaver>();
  }

  auto form_manager = std::make_unique<PasswordFormManager>(
      &client_, driver_.AsWeakPtr(), observed_form, &fetcher_,
      std::make_unique<password_manager::PasswordSaveManagerImpl>(
          /*profile_form_saver=*/std::move(form_saver),
          /*account_form_saver=*/account_store
              ? std::make_unique<password_manager::FormSaverImpl>(account_store)
              : nullptr),
      /*metrics_recorder=*/nullptr);

  insecure_credential_ = password_form_;
  insecure_credential_.password_issues.insert(
      {password_manager::InsecureType::kLeaked,
       password_manager::InsecurityMetadata(
           base::Time(), password_manager::IsMuted(false),
           password_manager::TriggerBackendNotification(false))});
  fetcher_.set_insecure_credentials({insecure_credential_});

  fetcher_.NotifyFetchCompleted();

  autofill::FormData submitted_form = observed_form;
  test_api(submitted_form).field(1).set_value(u"new_password");
  form_manager->ProvisionallySave(
      submitted_form, &driver_,
      base::LRUCache<PossibleUsernameFieldIdentifier, PossibleUsernameData>(
          /*max_size=*/2));

  return form_manager;
}