OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/logging.h" | |
9 #include "base/values.h" | 10 #include "base/values.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/chromeos/login/ui/webui_login_display.h" | 12 #include "chrome/browser/chromeos/login/ui/webui_login_display.h" |
12 #include "chrome/browser/chromeos/login/wizard_controller.h" | 13 #include "chrome/browser/chromeos/login/wizard_controller.h" |
13 #include "chrome/browser/chromeos/policy/login_policy_test_base.h" | 14 #include "chrome/browser/chromeos/policy/login_policy_test_base.h" |
14 #include "chrome/browser/policy/test/local_policy_test_server.h" | 15 #include "chrome/browser/policy/test/local_policy_test_server.h" |
15 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" | 16 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" |
16 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 17 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
17 #include "components/policy/core/common/policy_switches.h" | 18 #include "components/policy/core/common/policy_switches.h" |
18 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
19 #include "content/public/test/test_utils.h" | 20 #include "content/public/test/test_utils.h" |
20 #include "google_apis/gaia/fake_gaia.h" | 21 #include "google_apis/gaia/fake_gaia.h" |
21 #include "google_apis/gaia/gaia_constants.h" | 22 #include "google_apis/gaia/gaia_constants.h" |
22 #include "google_apis/gaia/gaia_urls.h" | 23 #include "google_apis/gaia/gaia_urls.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
25 | 26 |
27 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" | |
28 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom eos.h" | |
29 #include "components/policy/core/common/cloud/cloud_policy_client.h" | |
30 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | |
31 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" | |
32 #include "components/policy/core/browser/browser_policy_connector.h" | |
33 #include "chrome/browser/browser_process.h" | |
34 #include "chrome/browser/policy/profile_policy_connector.h" | |
35 #include "chrome/browser/policy/profile_policy_connector_factory.h" | |
36 #include "components/policy/core/common/policy_service.h" | |
37 | |
38 | |
26 namespace policy { | 39 namespace policy { |
27 | 40 |
28 namespace { | 41 namespace { |
29 | 42 |
30 const char kTestAuthCode[] = "fake-auth-code"; | 43 const char kTestAuthCode[] = "fake-auth-code"; |
31 const char kTestGaiaUberToken[] = "fake-uber-token"; | 44 const char kTestGaiaUberToken[] = "fake-uber-token"; |
32 const char kTestAuthLoginAccessToken[] = "fake-access-token"; | 45 const char kTestAuthLoginAccessToken[] = "fake-access-token"; |
33 const char kTestRefreshToken[] = "fake-refresh-token"; | 46 const char kTestRefreshToken[] = "fake-refresh-token"; |
34 const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie"; | 47 const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie"; |
35 const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie"; | 48 const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie"; |
36 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; | 49 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; |
37 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; | 50 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; |
38 const char kTestUserinfoToken[] = "fake-userinfo-token"; | 51 const char kTestUserinfoToken[] = "fake-userinfo-token"; |
39 | 52 |
40 std::string GetPolicy(scoped_ptr<base::DictionaryValue> mandatory, | 53 std::string GetPolicy(const base::DictionaryValue& mandatory, |
41 scoped_ptr<base::DictionaryValue> recommended, | 54 const base::DictionaryValue& recommended, |
42 const std::string& policyType, | 55 const std::string& policyType, |
43 const std::string& account) { | 56 const std::string& account) { |
44 scoped_ptr<base::DictionaryValue> policy_type_dict(new base::DictionaryValue); | 57 scoped_ptr<base::DictionaryValue> policy_type_dict(new base::DictionaryValue); |
45 policy_type_dict->Set("mandatory", mandatory.Pass()); | 58 policy_type_dict->Set("mandatory", mandatory.CreateDeepCopy()); |
bartfab (slow)
2015/06/11 23:33:09
I wonder whether the JSON will be slightly differe
pneubeck (no reviews)
2015/06/12 08:55:18
good point and indeed nullptr would lead to a cras
| |
46 policy_type_dict->Set("recommended", recommended.Pass()); | 59 policy_type_dict->Set("recommended", recommended.CreateDeepCopy()); |
47 | 60 |
48 scoped_ptr<base::ListValue> managed_users_list(new base::ListValue); | 61 scoped_ptr<base::ListValue> managed_users_list(new base::ListValue); |
49 managed_users_list->AppendString("*"); | 62 managed_users_list->AppendString("*"); |
50 | 63 |
51 base::DictionaryValue root_dict; | 64 base::DictionaryValue root_dict; |
52 root_dict.Set(policyType, policy_type_dict.Pass()); | 65 root_dict.Set(policyType, policy_type_dict.Pass()); |
53 root_dict.Set("managed_users", managed_users_list.Pass()); | 66 root_dict.Set("managed_users", managed_users_list.Pass()); |
54 root_dict.SetString("policy_user", account); | 67 root_dict.SetString("policy_user", account); |
55 root_dict.SetInteger("current_key_index", 0); | 68 root_dict.SetInteger("current_key_index", 0); |
56 | 69 |
(...skipping 10 matching lines...) Expand all Loading... | |
67 | 80 |
68 LoginPolicyTestBase::LoginPolicyTestBase() { | 81 LoginPolicyTestBase::LoginPolicyTestBase() { |
69 // TODO(nkostylev): Fix this test harness for webview. http://crbug.com/477402 | 82 // TODO(nkostylev): Fix this test harness for webview. http://crbug.com/477402 |
70 set_use_webview(false); | 83 set_use_webview(false); |
71 set_open_about_blank_on_browser_launch(false); | 84 set_open_about_blank_on_browser_launch(false); |
72 } | 85 } |
73 | 86 |
74 LoginPolicyTestBase::~LoginPolicyTestBase() { | 87 LoginPolicyTestBase::~LoginPolicyTestBase() { |
75 } | 88 } |
76 | 89 |
77 void LoginPolicyTestBase::SetUp() { | 90 PolicyTestHelper::PolicyTestHelper(const std::string& user_email, |
78 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 91 const base::DictionaryValue& mandatory, |
79 SetServerPolicy(); | 92 const base::DictionaryValue& recommended) |
93 : user_email_(user_email) { | |
94 CHECK(temp_dir_.CreateUniqueTempDir()); | |
bartfab (slow)
2015/06/11 23:33:09
Since this will be used in tests only, I think an
pneubeck (no reviews)
2015/06/12 08:55:18
Done.
| |
95 SetServerPolicy(mandatory, recommended); | |
80 | 96 |
81 test_server_.reset(new LocalPolicyTestServer(PolicyFilePath())); | 97 test_server_.reset(new LocalPolicyTestServer(PolicyFilePath())); |
82 ASSERT_TRUE(test_server_->Start()); | 98 CHECK(test_server_->Start()); |
99 } | |
83 | 100 |
101 void PolicyTestHelper::UpdateCommandLine(base::CommandLine* command_line) { | |
102 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | |
103 test_server_->GetServiceURL().spec()); | |
104 } | |
105 void PolicyTestHelper::UpdatePolicy(Profile* profile, | |
bartfab (slow)
2015/06/11 23:33:09
Nit: Missing blank line.
pneubeck (no reviews)
2015/06/12 08:55:18
Done.
| |
106 const base::DictionaryValue& mandatory, | |
107 const base::DictionaryValue& recommended) { | |
108 SetServerPolicy(mandatory, recommended); | |
109 | |
110 policy::ProfilePolicyConnector* profile_connector = | |
bartfab (slow)
2015/06/11 23:33:09
Nit: Here and below: const pointers.
pneubeck (no reviews)
2015/06/12 08:55:18
Done.
| |
111 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile); | |
112 policy::PolicyService* policy_service = profile_connector->policy_service(); | |
113 | |
114 base::RunLoop run_loop; | |
115 policy_service->RefreshPolicies(run_loop.QuitClosure()); | |
116 run_loop.Run(); | |
117 } | |
118 | |
119 void PolicyTestHelper::WaitForInitialPolicy(Profile* profile) { | |
120 BrowserPolicyConnector* connector = | |
121 g_browser_process->browser_policy_connector(); | |
122 connector->ScheduleServiceInitialization(0); | |
123 | |
124 UserCloudPolicyManagerChromeOS* policy_manager = | |
125 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile); | |
126 ASSERT_TRUE(policy_manager); | |
127 | |
128 ASSERT_TRUE(policy_manager->core()->client()); | |
129 | |
130 // Give a bogus OAuth token to the |policy_manager|. This should make its | |
131 // CloudPolicyClient fetch the DMToken. | |
132 ASSERT_FALSE(policy_manager->core()->client()->is_registered()); | |
133 enterprise_management::DeviceRegisterRequest::Type registration_type = | |
bartfab (slow)
2015/06/11 23:33:09
Nit: const.
pneubeck (no reviews)
2015/06/12 08:55:18
Done.
| |
134 enterprise_management::DeviceRegisterRequest::USER; | |
135 policy_manager->core()->client()->Register( | |
136 registration_type, | |
137 enterprise_management::DeviceRegisterRequest::FLAVOR_USER_REGISTRATION, | |
138 "bogus", std::string(), std::string(), std::string()); | |
139 | |
140 policy::ProfilePolicyConnector* profile_connector = | |
141 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile); | |
142 policy::PolicyService* policy_service = profile_connector->policy_service(); | |
143 | |
144 base::RunLoop run_loop; | |
145 policy_service->RefreshPolicies(run_loop.QuitClosure()); | |
146 run_loop.Run(); | |
147 } | |
148 | |
149 PolicyTestHelper::~PolicyTestHelper() { | |
bartfab (slow)
2015/06/11 23:33:09
Nit: Reorder methods so that declaration and defin
pneubeck (no reviews)
2015/06/12 08:55:18
Done.
| |
150 } | |
151 | |
152 void PolicyTestHelper::SetServerPolicy( | |
153 const base::DictionaryValue& mandatory, | |
154 const base::DictionaryValue& recommended) { | |
155 const std::string policy = GetPolicy( | |
156 mandatory, recommended, dm_protocol::kChromeUserPolicyType, user_email_); | |
157 const int bytes_written = | |
158 base::WriteFile(PolicyFilePath(), policy.data(), policy.size()); | |
159 CHECK_EQ(static_cast<int>(policy.size()), bytes_written); | |
160 } | |
161 | |
162 base::FilePath PolicyTestHelper::PolicyFilePath() const { | |
163 return temp_dir_.path().AppendASCII("policy.json"); | |
164 } | |
165 | |
166 void LoginPolicyTestBase::SetUp() { | |
167 scoped_ptr<base::DictionaryValue> mandatory(GetMandatoryPoliciesValue()); | |
bartfab (slow)
2015/06/11 23:33:09
If you are switching from scoped_ptrs to deep copi
pneubeck (no reviews)
2015/06/12 08:55:18
Done.
| |
168 scoped_ptr<base::DictionaryValue> recommended(GetRecommendedPoliciesValue()); | |
169 policy_helper_.reset( | |
170 new PolicyTestHelper(kAccountId, *mandatory, *recommended)); | |
84 OobeBaseTest::SetUp(); | 171 OobeBaseTest::SetUp(); |
85 } | 172 } |
86 | 173 |
87 void LoginPolicyTestBase::SetUpCommandLine(base::CommandLine* command_line) { | 174 void LoginPolicyTestBase::SetUpCommandLine(base::CommandLine* command_line) { |
88 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | 175 policy_helper_->UpdateCommandLine(command_line); |
89 test_server_->GetServiceURL().spec()); | |
90 OobeBaseTest::SetUpCommandLine(command_line); | 176 OobeBaseTest::SetUpCommandLine(command_line); |
91 } | 177 } |
92 | 178 |
93 void LoginPolicyTestBase::SetUpOnMainThread() { | 179 void LoginPolicyTestBase::SetUpOnMainThread() { |
94 SetMergeSessionParams(kAccountId); | 180 SetMergeSessionParams(); |
95 SetUpGaiaServerWithAccessTokens(); | 181 SetUpGaiaServerWithAccessTokens(); |
96 OobeBaseTest::SetUpOnMainThread(); | 182 OobeBaseTest::SetUpOnMainThread(); |
97 } | 183 } |
98 | 184 |
99 scoped_ptr<base::DictionaryValue> | 185 scoped_ptr<base::DictionaryValue> |
100 LoginPolicyTestBase::GetMandatoryPoliciesValue() const { | 186 LoginPolicyTestBase::GetMandatoryPoliciesValue() const { |
101 return make_scoped_ptr(new base::DictionaryValue); | 187 return make_scoped_ptr(new base::DictionaryValue); |
102 } | 188 } |
103 | 189 |
104 scoped_ptr<base::DictionaryValue> | 190 scoped_ptr<base::DictionaryValue> |
105 LoginPolicyTestBase::GetRecommendedPoliciesValue() const { | 191 LoginPolicyTestBase::GetRecommendedPoliciesValue() const { |
106 return make_scoped_ptr(new base::DictionaryValue); | 192 return make_scoped_ptr(new base::DictionaryValue); |
107 } | 193 } |
108 | 194 |
109 void LoginPolicyTestBase::SetUpGaiaServerWithAccessTokens() { | 195 void LoginPolicyTestBase::SetUpGaiaServerWithAccessTokens() { |
110 FakeGaia::AccessTokenInfo token_info; | 196 FakeGaia::AccessTokenInfo token_info; |
111 token_info.token = kTestUserinfoToken; | 197 token_info.token = kTestUserinfoToken; |
112 token_info.scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); | 198 token_info.scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); |
113 token_info.scopes.insert(GaiaConstants::kOAuthWrapBridgeUserInfoScope); | 199 token_info.scopes.insert(GaiaConstants::kOAuthWrapBridgeUserInfoScope); |
114 token_info.audience = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); | 200 token_info.audience = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); |
115 token_info.email = kAccountId; | 201 token_info.email = kAccountId; |
116 fake_gaia_->IssueOAuthToken(kTestRefreshToken, token_info); | 202 fake_gaia_->IssueOAuthToken(kTestRefreshToken, token_info); |
117 } | 203 } |
118 | 204 |
119 void LoginPolicyTestBase::SetMergeSessionParams(const std::string& email) { | 205 void LoginPolicyTestBase::SetMergeSessionParams() { |
120 FakeGaia::MergeSessionParams params; | 206 FakeGaia::MergeSessionParams params; |
121 params.auth_sid_cookie = kTestAuthSIDCookie; | 207 params.auth_sid_cookie = kTestAuthSIDCookie; |
122 params.auth_lsid_cookie = kTestAuthLSIDCookie; | 208 params.auth_lsid_cookie = kTestAuthLSIDCookie; |
123 params.auth_code = kTestAuthCode; | 209 params.auth_code = kTestAuthCode; |
124 params.refresh_token = kTestRefreshToken; | 210 params.refresh_token = kTestRefreshToken; |
125 params.access_token = kTestAuthLoginAccessToken; | 211 params.access_token = kTestAuthLoginAccessToken; |
126 params.gaia_uber_token = kTestGaiaUberToken; | 212 params.gaia_uber_token = kTestGaiaUberToken; |
127 params.session_sid_cookie = kTestSessionSIDCookie; | 213 params.session_sid_cookie = kTestSessionSIDCookie; |
128 params.session_lsid_cookie = kTestSessionLSIDCookie; | 214 params.session_lsid_cookie = kTestSessionLSIDCookie; |
129 params.email = email; | 215 params.email = kAccountId; |
130 fake_gaia_->SetMergeSessionParams(params); | 216 fake_gaia_->SetMergeSessionParams(params); |
131 } | 217 } |
132 | 218 |
133 void LoginPolicyTestBase::SkipToLoginScreen() { | 219 void LoginPolicyTestBase::SkipToLoginScreen() { |
134 chromeos::WizardController::SkipPostLoginScreensForTesting(); | 220 chromeos::WizardController::SkipPostLoginScreensForTesting(); |
135 chromeos::WizardController* const wizard_controller = | 221 chromeos::WizardController* const wizard_controller = |
136 chromeos::WizardController::default_controller(); | 222 chromeos::WizardController::default_controller(); |
137 ASSERT_TRUE(wizard_controller); | 223 ASSERT_TRUE(wizard_controller); |
138 wizard_controller->SkipToLoginForTesting(chromeos::LoginScreenContext()); | 224 wizard_controller->SkipToLoginForTesting(chromeos::LoginScreenContext()); |
139 | 225 |
140 content::WindowedNotificationObserver( | 226 content::WindowedNotificationObserver( |
141 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 227 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
142 content::NotificationService::AllSources()).Wait(); | 228 content::NotificationService::AllSources()).Wait(); |
143 } | 229 } |
144 | 230 |
145 void LoginPolicyTestBase::LogIn(const std::string& user_id, | 231 void LoginPolicyTestBase::LogIn(const std::string& user_id, |
146 const std::string& password) { | 232 const std::string& password) { |
147 GetLoginDisplay()->ShowSigninScreenForCreds(user_id, password); | 233 GetLoginDisplay()->ShowSigninScreenForCreds(user_id, password); |
148 | 234 |
149 content::WindowedNotificationObserver( | 235 content::WindowedNotificationObserver( |
150 chrome::NOTIFICATION_SESSION_STARTED, | 236 chrome::NOTIFICATION_SESSION_STARTED, |
151 content::NotificationService::AllSources()).Wait(); | 237 content::NotificationService::AllSources()).Wait(); |
152 } | 238 } |
153 | 239 |
154 void LoginPolicyTestBase::SetServerPolicy() { | |
155 const std::string policy = | |
156 GetPolicy(GetMandatoryPoliciesValue(), GetRecommendedPoliciesValue(), | |
157 dm_protocol::kChromeUserPolicyType, kAccountId); | |
158 | |
159 const int bytes_written = | |
160 base::WriteFile(PolicyFilePath(), policy.data(), policy.size()); | |
161 ASSERT_EQ(static_cast<int>(policy.size()), bytes_written); | |
162 } | |
163 | |
164 base::FilePath LoginPolicyTestBase::PolicyFilePath() const { | |
165 return temp_dir_.path().AppendASCII("policy.json"); | |
166 } | |
167 | |
168 } // namespace policy | 240 } // namespace policy |
OLD | NEW |