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 "chrome/browser/policy/test/local_policy_test_server.h" | 5 #include "chrome/browser/policy/test/local_policy_test_server.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/base_paths.h" | 13 #include "base/base_paths.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/numerics/safe_conversions.h" | 17 #include "base/numerics/safe_conversions.h" |
18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 20 #include "base/threading/thread_restrictions.h" |
20 #include "base/values.h" | 21 #include "base/values.h" |
21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
22 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 23 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
23 #include "crypto/rsa_private_key.h" | 24 #include "crypto/rsa_private_key.h" |
24 #include "net/test/python_utils.h" | 25 #include "net/test/python_utils.h" |
25 | 26 |
26 namespace policy { | 27 namespace policy { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
(...skipping 26 matching lines...) Expand all Loading... |
56 bool IsUnsafeCharacter(char c) { | 57 bool IsUnsafeCharacter(char c) { |
57 return !(isalnum(c) || c == '.' || c == '@' || c == '-'); | 58 return !(isalnum(c) || c == '.' || c == '@' || c == '-'); |
58 } | 59 } |
59 | 60 |
60 } // namespace | 61 } // namespace |
61 | 62 |
62 LocalPolicyTestServer::LocalPolicyTestServer() | 63 LocalPolicyTestServer::LocalPolicyTestServer() |
63 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, | 64 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, |
64 net::BaseTestServer::kLocalhost, | 65 net::BaseTestServer::kLocalhost, |
65 base::FilePath()) { | 66 base::FilePath()) { |
| 67 base::ThreadRestrictions::ScopedAllowIO allow_io; |
66 CHECK(server_data_dir_.CreateUniqueTempDir()); | 68 CHECK(server_data_dir_.CreateUniqueTempDir()); |
67 config_file_ = server_data_dir_.GetPath().Append(kPolicyFileName); | 69 config_file_ = server_data_dir_.GetPath().Append(kPolicyFileName); |
68 } | 70 } |
69 | 71 |
70 LocalPolicyTestServer::LocalPolicyTestServer(const base::FilePath& config_file) | 72 LocalPolicyTestServer::LocalPolicyTestServer(const base::FilePath& config_file) |
71 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, | 73 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, |
72 net::BaseTestServer::kLocalhost, | 74 net::BaseTestServer::kLocalhost, |
73 base::FilePath()), | 75 base::FilePath()), |
74 config_file_(config_file) {} | 76 config_file_(config_file) {} |
75 | 77 |
76 LocalPolicyTestServer::LocalPolicyTestServer(const std::string& test_name) | 78 LocalPolicyTestServer::LocalPolicyTestServer(const std::string& test_name) |
77 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, | 79 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, |
78 net::BaseTestServer::kLocalhost, | 80 net::BaseTestServer::kLocalhost, |
79 base::FilePath()) { | 81 base::FilePath()) { |
80 // Read configuration from a file in chrome/test/data/policy. | 82 // Read configuration from a file in chrome/test/data/policy. |
| 83 base::ThreadRestrictions::ScopedAllowIO allow_io; |
81 base::FilePath source_root; | 84 base::FilePath source_root; |
82 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &source_root)); | 85 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &source_root)); |
83 config_file_ = source_root | 86 config_file_ = source_root |
84 .AppendASCII("chrome") | 87 .AppendASCII("chrome") |
85 .AppendASCII("test") | 88 .AppendASCII("test") |
86 .AppendASCII("data") | 89 .AppendASCII("data") |
87 .AppendASCII("policy") | 90 .AppendASCII("policy") |
88 .AppendASCII(base::StringPrintf("policy_%s.json", test_name.c_str())); | 91 .AppendASCII(base::StringPrintf("policy_%s.json", test_name.c_str())); |
89 } | 92 } |
90 | 93 |
91 LocalPolicyTestServer::~LocalPolicyTestServer() {} | 94 LocalPolicyTestServer::~LocalPolicyTestServer() {} |
92 | 95 |
93 bool LocalPolicyTestServer::SetSigningKeyAndSignature( | 96 bool LocalPolicyTestServer::SetSigningKeyAndSignature( |
94 const crypto::RSAPrivateKey* key, const std::string& signature) { | 97 const crypto::RSAPrivateKey* key, const std::string& signature) { |
| 98 base::ThreadRestrictions::ScopedAllowIO allow_io; |
95 CHECK(server_data_dir_.IsValid()); | 99 CHECK(server_data_dir_.IsValid()); |
96 | 100 |
97 std::vector<uint8_t> signing_key_bits; | 101 std::vector<uint8_t> signing_key_bits; |
98 if (!key->ExportPrivateKey(&signing_key_bits)) | 102 if (!key->ExportPrivateKey(&signing_key_bits)) |
99 return false; | 103 return false; |
100 | 104 |
101 policy_key_ = server_data_dir_.GetPath().Append(kSigningKeyFileName); | 105 policy_key_ = server_data_dir_.GetPath().Append(kSigningKeyFileName); |
102 int bytes_written = base::WriteFile( | 106 int bytes_written = base::WriteFile( |
103 policy_key_, reinterpret_cast<const char*>(signing_key_bits.data()), | 107 policy_key_, reinterpret_cast<const char*>(signing_key_bits.data()), |
104 signing_key_bits.size()); | 108 signing_key_bits.size()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 types->AppendString(dm_protocol::kChromeExtensionPolicyType); | 144 types->AppendString(dm_protocol::kChromeExtensionPolicyType); |
141 types->AppendString(dm_protocol::kChromeSigninExtensionPolicyType); | 145 types->AppendString(dm_protocol::kChromeSigninExtensionPolicyType); |
142 | 146 |
143 client_dict->Set(kClientStateKeyAllowedPolicyTypes, types.release()); | 147 client_dict->Set(kClientStateKeyAllowedPolicyTypes, types.release()); |
144 clients_.Set(dm_token, client_dict.release()); | 148 clients_.Set(dm_token, client_dict.release()); |
145 } | 149 } |
146 | 150 |
147 bool LocalPolicyTestServer::UpdatePolicy(const std::string& type, | 151 bool LocalPolicyTestServer::UpdatePolicy(const std::string& type, |
148 const std::string& entity_id, | 152 const std::string& entity_id, |
149 const std::string& policy) { | 153 const std::string& policy) { |
| 154 base::ThreadRestrictions::ScopedAllowIO allow_io; |
150 CHECK(server_data_dir_.IsValid()); | 155 CHECK(server_data_dir_.IsValid()); |
151 | 156 |
152 std::string selector = GetSelector(type, entity_id); | 157 std::string selector = GetSelector(type, entity_id); |
153 base::FilePath policy_file = server_data_dir_.GetPath().AppendASCII( | 158 base::FilePath policy_file = server_data_dir_.GetPath().AppendASCII( |
154 base::StringPrintf("policy_%s.bin", selector.c_str())); | 159 base::StringPrintf("policy_%s.bin", selector.c_str())); |
155 | 160 |
156 return base::WriteFile(policy_file, policy.c_str(), policy.size()) == | 161 return base::WriteFile(policy_file, policy.c_str(), policy.size()) == |
157 base::checked_cast<int>(policy.size()); | 162 base::checked_cast<int>(policy.size()); |
158 } | 163 } |
159 | 164 |
160 bool LocalPolicyTestServer::UpdatePolicyData(const std::string& type, | 165 bool LocalPolicyTestServer::UpdatePolicyData(const std::string& type, |
161 const std::string& entity_id, | 166 const std::string& entity_id, |
162 const std::string& data) { | 167 const std::string& data) { |
| 168 base::ThreadRestrictions::ScopedAllowIO allow_io; |
163 CHECK(server_data_dir_.IsValid()); | 169 CHECK(server_data_dir_.IsValid()); |
164 | 170 |
165 std::string selector = GetSelector(type, entity_id); | 171 std::string selector = GetSelector(type, entity_id); |
166 base::FilePath data_file = server_data_dir_.GetPath().AppendASCII( | 172 base::FilePath data_file = server_data_dir_.GetPath().AppendASCII( |
167 base::StringPrintf("policy_%s.data", selector.c_str())); | 173 base::StringPrintf("policy_%s.data", selector.c_str())); |
168 | 174 |
169 return base::WriteFile(data_file, data.c_str(), data.size()) == | 175 return base::WriteFile(data_file, data.c_str(), data.size()) == |
170 base::checked_cast<int>(data.size()); | 176 base::checked_cast<int>(data.size()); |
171 } | 177 } |
172 | 178 |
173 GURL LocalPolicyTestServer::GetServiceURL() const { | 179 GURL LocalPolicyTestServer::GetServiceURL() const { |
174 return GetURL("device_management"); | 180 return GetURL("device_management"); |
175 } | 181 } |
176 | 182 |
177 bool LocalPolicyTestServer::SetPythonPath() const { | 183 bool LocalPolicyTestServer::SetPythonPath() const { |
| 184 base::ThreadRestrictions::ScopedAllowIO allow_io; |
178 if (!net::LocalTestServer::SetPythonPath()) | 185 if (!net::LocalTestServer::SetPythonPath()) |
179 return false; | 186 return false; |
180 | 187 |
181 // Add the net/tools/testserver directory to the path. | 188 // Add the net/tools/testserver directory to the path. |
182 base::FilePath net_testserver_path; | 189 base::FilePath net_testserver_path; |
183 if (!LocalTestServer::GetTestServerPath(&net_testserver_path)) { | 190 if (!LocalTestServer::GetTestServerPath(&net_testserver_path)) { |
184 LOG(ERROR) << "Failed to get net testserver path."; | 191 LOG(ERROR) << "Failed to get net testserver path."; |
185 return false; | 192 return false; |
186 } | 193 } |
187 AppendToPythonPath(net_testserver_path.DirName()); | 194 AppendToPythonPath(net_testserver_path.DirName()); |
(...skipping 27 matching lines...) Expand all Loading... |
215 .AppendASCII("chromeos") | 222 .AppendASCII("chromeos") |
216 .AppendASCII("policy") | 223 .AppendASCII("policy") |
217 .AppendASCII("proto")); | 224 .AppendASCII("proto")); |
218 #endif | 225 #endif |
219 | 226 |
220 return true; | 227 return true; |
221 } | 228 } |
222 | 229 |
223 bool LocalPolicyTestServer::GetTestServerPath( | 230 bool LocalPolicyTestServer::GetTestServerPath( |
224 base::FilePath* testserver_path) const { | 231 base::FilePath* testserver_path) const { |
| 232 base::ThreadRestrictions::ScopedAllowIO allow_io; |
225 base::FilePath source_root; | 233 base::FilePath source_root; |
226 if (!PathService::Get(base::DIR_SOURCE_ROOT, &source_root)) { | 234 if (!PathService::Get(base::DIR_SOURCE_ROOT, &source_root)) { |
227 LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT"; | 235 LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT"; |
228 return false; | 236 return false; |
229 } | 237 } |
230 *testserver_path = source_root | 238 *testserver_path = source_root |
231 .AppendASCII("chrome") | 239 .AppendASCII("chrome") |
232 .AppendASCII("browser") | 240 .AppendASCII("browser") |
233 .AppendASCII("policy") | 241 .AppendASCII("policy") |
234 .AppendASCII("test") | 242 .AppendASCII("test") |
235 .AppendASCII("policy_testserver.py"); | 243 .AppendASCII("policy_testserver.py"); |
236 return true; | 244 return true; |
237 } | 245 } |
238 | 246 |
239 bool LocalPolicyTestServer::GenerateAdditionalArguments( | 247 bool LocalPolicyTestServer::GenerateAdditionalArguments( |
240 base::DictionaryValue* arguments) const { | 248 base::DictionaryValue* arguments) const { |
| 249 base::ThreadRestrictions::ScopedAllowIO allow_io; |
241 if (!net::LocalTestServer::GenerateAdditionalArguments(arguments)) | 250 if (!net::LocalTestServer::GenerateAdditionalArguments(arguments)) |
242 return false; | 251 return false; |
243 | 252 |
244 arguments->SetString("config-file", config_file_.AsUTF8Unsafe()); | 253 arguments->SetString("config-file", config_file_.AsUTF8Unsafe()); |
245 if (!policy_key_.empty()) | 254 if (!policy_key_.empty()) |
246 arguments->SetString("policy-key", policy_key_.AsUTF8Unsafe()); | 255 arguments->SetString("policy-key", policy_key_.AsUTF8Unsafe()); |
247 if (automatic_rotation_of_signing_keys_enabled_) { | 256 if (automatic_rotation_of_signing_keys_enabled_) { |
248 arguments->Set("rotate-policy-keys-automatically", | 257 arguments->Set("rotate-policy-keys-automatically", |
249 base::MakeUnique<base::Value>()); | 258 base::MakeUnique<base::Value>()); |
250 } | 259 } |
(...skipping 19 matching lines...) Expand all Loading... |
270 std::string LocalPolicyTestServer::GetSelector(const std::string& type, | 279 std::string LocalPolicyTestServer::GetSelector(const std::string& type, |
271 const std::string& entity_id) { | 280 const std::string& entity_id) { |
272 std::string selector = type; | 281 std::string selector = type; |
273 if (!entity_id.empty()) | 282 if (!entity_id.empty()) |
274 selector = base::StringPrintf("%s/%s", type.c_str(), entity_id.c_str()); | 283 selector = base::StringPrintf("%s/%s", type.c_str(), entity_id.c_str()); |
275 std::replace_if(selector.begin(), selector.end(), IsUnsafeCharacter, '_'); | 284 std::replace_if(selector.begin(), selector.end(), IsUnsafeCharacter, '_'); |
276 return selector; | 285 return selector; |
277 } | 286 } |
278 | 287 |
279 } // namespace policy | 288 } // namespace policy |
OLD | NEW |