local_policy_test_server.h revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_POLICY_TEST_LOCAL_POLICY_TEST_SERVER_H_
6#define CHROME_BROWSER_POLICY_TEST_LOCAL_POLICY_TEST_SERVER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/files/file_path.h"
13#include "base/files/scoped_temp_dir.h"
14#include "base/values.h"
15#include "googleurl/src/gurl.h"
16#include "net/test/spawned_test_server/local_test_server.h"
17
18namespace crypto {
19class RSAPrivateKey;
20}
21
22namespace policy {
23
24// Runs a python implementation of the cloud policy server on the local machine.
25class LocalPolicyTestServer : public net::LocalTestServer {
26 public:
27  // Initializes the test server to serve its policy from a temporary directory,
28  // the contents of which can be updated via UpdatePolicy().
29  LocalPolicyTestServer();
30
31  // Initializes a test server configured by the configuration file
32  // |config_file|.
33  explicit LocalPolicyTestServer(const base::FilePath& config_file);
34
35  // Initializes the test server with the configuration read from
36  // chrome/test/data/policy/policy_|test_name|.json.
37  explicit LocalPolicyTestServer(const std::string& test_name);
38
39  virtual ~LocalPolicyTestServer();
40
41  // Sets the policy signing key used by the server. This must be called before
42  // starting the server, and only works when the server serves from a temporary
43  // directory.
44  bool SetSigningKey(const crypto::RSAPrivateKey* key);
45
46  // Pre-configures a registered client so the server returns policy without the
47  // client having to make a registration call. This must be called before
48  // starting the server, and only works when the server serves from a temporary
49  // directory.
50  void RegisterClient(const std::string& dm_token,
51                      const std::string& device_id);
52
53  // Updates policy served by the server for a given (type, entity_id) pair.
54  // This only works when the server serves from a temporary directory.
55  //
56  // |type| is the policy type as requested in the protocol via
57  // |PolicyFetchRequest.policy_type|. |policy| is the payload data in the
58  // format appropriate for |type|, which is usually a serialized protobuf (for
59  // example, CloudPolicySettings or ChromeDeviceSettingsProto).
60  bool UpdatePolicy(const std::string& type,
61                    const std::string& entity_id,
62                    const std::string& policy);
63
64  // Updates the external policy data served by the server for a given
65  // (type, entity_id) pair, at the /externalpolicydata path. Requests to that
66  // URL must include a 'key' parameter, whose value is the |type| and
67  // |entity_id| values joined by a '/'.
68  //
69  // If this data is set but no policy is set for the (type, entity_id) pair,
70  // then an ExternalPolicyData protobuf is automatically served that points to
71  // this data.
72  //
73  // This only works when the server serves from a temporary directory.
74  bool UpdatePolicyData(const std::string& type,
75                        const std::string& entity_id,
76                        const std::string& data);
77
78  // Gets the service URL.
79  GURL GetServiceURL() const;
80
81  // net::LocalTestServer:
82  virtual bool SetPythonPath() const OVERRIDE;
83  virtual bool GetTestServerPath(
84      base::FilePath* testserver_path) const OVERRIDE;
85  virtual bool GenerateAdditionalArguments(
86      base::DictionaryValue* arguments) const OVERRIDE;
87
88 private:
89  std::string GetSelector(const std::string& type,
90                          const std::string& entity_id);
91
92  base::FilePath config_file_;
93  base::FilePath policy_key_;
94  base::DictionaryValue clients_;
95  base::ScopedTempDir server_data_dir_;
96
97  DISALLOW_COPY_AND_ASSIGN(LocalPolicyTestServer);
98};
99
100}  // namespace
101
102#endif  // CHROME_BROWSER_POLICY_TEST_LOCAL_POLICY_TEST_SERVER_H_
103