password_generator_unittest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#include <locale>
6
7#include "components/autofill/core/browser/password_generator.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace autofill {
11
12TEST(PasswordGeneratorTest, PasswordLength) {
13  PasswordGenerator pg1(10);
14  std::string password = pg1.Generate();
15  EXPECT_EQ(password.size(), 10u);
16
17  PasswordGenerator pg2(-1);
18  password = pg2.Generate();
19  EXPECT_EQ(password.size(),
20            static_cast<size_t>(PasswordGenerator::kDefaultPasswordLength));
21
22  PasswordGenerator pg3(100);
23  password = pg3.Generate();
24  EXPECT_EQ(password.size(),
25            static_cast<size_t>(PasswordGenerator::kDefaultPasswordLength));
26}
27
28TEST(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;
33  int num_digits = 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]))
41      ++num_digits;
42    else
43      ++num_other_symbols;
44  }
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);
49}
50
51TEST(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]));
57  }
58}
59
60}  // namespace autofill
61