OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/mock_device_management_backend.h" | 5 #include "chrome/browser/policy/mock_device_management_backend.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 using ::testing::Invoke; | 21 using ::testing::Invoke; |
22 | 22 |
23 using em::DeviceRegisterRequest; | 23 using em::DeviceRegisterRequest; |
24 using em::DeviceUnregisterRequest; | 24 using em::DeviceUnregisterRequest; |
25 using em::DevicePolicyRequest; | 25 using em::DevicePolicyRequest; |
26 using em::DeviceRegisterResponse; | 26 using em::DeviceRegisterResponse; |
27 using em::DeviceUnregisterResponse; | 27 using em::DeviceUnregisterResponse; |
28 using em::DevicePolicyResponse; | 28 using em::DevicePolicyResponse; |
29 | 29 |
30 MockDeviceManagementBackend::MockDeviceManagementBackend() | 30 MockDeviceManagementBackend::MockDeviceManagementBackend() |
31 : DeviceManagementBackend() { | 31 : DeviceManagementBackend(), |
| 32 policy_remaining_fail_count_(0), |
| 33 register_remaining_fail_count_(0), |
| 34 policy_remaining_success_count_(0) { |
32 policy_setting_ = policy_response_.add_setting(); | 35 policy_setting_ = policy_response_.add_setting(); |
33 policy_setting_->set_policy_key("chrome-policy"); | 36 policy_setting_->set_policy_key("chrome-policy"); |
34 policy_setting_->set_watermark("fresh"); | 37 policy_setting_->set_watermark("fresh"); |
35 } | 38 } |
36 | 39 |
37 MockDeviceManagementBackend::~MockDeviceManagementBackend() { | 40 MockDeviceManagementBackend::~MockDeviceManagementBackend() { |
38 } | 41 } |
39 | 42 |
40 void MockDeviceManagementBackend::AllShouldSucceed() { | 43 void MockDeviceManagementBackend::AllShouldSucceed() { |
41 ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)). | 44 ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)). |
(...skipping 17 matching lines...) Expand all Loading... |
59 &MockDeviceManagementBackend::SimulateFailedPolicyRequest)); | 62 &MockDeviceManagementBackend::SimulateFailedPolicyRequest)); |
60 } | 63 } |
61 | 64 |
62 void MockDeviceManagementBackend::UnmanagedDevice() { | 65 void MockDeviceManagementBackend::UnmanagedDevice() { |
63 ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)). | 66 ON_CALL(*this, ProcessRegisterRequest(_, _, _, _)). |
64 WillByDefault(Invoke( | 67 WillByDefault(Invoke( |
65 this, | 68 this, |
66 &MockDeviceManagementBackend::SimulateUnmanagedRegisterRequest)); | 69 &MockDeviceManagementBackend::SimulateUnmanagedRegisterRequest)); |
67 } | 70 } |
68 | 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 |
69 void MockDeviceManagementBackend::AddBooleanPolicy(const char* policy_name, | 83 void MockDeviceManagementBackend::AddBooleanPolicy(const char* policy_name, |
70 bool value) { | 84 bool value) { |
71 em::GenericSetting* policy_value = policy_setting_->mutable_policy_value(); | 85 em::GenericSetting* policy_value = policy_setting_->mutable_policy_value(); |
72 em::GenericNamedValue* named_value = policy_value->add_named_value(); | 86 em::GenericNamedValue* named_value = policy_value->add_named_value(); |
73 named_value->set_name(policy_name); | 87 named_value->set_name(policy_name); |
74 named_value->mutable_value()->set_value_type( | 88 named_value->mutable_value()->set_value_type( |
75 em::GenericValue::VALUE_TYPE_BOOL); | 89 em::GenericValue::VALUE_TYPE_BOOL); |
76 named_value->mutable_value()->set_bool_value(value); | 90 named_value->mutable_value()->set_bool_value(value); |
77 } | 91 } |
78 | 92 |
79 void MockDeviceManagementBackend::SimulateSuccessfulRegisterRequest( | 93 void MockDeviceManagementBackend::SimulateSuccessfulRegisterRequest( |
80 const std::string& auth_token, | 94 const std::string& auth_token, |
81 const std::string& device_id, | 95 const std::string& device_id, |
82 const em::DeviceRegisterRequest& request, | 96 const em::DeviceRegisterRequest& request, |
83 DeviceRegisterResponseDelegate* delegate) { | 97 DeviceRegisterResponseDelegate* delegate) { |
84 DeviceRegisterResponse response; | 98 DeviceRegisterResponse response; |
85 std::string token(kFakeDeviceManagementToken); | 99 std::string token(kFakeDeviceManagementToken); |
86 token += next_token_suffix_++; | 100 token += next_token_suffix_++; |
87 response.set_device_management_token(token); | 101 response.set_device_management_token(token); |
88 delegate->HandleRegisterResponse(response); | 102 delegate->HandleRegisterResponse(response); |
89 } | 103 } |
90 | 104 |
91 void MockDeviceManagementBackend::SimulateSuccessfulPolicyRequest( | 105 void MockDeviceManagementBackend::SimulateSuccessfulPolicyRequest( |
92 const std::string& device_management_token, | 106 const std::string& device_management_token, |
93 const std::string& device_id, | 107 const std::string& device_id, |
94 const em::DevicePolicyRequest& request, | 108 const em::DevicePolicyRequest& request, |
95 DevicePolicyResponseDelegate* delegate) { | 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 } |
96 delegate->HandlePolicyResponse(policy_response_); | 119 delegate->HandlePolicyResponse(policy_response_); |
97 } | 120 } |
98 | 121 |
99 void MockDeviceManagementBackend::SimulateFailedRegisterRequest( | 122 void MockDeviceManagementBackend::SimulateFailedRegisterRequest( |
100 const std::string& auth_token, | 123 const std::string& auth_token, |
101 const std::string& device_id, | 124 const std::string& device_id, |
102 const em::DeviceRegisterRequest& request, | 125 const em::DeviceRegisterRequest& request, |
103 DeviceRegisterResponseDelegate* delegate) { | 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 } |
104 delegate->OnError(kErrorRequestFailed); | 135 delegate->OnError(kErrorRequestFailed); |
105 } | 136 } |
106 | 137 |
107 void MockDeviceManagementBackend::SimulateFailedPolicyRequest( | 138 void MockDeviceManagementBackend::SimulateFailedPolicyRequest( |
108 const std::string& device_management_token, | 139 const std::string& device_management_token, |
109 const std::string& device_id, | 140 const std::string& device_id, |
110 const em::DevicePolicyRequest& request, | 141 const em::DevicePolicyRequest& request, |
111 DevicePolicyResponseDelegate* delegate) { | 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 } |
112 delegate->OnError(kErrorRequestFailed); | 152 delegate->OnError(kErrorRequestFailed); |
113 } | 153 } |
114 | 154 |
115 void MockDeviceManagementBackend::SimulateUnmanagedRegisterRequest( | 155 void MockDeviceManagementBackend::SimulateUnmanagedRegisterRequest( |
116 const std::string& auth_token, | 156 const std::string& auth_token, |
117 const std::string& device_id, | 157 const std::string& device_id, |
118 const em::DeviceRegisterRequest& request, | 158 const em::DeviceRegisterRequest& request, |
119 DeviceRegisterResponseDelegate* delegate) { | 159 DeviceRegisterResponseDelegate* delegate) { |
120 delegate->OnError(kErrorServiceManagementNotSupported); | 160 delegate->OnError(kErrorServiceManagementNotSupported); |
121 } | 161 } |
122 | 162 |
123 } // namespace | 163 } // namespace |
OLD | NEW |