Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / password_generator_unittest.cc
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
10 namespace autofill {
11
12 TEST(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
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;
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
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]));
57   }
58 }
59
60 }  // namespace autofill