| 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> |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 return !(isalnum(c) || c == '.' || c == '@' || c == '-'); | 54 return !(isalnum(c) || c == '.' || c == '@' || c == '-'); |
| 55 } | 55 } |
| 56 | 56 |
| 57 } // namespace | 57 } // namespace |
| 58 | 58 |
| 59 LocalPolicyTestServer::LocalPolicyTestServer() | 59 LocalPolicyTestServer::LocalPolicyTestServer() |
| 60 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, | 60 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, |
| 61 net::BaseTestServer::kLocalhost, | 61 net::BaseTestServer::kLocalhost, |
| 62 base::FilePath()) { | 62 base::FilePath()) { |
| 63 CHECK(server_data_dir_.CreateUniqueTempDir()); | 63 CHECK(server_data_dir_.CreateUniqueTempDir()); |
| 64 config_file_ = server_data_dir_.path().Append(kPolicyFileName); | 64 config_file_ = server_data_dir_.GetPath().Append(kPolicyFileName); |
| 65 } | 65 } |
| 66 | 66 |
| 67 LocalPolicyTestServer::LocalPolicyTestServer(const base::FilePath& config_file) | 67 LocalPolicyTestServer::LocalPolicyTestServer(const base::FilePath& config_file) |
| 68 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, | 68 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, |
| 69 net::BaseTestServer::kLocalhost, | 69 net::BaseTestServer::kLocalhost, |
| 70 base::FilePath()), | 70 base::FilePath()), |
| 71 config_file_(config_file) {} | 71 config_file_(config_file) {} |
| 72 | 72 |
| 73 LocalPolicyTestServer::LocalPolicyTestServer(const std::string& test_name) | 73 LocalPolicyTestServer::LocalPolicyTestServer(const std::string& test_name) |
| 74 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, | 74 : net::LocalTestServer(net::BaseTestServer::TYPE_HTTP, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 88 LocalPolicyTestServer::~LocalPolicyTestServer() {} | 88 LocalPolicyTestServer::~LocalPolicyTestServer() {} |
| 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_t> signing_key_bits; | 94 std::vector<uint8_t> 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_.GetPath().Append(kSigningKeyFileName); |
| 99 int bytes_written = base::WriteFile( | 99 int bytes_written = base::WriteFile( |
| 100 policy_key_, reinterpret_cast<const char*>(signing_key_bits.data()), | 100 policy_key_, reinterpret_cast<const char*>(signing_key_bits.data()), |
| 101 signing_key_bits.size()); | 101 signing_key_bits.size()); |
| 102 | 102 |
| 103 if (bytes_written != static_cast<int>(signing_key_bits.size())) | 103 if (bytes_written != static_cast<int>(signing_key_bits.size())) |
| 104 return false; | 104 return false; |
| 105 | 105 |
| 106 // Write the signature data. | 106 // Write the signature data. |
| 107 base::FilePath signature_file = server_data_dir_.path().Append( | 107 base::FilePath signature_file = |
| 108 kSigningKeySignatureFileName); | 108 server_data_dir_.GetPath().Append(kSigningKeySignatureFileName); |
| 109 bytes_written = base::WriteFile( | 109 bytes_written = base::WriteFile( |
| 110 signature_file, | 110 signature_file, |
| 111 signature.c_str(), | 111 signature.c_str(), |
| 112 signature.size()); | 112 signature.size()); |
| 113 | 113 |
| 114 return bytes_written == static_cast<int>(signature.size()); | 114 return bytes_written == static_cast<int>(signature.size()); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void LocalPolicyTestServer::RegisterClient(const std::string& dm_token, | 117 void LocalPolicyTestServer::RegisterClient(const std::string& dm_token, |
| 118 const std::string& device_id) { | 118 const std::string& device_id) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 135 client_dict->Set(kClientStateKeyAllowedPolicyTypes, types.release()); | 135 client_dict->Set(kClientStateKeyAllowedPolicyTypes, types.release()); |
| 136 clients_.Set(dm_token, client_dict.release()); | 136 clients_.Set(dm_token, client_dict.release()); |
| 137 } | 137 } |
| 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_.GetPath().AppendASCII( |
| 146 base::StringPrintf("policy_%s.bin", selector.c_str())); | 146 base::StringPrintf("policy_%s.bin", selector.c_str())); |
| 147 | 147 |
| 148 return base::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_.GetPath().AppendASCII( |
| 159 base::StringPrintf("policy_%s.data", selector.c_str())); | 159 base::StringPrintf("policy_%s.data", selector.c_str())); |
| 160 | 160 |
| 161 return base::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 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 | 230 |
| 231 bool LocalPolicyTestServer::GenerateAdditionalArguments( | 231 bool LocalPolicyTestServer::GenerateAdditionalArguments( |
| 232 base::DictionaryValue* arguments) const { | 232 base::DictionaryValue* arguments) const { |
| 233 if (!net::LocalTestServer::GenerateAdditionalArguments(arguments)) | 233 if (!net::LocalTestServer::GenerateAdditionalArguments(arguments)) |
| 234 return false; | 234 return false; |
| 235 | 235 |
| 236 arguments->SetString("config-file", config_file_.AsUTF8Unsafe()); | 236 arguments->SetString("config-file", config_file_.AsUTF8Unsafe()); |
| 237 if (!policy_key_.empty()) | 237 if (!policy_key_.empty()) |
| 238 arguments->SetString("policy-key", policy_key_.AsUTF8Unsafe()); | 238 arguments->SetString("policy-key", policy_key_.AsUTF8Unsafe()); |
| 239 if (server_data_dir_.IsValid()) { | 239 if (server_data_dir_.IsValid()) { |
| 240 arguments->SetString("data-dir", server_data_dir_.path().AsUTF8Unsafe()); | 240 arguments->SetString("data-dir", server_data_dir_.GetPath().AsUTF8Unsafe()); |
| 241 | 241 |
| 242 if (!clients_.empty()) { | 242 if (!clients_.empty()) { |
| 243 std::string json; | 243 std::string json; |
| 244 base::JSONWriter::Write(clients_, &json); | 244 base::JSONWriter::Write(clients_, &json); |
| 245 base::FilePath client_state_file = | 245 base::FilePath client_state_file = |
| 246 server_data_dir_.path().Append(kClientStateFileName); | 246 server_data_dir_.GetPath().Append(kClientStateFileName); |
| 247 if (base::WriteFile(client_state_file, json.c_str(), json.size()) != | 247 if (base::WriteFile(client_state_file, json.c_str(), json.size()) != |
| 248 static_cast<int>(json.size())) { | 248 static_cast<int>(json.size())) { |
| 249 return false; | 249 return false; |
| 250 } | 250 } |
| 251 arguments->SetString("client-state", client_state_file.AsUTF8Unsafe()); | 251 arguments->SetString("client-state", client_state_file.AsUTF8Unsafe()); |
| 252 } | 252 } |
| 253 } | 253 } |
| 254 | 254 |
| 255 return true; | 255 return true; |
| 256 } | 256 } |
| 257 | 257 |
| 258 std::string LocalPolicyTestServer::GetSelector(const std::string& type, | 258 std::string LocalPolicyTestServer::GetSelector(const std::string& type, |
| 259 const std::string& entity_id) { | 259 const std::string& entity_id) { |
| 260 std::string selector = type; | 260 std::string selector = type; |
| 261 if (!entity_id.empty()) | 261 if (!entity_id.empty()) |
| 262 selector = base::StringPrintf("%s/%s", type.c_str(), entity_id.c_str()); | 262 selector = base::StringPrintf("%s/%s", type.c_str(), entity_id.c_str()); |
| 263 std::replace_if(selector.begin(), selector.end(), IsUnsafeCharacter, '_'); | 263 std::replace_if(selector.begin(), selector.end(), IsUnsafeCharacter, '_'); |
| 264 return selector; | 264 return selector; |
| 265 } | 265 } |
| 266 | 266 |
| 267 } // namespace policy | 267 } // namespace policy |
| OLD | NEW |