Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / policy / test / local_policy_test_server.h
blob7dbd2ef11f3529e090fd7647ac0190a018f9656c
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.
5 #ifndef CHROME_BROWSER_POLICY_TEST_LOCAL_POLICY_TEST_SERVER_H_
6 #define CHROME_BROWSER_POLICY_TEST_LOCAL_POLICY_TEST_SERVER_H_
8 #include <string>
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 "net/test/spawned_test_server/local_test_server.h"
16 #include "url/gurl.h"
18 namespace crypto {
19 class RSAPrivateKey;
22 namespace policy {
24 // Runs a python implementation of the cloud policy server on the local machine.
25 class 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();
31 // Initializes a test server configured by the configuration file
32 // |config_file|.
33 explicit LocalPolicyTestServer(const base::FilePath& config_file);
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);
39 virtual ~LocalPolicyTestServer();
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);
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);
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.
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);
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 '/'.
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.
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);
78 // Gets the service URL.
79 GURL GetServiceURL() const;
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;
88 private:
89 std::string GetSelector(const std::string& type,
90 const std::string& entity_id);
92 base::FilePath config_file_;
93 base::FilePath policy_key_;
94 base::DictionaryValue clients_;
95 base::ScopedTempDir server_data_dir_;
97 DISALLOW_COPY_AND_ASSIGN(LocalPolicyTestServer);
100 } // namespace
102 #endif // CHROME_BROWSER_POLICY_TEST_LOCAL_POLICY_TEST_SERVER_H_