| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 #include "chrome/browser/policy/mock_device_management_backend.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/stl_util-inl.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 static const char kFakeDeviceManagementToken[] = "FAKE_DEVICE_TOKEN_"; | |
| 14 static char next_token_suffix_ = '0'; | |
| 15 | |
| 16 } // namespace | |
| 17 | |
| 18 namespace policy { | |
| 19 | |
| 20 using ::testing::_; | |
| 21 using ::testing::Invoke; | |
| 22 | |
| 23 using em::DeviceRegisterRequest; | |
| 24 using em::DeviceUnregisterRequest; | |
| 25 using em::DevicePolicyRequest; | |
| 26 using em::DeviceRegisterResponse; | |
| 27 using em::DeviceUnregisterResponse; | |
| 28 using em::DevicePolicyResponse; | |
| 29 | |
| 30 MockDeviceManagementBackend::MockDeviceManagementBackend() | |
| 31 : DeviceManagementBackend(), | |
| 32 policy_remaining_fail_count_(0), | |
| 33 register_remaining_fail_count_(0), | |
| 34 policy_remaining_success_count_(0) { | |
| 35 policy_setting_ = policy_response_.add_setting(); | |
| 36 policy_setting_->set_policy_key("chrome-policy"); | |
| 37 policy_setting_->set_watermark("fresh"); | |
| 38 } | |
| 39 | |
| 40 MockDeviceManagementBackend::~MockDeviceManagementBackend() { | |
| 41 } | |
| 42 | |
| 43 void MockDeviceManagementBackend::AllShouldSucceed() { | |
| 44 ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)). | |
| 45 WillByDefault(Invoke( | |
| 46 this, | |
| 47 &MockDeviceManagementBackend::SimulateSuccessfulRegisterRequest)); | |
| 48 ON_CALL(*this, ProcessPolicyRequest(_, _, _, _)). | |
| 49 WillByDefault(Invoke( | |
| 50 this, | |
| 51 &MockDeviceManagementBackend::SimulateSuccessfulPolicyRequest)); | |
| 52 } | |
| 53 | |
| 54 void MockDeviceManagementBackend::AllShouldFail() { | |
| 55 ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)). | |
| 56 WillByDefault(Invoke( | |
| 57 this, | |
| 58 &MockDeviceManagementBackend::SimulateFailedRegisterRequest)); | |
| 59 ON_CALL(*this, ProcessPolicyRequest(_, _, _, _)). | |
| 60 WillByDefault(Invoke( | |
| 61 this, | |
| 62 &MockDeviceManagementBackend::SimulateFailedPolicyRequest)); | |
| 63 } | |
| 64 | |
| 65 void MockDeviceManagementBackend::UnmanagedDevice() { | |
| 66 ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)). | |
| 67 WillByDefault(Invoke( | |
| 68 this, | |
| 69 &MockDeviceManagementBackend::SimulateUnmanagedRegisterRequest)); | |
| 70 } | |
| 71 | |
| 72 void MockDeviceManagementBackend::RegisterFailsOncePolicyFailsTwice() { | |
| 73 register_remaining_fail_count_ = 1; | |
| 74 policy_remaining_fail_count_ = 2; | |
| 75 AllShouldFail(); | |
| 76 } | |
| 77 | |
| 78 void MockDeviceManagementBackend::AllWorksFirstPolicyFailsLater() { | |
| 79 policy_remaining_success_count_ = 3; | |
| 80 AllShouldSucceed(); | |
| 81 } | |
| 82 | |
| 83 void MockDeviceManagementBackend::AddBooleanPolicy(const char* policy_name, | |
| 84 bool value) { | |
| 85 em::GenericSetting* policy_value = policy_setting_->mutable_policy_value(); | |
| 86 em::GenericNamedValue* named_value = policy_value->add_named_value(); | |
| 87 named_value->set_name(policy_name); | |
| 88 named_value->mutable_value()->set_value_type( | |
| 89 em::GenericValue::VALUE_TYPE_BOOL); | |
| 90 named_value->mutable_value()->set_bool_value(value); | |
| 91 } | |
| 92 | |
| 93 void MockDeviceManagementBackend::SimulateSuccessfulRegisterRequest( | |
| 94 const std::string& auth_token, | |
| 95 const std::string& device_id, | |
| 96 const em::DeviceRegisterRequest& request, | |
| 97 DeviceRegisterResponseDelegate* delegate) { | |
| 98 DeviceRegisterResponse response; | |
| 99 std::string token(kFakeDeviceManagementToken); | |
| 100 token += next_token_suffix_++; | |
| 101 response.set_device_management_token(token); | |
| 102 delegate->HandleRegisterResponse(response); | |
| 103 } | |
| 104 | |
| 105 void MockDeviceManagementBackend::SimulateSuccessfulPolicyRequest( | |
| 106 const std::string& device_management_token, | |
| 107 const std::string& device_id, | |
| 108 const em::DevicePolicyRequest& request, | |
| 109 DevicePolicyResponseDelegate* delegate) { | |
| 110 if (policy_remaining_success_count_) { | |
| 111 policy_remaining_success_count_--; | |
| 112 if (!policy_remaining_success_count_) { | |
| 113 ON_CALL(*this, ProcessPolicyRequest(_, _, _, _)). | |
| 114 WillByDefault(Invoke( | |
| 115 this, | |
| 116 &MockDeviceManagementBackend::SimulateFailedPolicyRequest)); | |
| 117 } | |
| 118 } | |
| 119 delegate->HandlePolicyResponse(policy_response_); | |
| 120 } | |
| 121 | |
| 122 void MockDeviceManagementBackend::SimulateFailedRegisterRequest( | |
| 123 const std::string& auth_token, | |
| 124 const std::string& device_id, | |
| 125 const em::DeviceRegisterRequest& request, | |
| 126 DeviceRegisterResponseDelegate* delegate) { | |
| 127 if (register_remaining_fail_count_) { | |
| 128 register_remaining_fail_count_--; | |
| 129 if (!register_remaining_fail_count_) { | |
| 130 ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)).WillByDefault(Invoke( | |
| 131 this, | |
| 132 &MockDeviceManagementBackend::SimulateSuccessfulRegisterRequest)); | |
| 133 } | |
| 134 } | |
| 135 delegate->OnError(kErrorRequestFailed); | |
| 136 } | |
| 137 | |
| 138 void MockDeviceManagementBackend::SimulateFailedPolicyRequest( | |
| 139 const std::string& device_management_token, | |
| 140 const std::string& device_id, | |
| 141 const em::DevicePolicyRequest& request, | |
| 142 DevicePolicyResponseDelegate* delegate) { | |
| 143 if (policy_remaining_fail_count_) { | |
| 144 policy_remaining_fail_count_--; | |
| 145 if (!policy_remaining_fail_count_) { | |
| 146 ON_CALL(*this, ProcessPolicyRequest(_, _, _, _)). | |
| 147 WillByDefault(Invoke( | |
| 148 this, | |
| 149 &MockDeviceManagementBackend::SimulateSuccessfulPolicyRequest)); | |
| 150 } | |
| 151 } | |
| 152 delegate->OnError(kErrorRequestFailed); | |
| 153 } | |
| 154 | |
| 155 void MockDeviceManagementBackend::SimulateUnmanagedRegisterRequest( | |
| 156 const std::string& auth_token, | |
| 157 const std::string& device_id, | |
| 158 const em::DeviceRegisterRequest& request, | |
| 159 DeviceRegisterResponseDelegate* delegate) { | |
| 160 delegate->OnError(kErrorServiceManagementNotSupported); | |
| 161 } | |
| 162 | |
| 163 } // namespace | |
| OLD | NEW |