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