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.
7 #include "components/autofill/core/browser/password_generator.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 TEST(PasswordGeneratorTest, PasswordLength) {
13 PasswordGenerator pg1(10);
14 std::string password = pg1.Generate();
15 EXPECT_EQ(password.size(), 10u);
17 PasswordGenerator pg2(-1);
18 password = pg2.Generate();
19 EXPECT_EQ(password.size(), PasswordGenerator::kDefaultPasswordLength);
21 PasswordGenerator pg3(100);
22 password = pg3.Generate();
23 EXPECT_EQ(password.size(), PasswordGenerator::kDefaultPasswordLength);
26 TEST(PasswordGeneratorTest, PasswordPattern) {
27 PasswordGenerator pg(12);
28 std::string password = pg.Generate();
29 int num_upper_case_letters = 0;
30 int num_lower_case_letters = 0;
32 int num_other_symbols = 0;
33 for (size_t i = 0; i < password.size(); i++) {
34 if (isupper(password[i]))
35 ++num_upper_case_letters;
36 else if (islower(password[i]))
37 ++num_lower_case_letters;
38 else if (isdigit(password[i]))
43 EXPECT_GT(num_upper_case_letters, 0);
44 EXPECT_GT(num_lower_case_letters, 0);
45 EXPECT_GT(num_digits, 0);
46 EXPECT_EQ(num_other_symbols, 1);
49 TEST(PasswordGeneratorTest, Printable) {
50 PasswordGenerator pg(12);
51 std::string password = pg.Generate();
52 for (size_t i = 0; i < password.size(); i++) {
53 // Make sure that the character is printable.
54 EXPECT_TRUE(isgraph(password[i]));
58 } // namespace autofill