File: two_client_passwords_sync_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 (675 lines) | stat: -rw-r--r-- 27,302 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include <limits>
#include <tuple>

#include "base/hash/hash.h"
#include "base/location.h"
#include "base/rand_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "chrome/browser/sync/test/integration/encryption_helper.h"
#include "chrome/browser/sync/test/integration/passwords_helper.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/updated_progress_marker_checker.h"
#include "components/password_manager/core/browser/features/password_manager_features_util.h"
#include "components/password_manager/core/browser/password_form.h"
#include "components/password_manager/core/browser/password_manager_test_utils.h"
#include "components/password_manager/core/browser/password_store/password_store_interface.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/engine/cycle/entity_change_metric_recording.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_user_settings.h"
#include "components/sync/test/fake_server_http_post_provider.h"
#include "content/public/test/browser_test.h"
#include "net/base/network_change_notifier.h"

using passwords_helper::AllProfilesContainSamePasswordForms;
using passwords_helper::AllProfilesContainSamePasswordFormsAsVerifier;
using passwords_helper::CreateTestPasswordForm;
using passwords_helper::GetAccountPasswordStoreInterface;
using passwords_helper::GetAllLogins;
using passwords_helper::GetLogins;
using passwords_helper::GetPasswordCount;
using passwords_helper::GetProfilePasswordStoreInterface;
using passwords_helper::GetVerifierPasswordCount;
using passwords_helper::GetVerifierProfilePasswordStoreInterface;
using passwords_helper::RemoveLogins;

using password_manager::InsecureType;
using password_manager::InsecurityMetadata;
using password_manager::IsMuted;
using password_manager::PasswordForm;
using password_manager::TriggerBackendNotification;

using testing::ElementsAre;
using testing::Pointee;
using testing::UnorderedElementsAre;

static const char* kValidPassphrase = "passphrase!";

class TwoClientPasswordsSyncTest : public SyncTest {
 public:
  TwoClientPasswordsSyncTest() : SyncTest(TWO_CLIENT) {}

  ~TwoClientPasswordsSyncTest() override = default;
};

class TwoClientPasswordsSyncTestWithVerifier
    : public TwoClientPasswordsSyncTest {
 public:
  TwoClientPasswordsSyncTestWithVerifier() = default;
  ~TwoClientPasswordsSyncTestWithVerifier() override = default;

  bool UseVerifier() override {
    // TODO(crbug.com/40152785): rewrite tests to not use verifier.
    return true;
  }
};

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, E2E_ENABLED(Add)) {
  ASSERT_TRUE(ResetSyncForPrimaryAccount());
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());

  PasswordForm form = CreateTestPasswordForm(0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form);
  ASSERT_EQ(1, GetPasswordCount(0));

  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  ASSERT_EQ(1, GetPasswordCount(1));
}

// TwoClientPasswordsSyncTest.AddInTransportMode is disabled on CrOS as the
// signed in, non-syncing state does not exist
#if !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
                       E2E_ENABLED(AddInTransportMode)) {
  ASSERT_TRUE(ResetSyncForPrimaryAccount());
  ASSERT_TRUE(SetupClients());

  // Sign in on all clients without enabling Sync-the-feature.
  for (int i = 0; i < num_clients(); i++) {
    ASSERT_TRUE(GetClient(i)->SignInPrimaryAccount());
    ASSERT_TRUE(GetClient(i)->AwaitSyncTransportActive());
    ASSERT_FALSE(GetSyncService(i)->IsSyncFeatureEnabled());
    PasswordSyncActiveChecker(GetSyncService(i)).Wait();
  }

  ASSERT_TRUE(
      SamePasswordFormsChecker(PasswordForm::Store::kAccountStore).Wait());

  // Create an account password on the first client.
  PasswordForm form = CreateTestPasswordForm(0);
  GetAccountPasswordStoreInterface(0)->AddLogin(form);
  ASSERT_EQ(1, GetPasswordCount(0, PasswordForm::Store::kAccountStore));

  // The second client should receive the password in its own account store.
  EXPECT_TRUE(
      SamePasswordFormsChecker(PasswordForm::Store::kAccountStore).Wait());
  EXPECT_EQ(1, GetPasswordCount(1, PasswordForm::Store::kAccountStore));
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, E2E_ENABLED(Race)) {
  ASSERT_TRUE(ResetSyncForPrimaryAccount());
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordForms());

  PasswordForm form0 = CreateTestPasswordForm(0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form0);

  PasswordForm form1 = form0;
  form1.password_value = u"new_password";
  GetProfilePasswordStoreInterface(1)->AddLogin(form1);

  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, MergeWithTheMostRecent) {
  // Setup the test to have Form 0 and Form 1 added on both clients. Form 0 is
  // more recent on Client 0, and Form 1 is more recent on Client 1. They should
  // be merged such that recent passwords are chosen.

  base::Time now = base::Time::Now();
  base::Time yesterday = now - base::Days(1);

  PasswordForm form0_recent = CreateTestPasswordForm(0);
  form0_recent.date_created = now;
  PasswordForm form0_old = CreateTestPasswordForm(0);
  form0_old.date_created = yesterday;

  PasswordForm form1_recent = CreateTestPasswordForm(1);
  form1_recent.date_created = now;
  PasswordForm form1_old = CreateTestPasswordForm(1);
  form1_old.date_created = yesterday;

  ASSERT_TRUE(SetupClients());

  // Add the passwords to Client 0.
  GetProfilePasswordStoreInterface(0)->AddLogin(form0_recent);
  GetProfilePasswordStoreInterface(0)->AddLogin(form1_old);
  // Enable sync on Client 0 and wait until they are committed.
  ASSERT_TRUE(GetClient(0)->SetupSync());
  ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());

  // Add the passwords to Client 1.
  GetProfilePasswordStoreInterface(1)->AddLogin(form0_old);
  GetProfilePasswordStoreInterface(1)->AddLogin(form1_recent);

  // Enable sync on Client 1 and wait until all passwords are merged.
  ASSERT_TRUE(GetClient(1)->SetupSync());
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());

  // There should be only 2 passwords.
  EXPECT_EQ(2, GetPasswordCount(0));
  // All passwords should be the recent ones.
  for (const std::unique_ptr<PasswordForm>& form :
       GetLogins(GetProfilePasswordStoreInterface(0))) {
    EXPECT_EQ(now, form->date_created);
  }
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
                       E2E_ENABLED(SetPassphraseAndAddPassword)) {
  ASSERT_TRUE(ResetSyncForPrimaryAccount());
  ASSERT_TRUE(SetupSync());

  GetSyncService(0)->GetUserSettings()->SetEncryptionPassphrase(
      kValidPassphrase);
  ASSERT_TRUE(PassphraseAcceptedChecker(GetSyncService(0)).Wait());

  ASSERT_TRUE(PassphraseRequiredChecker(GetSyncService(1)).Wait());
  ASSERT_TRUE(GetSyncService(1)->GetUserSettings()->SetDecryptionPassphrase(
      kValidPassphrase));
  ASSERT_TRUE(PassphraseAcceptedChecker(GetSyncService(1)).Wait());

  PasswordForm form = CreateTestPasswordForm(0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form);
  ASSERT_EQ(1, GetPasswordCount(0));

  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTestWithVerifier, Update) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());

  PasswordForm form = CreateTestPasswordForm(0);
  GetVerifierProfilePasswordStoreInterface()->AddLogin(form);
  GetProfilePasswordStoreInterface(0)->AddLogin(form);

  // Wait for client 0 to commit and client 1 to receive the update.
  ASSERT_TRUE(SamePasswordFormsAsVerifierChecker(1).Wait());

  form.password_value = u"new_password";
  GetVerifierProfilePasswordStoreInterface()->UpdateLogin(form);
  GetProfilePasswordStoreInterface(1)->UpdateLogin(form);
  ASSERT_EQ(1, GetVerifierPasswordCount());

  // Wait for client 1 to commit and client 0 to receive the update.
  ASSERT_TRUE(SamePasswordFormsAsVerifierChecker(0).Wait());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTestWithVerifier,
                       SharedPasswordMetadataAreSynced) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());

  PasswordForm form = CreateTestPasswordForm(0);
  form.sender_email = u"sender@example.com";
  form.sender_name = u"Sender Name";
  form.sender_profile_image_url = GURL("http://www.sender.com/profile_image");
  form.date_received = form.date_created;
  form.sharing_notification_displayed = true;
  GetVerifierProfilePasswordStoreInterface()->AddLogin(form);
  GetProfilePasswordStoreInterface(0)->AddLogin(form);

  // Wait for client 0 to commit and client 1 to receive the update.
  ASSERT_TRUE(SamePasswordFormsAsVerifierChecker(1).Wait());

  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, AddTwice) {
  // Password store supports adding the same form twice, so this is testing this
  // behaviour.
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordForms());

  PasswordForm form = CreateTestPasswordForm(0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form);
  ASSERT_EQ(1, GetPasswordCount(0));

  // Wait for client 0 to commit and client 1 to receive the update.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  ASSERT_EQ(1, GetPasswordCount(1));

  // Update the password and add it again to client 0.
  form.password_value = u"new_password";
  GetProfilePasswordStoreInterface(0)->AddLogin(form);
  ASSERT_EQ(1, GetPasswordCount(0));

  // Wait for client 1 to receive the update.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  ASSERT_EQ(1, GetPasswordCount(1));
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTestWithVerifier, Delete) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());

  PasswordForm form0 = CreateTestPasswordForm(0);
  GetVerifierProfilePasswordStoreInterface()->AddLogin(form0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form0);
  PasswordForm form1 = CreateTestPasswordForm(1);
  GetVerifierProfilePasswordStoreInterface()->AddLogin(form1);
  GetProfilePasswordStoreInterface(0)->AddLogin(form1);

  // Wait for client 0 to commit and client 1 to receive the update.
  ASSERT_TRUE(SamePasswordFormsAsVerifierChecker(1).Wait());

  GetProfilePasswordStoreInterface(1)->RemoveLogin(FROM_HERE, form0);
  GetVerifierProfilePasswordStoreInterface()->RemoveLogin(FROM_HERE, form0);
  ASSERT_EQ(1, GetVerifierPasswordCount());

  // Wait for deletion from client 1 to propagate.
  ASSERT_TRUE(SamePasswordFormsAsVerifierChecker(0).Wait());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
                       SetPassphraseAndThenSetupSync) {
  ASSERT_TRUE(SetupClients());

  ASSERT_TRUE(GetClient(0)->SetupSync());
  GetSyncService(0)->GetUserSettings()->SetEncryptionPassphrase(
      kValidPassphrase);
  ASSERT_TRUE(PassphraseAcceptedChecker(GetSyncService(0)).Wait());
  // Wait for the client to commit the updates.
  ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());

  // When client 1 hits a passphrase required state, we can infer that
  // client 0's passphrase has been committed. to the server.
  ASSERT_TRUE(GetClient(1)->SetupSyncNoWaitForCompletion());
  ASSERT_TRUE(PassphraseRequiredChecker(GetSyncService(1)).Wait());

  // Get client 1 out of the passphrase required state.
  ASSERT_TRUE(GetSyncService(1)->GetUserSettings()->SetDecryptionPassphrase(
      kValidPassphrase));
  ASSERT_TRUE(PassphraseAcceptedChecker(GetSyncService(1)).Wait());

  // We must mark the setup complete now, since we just entered the passphrase
  // and the previous SetupSync() call failed.
  GetClient(1)->FinishSyncSetup();

  // Move around some passwords to make sure it's all working.
  PasswordForm form0 = CreateTestPasswordForm(0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form0);

  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, E2E_ONLY(DeleteTwo)) {
  ASSERT_TRUE(ResetSyncForPrimaryAccount());
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordForms());

  PasswordForm form0 = CreateTestPasswordForm(
      base::FastHash(base::Uuid::GenerateRandomV4().AsLowercaseString()));
  PasswordForm form1 = CreateTestPasswordForm(
      base::FastHash(base::Uuid::GenerateRandomV4().AsLowercaseString()));
  GetProfilePasswordStoreInterface(0)->AddLogin(form0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form1);

  const int init_password_count = GetPasswordCount(0);

  // Wait for client 0 to commit and client 1 to receive the update.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  ASSERT_EQ(init_password_count, GetPasswordCount(1));

  GetProfilePasswordStoreInterface(1)->RemoveLogin(FROM_HERE, form0);

  // Wait for deletion from client 1 to propagate.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  ASSERT_EQ(init_password_count - 1, GetPasswordCount(0));

  GetProfilePasswordStoreInterface(1)->RemoveLogin(FROM_HERE, form1);

  // Wait for deletion from client 1 to propagate.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  ASSERT_EQ(init_password_count - 2, GetPasswordCount(0));
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTestWithVerifier, DeleteAll) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());

  PasswordForm form0 = CreateTestPasswordForm(0);
  GetVerifierProfilePasswordStoreInterface()->AddLogin(form0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form0);
  PasswordForm form1 = CreateTestPasswordForm(1);
  GetVerifierProfilePasswordStoreInterface()->AddLogin(form1);
  GetProfilePasswordStoreInterface(0)->AddLogin(form1);
  ASSERT_TRUE(SamePasswordFormsAsVerifierChecker(1).Wait());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());

  RemoveLogins(GetProfilePasswordStoreInterface(1));
  RemoveLogins(GetVerifierProfilePasswordStoreInterface());
  ASSERT_TRUE(SamePasswordFormsAsVerifierChecker(0).Wait());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
  ASSERT_EQ(0, GetVerifierPasswordCount());
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, E2E_ENABLED(Merge)) {
  ASSERT_TRUE(ResetSyncForPrimaryAccount());
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordForms());

  PasswordForm form0 = CreateTestPasswordForm(0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form0);
  PasswordForm form1 = CreateTestPasswordForm(1);
  GetProfilePasswordStoreInterface(1)->AddLogin(form1);
  PasswordForm form2 = CreateTestPasswordForm(2);
  GetProfilePasswordStoreInterface(1)->AddLogin(form2);

  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  ASSERT_EQ(3, GetPasswordCount(0));
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, E2E_ONLY(TwoClientAddPass)) {
  ASSERT_TRUE(ResetSyncForPrimaryAccount());
  ASSERT_TRUE(SetupSync());
  // All profiles should sync same passwords.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait())
      << "Initial password forms did not match for all profiles";
  const int init_password_count = GetPasswordCount(0);

  // Add one new password per profile. A unique form is created for each to
  // prevent them from overwriting each other.
  for (int i = 0; i < num_clients(); ++i) {
    GetProfilePasswordStoreInterface(i)->AddLogin(CreateTestPasswordForm(
        base::RandInt(0, std::numeric_limits<int32_t>::max())));
  }

  // Blocks and waits for password forms in all profiles to match.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());

  // Check that total number of passwords is as expected.
  for (int i = 0; i < num_clients(); ++i) {
    ASSERT_EQ(GetPasswordCount(i), init_password_count + num_clients())
        << "Total password count is wrong.";
  }
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTestWithVerifier,
                       AddImmediatelyAfterDelete) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
  base::HistogramTester histogram_tester;

  PasswordForm form0 = CreateTestPasswordForm(0);
  GetVerifierProfilePasswordStoreInterface()->AddLogin(form0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form0);

  ASSERT_TRUE(SamePasswordFormsAsVerifierChecker(1).Wait());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());

  PasswordForm form1 = CreateTestPasswordForm(1);
  GetVerifierProfilePasswordStoreInterface()->UpdateLoginWithPrimaryKey(form1,
                                                                        form0);
  GetProfilePasswordStoreInterface(0)->UpdateLoginWithPrimaryKey(form1, form0);

  ASSERT_TRUE(SamePasswordFormsAsVerifierChecker(1).Wait());
  ASSERT_TRUE(AllProfilesContainSamePasswordFormsAsVerifier());
  // There should be only one deletion. This is to test the bug
  // (crbug.com/1046309) where the USS client was local deletions when receiving
  // remote deletions.
  EXPECT_EQ(1, histogram_tester.GetBucketCount(
                   "Sync.DataTypeEntityChange.PASSWORD",
                   syncer::DataTypeEntityChange::kLocalDeletion));
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
                       MergeFormsWithInsecureCredentials) {
  // Setup the test to have Form 0 on Client 0 and Form 1 on Client 1. Both
  // Forms has associated insecure credentials. After sync, both clients should
  // have both forms with their corresponding insecure credentials.

  PasswordForm form0 = CreateTestPasswordForm(0);
  PasswordForm form1 = CreateTestPasswordForm(1);

  form0.password_issues.insert(
      {InsecureType::kLeaked,
       InsecurityMetadata(base::Time(), IsMuted(false),
                          TriggerBackendNotification(false))});
  form1.password_issues.insert(
      {InsecureType::kPhished,
       InsecurityMetadata(base::Time(), IsMuted(false),
                          TriggerBackendNotification(false))});

  ASSERT_TRUE(SetupClients());

  // Add the passwords and security issues to Client 0.
  GetProfilePasswordStoreInterface(0)->AddLogin(form0);

  // Enable sync on Client 0 and wait until they are committed.
  ASSERT_TRUE(GetClient(0)->SetupSync());
  ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());

  // Add the passwords and security issues to Client 1.
  GetProfilePasswordStoreInterface(1)->AddLogin(form1);

  // Enable sync on Client 1 and wait until all passwords are merged.
  ASSERT_TRUE(GetClient(1)->SetupSync());
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());

  EXPECT_THAT(GetAllLogins(GetProfilePasswordStoreInterface(0)),
              UnorderedElementsAre(
                  Pointee(password_manager::HasPrimaryKeyAndEquals(form0)),
                  Pointee(password_manager::HasPrimaryKeyAndEquals(form1))));
  EXPECT_THAT(GetAllLogins(GetProfilePasswordStoreInterface(1)),
              UnorderedElementsAre(
                  Pointee(password_manager::HasPrimaryKeyAndEquals(form0)),
                  Pointee(password_manager::HasPrimaryKeyAndEquals(form1))));
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
                       AddFormWithInsecureCredentials) {
  // Tests that newly added form with security issues is successfully synced.
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordForms());

  PasswordForm form = CreateTestPasswordForm(0);
  form.password_issues.insert(
      {InsecureType::kLeaked,
       InsecurityMetadata(base::Time(), IsMuted(false),
                          TriggerBackendNotification(false))});
  form.password_issues.insert(
      {InsecureType::kPhished,
       InsecurityMetadata(base::Time(), IsMuted(false),
                          TriggerBackendNotification(false))});

  // Add the form and security issues to Client 0.
  GetProfilePasswordStoreInterface(0)->AddLogin(form);

  // Wait until Client 1 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  EXPECT_THAT(
      GetAllLogins(GetProfilePasswordStoreInterface(1)),
      ElementsAre(Pointee(password_manager::HasPrimaryKeyAndEquals(form))));
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest, RemoveInsecureCredentialss) {
  // Tests that removing security issues are successfully synced.
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordForms());

  PasswordForm form0 = CreateTestPasswordForm(0);
  PasswordForm form1 = CreateTestPasswordForm(1);

  form0.password_issues.insert(
      {InsecureType::kLeaked,
       InsecurityMetadata(base::Time(), IsMuted(false),
                          TriggerBackendNotification(false))});
  form1.password_issues.insert(
      {InsecureType::kPhished,
       InsecurityMetadata(base::Time(), IsMuted(false),
                          TriggerBackendNotification(false))});

  // Add the form and security issues to Client 0.
  GetProfilePasswordStoreInterface(0)->AddLogin(form0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form1);

  // Wait until Client 1 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  EXPECT_THAT(GetAllLogins(GetProfilePasswordStoreInterface(1)),
              UnorderedElementsAre(
                  Pointee(password_manager::HasPrimaryKeyAndEquals(form0)),
                  Pointee(password_manager::HasPrimaryKeyAndEquals(form1))));

  // Remove security issues on Client 1.
  form0.password_issues.clear();
  GetProfilePasswordStoreInterface(1)->UpdateLogin(form0);

  // Wait until Client 0 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  EXPECT_THAT(GetAllLogins(GetProfilePasswordStoreInterface(1)),
              UnorderedElementsAre(
                  Pointee(password_manager::HasPrimaryKeyAndEquals(form0)),
                  Pointee(password_manager::HasPrimaryKeyAndEquals(form1))));
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
                       InsecureCredentialUpdateMute) {
  // Tests that updating security issues are successfully synced.
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordForms());

  PasswordForm form = CreateTestPasswordForm(0);
  form.password_issues.insert(
      {InsecureType::kLeaked,
       InsecurityMetadata(base::Time(), IsMuted(false),
                          TriggerBackendNotification(false))});

  // Add the form and security issue to Client 0.
  GetProfilePasswordStoreInterface(0)->AddLogin(form);

  // Wait until Client 1 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());

  // Update is_muted field on Client 0.
  form.password_issues.at(InsecureType::kLeaked).is_muted = IsMuted(true);
  GetProfilePasswordStoreInterface(0)->UpdateLogin(form);

  // Wait until Client 1 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  EXPECT_THAT(
      GetAllLogins(GetProfilePasswordStoreInterface(1)),
      ElementsAre(Pointee(password_manager::HasPrimaryKeyAndEquals(form))));
}

// Regression test for crbug.com/1346576.
IN_PROC_BROWSER_TEST_F(
    TwoClientPasswordsSyncTest,
    MatchingDeletionsConflictDoesNotInvokeTrimmingEntitySpecifics) {
  // Add a password and wait until it is synced on both clients.
  ASSERT_TRUE(SetupSync());
  PasswordForm form = CreateTestPasswordForm(0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form);
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  ASSERT_EQ(GetPasswordCount(0), 1);

  // Simulate going offline on both clients.
  fake_server::FakeServerHttpPostProvider::DisableNetwork();

  // Remove the password from both clients to simulate a conflict with matching
  // remote and local deletion after Client 1 comes back online.
  GetProfilePasswordStoreInterface(0)->RemoveLogin(FROM_HERE, form);
  GetProfilePasswordStoreInterface(1)->RemoveLogin(FROM_HERE, form);

  // Simulate going online again.
  fake_server::FakeServerHttpPostProvider::EnableNetwork();
  net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
      net::NetworkChangeNotifier::CONNECTION_ETHERNET);

  // Checks that the client does not crash due to trimming entity specifics for
  // caching for a deleted entity (without a password field).
  ASSERT_TRUE(AwaitQuiescence());
}

IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
                       SyncPasswordNotesBetweenDevices) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordForms());

  // Add a password with note to Client 0.
  PasswordForm form = CreateTestPasswordForm(0);
  form.notes.emplace_back(
      /*unique_display_name=*/u"My Phone Pin", /*value=*/u"123456",
      /*date_created=*/base::Time::Now(), /*hide_by_default=*/true);
  GetProfilePasswordStoreInterface(0)->AddLogin(form);

  // Wait until Client 1 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  EXPECT_THAT(
      GetAllLogins(GetProfilePasswordStoreInterface(1)),
      ElementsAre(Pointee(password_manager::HasPrimaryKeyAndEquals(form))));

  // Update the note in Client 1.
  form.notes[0].value = u"78910";
  GetProfilePasswordStoreInterface(1)->UpdateLogin(form);

  // Wait until Client 0 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  EXPECT_THAT(
      GetAllLogins(GetProfilePasswordStoreInterface(0)),
      ElementsAre(Pointee(password_manager::HasPrimaryKeyAndEquals(form))));

  // Remove all notes on Client 0.
  form.notes.clear();
  GetProfilePasswordStoreInterface(0)->UpdateLogin(form);

  // Wait until Client 1 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  EXPECT_THAT(
      GetAllLogins(GetProfilePasswordStoreInterface(1)),
      ElementsAre(Pointee(password_manager::HasPrimaryKeyAndEquals(form))));
}

// This tests the  logic for reading and writing the notes backup blob when
// notes are empty.
IN_PROC_BROWSER_TEST_F(TwoClientPasswordsSyncTest,
                       SyncPasswordWithEmptyNotesBetweenDevices) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(AllProfilesContainSamePasswordForms());

  // Add a password with note to Client 0.
  PasswordForm form = CreateTestPasswordForm(0);
  GetProfilePasswordStoreInterface(0)->AddLogin(form);

  // Wait until Client 1 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  EXPECT_THAT(
      GetAllLogins(GetProfilePasswordStoreInterface(1)),
      ElementsAre(Pointee(password_manager::HasPrimaryKeyAndEquals(form))));

  // Update the password in Client 1.
  form.password_value = u"new_password";
  GetProfilePasswordStoreInterface(1)->UpdateLogin(form);

  // Wait until Client 0 picks up changes.
  ASSERT_TRUE(SamePasswordFormsChecker().Wait());
  EXPECT_THAT(
      GetAllLogins(GetProfilePasswordStoreInterface(0)),
      ElementsAre(Pointee(password_manager::HasPrimaryKeyAndEquals(form))));
}