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. 4 5#include <string> 6 7#include "base/command_line.h" 8#include "base/compiler_specific.h" 9#include "base/files/file_path.h" 10#include "base/files/file_util.h" 11#include "base/files/scoped_temp_dir.h" 12#include "base/macros.h" 13#include "base/memory/scoped_ptr.h" 14#include "base/strings/stringprintf.h" 15#include "chrome/browser/chrome_notification_types.h" 16#include "chrome/browser/chromeos/login/test/oobe_base_test.h" 17#include "chrome/browser/chromeos/login/wizard_controller.h" 18#include "chrome/browser/policy/test/local_policy_test_server.h" 19#include "chrome/browser/ui/browser.h" 20#include "chrome/browser/ui/browser_list.h" 21#include "chrome/browser/ui/host_desktop.h" 22#include "chrome/browser/ui/tabs/tab_strip_model.h" 23#include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" 24#include "components/policy/core/common/cloud/cloud_policy_constants.h" 25#include "components/policy/core/common/policy_switches.h" 26#include "content/public/browser/notification_service.h" 27#include "content/public/test/test_utils.h" 28#include "google_apis/gaia/fake_gaia.h" 29#include "google_apis/gaia/gaia_constants.h" 30#include "google_apis/gaia/gaia_urls.h" 31#include "testing/gtest/include/gtest/gtest.h" 32#include "url/gurl.h" 33 34namespace policy { 35 36namespace { 37 38const char kAccountId[] = "dla1@example.com"; 39const char kAccountPassword[] = "letmein"; 40const char* kStartupURLs[] = {"chrome://policy", "chrome://about"}; 41const char kTestAuthCode[] = "fake-auth-code"; 42const char kTestGaiaUberToken[] = "fake-uber-token"; 43const char kTestAuthLoginAccessToken[] = "fake-access-token"; 44const char kTestRefreshToken[] = "fake-refresh-token"; 45const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie"; 46const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie"; 47const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; 48const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; 49const char kTestUserinfoToken[] = "fake-userinfo-token"; 50 51} // namespace 52 53class UserCloudPolicyManagerTest : public chromeos::OobeBaseTest { 54 protected: 55 UserCloudPolicyManagerTest() { 56 set_open_about_blank_on_browser_launch(false); 57 } 58 59 virtual ~UserCloudPolicyManagerTest() {} 60 61 virtual void SetUp() OVERRIDE { 62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 63 SetServerPolicy(); 64 65 test_server_.reset(new LocalPolicyTestServer(policy_file_path())); 66 ASSERT_TRUE(test_server_->Start()); 67 68 OobeBaseTest::SetUp(); 69 } 70 71 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 72 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, 73 test_server_->GetServiceURL().spec()); 74 OobeBaseTest::SetUpCommandLine(command_line); 75 } 76 77 virtual void SetUpOnMainThread() OVERRIDE { 78 SetMergeSessionParams(kAccountId); 79 SetupGaiaServerWithAccessTokens(); 80 OobeBaseTest::SetUpOnMainThread(); 81 } 82 83 void SetupGaiaServerWithAccessTokens() { 84 FakeGaia::AccessTokenInfo token_info; 85 token_info.token = kTestUserinfoToken; 86 token_info.scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); 87 token_info.scopes.insert(GaiaConstants::kOAuthWrapBridgeUserInfoScope); 88 token_info.audience = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); 89 token_info.email = kAccountId; 90 fake_gaia_->IssueOAuthToken(kTestRefreshToken, token_info); 91 } 92 93 void SetMergeSessionParams(const std::string& email) { 94 FakeGaia::MergeSessionParams params; 95 params.auth_sid_cookie = kTestAuthSIDCookie; 96 params.auth_lsid_cookie = kTestAuthLSIDCookie; 97 params.auth_code = kTestAuthCode; 98 params.refresh_token = kTestRefreshToken; 99 params.access_token = kTestAuthLoginAccessToken; 100 params.gaia_uber_token = kTestGaiaUberToken; 101 params.session_sid_cookie = kTestSessionSIDCookie; 102 params.session_lsid_cookie = kTestSessionLSIDCookie; 103 params.email = email; 104 fake_gaia_->SetMergeSessionParams(params); 105 } 106 107 void SkipToLoginScreen() { 108 chromeos::WizardController::SkipPostLoginScreensForTesting(); 109 chromeos::WizardController* wizard_controller = 110 chromeos::WizardController::default_controller(); 111 ASSERT_TRUE(wizard_controller); 112 wizard_controller->SkipToLoginForTesting(chromeos::LoginScreenContext()); 113 114 content::WindowedNotificationObserver( 115 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, 116 content::NotificationService::AllSources()).Wait(); 117 } 118 119 void LogIn(const std::string& user_id, const std::string& password) { 120 GetLoginDisplay()->ShowSigninScreenForCreds(user_id, password); 121 122 content::WindowedNotificationObserver( 123 chrome::NOTIFICATION_SESSION_STARTED, 124 content::NotificationService::AllSources()).Wait(); 125 } 126 127 void SetServerPolicy() { 128 const char kPolicy[] = 129 "{" 130 " \"%s\": {" 131 " \"mandatory\": {" 132 " \"RestoreOnStartup\": 4," 133 " \"RestoreOnStartupURLs\": [" 134 " \"chrome://policy\"," 135 " \"chrome://about\"" 136 " ]" 137 " }," 138 " \"recommended\": {}" 139 " }," 140 " \"managed_users\": [ \"*\" ]," 141 " \"policy_user\": \"%s\"," 142 " \"current_key_index\": 0" 143 "}"; 144 145 const std::string policy = base::StringPrintf( 146 kPolicy, dm_protocol::kChromeUserPolicyType, kAccountId); 147 148 const int bytes_written = 149 base::WriteFile(policy_file_path(), policy.data(), policy.size()); 150 ASSERT_EQ(static_cast<int>(policy.size()), bytes_written); 151 } 152 153 base::FilePath policy_file_path() const { 154 return temp_dir_.path().AppendASCII("policy.json"); 155 } 156 157 scoped_ptr<LocalPolicyTestServer> test_server_; 158 159 base::ScopedTempDir temp_dir_; 160 161 private: 162 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); 163}; 164 165IN_PROC_BROWSER_TEST_F(UserCloudPolicyManagerTest, StartSession) { 166 SkipToLoginScreen(); 167 168 LogIn(kAccountId, kAccountPassword); 169 170 // Check that the startup pages specified in policy were opened. 171 BrowserList* browser_list = 172 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); 173 EXPECT_EQ(1U, browser_list->size()); 174 Browser* browser = browser_list->get(0); 175 ASSERT_TRUE(browser); 176 177 TabStripModel* tabs = browser->tab_strip_model(); 178 ASSERT_TRUE(tabs); 179 const int expected_tab_count = static_cast<int>(arraysize(kStartupURLs)); 180 EXPECT_EQ(expected_tab_count, tabs->count()); 181 for (int i = 0; i < expected_tab_count && i < tabs->count(); ++i) { 182 EXPECT_EQ(GURL(kStartupURLs[i]), 183 tabs->GetWebContentsAt(i)->GetVisibleURL()); 184 } 185} 186 187} // namespace policy 188