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(),
20 static_cast<size_t>(PasswordGenerator::kDefaultPasswordLength));
22 PasswordGenerator pg3(100);
23 password = pg3.Generate();
24 EXPECT_EQ(password.size(),
25 static_cast<size_t>(PasswordGenerator::kDefaultPasswordLength));
28 TEST(PasswordGeneratorTest, PasswordPattern) {
29 PasswordGenerator pg(12);
30 std::string password = pg.Generate();
31 int num_upper_case_letters = 0;
32 int num_lower_case_letters = 0;
34 int num_other_symbols = 0;
35 for (size_t i = 0; i < password.size(); i++) {
36 if (isupper(password[i]))
37 ++num_upper_case_letters;
38 else if (islower(password[i]))
39 ++num_lower_case_letters;
40 else if (isdigit(password[i]))
45 EXPECT_GT(num_upper_case_letters, 0);
46 EXPECT_GT(num_lower_case_letters, 0);
47 EXPECT_GT(num_digits, 0);
48 EXPECT_EQ(num_other_symbols, 1);
51 TEST(PasswordGeneratorTest, Printable) {
52 PasswordGenerator pg(12);
53 std::string password = pg.Generate();
54 for (size_t i = 0; i < password.size(); i++) {
55 // Make sure that the character is printable.
56 EXPECT_TRUE(isgraph(password[i]));
60 } // namespace autofill