fake_dmserver.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2022 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. A bare-bones test server for testing cloud policy support.
  6. This implements a simple cloud policy test server that can be used to test
  7. Chrome's device management service client. The policy information is read from
  8. the file named policy.json in the server's data directory. It contains
  9. policies for the device and user scope, and a list of managed users. The format
  10. of the file is JSON.
  11. The root dictionary contains a list under the key
  12. "managed_users". It contains auth tokens for which the server will claim that
  13. the user is managed. The token string "*" indicates that all users are claimed
  14. to be managed.
  15. The root dictionary also contains a list under the key "policies". It contains
  16. all the policies to be set, each policy has 3 fields, "policy_type" is the type
  17. or scope of the policy (user, device or publicaccount), "entity_id" is the
  18. account id used for public account policies, "value" is the seralized proto
  19. message of the policies value encoded in base64.
  20. The root dictionary also contains a "policy_user" key which indicates the
  21. current user.
  22. Example:
  23. {
  24. "policies" : [
  25. {
  26. "policy_type" : "google/chromeos/user",
  27. "value" : "base64 encoded proto message",
  28. },
  29. {
  30. "policy_type" : "google/chromeos/device",
  31. "value" : "base64 encoded proto message",
  32. },
  33. {
  34. "policy_type" : "google/chromeos/publicaccount",
  35. "entity_id" : "accountid@managedchrome.com",
  36. "value" : "base64 encoded proto message",
  37. }
  38. ],
  39. "managed_users" : [
  40. "secret123456"
  41. ],
  42. "policy_user" : "tast-user@managedchrome.com",
  43. }
  44. */
  45. #ifndef COMPONENTS_POLICY_TEST_SUPPORT_FAKE_DMSERVER_H_
  46. #define COMPONENTS_POLICY_TEST_SUPPORT_FAKE_DMSERVER_H_
  47. #include <string>
  48. #include "base/command_line.h"
  49. #include "base/values.h"
  50. #include "components/policy/test_support/client_storage.h"
  51. #include "components/policy/test_support/embedded_policy_test_server.h"
  52. namespace fakedms {
  53. void InitLogging(const std::string& log_path);
  54. void ParseFlags(const base::CommandLine& command_line,
  55. std::string& policy_blob_path,
  56. std::string& client_state_path,
  57. absl::optional<std::string>& log_path,
  58. base::ScopedFD& startup_pipe);
  59. class FakeDMServer : public policy::EmbeddedPolicyTestServer {
  60. public:
  61. FakeDMServer(const std::string& policy_blob_path,
  62. const std::string& client_state_path,
  63. base::OnceClosure shutdown_cb = base::DoNothing());
  64. ~FakeDMServer() override;
  65. // Starts the FakeDMServer and EmbeddedPolicyTestServer, it will return true
  66. // if it's able to start the server successfully, and false otherwise.
  67. bool Start() override;
  68. // Writes the host and port of the EmbeddedPolicyTestServer to the given pipe
  69. // in a json format {"host": "localhost", "port": 1234}, it will return true
  70. // if it's able to write the URL to the pipe, and false otherwise.
  71. bool WriteURLToPipe(const base::ScopedFD& startup_pipe);
  72. // Overrides the EmbeddedPolicyTestServer request handler.
  73. std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
  74. const net::test_server::HttpRequest& request) override;
  75. private:
  76. // Sets the policy payload in the policy storage, it will return true if it's
  77. // able to set the policy and false otherwise.
  78. bool SetPolicyPayload(const std::string* policy_type,
  79. const std::string* entity_id,
  80. const std::string* serialized_proto);
  81. // Reads and sets the values in the policy blob file, it will return true if
  82. // the policy blob file doesn't exist yet or all the values are read
  83. // correctly, and false otherwise.
  84. bool ReadPolicyBlobFile();
  85. // Writes all the clients to the client state file, it will return true if
  86. // it's able to write the client storage to the state file, and false
  87. // otherwise.
  88. bool WriteClientStateFile();
  89. // Reads the client state file and registers the clients, it will return true
  90. // if the state file doesn't exist yet or all the values are read
  91. // correctly, and false otherwise.
  92. bool ReadClientStateFile();
  93. // Returns true if the key of the specific type is in the dictionary.
  94. static bool FindKey(const base::Value::Dict& dict,
  95. const std::string& key,
  96. base::Value::Type type);
  97. // Converts the client to Dictionary.
  98. static base::Value::Dict GetValueFromClient(
  99. const policy::ClientStorage::ClientInfo& c);
  100. // Converts the value to Client.
  101. static absl::optional<policy::ClientStorage::ClientInfo> GetClientFromValue(
  102. const base::Value& v);
  103. std::string policy_blob_path_, client_state_path_;
  104. base::OnceClosure shutdown_cb_;
  105. };
  106. } // namespace fakedms
  107. #endif // COMPONENTS_POLICY_TEST_SUPPORT_FAKE_DMSERVER_H_