1// Copyright (c) 2012 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 "components/policy/core/common/cloud/mock_device_management_service.h" 6 7#include "base/strings/string_util.h" 8#include "net/base/net_errors.h" 9#include "net/url_request/url_request_context_getter.h" 10 11using testing::Action; 12 13namespace em = enterprise_management; 14 15namespace policy { 16namespace { 17 18const char kServerUrl[] = "https://example.com/management_service"; 19const char kUserAgent[] = "Chrome 1.2.3(456)"; 20const char kPlatform[] = "Test|Unit|1.2.3"; 21 22// Common mock request job functionality. 23class MockRequestJobBase : public DeviceManagementRequestJob { 24 public: 25 MockRequestJobBase(JobType type, 26 MockDeviceManagementService* service) 27 : DeviceManagementRequestJob(type, std::string(), std::string()), 28 service_(service) {} 29 virtual ~MockRequestJobBase() {} 30 31 protected: 32 virtual void Run() OVERRIDE { 33 service_->StartJob(ExtractParameter(dm_protocol::kParamRequest), 34 gaia_token_, 35 ExtractParameter(dm_protocol::kParamOAuthToken), 36 dm_token_, 37 ExtractParameter(dm_protocol::kParamUserAffiliation), 38 ExtractParameter(dm_protocol::kParamDeviceID), 39 request_); 40 } 41 42 private: 43 // Searches for a query parameter and returns the associated value. 44 const std::string& ExtractParameter(const std::string& name) const { 45 for (ParameterMap::const_iterator entry(query_params_.begin()); 46 entry != query_params_.end(); 47 ++entry) { 48 if (name == entry->first) 49 return entry->second; 50 } 51 52 return base::EmptyString(); 53 } 54 55 MockDeviceManagementService* service_; 56 57 DISALLOW_COPY_AND_ASSIGN(MockRequestJobBase); 58}; 59 60// Synchronous mock request job that immediately completes on calling Run(). 61class SyncRequestJob : public MockRequestJobBase { 62 public: 63 SyncRequestJob(JobType type, 64 MockDeviceManagementService* service, 65 DeviceManagementStatus status, 66 const em::DeviceManagementResponse& response) 67 : MockRequestJobBase(type, service), 68 status_(status), 69 response_(response) {} 70 virtual ~SyncRequestJob() {} 71 72 protected: 73 virtual void Run() OVERRIDE { 74 MockRequestJobBase::Run(); 75 callback_.Run(status_, net::OK, response_); 76 } 77 78 private: 79 DeviceManagementStatus status_; 80 em::DeviceManagementResponse response_; 81 82 DISALLOW_COPY_AND_ASSIGN(SyncRequestJob); 83}; 84 85// Asynchronous job that allows the test to delay job completion. 86class AsyncRequestJob : public MockRequestJobBase, 87 public MockDeviceManagementJob { 88 public: 89 AsyncRequestJob(JobType type, MockDeviceManagementService* service) 90 : MockRequestJobBase(type, service) {} 91 virtual ~AsyncRequestJob() {} 92 93 protected: 94 virtual void RetryJob() OVERRIDE { 95 if (!retry_callback_.is_null()) 96 retry_callback_.Run(this); 97 Run(); 98 } 99 100 virtual void SendResponse( 101 DeviceManagementStatus status, 102 const em::DeviceManagementResponse& response) OVERRIDE { 103 callback_.Run(status, net::OK, response); 104 } 105 106 private: 107 DISALLOW_COPY_AND_ASSIGN(AsyncRequestJob); 108}; 109 110} // namespace 111 112ACTION_P3(CreateSyncMockDeviceManagementJob, service, status, response) { 113 return new SyncRequestJob(arg0, service, status, response); 114} 115 116ACTION_P2(CreateAsyncMockDeviceManagementJob, service, mock_job) { 117 AsyncRequestJob* job = new AsyncRequestJob(arg0, service); 118 *mock_job = job; 119 return job; 120} 121 122MockDeviceManagementJob::~MockDeviceManagementJob() {} 123 124MockDeviceManagementServiceConfiguration:: 125 MockDeviceManagementServiceConfiguration() 126 : server_url_(kServerUrl) {} 127 128MockDeviceManagementServiceConfiguration:: 129 MockDeviceManagementServiceConfiguration(const std::string& server_url) 130 : server_url_(server_url) {} 131 132MockDeviceManagementServiceConfiguration:: 133 ~MockDeviceManagementServiceConfiguration() {} 134 135std::string MockDeviceManagementServiceConfiguration::GetServerUrl() { 136 return server_url_; 137} 138 139std::string MockDeviceManagementServiceConfiguration::GetAgentParameter() { 140 return kUserAgent; 141} 142 143std::string MockDeviceManagementServiceConfiguration::GetPlatformParameter() { 144 return kPlatform; 145} 146 147MockDeviceManagementService::MockDeviceManagementService() 148 : DeviceManagementService(scoped_ptr<Configuration>( 149 new MockDeviceManagementServiceConfiguration)) {} 150 151MockDeviceManagementService::~MockDeviceManagementService() {} 152 153Action<MockDeviceManagementService::CreateJobFunction> 154 MockDeviceManagementService::SucceedJob( 155 const em::DeviceManagementResponse& response) { 156 return CreateSyncMockDeviceManagementJob(this, DM_STATUS_SUCCESS, response); 157} 158 159Action<MockDeviceManagementService::CreateJobFunction> 160 MockDeviceManagementService::FailJob(DeviceManagementStatus status) { 161 const em::DeviceManagementResponse dummy_response; 162 return CreateSyncMockDeviceManagementJob(this, status, dummy_response); 163} 164 165Action<MockDeviceManagementService::CreateJobFunction> 166 MockDeviceManagementService::CreateAsyncJob(MockDeviceManagementJob** job) { 167 return CreateAsyncMockDeviceManagementJob(this, job); 168} 169 170} // namespace policy 171