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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <locale>
#include "components/autofill/core/browser/password_generator.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
void CheckPasswordCorrectness(const std::string& password) {
int num_upper_case_letters = 0;
int num_lower_case_letters = 0;
int num_digits = 0;
for (size_t i = 0; i < password.size(); i++) {
if (isupper(password[i]))
++num_upper_case_letters;
else if (islower(password[i]))
++num_lower_case_letters;
else if (isdigit(password[i]))
++num_digits;
}
EXPECT_GT(num_upper_case_letters, 0) << password;
EXPECT_GT(num_lower_case_letters, 0) << password;
EXPECT_GT(num_digits, 0) << password;
}
} // namespace
namespace autofill {
TEST(PasswordGeneratorTest, PasswordLength) {
PasswordGenerator pg1(10);
std::string password = pg1.Generate();
EXPECT_EQ(password.size(), 10u);
PasswordGenerator pg2(-1);
password = pg2.Generate();
EXPECT_EQ(password.size(),
static_cast<size_t>(PasswordGenerator::kDefaultPasswordLength));
PasswordGenerator pg3(100);
password = pg3.Generate();
EXPECT_EQ(password.size(),
static_cast<size_t>(PasswordGenerator::kDefaultPasswordLength));
}
TEST(PasswordGeneratorTest, PasswordPattern) {
PasswordGenerator pg(12);
std::string password = pg.Generate();
CheckPasswordCorrectness(password);
}
TEST(PasswordGeneratorTest, Printable) {
PasswordGenerator pg(12);
std::string password = pg.Generate();
for (size_t i = 0; i < password.size(); i++) {
// Make sure that the character is printable.
EXPECT_TRUE(isgraph(password[i]));
}
}
TEST(PasswordGeneratorTest, ForceFixPasswordTest) {
std::string passwords_to_fix[] = {"nonumbersoruppercase",
"nonumbersWithuppercase",
"numbers3Anduppercase", "UmpAwgemHoc"};
for (auto& password : passwords_to_fix) {
ForceFixPassword(&password);
CheckPasswordCorrectness(password);
}
}
} // namespace autofill
|