Home | History | Annotate | Download | only in browser
      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 "base/logging.h"
      8 #include "components/autofill/core/browser/password_generator.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace autofill {
     12 
     13 TEST(PasswordGeneratorTest, PasswordLength) {
     14   PasswordGenerator pg1(10);
     15   std::string password = pg1.Generate();
     16   EXPECT_EQ(password.size(), 10u);
     17 
     18   PasswordGenerator pg2(-1);
     19   password = pg2.Generate();
     20   EXPECT_EQ(password.size(),
     21             static_cast<size_t>(PasswordGenerator::kDefaultPasswordLength));
     22 
     23   PasswordGenerator pg3(100);
     24   password = pg3.Generate();
     25   EXPECT_EQ(password.size(),
     26             static_cast<size_t>(PasswordGenerator::kDefaultPasswordLength));
     27 }
     28 
     29 TEST(PasswordGeneratorTest, PasswordPattern) {
     30   PasswordGenerator pg(12);
     31   std::string password = pg.Generate();
     32   int num_upper_case_letters = 0;
     33   int num_lower_case_letters = 0;
     34   int num_digits = 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   }
     43   EXPECT_GT(num_upper_case_letters, 0) << password;
     44   EXPECT_GT(num_lower_case_letters, 0) << password;
     45   EXPECT_GT(num_digits, 0) << password;
     46 }
     47 
     48 TEST(PasswordGeneratorTest, Printable) {
     49   PasswordGenerator pg(12);
     50   std::string password = pg.Generate();
     51   for (size_t i = 0; i < password.size(); i++) {
     52     // Make sure that the character is printable.
     53     EXPECT_TRUE(isgraph(password[i]));
     54   }
     55 }
     56 
     57 }  // namespace autofill
     58