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