OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/policy/user_policy_test_helper.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/json/json_writer.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/values.h" |
| 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" |
| 15 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom
eos.h" |
| 16 #include "chrome/browser/policy/profile_policy_connector.h" |
| 17 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 18 #include "chrome/browser/policy/test/local_policy_test_server.h" |
| 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "components/policy/core/browser/browser_policy_connector.h" |
| 21 #include "components/policy/core/common/cloud/cloud_policy_client.h" |
| 22 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| 23 #include "components/policy/core/common/cloud/cloud_policy_core.h" |
| 24 #include "components/policy/core/common/policy_service.h" |
| 25 #include "components/policy/core/common/policy_switches.h" |
| 26 #include "policy/proto/device_management_backend.pb.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "url/gurl.h" |
| 29 |
| 30 namespace policy { |
| 31 |
| 32 namespace { |
| 33 |
| 34 std::string BuildPolicy(const base::DictionaryValue& mandatory, |
| 35 const base::DictionaryValue& recommended, |
| 36 const std::string& policyType, |
| 37 const std::string& account_id) { |
| 38 scoped_ptr<base::DictionaryValue> policy_type_dict(new base::DictionaryValue); |
| 39 policy_type_dict->SetWithoutPathExpansion("mandatory", |
| 40 mandatory.CreateDeepCopy()); |
| 41 policy_type_dict->SetWithoutPathExpansion("recommended", |
| 42 recommended.CreateDeepCopy()); |
| 43 |
| 44 scoped_ptr<base::ListValue> managed_users_list(new base::ListValue); |
| 45 managed_users_list->AppendString("*"); |
| 46 |
| 47 base::DictionaryValue root_dict; |
| 48 root_dict.SetWithoutPathExpansion(policyType, policy_type_dict.Pass()); |
| 49 root_dict.SetWithoutPathExpansion("managed_users", managed_users_list.Pass()); |
| 50 root_dict.SetStringWithoutPathExpansion("policy_user", account_id); |
| 51 root_dict.SetIntegerWithoutPathExpansion("current_key_index", 0); |
| 52 |
| 53 std::string json_policy; |
| 54 base::JSONWriter::WriteWithOptions( |
| 55 root_dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_policy); |
| 56 return json_policy; |
| 57 } |
| 58 |
| 59 } // namespace |
| 60 |
| 61 UserPolicyTestHelper::UserPolicyTestHelper(const std::string& account_id) |
| 62 : account_id_(account_id) { |
| 63 } |
| 64 |
| 65 UserPolicyTestHelper::~UserPolicyTestHelper() { |
| 66 } |
| 67 |
| 68 void UserPolicyTestHelper::Init( |
| 69 const base::DictionaryValue& mandatory_policy, |
| 70 const base::DictionaryValue& recommended_policy) { |
| 71 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 72 WritePolicyFile(mandatory_policy, recommended_policy); |
| 73 |
| 74 test_server_.reset(new LocalPolicyTestServer(PolicyFilePath())); |
| 75 ASSERT_TRUE(test_server_->Start()); |
| 76 } |
| 77 |
| 78 void UserPolicyTestHelper::UpdateCommandLine( |
| 79 base::CommandLine* command_line) const { |
| 80 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, |
| 81 test_server_->GetServiceURL().spec()); |
| 82 } |
| 83 |
| 84 void UserPolicyTestHelper::WaitForInitialPolicy(Profile* profile) { |
| 85 BrowserPolicyConnector* const connector = |
| 86 g_browser_process->browser_policy_connector(); |
| 87 connector->ScheduleServiceInitialization(0); |
| 88 |
| 89 UserCloudPolicyManagerChromeOS* const policy_manager = |
| 90 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile); |
| 91 |
| 92 // Give a bogus OAuth token to the |policy_manager|. This should make its |
| 93 // CloudPolicyClient fetch the DMToken. |
| 94 ASSERT_FALSE(policy_manager->core()->client()->is_registered()); |
| 95 const enterprise_management::DeviceRegisterRequest::Type registration_type = |
| 96 enterprise_management::DeviceRegisterRequest::USER; |
| 97 policy_manager->core()->client()->Register( |
| 98 registration_type, |
| 99 enterprise_management::DeviceRegisterRequest::FLAVOR_USER_REGISTRATION, |
| 100 "bogus", std::string(), std::string(), std::string()); |
| 101 |
| 102 policy::ProfilePolicyConnector* const profile_connector = |
| 103 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile); |
| 104 policy::PolicyService* const policy_service = |
| 105 profile_connector->policy_service(); |
| 106 |
| 107 base::RunLoop run_loop; |
| 108 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
| 109 run_loop.Run(); |
| 110 } |
| 111 |
| 112 void UserPolicyTestHelper::UpdatePolicy( |
| 113 const base::DictionaryValue& mandatory_policy, |
| 114 const base::DictionaryValue& recommended_policy, |
| 115 Profile* profile) { |
| 116 WritePolicyFile(mandatory_policy, recommended_policy); |
| 117 |
| 118 policy::ProfilePolicyConnector* const profile_connector = |
| 119 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile); |
| 120 policy::PolicyService* const policy_service = |
| 121 profile_connector->policy_service(); |
| 122 |
| 123 base::RunLoop run_loop; |
| 124 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
| 125 run_loop.Run(); |
| 126 } |
| 127 |
| 128 void UserPolicyTestHelper::WritePolicyFile( |
| 129 const base::DictionaryValue& mandatory, |
| 130 const base::DictionaryValue& recommended) { |
| 131 const std::string policy = BuildPolicy( |
| 132 mandatory, recommended, dm_protocol::kChromeUserPolicyType, account_id_); |
| 133 const int bytes_written = |
| 134 base::WriteFile(PolicyFilePath(), policy.data(), policy.size()); |
| 135 ASSERT_EQ(static_cast<int>(policy.size()), bytes_written); |
| 136 } |
| 137 |
| 138 base::FilePath UserPolicyTestHelper::PolicyFilePath() const { |
| 139 return temp_dir_.path().AppendASCII("policy.json"); |
| 140 } |
| 141 |
| 142 } // namespace policy |
OLD | NEW |