Home | History | Annotate | Download | only in password_manager
      1 // Copyright (c) 2010 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 "chrome/browser/keychain_mock_mac.h"
      6 #include "chrome/browser/password_manager/encryptor_password_mac.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace {
     10 
     11 // Test that if we have an existing password in the Keychain and we are
     12 // authorized by the user to read it then we get it back correctly.
     13 TEST(EncryptorPasswordTest, FindPasswordSuccess) {
     14   MockKeychain keychain(0);
     15   keychain.set_find_generic_result(noErr);
     16   EncryptorPassword password(keychain);
     17   EXPECT_FALSE(password.GetEncryptorPassword().empty());
     18   EXPECT_FALSE(keychain.called_add_generic());
     19   EXPECT_EQ(0, keychain.password_data_count());
     20 }
     21 
     22 // Test that if we do not have an existing password in the Keychain then it
     23 // gets added successfully and returned.
     24 TEST(EncryptorPasswordTest, FindPasswordNotFound) {
     25   MockKeychain keychain(0);
     26   keychain.set_find_generic_result(errSecItemNotFound);
     27   EncryptorPassword password(keychain);
     28   EXPECT_FALSE(password.GetEncryptorPassword().empty());
     29   EXPECT_TRUE(keychain.called_add_generic());
     30   EXPECT_EQ(0, keychain.password_data_count());
     31 }
     32 
     33 // Test that if get denied access by the user then we return an empty password.
     34 // And we should not try to add one.
     35 TEST(EncryptorPasswordTest, FindPasswordNotAuthorized) {
     36   MockKeychain keychain(0);
     37   keychain.set_find_generic_result(errSecAuthFailed);
     38   EncryptorPassword password(keychain);
     39   EXPECT_TRUE(password.GetEncryptorPassword().empty());
     40   EXPECT_FALSE(keychain.called_add_generic());
     41   EXPECT_EQ(0, keychain.password_data_count());
     42 }
     43 
     44 // Test that if some random other error happens then we return an empty
     45 // password, and we should not try to add one.
     46 TEST(EncryptorPasswordTest, FindPasswordOtherError) {
     47   MockKeychain keychain(0);
     48   keychain.set_find_generic_result(errSecNotAvailable);
     49   EncryptorPassword password(keychain);
     50   EXPECT_TRUE(password.GetEncryptorPassword().empty());
     51   EXPECT_FALSE(keychain.called_add_generic());
     52   EXPECT_EQ(0, keychain.password_data_count());
     53 }
     54 
     55 // Test that subsequent additions to the keychain give different passwords.
     56 TEST(EncryptorPasswordTest, PasswordsDiffer) {
     57   MockKeychain keychain1(0);
     58   keychain1.set_find_generic_result(errSecItemNotFound);
     59   EncryptorPassword encryptor_password1(keychain1);
     60   std::string password1 = encryptor_password1.GetEncryptorPassword();
     61   EXPECT_FALSE(password1.empty());
     62   EXPECT_TRUE(keychain1.called_add_generic());
     63   EXPECT_EQ(0, keychain1.password_data_count());
     64 
     65   MockKeychain keychain2(0);
     66   keychain2.set_find_generic_result(errSecItemNotFound);
     67   EncryptorPassword encryptor_password2(keychain2);
     68   std::string password2 = encryptor_password2.GetEncryptorPassword();
     69   EXPECT_FALSE(password2.empty());
     70   EXPECT_TRUE(keychain2.called_add_generic());
     71   EXPECT_EQ(0, keychain2.password_data_count());
     72 
     73   // And finally check that the passwords are different.
     74   EXPECT_NE(password1, password2);
     75 }
     76 
     77 }  // namespace
     78