• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 using testing::Action;
12 
13 namespace em = enterprise_management;
14 
15 namespace policy {
16 namespace {
17 
18 const char kServerUrl[] = "https://example.com/management_service";
19 const char kUserAgent[] = "Chrome 1.2.3(456)";
20 const char kPlatform[] = "Test|Unit|1.2.3";
21 
22 // Common mock request job functionality.
23 class MockRequestJobBase : public DeviceManagementRequestJob {
24  public:
MockRequestJobBase(JobType type,MockDeviceManagementService * service)25   MockRequestJobBase(JobType type,
26                      MockDeviceManagementService* service)
27       : DeviceManagementRequestJob(type, std::string(), std::string()),
28         service_(service) {}
~MockRequestJobBase()29   virtual ~MockRequestJobBase() {}
30 
31  protected:
Run()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.
ExtractParameter(const std::string & name) const44   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().
61 class SyncRequestJob : public MockRequestJobBase {
62  public:
SyncRequestJob(JobType type,MockDeviceManagementService * service,DeviceManagementStatus status,const em::DeviceManagementResponse & response)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) {}
~SyncRequestJob()70   virtual ~SyncRequestJob() {}
71 
72  protected:
Run()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.
86 class AsyncRequestJob : public MockRequestJobBase,
87                         public MockDeviceManagementJob {
88  public:
AsyncRequestJob(JobType type,MockDeviceManagementService * service)89   AsyncRequestJob(JobType type, MockDeviceManagementService* service)
90       : MockRequestJobBase(type, service) {}
~AsyncRequestJob()91   virtual ~AsyncRequestJob() {}
92 
93  protected:
RetryJob()94   virtual void RetryJob() OVERRIDE {
95     if (!retry_callback_.is_null())
96       retry_callback_.Run(this);
97     Run();
98   }
99 
SendResponse(DeviceManagementStatus status,const em::DeviceManagementResponse & response)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 
ACTION_P3(CreateSyncMockDeviceManagementJob,service,status,response)112 ACTION_P3(CreateSyncMockDeviceManagementJob, service, status, response) {
113   return new SyncRequestJob(arg0, service, status, response);
114 }
115 
ACTION_P2(CreateAsyncMockDeviceManagementJob,service,mock_job)116 ACTION_P2(CreateAsyncMockDeviceManagementJob, service, mock_job) {
117   AsyncRequestJob* job = new AsyncRequestJob(arg0, service);
118   *mock_job = job;
119   return job;
120 }
121 
~MockDeviceManagementJob()122 MockDeviceManagementJob::~MockDeviceManagementJob() {}
123 
124 MockDeviceManagementServiceConfiguration::
MockDeviceManagementServiceConfiguration()125     MockDeviceManagementServiceConfiguration()
126     : server_url_(kServerUrl) {}
127 
128 MockDeviceManagementServiceConfiguration::
MockDeviceManagementServiceConfiguration(const std::string & server_url)129     MockDeviceManagementServiceConfiguration(const std::string& server_url)
130     : server_url_(server_url) {}
131 
132 MockDeviceManagementServiceConfiguration::
~MockDeviceManagementServiceConfiguration()133     ~MockDeviceManagementServiceConfiguration() {}
134 
GetServerUrl()135 std::string MockDeviceManagementServiceConfiguration::GetServerUrl() {
136   return server_url_;
137 }
138 
GetAgentParameter()139 std::string MockDeviceManagementServiceConfiguration::GetAgentParameter() {
140   return kUserAgent;
141 }
142 
GetPlatformParameter()143 std::string MockDeviceManagementServiceConfiguration::GetPlatformParameter() {
144   return kPlatform;
145 }
146 
MockDeviceManagementService()147 MockDeviceManagementService::MockDeviceManagementService()
148     : DeviceManagementService(scoped_ptr<Configuration>(
149           new MockDeviceManagementServiceConfiguration)) {}
150 
~MockDeviceManagementService()151 MockDeviceManagementService::~MockDeviceManagementService() {}
152 
153 Action<MockDeviceManagementService::CreateJobFunction>
SucceedJob(const em::DeviceManagementResponse & response)154     MockDeviceManagementService::SucceedJob(
155         const em::DeviceManagementResponse& response) {
156   return CreateSyncMockDeviceManagementJob(this, DM_STATUS_SUCCESS, response);
157 }
158 
159 Action<MockDeviceManagementService::CreateJobFunction>
FailJob(DeviceManagementStatus status)160     MockDeviceManagementService::FailJob(DeviceManagementStatus status) {
161   const em::DeviceManagementResponse dummy_response;
162   return CreateSyncMockDeviceManagementJob(this, status, dummy_response);
163 }
164 
165 Action<MockDeviceManagementService::CreateJobFunction>
CreateAsyncJob(MockDeviceManagementJob ** job)166     MockDeviceManagementService::CreateAsyncJob(MockDeviceManagementJob** job) {
167   return CreateAsyncMockDeviceManagementJob(this, job);
168 }
169 
170 }  // namespace policy
171