summaryrefslogtreecommitdiffstats
path: root/chromium/components/policy/test_support/embedded_policy_test_server.h
blob: 736b7cb7e3afb5835edd3f0c4c4d327b8f342ad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_POLICY_TEST_SUPPORT_EMBEDDED_POLICY_TEST_SERVER_H_
#define COMPONENTS_POLICY_TEST_SUPPORT_EMBEDDED_POLICY_TEST_SERVER_H_

#include <map>
#include <memory>
#include <set>
#include <string>

#include "base/memory/raw_ptr.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_response.h"
#include "url/gurl.h"

namespace net {
namespace test_server {
class HttpResponse;
struct HttpRequest;
}  // namespace test_server
}  // namespace net

namespace policy {

class ClientStorage;
class PolicyStorage;

extern const char kFakeDeviceToken[];
extern const char kInvalidEnrollmentToken[];

// Runs a fake implementation of the cloud policy server on the local machine.
class EmbeddedPolicyTestServer {
 public:
  class RequestHandler {
   public:
    RequestHandler(ClientStorage* client_storage,
                   PolicyStorage* policy_storage);
    virtual ~RequestHandler();

    // Returns the value associated with the "request_type" query param handled
    // by this request handler.
    virtual std::string RequestType() = 0;

    // Returns a response if this request can be handled by this handler, or
    // nullptr otherwise.
    virtual std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
        const net::test_server::HttpRequest& request) = 0;

    const ClientStorage* client_storage() const { return client_storage_; }
    ClientStorage* client_storage() { return client_storage_; }

    const PolicyStorage* policy_storage() const { return policy_storage_; }
    PolicyStorage* policy_storage() { return policy_storage_; }

   private:
    raw_ptr<ClientStorage> client_storage_;
    raw_ptr<PolicyStorage> policy_storage_;
  };

  EmbeddedPolicyTestServer();
  EmbeddedPolicyTestServer(const EmbeddedPolicyTestServer&) = delete;
  EmbeddedPolicyTestServer& operator=(const EmbeddedPolicyTestServer&) = delete;
  virtual ~EmbeddedPolicyTestServer();

  // Initializes and waits until the server is ready to accept requests.
  bool Start();

  ClientStorage* client_storage() const { return client_storage_.get(); }

  PolicyStorage* policy_storage() const { return policy_storage_.get(); }

  // Returns the service URL.
  GURL GetServiceURL() const;

  // Public so it can be used by tests.
  void RegisterHandler(std::unique_ptr<EmbeddedPolicyTestServer::RequestHandler>
                           request_handler);

  // Configures requests of a given |request_type| to always fail with
  // |error_code|.
  void ConfigureRequestError(const std::string& request_type,
                             net::HttpStatusCode error_code);

#if !BUILDFLAG(IS_ANDROID)
  // Updates policy selected by |type| and optional |entity_id|. The
  // |raw_policy| is served via an external endpoint. This does not trigger
  // policy invalidation, hence test authors must manually trigger a policy
  // fetch.
  void UpdateExternalPolicy(const std::string& type,
                            const std::string& entity_id,
                            const std::string& raw_policy);
#endif  // !BUILDFLAG(IS_ANDROID)

 private:
  // Default request handler.
  std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
      const net::test_server::HttpRequest& request);

  // Request handler for external policy data.
  std::unique_ptr<net::test_server::HttpResponse>
  HandleExternalPolicyDataRequest(const GURL& request);

  net::test_server::EmbeddedTestServer http_server_;
  std::map<std::string, std::unique_ptr<RequestHandler>> request_handlers_;
  std::unique_ptr<ClientStorage> client_storage_;
  std::unique_ptr<PolicyStorage> policy_storage_;
};

}  // namespace policy

#endif  // COMPONENTS_POLICY_TEST_SUPPORT_EMBEDDED_POLICY_TEST_SERVER_H_