OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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/password_manager/core/browser/login_database.h" | |
6 | |
7 #include <Security/Security.h> | |
8 | |
9 #include "base/files/scoped_temp_dir.h" | |
10 #include "base/mac/scoped_cftyperef.h" | |
11 #include "base/macros.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "components/autofill/core/common/password_form.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "testing/platform_test.h" | |
16 | |
17 using base::ScopedCFTypeRef; | |
vabr (Chromium)
2015/07/22 12:44:32
nit: alphabetise
| |
18 using autofill::PasswordForm; | |
19 | |
20 namespace password_manager { | |
21 | |
22 class LoginDatabaseIOSTest : public PlatformTest { | |
23 public: | |
24 void SetUp() override { | |
25 ClearKeychain(); | |
26 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
27 base::FilePath login_db_path = | |
28 temp_dir_.path().AppendASCII("temp_login.db"); | |
29 login_db_.reset(new password_manager::LoginDatabase(login_db_path)); | |
30 login_db_->Init(); | |
31 } | |
32 | |
33 void TearDown() override { ClearKeychain(); } | |
34 | |
35 // Removes all passwords from the keychain. Since the unit test | |
36 // executable does not share the keychain with anything else on iOS, clearing | |
37 // the keychain will not affect any other applications. | |
38 void ClearKeychain(); | |
39 | |
40 // Returns the number of items in the keychain. | |
41 size_t GetKeychainSize(); | |
42 | |
43 protected: | |
44 base::ScopedTempDir temp_dir_; | |
45 scoped_ptr<LoginDatabase> login_db_; | |
46 }; | |
47 | |
48 void LoginDatabaseIOSTest::ClearKeychain() { | |
49 const void* queryKeys[] = {kSecClass}; | |
50 const void* queryValues[] = {kSecClassGenericPassword}; | |
51 ScopedCFTypeRef<CFDictionaryRef> query(CFDictionaryCreate( | |
52 NULL, queryKeys, queryValues, arraysize(queryKeys), | |
vabr (Chromium)
2015/07/22 12:44:32
nit: Please convert NULL to nullptr in the whole C
| |
53 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); | |
54 OSStatus status = SecItemDelete(query); | |
55 // iOS7 returns an error of |errSecItemNotFound| if you try to clear an empty | |
56 // keychain. | |
57 ASSERT_TRUE(status == errSecSuccess || status == errSecItemNotFound); | |
58 } | |
59 | |
60 size_t LoginDatabaseIOSTest::GetKeychainSize() { | |
61 // Verify that the keychain now contains exactly one item. | |
vabr (Chromium)
2015/07/22 12:44:32
This comment looks obsolete.
| |
62 ScopedCFTypeRef<CFMutableDictionaryRef> query( | |
63 CFDictionaryCreateMutable(NULL, 3, &kCFTypeDictionaryKeyCallBacks, | |
64 &kCFTypeDictionaryValueCallBacks)); | |
65 CFDictionarySetValue(query, kSecClass, kSecClassGenericPassword); | |
66 CFDictionarySetValue(query, kSecReturnAttributes, kCFBooleanTrue); | |
67 CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitAll); | |
68 | |
69 CFTypeRef result; | |
70 OSStatus status = SecItemCopyMatching(query, &result); | |
71 if (status == errSecItemNotFound) | |
72 return 0; | |
73 | |
74 EXPECT_EQ(errSecSuccess, status); | |
75 size_t size = CFArrayGetCount((CFArrayRef)result); | |
76 CFRelease(result); | |
77 return size; | |
78 } | |
79 | |
80 TEST_F(LoginDatabaseIOSTest, KeychainStorage) { | |
81 base::string16 test_passwords[] = { | |
82 base::ASCIIToUTF16("foo"), base::ASCIIToUTF16("bar"), | |
83 base::WideToUTF16(L"\u043F\u0430\u0440\u043E\u043B\u044C"), | |
84 base::string16(), | |
85 }; | |
86 | |
87 for (unsigned int i = 0; i < arraysize(test_passwords); i++) { | |
vabr (Chromium)
2015/07/22 12:44:31
nit: Please use size_t instead of unsigned int for
| |
88 std::string encrypted; | |
89 EXPECT_EQ(LoginDatabase::ENCRYPTION_RESULT_SUCCESS, | |
90 login_db_->EncryptedString(test_passwords[i], &encrypted)); | |
91 base::string16 decrypted; | |
92 EXPECT_EQ(LoginDatabase::ENCRYPTION_RESULT_SUCCESS, | |
93 login_db_->DecryptedString(encrypted, &decrypted)); | |
94 EXPECT_STREQ(UTF16ToUTF8(test_passwords[i]).c_str(), | |
vabr (Chromium)
2015/07/22 12:44:32
Would just
EXPECT_EQ(test_passwords[i], decrypted)
| |
95 UTF16ToUTF8(decrypted).c_str()); | |
96 } | |
97 } | |
98 | |
99 TEST_F(LoginDatabaseIOSTest, UpdateLogin) { | |
100 PasswordForm form; | |
101 form.origin = GURL("http://0.com"); | |
vabr (Chromium)
2015/07/22 12:44:32
Also, to shorten the file and increase readability
vabr (Chromium)
2015/07/22 12:44:32
nit: Make the origin equal to the signon realm. Th
| |
102 form.signon_realm = "http://www.example.com"; | |
103 form.action = GURL("http://www.example.com/action"); | |
104 form.password_element = base::ASCIIToUTF16("pwd"); | |
105 form.password_value = base::ASCIIToUTF16("example"); | |
106 | |
107 ignore_result(login_db_->AddLogin(form)); | |
vabr (Chromium)
2015/07/22 12:44:32
Instead of ignoring the result, please let's compa
| |
108 | |
109 form.password_value = base::ASCIIToUTF16("secret"); | |
vabr (Chromium)
2015/07/22 12:44:32
nit: Please store the UTF16 version of "secret" as
| |
110 | |
111 password_manager::PasswordStoreChangeList changes = | |
112 login_db_->UpdateLogin(form); | |
113 ASSERT_EQ(1u, changes.size()); | |
vabr (Chromium)
2015/07/22 12:44:32
This looks like it should be an EXPECT_EQ instead.
| |
114 | |
115 form.password_value = base::string16(); | |
vabr (Chromium)
2015/07/22 12:44:32
form.password_value.clear();
| |
116 | |
117 ScopedVector<PasswordForm> forms; | |
118 EXPECT_TRUE(login_db_->GetLogins(form, &forms)); | |
119 | |
120 ASSERT_EQ(1U, forms.size()); | |
121 EXPECT_STREQ("secret", UTF16ToUTF8(forms[0]->password_value).c_str()); | |
122 ASSERT_EQ(1U, GetKeychainSize()); | |
123 } | |
124 | |
125 TEST_F(LoginDatabaseIOSTest, RemoveLogin) { | |
126 PasswordForm form; | |
127 form.signon_realm = "www.example.com"; | |
128 form.action = GURL("www.example.com/action"); | |
129 form.password_element = base::ASCIIToUTF16("pwd"); | |
130 form.password_value = base::ASCIIToUTF16("example"); | |
131 | |
132 ignore_result(login_db_->AddLogin(form)); | |
133 | |
134 ignore_result(login_db_->RemoveLogin(form)); | |
135 | |
136 ScopedVector<PasswordForm> forms; | |
137 EXPECT_TRUE(login_db_->GetLogins(form, &forms)); | |
138 | |
139 ASSERT_EQ(0U, forms.size()); | |
140 ASSERT_EQ(0U, GetKeychainSize()); | |
141 } | |
142 | |
143 TEST_F(LoginDatabaseIOSTest, RemoveLoginsCreatedBetween) { | |
144 PasswordForm forms[3]; | |
145 forms[0].origin = GURL("http://0.com"); | |
146 forms[0].signon_realm = "http://www.example.com"; | |
147 forms[0].username_element = base::ASCIIToUTF16("login0"); | |
148 forms[0].date_created = base::Time::FromDoubleT(100); | |
149 forms[0].password_value = base::ASCIIToUTF16("pass0"); | |
150 | |
151 forms[1].origin = GURL("http://1.com"); | |
152 forms[1].signon_realm = "http://www.example.com"; | |
153 forms[1].username_element = base::ASCIIToUTF16("login1"); | |
154 forms[1].date_created = base::Time::FromDoubleT(200); | |
155 forms[1].password_value = base::ASCIIToUTF16("pass1"); | |
156 | |
157 forms[2].origin = GURL("http://2.com"); | |
158 forms[2].signon_realm = "http://www.example.com"; | |
159 forms[2].username_element = base::ASCIIToUTF16("login2"); | |
160 forms[2].date_created = base::Time::FromDoubleT(300); | |
161 forms[2].password_value = base::ASCIIToUTF16("pass2"); | |
162 | |
163 for (size_t i = 0; i < arraysize(forms); i++) { | |
164 ignore_result(login_db_->AddLogin(forms[i])); | |
165 } | |
166 | |
167 login_db_->RemoveLoginsCreatedBetween(base::Time::FromDoubleT(150), | |
168 base::Time::FromDoubleT(250)); | |
169 | |
170 PasswordForm form; | |
171 form.signon_realm = "http://www.example.com"; | |
172 ScopedVector<PasswordForm> logins; | |
173 EXPECT_TRUE(login_db_->GetLogins(form, &logins)); | |
174 | |
175 ASSERT_EQ(2U, logins.size()); | |
176 ASSERT_EQ(2U, GetKeychainSize()); | |
177 | |
178 EXPECT_STREQ("login0", UTF16ToUTF8(logins[0]->username_element).c_str()); | |
vabr (Chromium)
2015/07/22 12:44:31
I would recommend storing the passwords in string1
| |
179 EXPECT_STREQ("pass0", UTF16ToUTF8(logins[0]->password_value).c_str()); | |
180 EXPECT_STREQ("login2", UTF16ToUTF8(logins[1]->username_element).c_str()); | |
181 EXPECT_STREQ("pass2", UTF16ToUTF8(logins[1]->password_value).c_str()); | |
182 } | |
183 | |
184 } // namespace password_manager | |
OLD | NEW |