Home | History | Annotate | Download | only in test
      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 "net/test/spawned_test_server/local_test_server.h"
     16 #include "url/gurl.h"
     17 
     18 namespace crypto {
     19 class RSAPrivateKey;
     20 }
     21 
     22 namespace policy {
     23 
     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();
     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 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);
     46 
     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
     50   // directory.
     51   void RegisterClient(const std::string& dm_token,
     52                       const std::string& device_id);
     53 
     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.
     56   //
     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);
     64 
     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 '/'.
     69   //
     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
     72   // this data.
     73   //
     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);
     78 
     79   // Gets the service URL.
     80   GURL GetServiceURL() const;
     81 
     82   // net::LocalTestServer:
     83   virtual bool SetPythonPath() const OVERRIDE;
     84   virtual bool GetTestServerPath(
     85       base::FilePath* testserver_path) const OVERRIDE;
     86   virtual bool GenerateAdditionalArguments(
     87       base::DictionaryValue* arguments) const OVERRIDE;
     88 
     89  private:
     90   std::string GetSelector(const std::string& type,
     91                           const std::string& entity_id);
     92 
     93   base::FilePath config_file_;
     94   base::FilePath policy_key_;
     95   base::DictionaryValue clients_;
     96   base::ScopedTempDir server_data_dir_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(LocalPolicyTestServer);
     99 };
    100 
    101 }  // namespace policy
    102 
    103 #endif  // CHROME_BROWSER_POLICY_TEST_LOCAL_POLICY_TEST_SERVER_H_
    104