OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 <map> | 5 #include <map> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 #include "chrome/browser/ui/browser_finder.h" | 36 #include "chrome/browser/ui/browser_finder.h" |
37 #include "chrome/browser/ui/host_desktop.h" | 37 #include "chrome/browser/ui/host_desktop.h" |
38 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 38 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
39 #include "chrome/common/chrome_notification_types.h" | 39 #include "chrome/common/chrome_notification_types.h" |
40 #include "chrome/common/chrome_paths.h" | 40 #include "chrome/common/chrome_paths.h" |
41 #include "chrome/common/chrome_switches.h" | 41 #include "chrome/common/chrome_switches.h" |
42 #include "chrome/test/base/in_process_browser_test.h" | 42 #include "chrome/test/base/in_process_browser_test.h" |
43 #include "chromeos/dbus/cryptohome_client.h" | 43 #include "chromeos/dbus/cryptohome_client.h" |
44 #include "chromeos/dbus/dbus_method_call_status.h" | 44 #include "chromeos/dbus/dbus_method_call_status.h" |
45 #include "chromeos/dbus/dbus_thread_manager.h" | 45 #include "chromeos/dbus/dbus_thread_manager.h" |
| 46 #include "chromeos/dbus/fake_session_manager_client.h" |
46 #include "chromeos/dbus/mock_dbus_thread_manager.h" | 47 #include "chromeos/dbus/mock_dbus_thread_manager.h" |
47 #include "chromeos/dbus/session_manager_client.h" | |
48 #include "content/public/browser/notification_observer.h" | 48 #include "content/public/browser/notification_observer.h" |
49 #include "content/public/browser/notification_registrar.h" | 49 #include "content/public/browser/notification_registrar.h" |
50 #include "content/public/browser/notification_service.h" | 50 #include "content/public/browser/notification_service.h" |
| 51 #include "content/public/browser/notification_watcher.h" |
51 #include "content/public/browser/web_contents.h" | 52 #include "content/public/browser/web_contents.h" |
52 #include "testing/gmock/include/gmock/gmock.h" | 53 #include "testing/gmock/include/gmock/gmock.h" |
53 #include "third_party/cros_system_api/dbus/service_constants.h" | 54 #include "third_party/cros_system_api/dbus/service_constants.h" |
54 | 55 |
55 namespace em = enterprise_management; | 56 namespace em = enterprise_management; |
56 | 57 |
57 using testing::Return; | 58 using testing::Return; |
58 | 59 |
59 namespace policy { | 60 namespace policy { |
60 | 61 |
61 namespace { | 62 namespace { |
62 | 63 |
63 const char kAccountId1[] = "dla1@example.com"; | 64 const char kAccountId1[] = "dla1@example.com"; |
64 const char kAccountId2[] = "dla2@example.com"; | 65 const char kAccountId2[] = "dla2@example.com"; |
65 const char kDisplayName1[] = "display name for account 1"; | 66 const char kDisplayName1[] = "display name for account 1"; |
66 const char kDisplayName2[] = "display name for account 2"; | 67 const char kDisplayName2[] = "display name for account 2"; |
67 const char* kStartupURLs[] = { | 68 const char* kStartupURLs[] = { |
68 "chrome://policy", | 69 "chrome://policy", |
69 "chrome://about", | 70 "chrome://about", |
70 }; | 71 }; |
71 | 72 |
72 // Observes a specific notification type and quits the message loop once a | |
73 // condition holds. | |
74 class NotificationWatcher : public content::NotificationObserver { | |
75 public: | |
76 // Callback invoked on notifications. Should return true when the condition | |
77 // that the caller is waiting for is satisfied. | |
78 typedef base::Callback<bool(void)> ConditionTestCallback; | |
79 | |
80 explicit NotificationWatcher(int notification_type, | |
81 const ConditionTestCallback& callback) | |
82 : type_(notification_type), | |
83 callback_(callback) {} | |
84 | |
85 void Run() { | |
86 if (callback_.Run()) | |
87 return; | |
88 | |
89 content::NotificationRegistrar registrar; | |
90 registrar.Add(this, type_, content::NotificationService::AllSources()); | |
91 run_loop_.Run(); | |
92 } | |
93 | |
94 // content::NotificationObserver: | |
95 virtual void Observe(int type, | |
96 const content::NotificationSource& source, | |
97 const content::NotificationDetails& details) OVERRIDE { | |
98 if (callback_.Run()) | |
99 run_loop_.Quit(); | |
100 } | |
101 | |
102 private: | |
103 int type_; | |
104 ConditionTestCallback callback_; | |
105 base::RunLoop run_loop_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(NotificationWatcher); | |
108 }; | |
109 | |
110 // A fake implementation of session_manager. Accepts policy blobs to be set and | |
111 // returns them unmodified. | |
112 class FakeSessionManagerClient : public chromeos::SessionManagerClient { | |
113 public: | |
114 FakeSessionManagerClient() {} | |
115 virtual ~FakeSessionManagerClient() {} | |
116 | |
117 // SessionManagerClient: | |
118 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
119 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
120 virtual bool HasObserver(Observer* observer) OVERRIDE { return false; } | |
121 virtual void EmitLoginPromptReady() OVERRIDE {} | |
122 virtual void EmitLoginPromptVisible() OVERRIDE {} | |
123 virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE {} | |
124 virtual void RestartEntd() OVERRIDE {} | |
125 virtual void StartSession(const std::string& user_email) OVERRIDE {} | |
126 virtual void StopSession() OVERRIDE {} | |
127 virtual void StartDeviceWipe() OVERRIDE {} | |
128 virtual void RequestLockScreen() OVERRIDE {} | |
129 virtual void NotifyLockScreenShown() OVERRIDE {} | |
130 virtual void RequestUnlockScreen() OVERRIDE {} | |
131 virtual void NotifyLockScreenDismissed() OVERRIDE {} | |
132 virtual void RetrieveDevicePolicy( | |
133 const RetrievePolicyCallback& callback) OVERRIDE { | |
134 MessageLoop::current()->PostTask(FROM_HERE, | |
135 base::Bind(callback, device_policy_)); | |
136 } | |
137 virtual void RetrieveUserPolicy( | |
138 const RetrievePolicyCallback& callback) OVERRIDE { | |
139 MessageLoop::current()->PostTask(FROM_HERE, | |
140 base::Bind(callback, user_policy_)); | |
141 } | |
142 virtual void RetrieveDeviceLocalAccountPolicy( | |
143 const std::string& account_id, | |
144 const RetrievePolicyCallback& callback) OVERRIDE { | |
145 MessageLoop::current()->PostTask( | |
146 FROM_HERE, | |
147 base::Bind(callback, device_local_account_policy_[account_id])); | |
148 } | |
149 virtual void StoreDevicePolicy(const std::string& policy_blob, | |
150 const StorePolicyCallback& callback) OVERRIDE { | |
151 device_policy_ = policy_blob; | |
152 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true)); | |
153 } | |
154 virtual void StoreUserPolicy(const std::string& policy_blob, | |
155 const StorePolicyCallback& callback) OVERRIDE { | |
156 user_policy_ = policy_blob; | |
157 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true)); | |
158 } | |
159 virtual void StoreDeviceLocalAccountPolicy( | |
160 const std::string& account_id, | |
161 const std::string& policy_blob, | |
162 const StorePolicyCallback& callback) OVERRIDE { | |
163 device_local_account_policy_[account_id] = policy_blob; | |
164 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true)); | |
165 } | |
166 | |
167 const std::string& device_policy() const { | |
168 return device_policy_; | |
169 } | |
170 void set_device_policy(const std::string& policy_blob) { | |
171 device_policy_ = policy_blob; | |
172 } | |
173 | |
174 const std::string& user_policy() const { | |
175 return user_policy_; | |
176 } | |
177 void set_user_policy(const std::string& policy_blob) { | |
178 user_policy_ = policy_blob; | |
179 } | |
180 | |
181 const std::string& device_local_account_policy( | |
182 const std::string& account_id) const { | |
183 std::map<std::string, std::string>::const_iterator entry = | |
184 device_local_account_policy_.find(account_id); | |
185 return entry != device_local_account_policy_.end() ? entry->second | |
186 : EmptyString(); | |
187 } | |
188 void set_device_local_account_policy(const std::string& account_id, | |
189 const std::string& policy_blob) { | |
190 device_local_account_policy_[account_id] = policy_blob; | |
191 } | |
192 | |
193 private: | |
194 std::string device_policy_; | |
195 std::string user_policy_; | |
196 std::map<std::string, std::string> device_local_account_policy_; | |
197 | |
198 DISALLOW_COPY_AND_ASSIGN(FakeSessionManagerClient); | |
199 }; | |
200 | |
201 class FakeCryptohomeClient : public chromeos::CryptohomeClient { | 73 class FakeCryptohomeClient : public chromeos::CryptohomeClient { |
202 public: | 74 public: |
203 using chromeos::CryptohomeClient::AsyncMethodCallback; | 75 using chromeos::CryptohomeClient::AsyncMethodCallback; |
204 using chromeos::CryptohomeClient::AsyncCallStatusHandler; | 76 using chromeos::CryptohomeClient::AsyncCallStatusHandler; |
205 using chromeos::CryptohomeClient::AsyncCallStatusWithDataHandler; | 77 using chromeos::CryptohomeClient::AsyncCallStatusWithDataHandler; |
206 | 78 |
207 FakeCryptohomeClient() {} | 79 FakeCryptohomeClient() {} |
208 virtual ~FakeCryptohomeClient() {} | 80 virtual ~FakeCryptohomeClient() {} |
209 | 81 |
210 virtual void SetAsyncCallStatusHandlers( | 82 virtual void SetAsyncCallStatusHandlers( |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 void CheckPublicSessionPresent(const std::string& id) { | 344 void CheckPublicSessionPresent(const std::string& id) { |
473 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(id); | 345 const chromeos::User* user = chromeos::UserManager::Get()->FindUser(id); |
474 ASSERT_TRUE(user); | 346 ASSERT_TRUE(user); |
475 EXPECT_EQ(id, user->email()); | 347 EXPECT_EQ(id, user->email()); |
476 EXPECT_EQ(chromeos::User::USER_TYPE_PUBLIC_ACCOUNT, user->GetType()); | 348 EXPECT_EQ(chromeos::User::USER_TYPE_PUBLIC_ACCOUNT, user->GetType()); |
477 } | 349 } |
478 | 350 |
479 LocalPolicyTestServer test_server_; | 351 LocalPolicyTestServer test_server_; |
480 base::ScopedTempDir temp_dir_; | 352 base::ScopedTempDir temp_dir_; |
481 | 353 |
482 FakeSessionManagerClient session_manager_client_; | 354 chromeos::FakeSessionManagerClient session_manager_client_; |
483 FakeCryptohomeClient cryptohome_client_; | 355 FakeCryptohomeClient cryptohome_client_; |
484 }; | 356 }; |
485 | 357 |
486 static bool IsKnownUser(const std::string& account_id) { | 358 static bool IsKnownUser(const std::string& account_id) { |
487 return chromeos::UserManager::Get()->IsKnownUser(account_id); | 359 return chromeos::UserManager::Get()->IsKnownUser(account_id); |
488 } | 360 } |
489 | 361 |
490 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, LoginScreen) { | 362 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, LoginScreen) { |
491 NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, | 363 content::NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, |
492 base::Bind(&IsKnownUser, kAccountId1)).Run(); | 364 base::Bind(&IsKnownUser, kAccountId1)).Run(); |
493 NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, | 365 content::NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, |
494 base::Bind(&IsKnownUser, kAccountId2)).Run(); | 366 base::Bind(&IsKnownUser, kAccountId2)).Run(); |
495 | 367 |
496 CheckPublicSessionPresent(kAccountId1); | 368 CheckPublicSessionPresent(kAccountId1); |
497 CheckPublicSessionPresent(kAccountId2); | 369 CheckPublicSessionPresent(kAccountId2); |
498 } | 370 } |
499 | 371 |
500 static bool DisplayNameMatches(const std::string& account_id, | 372 static bool DisplayNameMatches(const std::string& account_id, |
501 const std::string& display_name) { | 373 const std::string& display_name) { |
502 const chromeos::User* user = | 374 const chromeos::User* user = |
503 chromeos::UserManager::Get()->FindUser(account_id); | 375 chromeos::UserManager::Get()->FindUser(account_id); |
504 if (!user || user->display_name().empty()) | 376 if (!user || user->display_name().empty()) |
505 return false; | 377 return false; |
506 EXPECT_EQ(UTF8ToUTF16(display_name), user->display_name()); | 378 EXPECT_EQ(UTF8ToUTF16(display_name), user->display_name()); |
507 return true; | 379 return true; |
508 } | 380 } |
509 | 381 |
510 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, DisplayName) { | 382 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, DisplayName) { |
511 NotificationWatcher( | 383 content::NotificationWatcher( |
512 chrome::NOTIFICATION_USER_LIST_CHANGED, | 384 chrome::NOTIFICATION_USER_LIST_CHANGED, |
513 base::Bind(&DisplayNameMatches, kAccountId1, kDisplayName1)).Run(); | 385 base::Bind(&DisplayNameMatches, kAccountId1, kDisplayName1)).Run(); |
514 } | 386 } |
515 | 387 |
516 #if defined(OS_CHROMEOS) | 388 #if defined(OS_CHROMEOS) |
517 // Fails on ChromeOS http://crbug.com/177880 | 389 // Fails on ChromeOS http://crbug.com/177880 |
518 #define MAYBE_PolicyDownload DISABLED_PolicyDownload | 390 #define MAYBE_PolicyDownload DISABLED_PolicyDownload |
519 #else | 391 #else |
520 #define MAYBE_PolicyDownload PolicyDownload | 392 #define MAYBE_PolicyDownload PolicyDownload |
521 #endif | 393 #endif |
522 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, MAYBE_PolicyDownload) { | 394 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, MAYBE_PolicyDownload) { |
523 // Policy for kAccountId2 is not installed in session_manager_client, make | 395 // Policy for kAccountId2 is not installed in session_manager_client, make |
524 // sure it gets fetched from the server. Note that the test setup doesn't set | 396 // sure it gets fetched from the server. Note that the test setup doesn't set |
525 // up policy for kAccountId2, so the presence of the display name can be used | 397 // up policy for kAccountId2, so the presence of the display name can be used |
526 // as signal to indicate successful policy download. | 398 // as signal to indicate successful policy download. |
527 ASSERT_TRUE(session_manager_client_.device_local_account_policy( | 399 ASSERT_TRUE(session_manager_client_.device_local_account_policy( |
528 kAccountId2).empty()); | 400 kAccountId2).empty()); |
529 NotificationWatcher( | 401 content::NotificationWatcher( |
530 chrome::NOTIFICATION_USER_LIST_CHANGED, | 402 chrome::NOTIFICATION_USER_LIST_CHANGED, |
531 base::Bind(&DisplayNameMatches, kAccountId2, kDisplayName2)).Run(); | 403 base::Bind(&DisplayNameMatches, kAccountId2, kDisplayName2)).Run(); |
532 | 404 |
533 // Sanity check: The policy should be present now. | 405 // Sanity check: The policy should be present now. |
534 ASSERT_FALSE(session_manager_client_.device_local_account_policy( | 406 ASSERT_FALSE(session_manager_client_.device_local_account_policy( |
535 kAccountId2).empty()); | 407 kAccountId2).empty()); |
536 } | 408 } |
537 | 409 |
538 static bool IsNotKnownUser(const std::string& account_id) { | 410 static bool IsNotKnownUser(const std::string& account_id) { |
539 return !IsKnownUser(account_id); | 411 return !IsKnownUser(account_id); |
540 } | 412 } |
541 | 413 |
542 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, DevicePolicyChange) { | 414 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, DevicePolicyChange) { |
543 // Wait until the login screen is up. | 415 // Wait until the login screen is up. |
544 NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, | 416 content::NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, |
545 base::Bind(&IsKnownUser, kAccountId1)).Run(); | 417 base::Bind(&IsKnownUser, kAccountId1)).Run(); |
546 NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, | 418 content::NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, |
547 base::Bind(&IsKnownUser, kAccountId2)).Run(); | 419 base::Bind(&IsKnownUser, kAccountId2)).Run(); |
548 | 420 |
549 // Update policy to remove kAccountId2. | 421 // Update policy to remove kAccountId2. |
550 em::ChromeDeviceSettingsProto policy; | 422 em::ChromeDeviceSettingsProto policy; |
551 policy.mutable_show_user_names()->set_show_user_names(true); | 423 policy.mutable_show_user_names()->set_show_user_names(true); |
552 policy.mutable_device_local_accounts()->add_account()->set_id(kAccountId1); | 424 policy.mutable_device_local_accounts()->add_account()->set_id(kAccountId1); |
553 | 425 |
554 test_server_.UpdatePolicy(dm_protocol::kChromeDevicePolicyType, std::string(), | 426 test_server_.UpdatePolicy(dm_protocol::kChromeDevicePolicyType, std::string(), |
555 policy.SerializeAsString()); | 427 policy.SerializeAsString()); |
556 g_browser_process->policy_service()->RefreshPolicies(base::Closure()); | 428 g_browser_process->policy_service()->RefreshPolicies(base::Closure()); |
557 | 429 |
558 // Make sure the second device-local account disappears. | 430 // Make sure the second device-local account disappears. |
559 NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, | 431 content::NotificationWatcher(chrome::NOTIFICATION_USER_LIST_CHANGED, |
560 base::Bind(&IsNotKnownUser, kAccountId2)).Run(); | 432 base::Bind(&IsNotKnownUser, kAccountId2)).Run(); |
561 } | 433 } |
562 | 434 |
563 static bool IsSessionStarted() { | 435 static bool IsSessionStarted() { |
564 return chromeos::UserManager::Get()->IsSessionStarted(); | 436 return chromeos::UserManager::Get()->IsSessionStarted(); |
565 } | 437 } |
566 | 438 |
567 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, StartSession) { | 439 IN_PROC_BROWSER_TEST_F(DeviceLocalAccountTest, StartSession) { |
568 // This observes the display name becoming available as this indicates | 440 // This observes the display name becoming available as this indicates |
569 // device-local account policy is fully loaded, which is a prerequisite for | 441 // device-local account policy is fully loaded, which is a prerequisite for |
570 // successful login. | 442 // successful login. |
571 NotificationWatcher( | 443 content::NotificationWatcher( |
572 chrome::NOTIFICATION_USER_LIST_CHANGED, | 444 chrome::NOTIFICATION_USER_LIST_CHANGED, |
573 base::Bind(&DisplayNameMatches, kAccountId1, kDisplayName1)).Run(); | 445 base::Bind(&DisplayNameMatches, kAccountId1, kDisplayName1)).Run(); |
574 | 446 |
575 chromeos::ExistingUserController* controller = | 447 chromeos::ExistingUserController* controller = |
576 chromeos::ExistingUserController::current_controller(); | 448 chromeos::ExistingUserController::current_controller(); |
577 ASSERT_TRUE(controller); | 449 ASSERT_TRUE(controller); |
578 controller->LoginAsPublicAccount(kAccountId1); | 450 controller->LoginAsPublicAccount(kAccountId1); |
579 | 451 |
580 // Wait for the session to start. | 452 // Wait for the session to start. |
581 NotificationWatcher(chrome::NOTIFICATION_SESSION_STARTED, | 453 content::NotificationWatcher(chrome::NOTIFICATION_SESSION_STARTED, |
582 base::Bind(IsSessionStarted)).Run(); | 454 base::Bind(IsSessionStarted)).Run(); |
583 | 455 |
584 // Check that the startup pages specified in policy were opened. | 456 // Check that the startup pages specified in policy were opened. |
585 EXPECT_EQ(1U, chrome::GetTotalBrowserCount()); | 457 EXPECT_EQ(1U, chrome::GetTotalBrowserCount()); |
586 Browser* browser = | 458 Browser* browser = |
587 chrome::FindLastActiveWithHostDesktopType(chrome::HOST_DESKTOP_TYPE_ASH); | 459 chrome::FindLastActiveWithHostDesktopType(chrome::HOST_DESKTOP_TYPE_ASH); |
588 ASSERT_TRUE(browser); | 460 ASSERT_TRUE(browser); |
589 | 461 |
590 TabStripModel* tabs = browser->tab_strip_model(); | 462 TabStripModel* tabs = browser->tab_strip_model(); |
591 ASSERT_TRUE(tabs); | 463 ASSERT_TRUE(tabs); |
592 int expected_tab_count = static_cast<int>(arraysize(kStartupURLs)); | 464 int expected_tab_count = static_cast<int>(arraysize(kStartupURLs)); |
593 EXPECT_EQ(expected_tab_count, tabs->count()); | 465 EXPECT_EQ(expected_tab_count, tabs->count()); |
594 for (int i = 0; i < expected_tab_count && i < tabs->count(); ++i) | 466 for (int i = 0; i < expected_tab_count && i < tabs->count(); ++i) |
595 EXPECT_EQ(GURL(kStartupURLs[i]), tabs->GetWebContentsAt(i)->GetURL()); | 467 EXPECT_EQ(GURL(kStartupURLs[i]), tabs->GetWebContentsAt(i)->GetURL()); |
596 } | 468 } |
597 | 469 |
598 } // namespace policy | 470 } // namespace policy |
OLD | NEW |