| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/passwords/password_manager_presenter.h" | 5 #include "chrome/browser/ui/passwords/password_manager_presenter.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 using testing::Eq; | 23 using testing::Eq; |
| 24 using testing::Property; | 24 using testing::Property; |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 struct SortEntry { | 28 struct SortEntry { |
| 29 const char* const origin; | 29 const char* const origin; |
| 30 const char* const username; | 30 const char* const username; |
| 31 const char* const password; | 31 const char* const password; |
| 32 const char* const affiliated_web_realm; | 32 const char* const affiliated_web_realm; |
| 33 const char* const federation; |
| 33 const int expected_position; | 34 const int expected_position; |
| 34 }; | 35 }; |
| 35 | 36 |
| 36 } // namespace | 37 } // namespace |
| 37 | 38 |
| 38 class MockPasswordUIView : public PasswordUIView { | 39 class MockPasswordUIView : public PasswordUIView { |
| 39 public: | 40 public: |
| 40 explicit MockPasswordUIView(Profile* profile) | 41 explicit MockPasswordUIView(Profile* profile) |
| 41 : profile_(profile), password_manager_presenter_(this) { | 42 : profile_(profile), password_manager_presenter_(this) { |
| 42 password_manager_presenter_.Initialize(); | 43 password_manager_presenter_.Initialize(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 mock_controller_.reset(new MockPasswordUIView(&profile_)); | 84 mock_controller_.reset(new MockPasswordUIView(&profile_)); |
| 84 } | 85 } |
| 85 void AddPasswordEntry(const GURL& origin, | 86 void AddPasswordEntry(const GURL& origin, |
| 86 const std::string& user_name, | 87 const std::string& user_name, |
| 87 const std::string& password); | 88 const std::string& password); |
| 88 void AddPasswordException(const GURL& origin); | 89 void AddPasswordException(const GURL& origin); |
| 89 void UpdateLists(); | 90 void UpdateLists(); |
| 90 MockPasswordUIView* GetUIController() { return mock_controller_.get(); } | 91 MockPasswordUIView* GetUIController() { return mock_controller_.get(); } |
| 91 void SortAndCheckPositions(const SortEntry test_entries[], | 92 void SortAndCheckPositions(const SortEntry test_entries[], |
| 92 size_t number_of_entries, | 93 size_t number_of_entries, |
| 93 bool username_and_password_in_key); | 94 PasswordEntryType entry_type); |
| 94 | 95 |
| 95 private: | 96 private: |
| 96 content::TestBrowserThreadBundle thread_bundle_; | 97 content::TestBrowserThreadBundle thread_bundle_; |
| 97 TestingProfile profile_; | 98 TestingProfile profile_; |
| 98 std::unique_ptr<MockPasswordUIView> mock_controller_; | 99 std::unique_ptr<MockPasswordUIView> mock_controller_; |
| 99 | 100 |
| 100 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); | 101 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); |
| 101 }; | 102 }; |
| 102 | 103 |
| 103 void PasswordManagerPresenterTest::AddPasswordEntry( | 104 void PasswordManagerPresenterTest::AddPasswordEntry( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 122 } | 123 } |
| 123 | 124 |
| 124 void PasswordManagerPresenterTest::UpdateLists() { | 125 void PasswordManagerPresenterTest::UpdateLists() { |
| 125 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList(); | 126 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList(); |
| 126 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList(); | 127 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList(); |
| 127 } | 128 } |
| 128 | 129 |
| 129 void PasswordManagerPresenterTest::SortAndCheckPositions( | 130 void PasswordManagerPresenterTest::SortAndCheckPositions( |
| 130 const SortEntry test_entries[], | 131 const SortEntry test_entries[], |
| 131 size_t number_of_entries, | 132 size_t number_of_entries, |
| 132 bool username_and_password_in_key) { | 133 PasswordEntryType entry_type) { |
| 133 std::vector<std::unique_ptr<autofill::PasswordForm>> list; | 134 std::vector<std::unique_ptr<autofill::PasswordForm>> list; |
| 134 size_t expected_number_of_unique_entries = 0; | 135 size_t expected_number_of_unique_entries = 0; |
| 135 for (size_t i = 0; i < number_of_entries; i++) { | 136 for (size_t i = 0; i < number_of_entries; i++) { |
| 136 const SortEntry& entry = test_entries[i]; | 137 const SortEntry& entry = test_entries[i]; |
| 137 std::unique_ptr<autofill::PasswordForm> form(new autofill::PasswordForm()); | 138 std::unique_ptr<autofill::PasswordForm> form(new autofill::PasswordForm()); |
| 138 form->signon_realm = entry.origin; | 139 form->signon_realm = entry.origin; |
| 139 form->origin = GURL(base::ASCIIToUTF16(entry.origin)); | 140 form->origin = GURL(base::ASCIIToUTF16(entry.origin)); |
| 140 if (username_and_password_in_key) { | 141 if (entry_type == PasswordEntryType::SAVED) { |
| 141 form->username_value = base::ASCIIToUTF16(entry.username); | 142 form->username_value = base::ASCIIToUTF16(entry.username); |
| 142 form->password_value = base::ASCIIToUTF16(entry.password); | 143 form->password_value = base::ASCIIToUTF16(entry.password); |
| 144 if (entry.federation != nullptr) |
| 145 form->federation_origin = url::Origin(GURL(entry.federation)); |
| 143 } | 146 } |
| 144 if (entry.affiliated_web_realm) | 147 if (entry.affiliated_web_realm) |
| 145 form->affiliated_web_realm = entry.affiliated_web_realm; | 148 form->affiliated_web_realm = entry.affiliated_web_realm; |
| 146 list.push_back(std::move(form)); | 149 list.push_back(std::move(form)); |
| 147 if (entry.expected_position >= 0) | 150 if (entry.expected_position >= 0) |
| 148 expected_number_of_unique_entries++; | 151 expected_number_of_unique_entries++; |
| 149 } | 152 } |
| 150 | 153 |
| 151 DuplicatesMap duplicates; | 154 DuplicatesMap duplicates; |
| 152 mock_controller_->GetPasswordManagerPresenter()->SortEntriesAndHideDuplicates( | 155 mock_controller_->GetPasswordManagerPresenter()->SortEntriesAndHideDuplicates( |
| 153 &list, &duplicates, username_and_password_in_key); | 156 &list, &duplicates, entry_type); |
| 154 | 157 |
| 155 ASSERT_EQ(expected_number_of_unique_entries, list.size()); | 158 ASSERT_EQ(expected_number_of_unique_entries, list.size()); |
| 156 ASSERT_EQ(number_of_entries - expected_number_of_unique_entries, | 159 ASSERT_EQ(number_of_entries - expected_number_of_unique_entries, |
| 157 duplicates.size()); | 160 duplicates.size()); |
| 158 for (size_t i = 0; i < number_of_entries; i++) { | 161 for (size_t i = 0; i < number_of_entries; i++) { |
| 159 const SortEntry& entry = test_entries[i]; | 162 const SortEntry& entry = test_entries[i]; |
| 160 if (entry.expected_position >= 0) { | 163 if (entry.expected_position >= 0) { |
| 161 SCOPED_TRACE(testing::Message("position in sorted list: ") | 164 SCOPED_TRACE(testing::Message("position in sorted list: ") |
| 162 << entry.expected_position); | 165 << entry.expected_position); |
| 163 EXPECT_EQ(GURL(base::ASCIIToUTF16(entry.origin)), | 166 EXPECT_EQ(GURL(base::ASCIIToUTF16(entry.origin)), |
| 164 list[entry.expected_position]->origin); | 167 list[entry.expected_position]->origin); |
| 165 if (username_and_password_in_key) { | 168 if (entry_type == PasswordEntryType::SAVED) { |
| 166 EXPECT_EQ(base::ASCIIToUTF16(entry.username), | 169 EXPECT_EQ(base::ASCIIToUTF16(entry.username), |
| 167 list[entry.expected_position]->username_value); | 170 list[entry.expected_position]->username_value); |
| 168 EXPECT_EQ(base::ASCIIToUTF16(entry.password), | 171 EXPECT_EQ(base::ASCIIToUTF16(entry.password), |
| 169 list[entry.expected_position]->password_value); | 172 list[entry.expected_position]->password_value); |
| 173 if (entry.federation != nullptr) |
| 174 EXPECT_EQ(url::Origin(GURL(entry.federation)), |
| 175 list[entry.expected_position]->federation_origin); |
| 170 } | 176 } |
| 171 } | 177 } |
| 172 } | 178 } |
| 173 } | 179 } |
| 174 | 180 |
| 175 namespace { | 181 namespace { |
| 176 | 182 |
| 177 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { | 183 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { |
| 178 EXPECT_CALL(*GetUIController(), | 184 EXPECT_CALL(*GetUIController(), |
| 179 SetPasswordList(Property( | 185 SetPasswordList(Property( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 Eq(2u)))); | 220 Eq(2u)))); |
| 215 EXPECT_CALL(*GetUIController(), | 221 EXPECT_CALL(*GetUIController(), |
| 216 SetPasswordExceptionList(Property( | 222 SetPasswordExceptionList(Property( |
| 217 &std::vector<std::unique_ptr<autofill::PasswordForm>>::size, | 223 &std::vector<std::unique_ptr<autofill::PasswordForm>>::size, |
| 218 Eq(1u)))); | 224 Eq(1u)))); |
| 219 UpdateLists(); | 225 UpdateLists(); |
| 220 } | 226 } |
| 221 | 227 |
| 222 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentOrigins) { | 228 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentOrigins) { |
| 223 const SortEntry test_cases[] = { | 229 const SortEntry test_cases[] = { |
| 224 {"http://example-b.com", "user_a", "pwd", nullptr, 2}, | 230 {"http://example-b.com", "user_a", "pwd", nullptr, nullptr, 2}, |
| 225 {"http://example-a.com", "user_a1", "pwd", nullptr, 0}, | 231 {"http://example-a.com", "user_a1", "pwd", nullptr, nullptr, 0}, |
| 226 {"http://example-a.com", "user_a2", "pwd", nullptr, 1}, | 232 {"http://example-a.com", "user_a2", "pwd", nullptr, nullptr, 1}, |
| 227 {"http://example-c.com", "user_a", "pwd", nullptr, 3}}; | 233 {"http://example-c.com", "user_a", "pwd", nullptr, nullptr, 3}}; |
| 228 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | 234 SortAndCheckPositions(test_cases, arraysize(test_cases), |
| 235 PasswordEntryType::SAVED); |
| 229 } | 236 } |
| 230 | 237 |
| 231 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentUsernames) { | 238 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentUsernames) { |
| 232 const SortEntry test_cases[] = { | 239 const SortEntry test_cases[] = { |
| 233 {"http://example.com", "user_a", "pwd", nullptr, 0}, | 240 {"http://example.com", "user_a", "pwd", nullptr, nullptr, 0}, |
| 234 {"http://example.com", "user_c", "pwd", nullptr, 2}, | 241 {"http://example.com", "user_c", "pwd", nullptr, nullptr, 2}, |
| 235 {"http://example.com", "user_b", "pwd", nullptr, 1}}; | 242 {"http://example.com", "user_b", "pwd", nullptr, nullptr, 1}}; |
| 236 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | 243 SortAndCheckPositions(test_cases, arraysize(test_cases), |
| 244 PasswordEntryType::SAVED); |
| 237 } | 245 } |
| 238 | 246 |
| 239 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentPasswords) { | 247 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentPasswords) { |
| 240 const SortEntry test_cases[] = { | 248 const SortEntry test_cases[] = { |
| 241 {"http://example.com", "user_a", "1", nullptr, 0}, | 249 {"http://example.com", "user_a", "1", nullptr, nullptr, 0}, |
| 242 {"http://example.com", "user_a", "2", nullptr, 1}, | 250 {"http://example.com", "user_a", "2", nullptr, nullptr, 1}, |
| 243 {"http://example.com", "user_a", "3", nullptr, 2}}; | 251 {"http://example.com", "user_a", "3", nullptr, nullptr, 2}}; |
| 244 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | 252 SortAndCheckPositions(test_cases, arraysize(test_cases), |
| 253 PasswordEntryType::SAVED); |
| 245 } | 254 } |
| 246 | 255 |
| 247 TEST_F(PasswordManagerPresenterTest, Sorting_HideDuplicates) { | 256 TEST_F(PasswordManagerPresenterTest, Sorting_HideDuplicates) { |
| 248 const SortEntry test_cases[] = { | 257 const SortEntry test_cases[] = { |
| 249 {"http://example.com", "user_a", "pwd", nullptr, 0}, | 258 {"http://example.com", "user_a", "pwd", nullptr, nullptr, 0}, |
| 250 // Different username. | 259 // Different username. |
| 251 {"http://example.com", "user_b", "pwd", nullptr, 2}, | 260 {"http://example.com", "user_b", "pwd", nullptr, nullptr, 2}, |
| 252 // Different password. | 261 // Different password. |
| 253 {"http://example.com", "user_a", "secret", nullptr, 1}, | 262 {"http://example.com", "user_a", "secret", nullptr, nullptr, 1}, |
| 254 // Different origin. | 263 // Different origin. |
| 255 {"http://sub1.example.com", "user_a", "pwd", nullptr, 3}, | 264 {"http://sub1.example.com", "user_a", "pwd", nullptr, nullptr, 3}, |
| 256 {"http://example.com", "user_a", "pwd", nullptr, -1} // Hide it. | 265 {"http://example.com", "user_a", "pwd", nullptr, nullptr, -1} // Hide it. |
| 257 }; | 266 }; |
| 258 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | 267 SortAndCheckPositions(test_cases, arraysize(test_cases), |
| 268 PasswordEntryType::SAVED); |
| 259 } | 269 } |
| 260 | 270 |
| 261 TEST_F(PasswordManagerPresenterTest, Sorting_DontUseUsernameAndPasswordInKey) { | 271 TEST_F(PasswordManagerPresenterTest, Sorting_PasswordExceptions) { |
| 262 const SortEntry test_cases[] = { | 272 const SortEntry test_cases[] = { |
| 263 {"http://example-b.com", nullptr, nullptr, nullptr, 1}, | 273 {"http://example-b.com", nullptr, nullptr, nullptr, nullptr, 1}, |
| 264 {"http://example-a.com", nullptr, nullptr, nullptr, 0}, | 274 {"http://example-a.com", nullptr, nullptr, nullptr, nullptr, 0}, |
| 265 {"http://example-a.com", nullptr, nullptr, nullptr, -1}, // Hide it. | 275 {"http://example-a.com", nullptr, nullptr, nullptr, nullptr, |
| 266 {"http://example-c.com", nullptr, nullptr, nullptr, 2}}; | 276 -1}, // Hide it. |
| 267 SortAndCheckPositions(test_cases, arraysize(test_cases), false); | 277 {"http://example-c.com", nullptr, nullptr, nullptr, nullptr, 2}}; |
| 278 SortAndCheckPositions(test_cases, arraysize(test_cases), |
| 279 PasswordEntryType::BLACKLISTED); |
| 268 } | 280 } |
| 269 | 281 |
| 270 TEST_F(PasswordManagerPresenterTest, Sorting_AndroidCredentials) { | 282 TEST_F(PasswordManagerPresenterTest, Sorting_AndroidCredentials) { |
| 271 const SortEntry test_cases[] = { | 283 const SortEntry test_cases[] = { |
| 272 {"https://alpha.com", "user", "secret", nullptr, 0}, | 284 {"https://alpha.com", "user", "secret", nullptr, nullptr, 0}, |
| 273 {"android://hash@com.alpha", "user", "secret", "https://alpha.com", 1}, | 285 {"android://hash@com.alpha", "user", "secret", "https://alpha.com", |
| 274 {"android://hash@com.alpha", "user", "secret", "https://alpha.com", -1}, | 286 nullptr, 1}, |
| 275 {"android://hash@com.alpha", "user", "secret", nullptr, 2}, | 287 {"android://hash@com.alpha", "user", "secret", "https://alpha.com", |
| 288 nullptr, -1}, |
| 289 {"android://hash@com.alpha", "user", "secret", nullptr, nullptr, 2}, |
| 290 {"android://hash@com.betta.android", "user", "secret", |
| 291 "https://betta.com", nullptr, 3}, |
| 292 {"android://hash@com.betta.android", "user", "secret", nullptr, nullptr, |
| 293 4}}; |
| 294 SortAndCheckPositions(test_cases, arraysize(test_cases), |
| 295 PasswordEntryType::SAVED); |
| 296 } |
| 276 | 297 |
| 277 {"android://hash@com.betta.android", "user", "secret", | 298 TEST_F(PasswordManagerPresenterTest, Sorting_Federations) { |
| 278 "https://betta.com", 3}, | 299 const SortEntry test_cases[] = { |
| 279 {"android://hash@com.betta.android", "user", "secret", nullptr, 4}}; | 300 {"https://example.com", "user", "secret", nullptr, nullptr, 0}, |
| 280 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | 301 {"https://example.com", "user", "secret", nullptr, "https://fed1.com", 1}, |
| 302 {"https://example.com", "user", "secret", nullptr, "https://fed2.com", |
| 303 2}}; |
| 304 SortAndCheckPositions(test_cases, arraysize(test_cases), |
| 305 PasswordEntryType::SAVED); |
| 281 } | 306 } |
| 282 | 307 |
| 283 } // namespace | 308 } // namespace |
| OLD | NEW |