OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 5 #include <string> |
6 | 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" | 7 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
14 #include "base/strings/stringprintf.h" | 9 #include "base/values.h" |
15 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chromeos/policy/login_policy_test_base.h" |
16 #include "chrome/browser/chromeos/login/test/oobe_base_test.h" | 11 #include "chrome/browser/prefs/session_startup_pref.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" | 12 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/browser/ui/browser_list.h" | 13 #include "chrome/browser/ui/browser_list.h" |
21 #include "chrome/browser/ui/host_desktop.h" | 14 #include "chrome/browser/ui/host_desktop.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
23 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" | 16 #include "policy/policy_constants.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 | 17 |
34 namespace policy { | 18 namespace policy { |
35 | 19 |
36 namespace { | 20 class UserCloudPolicyManagerTest : public LoginPolicyTestBase { |
| 21 protected: |
| 22 UserCloudPolicyManagerTest() : LoginPolicyTestBase() {} |
37 | 23 |
38 const char kAccountId[] = "dla1@example.com"; | 24 scoped_ptr<base::DictionaryValue> GetMandatoryPoliciesValue() const override { |
39 const char kAccountPassword[] = "letmein"; | 25 scoped_ptr<base::ListValue> list(new base::ListValue); |
40 const char* const kStartupURLs[] = {"chrome://policy", "chrome://about"}; | 26 list->AppendString("chrome://policy"); |
41 const char kTestAuthCode[] = "fake-auth-code"; | 27 list->AppendString("chrome://about"); |
42 const char kTestGaiaUberToken[] = "fake-uber-token"; | |
43 const char kTestAuthLoginAccessToken[] = "fake-access-token"; | |
44 const char kTestRefreshToken[] = "fake-refresh-token"; | |
45 const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie"; | |
46 const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie"; | |
47 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; | |
48 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; | |
49 const char kTestUserinfoToken[] = "fake-userinfo-token"; | |
50 | 28 |
51 } // namespace | 29 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 30 dict->Set(key::kRestoreOnStartupURLs, list.Pass()); |
| 31 dict->SetInteger(key::kRestoreOnStartup, |
| 32 SessionStartupPref::kPrefValueURLs); |
52 | 33 |
53 class UserCloudPolicyManagerTest : public chromeos::OobeBaseTest { | 34 return dict; |
54 protected: | |
55 UserCloudPolicyManagerTest() { | |
56 set_open_about_blank_on_browser_launch(false); | |
57 } | 35 } |
58 | 36 |
59 ~UserCloudPolicyManagerTest() override {} | |
60 | |
61 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 void SetUpCommandLine(base::CommandLine* command_line) override { | |
72 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | |
73 test_server_->GetServiceURL().spec()); | |
74 OobeBaseTest::SetUpCommandLine(command_line); | |
75 } | |
76 | |
77 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: | 37 private: |
162 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); | 38 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); |
163 }; | 39 }; |
164 | 40 |
165 IN_PROC_BROWSER_TEST_F(UserCloudPolicyManagerTest, StartSession) { | 41 IN_PROC_BROWSER_TEST_F(UserCloudPolicyManagerTest, StartSession) { |
| 42 const char* const kStartupURLs[] = {"chrome://policy", "chrome://about"}; |
| 43 |
166 SkipToLoginScreen(); | 44 SkipToLoginScreen(); |
167 | |
168 LogIn(kAccountId, kAccountPassword); | 45 LogIn(kAccountId, kAccountPassword); |
169 | 46 |
170 // Check that the startup pages specified in policy were opened. | 47 // Check that the startup pages specified in policy were opened. |
171 BrowserList* browser_list = | 48 BrowserList* browser_list = |
172 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); | 49 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); |
173 EXPECT_EQ(1U, browser_list->size()); | 50 EXPECT_EQ(1U, browser_list->size()); |
174 Browser* browser = browser_list->get(0); | 51 Browser* browser = browser_list->get(0); |
175 ASSERT_TRUE(browser); | 52 ASSERT_TRUE(browser); |
176 | 53 |
177 TabStripModel* tabs = browser->tab_strip_model(); | 54 TabStripModel* tabs = browser->tab_strip_model(); |
178 ASSERT_TRUE(tabs); | 55 ASSERT_TRUE(tabs); |
179 const int expected_tab_count = static_cast<int>(arraysize(kStartupURLs)); | 56 const int expected_tab_count = static_cast<int>(arraysize(kStartupURLs)); |
180 EXPECT_EQ(expected_tab_count, tabs->count()); | 57 EXPECT_EQ(expected_tab_count, tabs->count()); |
181 for (int i = 0; i < expected_tab_count && i < tabs->count(); ++i) { | 58 for (int i = 0; i < expected_tab_count && i < tabs->count(); ++i) { |
182 EXPECT_EQ(GURL(kStartupURLs[i]), | 59 EXPECT_EQ(GURL(kStartupURLs[i]), |
183 tabs->GetWebContentsAt(i)->GetVisibleURL()); | 60 tabs->GetWebContentsAt(i)->GetVisibleURL()); |
184 } | 61 } |
185 } | 62 } |
186 | 63 |
187 } // namespace policy | 64 } // namespace policy |
OLD | NEW |