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_
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"
24 // Runs a python implementation of the cloud policy server on the local machine.
25 class LocalPolicyTestServer
: public net::LocalTestServer
{
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
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 ~LocalPolicyTestServer() override
;
41 // Sets the policy signing key and verification signature used by the server.
42 // This must be called before starting the server, and only works when the
43 // server serves from a temporary directory.
44 bool SetSigningKeyAndSignature(const crypto::RSAPrivateKey
* key
,
45 const std::string
& signature
);
47 // Pre-configures a registered client so the server returns policy without the
48 // client having to make a registration call. This must be called before
49 // starting the server, and only works when the server serves from a temporary
51 void RegisterClient(const std::string
& dm_token
,
52 const std::string
& device_id
);
54 // Updates policy served by the server for a given (type, entity_id) pair.
55 // This only works when the server serves from a temporary directory.
57 // |type| is the policy type as requested in the protocol via
58 // |PolicyFetchRequest.policy_type|. |policy| is the payload data in the
59 // format appropriate for |type|, which is usually a serialized protobuf (for
60 // example, CloudPolicySettings or ChromeDeviceSettingsProto).
61 bool UpdatePolicy(const std::string
& type
,
62 const std::string
& entity_id
,
63 const std::string
& policy
);
65 // Updates the external policy data served by the server for a given
66 // (type, entity_id) pair, at the /externalpolicydata path. Requests to that
67 // URL must include a 'key' parameter, whose value is the |type| and
68 // |entity_id| values joined by a '/'.
70 // If this data is set but no policy is set for the (type, entity_id) pair,
71 // then an ExternalPolicyData protobuf is automatically served that points to
74 // This only works when the server serves from a temporary directory.
75 bool UpdatePolicyData(const std::string
& type
,
76 const std::string
& entity_id
,
77 const std::string
& data
);
79 // Gets the service URL.
80 GURL
GetServiceURL() const;
82 // net::LocalTestServer:
83 bool SetPythonPath() const override
;
84 bool GetTestServerPath(base::FilePath
* testserver_path
) const override
;
85 bool GenerateAdditionalArguments(
86 base::DictionaryValue
* arguments
) const override
;
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 policy
102 #endif // CHROME_BROWSER_POLICY_TEST_LOCAL_POLICY_TEST_SERVER_H_