summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/mock_device_management_backend.cc
blob: ca61feb5e26540b09c06b6dd70b8c2d790800511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/mock_device_management_backend.h"

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/stl_util-inl.h"

namespace {

static const char kFakeDeviceManagementToken[] = "FAKE_DEVICE_TOKEN_";
static char next_token_suffix_ = '0';

}  // namespace

namespace policy {

using ::testing::_;
using ::testing::Invoke;

using em::DeviceRegisterRequest;
using em::DeviceUnregisterRequest;
using em::DevicePolicyRequest;
using em::DeviceRegisterResponse;
using em::DeviceUnregisterResponse;
using em::DevicePolicyResponse;

MockDeviceManagementBackend::MockDeviceManagementBackend()
    : DeviceManagementBackend(),
      policy_remaining_fail_count_(0),
      register_remaining_fail_count_(0),
      policy_remaining_success_count_(0) {
  policy_setting_ = policy_response_.add_setting();
  policy_setting_->set_policy_key("chrome-policy");
  policy_setting_->set_watermark("fresh");
}

MockDeviceManagementBackend::~MockDeviceManagementBackend() {
}

void MockDeviceManagementBackend::AllShouldSucceed() {
  ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)).
      WillByDefault(Invoke(
          this,
          &MockDeviceManagementBackend::SimulateSuccessfulRegisterRequest));
  ON_CALL(*this, ProcessPolicyRequest(_, _, _, _)).
      WillByDefault(Invoke(
          this,
          &MockDeviceManagementBackend::SimulateSuccessfulPolicyRequest));
}

void MockDeviceManagementBackend::AllShouldFail() {
  ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)).
      WillByDefault(Invoke(
          this,
          &MockDeviceManagementBackend::SimulateFailedRegisterRequest));
  ON_CALL(*this, ProcessPolicyRequest(_, _, _, _)).
      WillByDefault(Invoke(
          this,
          &MockDeviceManagementBackend::SimulateFailedPolicyRequest));
}

void MockDeviceManagementBackend::UnmanagedDevice() {
  ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)).
      WillByDefault(Invoke(
          this,
          &MockDeviceManagementBackend::SimulateUnmanagedRegisterRequest));
}

void MockDeviceManagementBackend::RegisterFailsOncePolicyFailsTwice() {
  register_remaining_fail_count_ = 1;
  policy_remaining_fail_count_ = 2;
  AllShouldFail();
}

void MockDeviceManagementBackend::AllWorksFirstPolicyFailsLater() {
  policy_remaining_success_count_ = 3;
  AllShouldSucceed();
}

void MockDeviceManagementBackend::AddBooleanPolicy(const char* policy_name,
                                                   bool value) {
  em::GenericSetting* policy_value = policy_setting_->mutable_policy_value();
  em::GenericNamedValue* named_value = policy_value->add_named_value();
  named_value->set_name(policy_name);
  named_value->mutable_value()->set_value_type(
      em::GenericValue::VALUE_TYPE_BOOL);
  named_value->mutable_value()->set_bool_value(value);
}

void MockDeviceManagementBackend::SimulateSuccessfulRegisterRequest(
    const std::string& auth_token,
    const std::string& device_id,
    const em::DeviceRegisterRequest& request,
    DeviceRegisterResponseDelegate* delegate) {
  DeviceRegisterResponse response;
  std::string token(kFakeDeviceManagementToken);
  token += next_token_suffix_++;
  response.set_device_management_token(token);
  delegate->HandleRegisterResponse(response);
}

void MockDeviceManagementBackend::SimulateSuccessfulPolicyRequest(
    const std::string& device_management_token,
    const std::string& device_id,
    const em::DevicePolicyRequest& request,
    DevicePolicyResponseDelegate* delegate) {
  if (policy_remaining_success_count_) {
    policy_remaining_success_count_--;
    if (!policy_remaining_success_count_) {
      ON_CALL(*this, ProcessPolicyRequest(_, _, _, _)).
          WillByDefault(Invoke(
              this,
              &MockDeviceManagementBackend::SimulateFailedPolicyRequest));
    }
  }
  delegate->HandlePolicyResponse(policy_response_);
}

void MockDeviceManagementBackend::SimulateFailedRegisterRequest(
    const std::string& auth_token,
    const std::string& device_id,
    const em::DeviceRegisterRequest& request,
    DeviceRegisterResponseDelegate* delegate) {
  if (register_remaining_fail_count_) {
    register_remaining_fail_count_--;
    if (!register_remaining_fail_count_) {
      ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)).WillByDefault(Invoke(
          this,
          &MockDeviceManagementBackend::SimulateSuccessfulRegisterRequest));
    }
  }
  delegate->OnError(kErrorRequestFailed);
}

void MockDeviceManagementBackend::SimulateFailedPolicyRequest(
    const std::string& device_management_token,
    const std::string& device_id,
    const em::DevicePolicyRequest& request,
    DevicePolicyResponseDelegate* delegate) {
  if (policy_remaining_fail_count_) {
    policy_remaining_fail_count_--;
    if (!policy_remaining_fail_count_) {
      ON_CALL(*this, ProcessPolicyRequest(_, _, _, _)).
          WillByDefault(Invoke(
              this,
              &MockDeviceManagementBackend::SimulateSuccessfulPolicyRequest));
    }
  }
  delegate->OnError(kErrorRequestFailed);
}

void MockDeviceManagementBackend::SimulateUnmanagedRegisterRequest(
    const std::string& auth_token,
    const std::string& device_id,
    const em::DeviceRegisterRequest& request,
    DeviceRegisterResponseDelegate* delegate) {
  delegate->OnError(kErrorServiceManagementNotSupported);
}

}  // namespace