Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / password_manager / chrome_password_manager_client_unittest.cc
blob0a497986eec18625570c7989a574143e9481d622
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
3 // found in the LICENSE file.
5 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
7 #include "base/command_line.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/sync/profile_sync_service_factory.h"
11 #include "chrome/browser/sync/profile_sync_service_mock.h"
12 #include "chrome/common/chrome_version_info.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/autofill/content/common/autofill_messages.h"
16 #include "components/password_manager/content/browser/password_manager_internals_service_factory.h"
17 #include "components/password_manager/content/common/credential_manager_messages.h"
18 #include "components/password_manager/content/common/credential_manager_types.h"
19 #include "components/password_manager/core/browser/log_receiver.h"
20 #include "components/password_manager/core/browser/password_manager_internals_service.h"
21 #include "components/password_manager/core/common/password_manager_switches.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/mock_render_process_host.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 using content::BrowserContext;
29 using content::WebContents;
30 using testing::Return;
32 namespace {
34 const char kTestText[] = "abcd1234";
36 class MockLogReceiver : public password_manager::LogReceiver {
37 public:
38 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&));
41 class TestChromePasswordManagerClient : public ChromePasswordManagerClient {
42 public:
43 explicit TestChromePasswordManagerClient(content::WebContents* web_contents)
44 : ChromePasswordManagerClient(web_contents, nullptr),
45 is_sync_account_credential_(false) {}
46 ~TestChromePasswordManagerClient() override {}
48 bool IsSyncAccountCredential(const std::string& username,
49 const std::string& origin) const override {
50 return is_sync_account_credential_;
53 void set_is_sync_account_credential(bool is_sync_account_credential) {
54 is_sync_account_credential_ = is_sync_account_credential;
57 private:
58 bool is_sync_account_credential_;
60 DISALLOW_COPY_AND_ASSIGN(TestChromePasswordManagerClient);
63 } // namespace
65 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness {
66 public:
67 ChromePasswordManagerClientTest();
69 virtual void SetUp() override;
71 protected:
72 ChromePasswordManagerClient* GetClient();
74 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then
75 // copies its argument into |activation_flag| and returns true. Otherwise
76 // returns false.
77 bool WasLoggingActivationMessageSent(bool* activation_flag);
79 password_manager::PasswordManagerInternalsService* service_;
81 testing::StrictMock<MockLogReceiver> receiver_;
84 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest()
85 : service_(nullptr) {
88 void ChromePasswordManagerClientTest::SetUp() {
89 ChromeRenderViewHostTestHarness::SetUp();
90 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
91 web_contents(), nullptr);
92 service_ = password_manager::PasswordManagerInternalsServiceFactory::
93 GetForBrowserContext(profile());
94 ASSERT_TRUE(service_);
97 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() {
98 return ChromePasswordManagerClient::FromWebContents(web_contents());
101 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent(
102 bool* activation_flag) {
103 const uint32 kMsgID = AutofillMsg_SetLoggingState::ID;
104 const IPC::Message* message =
105 process()->sink().GetFirstMessageMatching(kMsgID);
106 if (!message)
107 return false;
108 Tuple<bool> param;
109 AutofillMsg_SetLoggingState::Read(message, &param);
110 *activation_flag = get<0>(param);
111 process()->sink().ClearMessages();
112 return true;
115 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoReceiver) {
116 ChromePasswordManagerClient* client = GetClient();
118 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0);
119 // Before attaching the receiver, no text should be passed.
120 client->LogSavePasswordProgress(kTestText);
121 EXPECT_FALSE(client->IsLoggingActive());
124 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachReceiver) {
125 ChromePasswordManagerClient* client = GetClient();
126 EXPECT_FALSE(client->IsLoggingActive());
128 // After attaching the logger, text should be passed.
129 service_->RegisterReceiver(&receiver_);
130 EXPECT_TRUE(client->IsLoggingActive());
131 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1);
132 client->LogSavePasswordProgress(kTestText);
133 service_->UnregisterReceiver(&receiver_);
134 EXPECT_FALSE(client->IsLoggingActive());
137 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachReceiver) {
138 ChromePasswordManagerClient* client = GetClient();
140 service_->RegisterReceiver(&receiver_);
141 EXPECT_TRUE(client->IsLoggingActive());
142 service_->UnregisterReceiver(&receiver_);
143 EXPECT_FALSE(client->IsLoggingActive());
145 // After detaching the logger, no text should be passed.
146 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0);
147 client->LogSavePasswordProgress(kTestText);
150 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) {
151 ChromePasswordManagerClient* client = GetClient();
152 bool logging_active = false;
154 // Initially, the logging should be off, so no IPC messages.
155 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active));
157 service_->RegisterReceiver(&receiver_);
158 EXPECT_TRUE(client->IsLoggingActive());
159 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
160 EXPECT_TRUE(logging_active);
162 service_->UnregisterReceiver(&receiver_);
163 EXPECT_FALSE(client->IsLoggingActive());
164 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
165 EXPECT_FALSE(logging_active);
168 TEST_F(ChromePasswordManagerClientTest, AnswerToPingsAboutLoggingState_Active) {
169 service_->RegisterReceiver(&receiver_);
171 process()->sink().ClearMessages();
173 // Ping the client for logging activity update.
174 AutofillHostMsg_PasswordAutofillAgentConstructed msg(0);
175 static_cast<content::WebContentsObserver*>(GetClient())->OnMessageReceived(
176 msg, web_contents()->GetMainFrame());
178 bool logging_active = false;
179 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
180 EXPECT_TRUE(logging_active);
182 service_->UnregisterReceiver(&receiver_);
185 TEST_F(ChromePasswordManagerClientTest,
186 AnswerToPingsAboutLoggingState_Inactive) {
187 process()->sink().ClearMessages();
189 // Ping the client for logging activity update.
190 AutofillHostMsg_PasswordAutofillAgentConstructed msg(0);
191 static_cast<content::WebContentsObserver*>(GetClient())->OnMessageReceived(
192 msg, web_contents()->GetMainFrame());
194 bool logging_active = true;
195 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
196 EXPECT_FALSE(logging_active);
199 TEST_F(ChromePasswordManagerClientTest,
200 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) {
201 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
204 TEST_F(ChromePasswordManagerClientTest,
205 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) {
206 base::CommandLine::ForCurrentProcess()->AppendSwitch(
207 password_manager::switches::kEnableAutomaticPasswordSaving);
208 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_UNKNOWN)
209 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled());
210 else
211 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
214 TEST_F(ChromePasswordManagerClientTest, LogToAReceiver) {
215 ChromePasswordManagerClient* client = GetClient();
216 service_->RegisterReceiver(&receiver_);
217 EXPECT_TRUE(client->IsLoggingActive());
219 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1);
220 client->LogSavePasswordProgress(kTestText);
222 service_->UnregisterReceiver(&receiver_);
223 EXPECT_FALSE(client->IsLoggingActive());
226 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult_Reauth) {
227 // Make client disallow only reauth requests.
228 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
229 command_line->AppendSwitch(
230 password_manager::switches::kDisallowAutofillSyncCredentialForReauth);
231 scoped_ptr<TestChromePasswordManagerClient> client(
232 new TestChromePasswordManagerClient(web_contents()));
233 autofill::PasswordForm form;
235 client->set_is_sync_account_credential(false);
236 NavigateAndCommit(
237 GURL("https://accounts.google.com/login?rart=123&continue=blah"));
238 EXPECT_FALSE(client->ShouldFilterAutofillResult(form));
240 client->set_is_sync_account_credential(true);
241 NavigateAndCommit(
242 GURL("https://accounts.google.com/login?rart=123&continue=blah"));
243 EXPECT_TRUE(client->ShouldFilterAutofillResult(form));
245 // This counts as a reauth url, though a valid URL should have a value for
246 // "rart"
247 NavigateAndCommit(GURL("https://accounts.google.com/addlogin?rart"));
248 EXPECT_TRUE(client->ShouldFilterAutofillResult(form));
250 NavigateAndCommit(GURL("https://accounts.google.com/login?param=123"));
251 EXPECT_FALSE(client->ShouldFilterAutofillResult(form));
253 NavigateAndCommit(GURL("https://site.com/login?rart=678"));
254 EXPECT_FALSE(client->ShouldFilterAutofillResult(form));
257 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult) {
258 // Normally the client should allow any credentials through, even if they
259 // are the sync credential.
260 scoped_ptr<TestChromePasswordManagerClient> client(
261 new TestChromePasswordManagerClient(web_contents()));
262 autofill::PasswordForm form;
263 client->set_is_sync_account_credential(true);
264 NavigateAndCommit(GURL("https://accounts.google.com/Login"));
265 EXPECT_FALSE(client->ShouldFilterAutofillResult(form));
267 // Adding disallow switch should cause sync credential to be filtered.
268 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
269 command_line->AppendSwitch(
270 password_manager::switches::kDisallowAutofillSyncCredential);
271 client.reset(new TestChromePasswordManagerClient(web_contents()));
272 client->set_is_sync_account_credential(true);
273 NavigateAndCommit(GURL("https://accounts.google.com/Login"));
274 EXPECT_TRUE(client->ShouldFilterAutofillResult(form));
277 TEST_F(ChromePasswordManagerClientTest,
278 IsPasswordManagerEnabledForCurrentPage) {
279 ChromePasswordManagerClient* client = GetClient();
280 NavigateAndCommit(
281 GURL("https://accounts.google.com/ServiceLogin?continue="
282 "https://passwords.google.com/settings&rart=123"));
283 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage());
285 // Password site is inaccesible via HTTP, but because of HSTS the following
286 // link should still continue to https://passwords.google.com.
287 NavigateAndCommit(
288 GURL("https://accounts.google.com/ServiceLogin?continue="
289 "http://passwords.google.com/settings&rart=123"));
290 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage());
292 // Specifying default port still passes.
293 NavigateAndCommit(
294 GURL("https://accounts.google.com/ServiceLogin?continue="
295 "https://passwords.google.com:443/settings&rart=123"));
296 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage());
298 // Encoded URL is considered the same.
299 NavigateAndCommit(
300 GURL("https://accounts.google.com/ServiceLogin?continue="
301 "https://passwords.%67oogle.com/settings&rart=123"));
302 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage());
304 // Make sure testing sites are disabled as well.
305 NavigateAndCommit(
306 GURL("https://accounts.google.com/Login?continue="
307 "https://passwords-ac-testing.corp.google.com/settings&rart=456"));
308 EXPECT_FALSE(client->IsPasswordManagerEnabledForCurrentPage());
310 // Fully qualified domain name is considered a different hostname by GURL.
311 // Ideally this would not be the case, but this quirk can be avoided by
312 // verification on the server. This test is simply documentation of this
313 // behavior.
314 NavigateAndCommit(
315 GURL("https://accounts.google.com/ServiceLogin?continue="
316 "https://passwords.google.com./settings&rart=123"));
317 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage());
319 // Not a transactional reauth page.
320 NavigateAndCommit(
321 GURL("https://accounts.google.com/ServiceLogin?continue="
322 "https://passwords.google.com/settings"));
323 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage());
325 // Should be enabled for other transactional reauth pages.
326 NavigateAndCommit(
327 GURL("https://accounts.google.com/ServiceLogin?continue="
328 "https://mail.google.com&rart=234"));
329 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage());
331 // Reauth pages are only on accounts.google.com
332 NavigateAndCommit(
333 GURL("https://other.site.com/ServiceLogin?continue="
334 "https://passwords.google.com&rart=234"));
335 EXPECT_TRUE(client->IsPasswordManagerEnabledForCurrentPage());
338 TEST_F(ChromePasswordManagerClientTest, IsPasswordSyncEnabled) {
339 ChromePasswordManagerClient* client = GetClient();
341 ProfileSyncServiceMock* mock_sync_service =
342 static_cast<ProfileSyncServiceMock*>(
343 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
344 profile(), ProfileSyncServiceMock::BuildMockProfileSyncService));
346 syncer::ModelTypeSet active_types;
347 active_types.Put(syncer::PASSWORDS);
348 EXPECT_CALL(*mock_sync_service, HasSyncSetupCompleted())
349 .WillRepeatedly(Return(true));
350 EXPECT_CALL(*mock_sync_service, SyncActive()).WillRepeatedly(Return(true));
351 EXPECT_CALL(*mock_sync_service, GetActiveDataTypes())
352 .WillRepeatedly(Return(active_types));
353 EXPECT_CALL(*mock_sync_service, IsUsingSecondaryPassphrase())
354 .WillRepeatedly(Return(false));
356 // Passwords are syncing and custom passphrase isn't used.
357 EXPECT_FALSE(
358 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE));
359 EXPECT_TRUE(client->IsPasswordSyncEnabled(
360 password_manager::WITHOUT_CUSTOM_PASSPHRASE));
362 // Again, using a custom passphrase.
363 EXPECT_CALL(*mock_sync_service, IsUsingSecondaryPassphrase())
364 .WillRepeatedly(Return(true));
366 EXPECT_TRUE(
367 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE));
368 EXPECT_FALSE(client->IsPasswordSyncEnabled(
369 password_manager::WITHOUT_CUSTOM_PASSPHRASE));
371 // Always return false if we aren't syncing passwords.
372 active_types.Remove(syncer::PASSWORDS);
373 active_types.Put(syncer::BOOKMARKS);
374 EXPECT_CALL(*mock_sync_service, GetActiveDataTypes())
375 .WillRepeatedly(Return(active_types));
377 EXPECT_FALSE(
378 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE));
379 EXPECT_FALSE(client->IsPasswordSyncEnabled(
380 password_manager::WITHOUT_CUSTOM_PASSPHRASE));
382 // Again, without a custom passphrase.
383 EXPECT_CALL(*mock_sync_service, IsUsingSecondaryPassphrase())
384 .WillRepeatedly(Return(false));
386 EXPECT_FALSE(
387 client->IsPasswordSyncEnabled(password_manager::ONLY_CUSTOM_PASSPHRASE));
388 EXPECT_FALSE(client->IsPasswordSyncEnabled(
389 password_manager::WITHOUT_CUSTOM_PASSPHRASE));