Home | History | Annotate | Download | only in policy
      1 // Copyright (c) 2011 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_MOCK_DEVICE_MANAGEMENT_BACKEND_H_
      6 #define CHROME_BROWSER_POLICY_MOCK_DEVICE_MANAGEMENT_BACKEND_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <string>
     11 
     12 #include "base/time.h"
     13 #include "base/values.h"
     14 #include "chrome/browser/policy/device_management_backend.h"
     15 #include "chrome/browser/policy/proto/cloud_policy.pb.h"
     16 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
     17 #include "chrome/browser/policy/proto/device_management_constants.h"
     18 #include "testing/gmock/include/gmock/gmock.h"
     19 #include "testing/gtest/include/gtest/gtest.h"
     20 
     21 namespace policy {
     22 
     23 namespace em = enterprise_management;
     24 
     25 // Useful for unit testing when a server-based backend isn't
     26 // available. Simulates both successful and failed requests to the device
     27 // management server.
     28 class MockDeviceManagementBackend : public DeviceManagementBackend {
     29  public:
     30   MockDeviceManagementBackend();
     31   virtual ~MockDeviceManagementBackend();
     32 
     33   // DeviceManagementBackend method overrides:
     34   MOCK_METHOD4(ProcessRegisterRequest, void(
     35       const std::string& auth_token,
     36       const std::string& device_id,
     37       const em::DeviceRegisterRequest& request,
     38       DeviceRegisterResponseDelegate* delegate));
     39 
     40   MOCK_METHOD4(ProcessUnregisterRequest, void(
     41       const std::string& device_management_token,
     42       const std::string& device_id,
     43       const em::DeviceUnregisterRequest& request,
     44       DeviceUnregisterResponseDelegate* delegate));
     45 
     46   MOCK_METHOD4(ProcessPolicyRequest, void(
     47       const std::string& device_management_token,
     48       const std::string& device_id,
     49       const em::DevicePolicyRequest& request,
     50       DevicePolicyResponseDelegate* delegate));
     51 
     52  private:
     53   DISALLOW_COPY_AND_ASSIGN(MockDeviceManagementBackend);
     54 };
     55 
     56 ACTION(MockDeviceManagementBackendSucceedRegister) {
     57   em::DeviceRegisterResponse response;
     58   std::string token("FAKE_DEVICE_TOKEN_");
     59   static int next_token_suffix;
     60   token += next_token_suffix++;
     61   response.set_device_management_token(token);
     62   arg3->HandleRegisterResponse(response);
     63 }
     64 
     65 ACTION(MockDeviceManagementBackendSucceedSpdyCloudPolicy) {
     66   em::PolicyData signed_response;
     67   em::CloudPolicySettings settings;
     68   em::DisableSpdyProto* spdy_proto = settings.mutable_disablespdy();
     69   spdy_proto->set_disablespdy(true);
     70   spdy_proto->mutable_policy_options()->set_mode(em::PolicyOptions::MANDATORY);
     71   EXPECT_TRUE(
     72       settings.SerializeToString(signed_response.mutable_policy_value()));
     73   base::TimeDelta timestamp =
     74       base::Time::NowFromSystemTime() - base::Time::UnixEpoch();
     75   signed_response.set_timestamp(timestamp.InMilliseconds());
     76   std::string serialized_signed_response;
     77   EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response));
     78   em::DevicePolicyResponse response;
     79   em::PolicyFetchResponse* fetch_response = response.add_response();
     80   fetch_response->set_policy_data(serialized_signed_response);
     81   // TODO(jkummerow): Set proper new_public_key and signature (when
     82   // implementing support for signature verification).
     83   fetch_response->set_policy_data_signature("TODO");
     84   fetch_response->set_new_public_key("TODO");
     85   arg3->HandlePolicyResponse(response);
     86 }
     87 
     88 ACTION_P(MockDeviceManagementBackendFailRegister, error) {
     89   arg3->OnError(error);
     90 }
     91 
     92 ACTION_P(MockDeviceManagementBackendFailPolicy, error) {
     93   arg3->OnError(error);
     94 }
     95 
     96 }  // namespace policy
     97 
     98 #endif  // CHROME_BROWSER_POLICY_MOCK_DEVICE_MANAGEMENT_BACKEND_H_
     99