File: password_status_check_result_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 5,337 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
// Copyright 2023 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/safety_hub/password_status_check_result.h"

#include "base/json/values_util.h"
#include "chrome/browser/ui/safety_hub/safety_hub_constants.h"
#include "chrome/grit/generated_resources.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

constexpr char kOrigin1[] = "https://example1.com/";
constexpr char kOrigin2[] = "https://example2.com/";
constexpr char kUsername1[] = "user1";
constexpr char kUsername2[] = "user2";

void ValidateNotification(const auto& new_result,
                          const auto& old_result,
                          bool is_warrant,
                          int count = -1) {
  EXPECT_EQ(is_warrant,
            new_result->WarrantsNewMenuNotification(old_result->ToDictValue()));

  if (count != -1) {
    EXPECT_EQ(
        new_result->GetNotificationString(),
        l10n_util::GetPluralStringFUTF16(
            IDS_SETTINGS_SAFETY_HUB_COMPROMISED_PASSWORDS_MENU_NOTIFICATION,
            count));
  }
}

}  // namespace

TEST(PasswordStatusCheckResultTest, ResultToDict) {
  auto result = std::make_unique<PasswordStatusCheckResult>();
  result->AddToCompromisedPasswords(kOrigin1, kUsername1);
  EXPECT_THAT(result->GetCompromisedPasswords(),
              testing::ElementsAre(PasswordPair(kOrigin1, kUsername1)));

  // When converting to dict, the values of the password data should be
  // correctly converted to base::Value.
  base::Value::Dict dict = result->ToDictValue();
  auto* compromised_password_list =
      dict.FindList(safety_hub::kSafetyHubPasswordCheckOriginsKey);
  EXPECT_EQ(1U, compromised_password_list->size());
  base::Value::Dict& password_data =
      compromised_password_list->front().GetDict();
  EXPECT_EQ(kOrigin1, password_data.Find(safety_hub::kOrigin)->GetString());
  EXPECT_EQ(kUsername1, password_data.Find(safety_hub::kUsername)->GetString());
}

TEST(PasswordStatusCheckResultTest, ResultIsTrigger) {
  auto result = std::make_unique<PasswordStatusCheckResult>();

  // When there is no leaked password, then notification should not be
  // triggered.
  EXPECT_FALSE(result->IsTriggerForMenuNotification());

  // When there is a leaked password, then notification can be triggered.
  result->AddToCompromisedPasswords(kOrigin1, kUsername1);
  EXPECT_TRUE(result->IsTriggerForMenuNotification());
}

TEST(PasswordStatusCheckResultTest, ResultWarrantsNewNotification) {
  auto old_result = std::make_unique<PasswordStatusCheckResult>();
  auto new_result = std::make_unique<PasswordStatusCheckResult>();
  ValidateNotification(new_result, old_result, false);

  // kOrigin1 and kUsername1 is set in new, but not in old -> warrants
  // notification
  new_result->AddToCompromisedPasswords(kOrigin1, kUsername1);
  ValidateNotification(new_result, old_result, true, 1);
  // kOrigin1 and kUsername1  is in both new and old -> no notification
  old_result->AddToCompromisedPasswords(kOrigin1, kUsername1);
  ValidateNotification(new_result, old_result, false, 1);
  // kOrigin1 and kUsername1 is set again in new -> no notification
  new_result->AddToCompromisedPasswords(kOrigin1, kUsername1);
  ValidateNotification(new_result, old_result, false, 1);

  // kOrigin2 and kUsername1 is added in new, but not in old -> warrants
  // notification
  new_result->AddToCompromisedPasswords(kOrigin2, kUsername1);
  ValidateNotification(new_result, old_result, true, 2);
  // kOrigin2 and kUsername1 is also in both new and old -> no notification
  old_result->AddToCompromisedPasswords(kOrigin2, kUsername1);
  ValidateNotification(new_result, old_result, false, 2);

  // kOrigin2 and kUsername2 is added in new, but not in old -> warrants
  // notification
  new_result->AddToCompromisedPasswords(kOrigin2, kUsername2);
  ValidateNotification(new_result, old_result, true, 3);
  // kOrigin2 and kUsername2 is also in both new and old -> no notification
  old_result->AddToCompromisedPasswords(kOrigin2, kUsername2);
  ValidateNotification(new_result, old_result, false, 3);
}

TEST(PasswordStatusCheckResultTest,
     ResultWarrantsNewNotification_SameOriginDifferentUser) {
  auto old_result = std::make_unique<PasswordStatusCheckResult>();
  auto new_result = std::make_unique<PasswordStatusCheckResult>();
  EXPECT_FALSE(
      new_result->WarrantsNewMenuNotification(old_result->ToDictValue()));

  // kOrigin1 and kUsername1 is set in new, but not in old -> warrants
  // notification
  new_result->AddToCompromisedPasswords(kOrigin1, kUsername1);
  ValidateNotification(new_result, old_result, true, 1);

  // kOrigin1 and kUsername2  is in old, but not in new -> warrants notification
  old_result->AddToCompromisedPasswords(kOrigin1, kUsername2);
  ValidateNotification(new_result, old_result, true, 1);
}

TEST(PasswordStatusCheckResultTest, CloneResult) {
  auto result = std::make_unique<PasswordStatusCheckResult>();
  result->AddToCompromisedPasswords(kOrigin1, kUsername1);

  auto cloned_result = result->Clone();
  EXPECT_EQ(result->GetCompromisedPasswords(),
            static_cast<PasswordStatusCheckResult*>(cloned_result.get())
                ->GetCompromisedPasswords());
}