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