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 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 89 |
90 bool LocalPolicyTestServer::SetSigningKeyAndSignature( | 90 bool LocalPolicyTestServer::SetSigningKeyAndSignature( |
91 const crypto::RSAPrivateKey* key, const std::string& signature) { | 91 const crypto::RSAPrivateKey* key, const std::string& signature) { |
92 CHECK(server_data_dir_.IsValid()); | 92 CHECK(server_data_dir_.IsValid()); |
93 | 93 |
94 std::vector<uint8> signing_key_bits; | 94 std::vector<uint8> signing_key_bits; |
95 if (!key->ExportPrivateKey(&signing_key_bits)) | 95 if (!key->ExportPrivateKey(&signing_key_bits)) |
96 return false; | 96 return false; |
97 | 97 |
98 policy_key_ = server_data_dir_.path().Append(kSigningKeyFileName); | 98 policy_key_ = server_data_dir_.path().Append(kSigningKeyFileName); |
99 int bytes_written = file_util::WriteFile( | 99 int bytes_written = base::WriteFile( |
100 policy_key_, | 100 policy_key_, |
101 reinterpret_cast<const char*>(vector_as_array(&signing_key_bits)), | 101 reinterpret_cast<const char*>(vector_as_array(&signing_key_bits)), |
102 signing_key_bits.size()); | 102 signing_key_bits.size()); |
103 | 103 |
104 if (bytes_written != static_cast<int>(signing_key_bits.size())) | 104 if (bytes_written != static_cast<int>(signing_key_bits.size())) |
105 return false; | 105 return false; |
106 | 106 |
107 // Write the signature data. | 107 // Write the signature data. |
108 base::FilePath signature_file = server_data_dir_.path().Append( | 108 base::FilePath signature_file = server_data_dir_.path().Append( |
109 kSigningKeySignatureFileName); | 109 kSigningKeySignatureFileName); |
110 bytes_written = file_util::WriteFile( | 110 bytes_written = base::WriteFile( |
111 signature_file, | 111 signature_file, |
112 signature.c_str(), | 112 signature.c_str(), |
113 signature.size()); | 113 signature.size()); |
114 | 114 |
115 return bytes_written == static_cast<int>(signature.size()); | 115 return bytes_written == static_cast<int>(signature.size()); |
116 } | 116 } |
117 | 117 |
118 void LocalPolicyTestServer::RegisterClient(const std::string& dm_token, | 118 void LocalPolicyTestServer::RegisterClient(const std::string& dm_token, |
119 const std::string& device_id) { | 119 const std::string& device_id) { |
120 CHECK(server_data_dir_.IsValid()); | 120 CHECK(server_data_dir_.IsValid()); |
(...skipping 17 matching lines...) Expand all Loading... |
138 | 138 |
139 bool LocalPolicyTestServer::UpdatePolicy(const std::string& type, | 139 bool LocalPolicyTestServer::UpdatePolicy(const std::string& type, |
140 const std::string& entity_id, | 140 const std::string& entity_id, |
141 const std::string& policy) { | 141 const std::string& policy) { |
142 CHECK(server_data_dir_.IsValid()); | 142 CHECK(server_data_dir_.IsValid()); |
143 | 143 |
144 std::string selector = GetSelector(type, entity_id); | 144 std::string selector = GetSelector(type, entity_id); |
145 base::FilePath policy_file = server_data_dir_.path().AppendASCII( | 145 base::FilePath policy_file = server_data_dir_.path().AppendASCII( |
146 base::StringPrintf("policy_%s.bin", selector.c_str())); | 146 base::StringPrintf("policy_%s.bin", selector.c_str())); |
147 | 147 |
148 return file_util::WriteFile(policy_file, policy.c_str(), policy.size()) == | 148 return base::WriteFile(policy_file, policy.c_str(), policy.size()) == |
149 static_cast<int>(policy.size()); | 149 static_cast<int>(policy.size()); |
150 } | 150 } |
151 | 151 |
152 bool LocalPolicyTestServer::UpdatePolicyData(const std::string& type, | 152 bool LocalPolicyTestServer::UpdatePolicyData(const std::string& type, |
153 const std::string& entity_id, | 153 const std::string& entity_id, |
154 const std::string& data) { | 154 const std::string& data) { |
155 CHECK(server_data_dir_.IsValid()); | 155 CHECK(server_data_dir_.IsValid()); |
156 | 156 |
157 std::string selector = GetSelector(type, entity_id); | 157 std::string selector = GetSelector(type, entity_id); |
158 base::FilePath data_file = server_data_dir_.path().AppendASCII( | 158 base::FilePath data_file = server_data_dir_.path().AppendASCII( |
159 base::StringPrintf("policy_%s.data", selector.c_str())); | 159 base::StringPrintf("policy_%s.data", selector.c_str())); |
160 | 160 |
161 return file_util::WriteFile(data_file, data.c_str(), data.size()) == | 161 return base::WriteFile(data_file, data.c_str(), data.size()) == |
162 static_cast<int>(data.size()); | 162 static_cast<int>(data.size()); |
163 } | 163 } |
164 | 164 |
165 GURL LocalPolicyTestServer::GetServiceURL() const { | 165 GURL LocalPolicyTestServer::GetServiceURL() const { |
166 return GetURL("device_management"); | 166 return GetURL("device_management"); |
167 } | 167 } |
168 | 168 |
169 bool LocalPolicyTestServer::SetPythonPath() const { | 169 bool LocalPolicyTestServer::SetPythonPath() const { |
170 if (!net::LocalTestServer::SetPythonPath()) | 170 if (!net::LocalTestServer::SetPythonPath()) |
171 return false; | 171 return false; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 if (!policy_key_.empty()) | 242 if (!policy_key_.empty()) |
243 arguments->SetString("policy-key", policy_key_.AsUTF8Unsafe()); | 243 arguments->SetString("policy-key", policy_key_.AsUTF8Unsafe()); |
244 if (server_data_dir_.IsValid()) { | 244 if (server_data_dir_.IsValid()) { |
245 arguments->SetString("data-dir", server_data_dir_.path().AsUTF8Unsafe()); | 245 arguments->SetString("data-dir", server_data_dir_.path().AsUTF8Unsafe()); |
246 | 246 |
247 if (!clients_.empty()) { | 247 if (!clients_.empty()) { |
248 std::string json; | 248 std::string json; |
249 base::JSONWriter::Write(&clients_, &json); | 249 base::JSONWriter::Write(&clients_, &json); |
250 base::FilePath client_state_file = | 250 base::FilePath client_state_file = |
251 server_data_dir_.path().Append(kClientStateFileName); | 251 server_data_dir_.path().Append(kClientStateFileName); |
252 if (file_util::WriteFile(client_state_file, json.c_str(), json.size()) != | 252 if (base::WriteFile(client_state_file, json.c_str(), json.size()) != |
253 static_cast<int>(json.size())) { | 253 static_cast<int>(json.size())) { |
254 return false; | 254 return false; |
255 } | 255 } |
256 arguments->SetString("client-state", client_state_file.AsUTF8Unsafe()); | 256 arguments->SetString("client-state", client_state_file.AsUTF8Unsafe()); |
257 } | 257 } |
258 } | 258 } |
259 | 259 |
260 return true; | 260 return true; |
261 } | 261 } |
262 | 262 |
263 std::string LocalPolicyTestServer::GetSelector(const std::string& type, | 263 std::string LocalPolicyTestServer::GetSelector(const std::string& type, |
264 const std::string& entity_id) { | 264 const std::string& entity_id) { |
265 std::string selector = type; | 265 std::string selector = type; |
266 if (!entity_id.empty()) | 266 if (!entity_id.empty()) |
267 selector = base::StringPrintf("%s/%s", type.c_str(), entity_id.c_str()); | 267 selector = base::StringPrintf("%s/%s", type.c_str(), entity_id.c_str()); |
268 std::replace_if(selector.begin(), selector.end(), IsUnsafeCharacter, '_'); | 268 std::replace_if(selector.begin(), selector.end(), IsUnsafeCharacter, '_'); |
269 return selector; | 269 return selector; |
270 } | 270 } |
271 | 271 |
272 } // namespace policy | 272 } // namespace policy |
OLD | NEW |