OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 base::TimeDelta::FromSeconds( | 140 base::TimeDelta::FromSeconds( |
141 kTestClientServerCommunicationDelayInSeconds)); | 141 kTestClientServerCommunicationDelayInSeconds)); |
142 } | 142 } |
143 | 143 |
144 void DoFetchRemoteCommands( | 144 void DoFetchRemoteCommands( |
145 scoped_ptr<RemoteCommandJob::UniqueIDType> last_command_id, | 145 scoped_ptr<RemoteCommandJob::UniqueIDType> last_command_id, |
146 const std::vector<em::RemoteCommandResult>& command_results, | 146 const std::vector<em::RemoteCommandResult>& command_results, |
147 const RemoteCommandCallback& callback, | 147 const RemoteCommandCallback& callback, |
148 const FetchCallExpectation& fetch_call_expectation) { | 148 const FetchCallExpectation& fetch_call_expectation) { |
149 const std::vector<em::RemoteCommand> fetched_commands = | 149 const std::vector<em::RemoteCommand> fetched_commands = |
150 server_->FetchCommands(last_command_id.Pass(), command_results); | 150 server_->FetchCommands(std::move(last_command_id), command_results); |
151 | 151 |
152 EXPECT_EQ(fetch_call_expectation.expected_command_results, | 152 EXPECT_EQ(fetch_call_expectation.expected_command_results, |
153 command_results.size()); | 153 command_results.size()); |
154 EXPECT_EQ(fetch_call_expectation.expected_fetched_commands, | 154 EXPECT_EQ(fetch_call_expectation.expected_fetched_commands, |
155 fetched_commands.size()); | 155 fetched_commands.size()); |
156 | 156 |
157 if (!fetch_call_expectation.commands_fetched_callback.is_null()) | 157 if (!fetch_call_expectation.commands_fetched_callback.is_null()) |
158 fetch_call_expectation.commands_fetched_callback.Run(); | 158 fetch_call_expectation.commands_fetched_callback.Run(); |
159 | 159 |
160 // Simulate delay from DMServer back to client. | 160 // Simulate delay from DMServer back to client. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 | 240 |
241 // Base class for unit tests regarding remote commands service. | 241 // Base class for unit tests regarding remote commands service. |
242 class RemoteCommandsServiceTest : public testing::Test { | 242 class RemoteCommandsServiceTest : public testing::Test { |
243 protected: | 243 protected: |
244 RemoteCommandsServiceTest() | 244 RemoteCommandsServiceTest() |
245 : task_runner_(new ScopedMockTimeTaskRunner()), | 245 : task_runner_(new ScopedMockTimeTaskRunner()), |
246 runner_handle_(task_runner_) {} | 246 runner_handle_(task_runner_) {} |
247 | 247 |
248 void SetUp() override { | 248 void SetUp() override { |
249 server_.reset(new TestingRemoteCommandsServer()); | 249 server_.reset(new TestingRemoteCommandsServer()); |
250 server_->SetClock(task_runner_->GetMockTickClock().Pass()); | 250 server_->SetClock(task_runner_->GetMockTickClock()); |
251 cloud_policy_client_.reset( | 251 cloud_policy_client_.reset( |
252 new TestingCloudPolicyClientForRemoteCommands(server_.get())); | 252 new TestingCloudPolicyClientForRemoteCommands(server_.get())); |
253 } | 253 } |
254 | 254 |
255 void TearDown() override { | 255 void TearDown() override { |
256 remote_commands_service_.reset(); | 256 remote_commands_service_.reset(); |
257 cloud_policy_client_.reset(); | 257 cloud_policy_client_.reset(); |
258 server_.reset(); | 258 server_.reset(); |
259 } | 259 } |
260 | 260 |
261 void StartService(scoped_ptr<RemoteCommandsFactory> factory) { | 261 void StartService(scoped_ptr<RemoteCommandsFactory> factory) { |
262 remote_commands_service_.reset( | 262 remote_commands_service_.reset(new RemoteCommandsService( |
263 new RemoteCommandsService(factory.Pass(), cloud_policy_client_.get())); | 263 std::move(factory), cloud_policy_client_.get())); |
264 remote_commands_service_->SetClockForTesting( | 264 remote_commands_service_->SetClockForTesting( |
265 task_runner_->GetMockTickClock()); | 265 task_runner_->GetMockTickClock()); |
266 } | 266 } |
267 | 267 |
268 void FlushAllTasks() { | 268 void FlushAllTasks() { |
269 task_runner_->FastForwardUntilNoTasksRemain(); | 269 task_runner_->FastForwardUntilNoTasksRemain(); |
270 } | 270 } |
271 | 271 |
272 scoped_ptr<TestingRemoteCommandsServer> server_; | 272 scoped_ptr<TestingRemoteCommandsServer> server_; |
273 scoped_ptr<TestingCloudPolicyClientForRemoteCommands> cloud_policy_client_; | 273 scoped_ptr<TestingCloudPolicyClientForRemoteCommands> cloud_policy_client_; |
274 scoped_ptr<RemoteCommandsService> remote_commands_service_; | 274 scoped_ptr<RemoteCommandsService> remote_commands_service_; |
275 | 275 |
276 scoped_refptr<ScopedMockTimeTaskRunner> task_runner_; | 276 scoped_refptr<ScopedMockTimeTaskRunner> task_runner_; |
277 | 277 |
278 private: | 278 private: |
279 bool service_started_ = false; | 279 bool service_started_ = false; |
280 | 280 |
281 base::ThreadTaskRunnerHandle runner_handle_; | 281 base::ThreadTaskRunnerHandle runner_handle_; |
282 | 282 |
283 DISALLOW_COPY_AND_ASSIGN(RemoteCommandsServiceTest); | 283 DISALLOW_COPY_AND_ASSIGN(RemoteCommandsServiceTest); |
284 }; | 284 }; |
285 | 285 |
286 // Tests that no command will be fetched if no commands is issued. | 286 // Tests that no command will be fetched if no commands is issued. |
287 TEST_F(RemoteCommandsServiceTest, NoCommands) { | 287 TEST_F(RemoteCommandsServiceTest, NoCommands) { |
288 scoped_ptr<MockTestRemoteCommandFactory> factory( | 288 scoped_ptr<MockTestRemoteCommandFactory> factory( |
289 new MockTestRemoteCommandFactory()); | 289 new MockTestRemoteCommandFactory()); |
290 EXPECT_CALL(*factory, BuildTestCommand()).Times(0); | 290 EXPECT_CALL(*factory, BuildTestCommand()).Times(0); |
291 | 291 |
292 StartService(factory.Pass()); | 292 StartService(std::move(factory)); |
293 | 293 |
294 // A fetch requst should get nothing from server. | 294 // A fetch requst should get nothing from server. |
295 cloud_policy_client_->ExpectFetchCommands(0u, 0u, base::Closure()); | 295 cloud_policy_client_->ExpectFetchCommands(0u, 0u, base::Closure()); |
296 EXPECT_TRUE(remote_commands_service_->FetchRemoteCommands()); | 296 EXPECT_TRUE(remote_commands_service_->FetchRemoteCommands()); |
297 | 297 |
298 FlushAllTasks(); | 298 FlushAllTasks(); |
299 } | 299 } |
300 | 300 |
301 // Tests that existing commands issued before service started will be fetched. | 301 // Tests that existing commands issued before service started will be fetched. |
302 TEST_F(RemoteCommandsServiceTest, ExistingCommand) { | 302 TEST_F(RemoteCommandsServiceTest, ExistingCommand) { |
303 scoped_ptr<MockTestRemoteCommandFactory> factory( | 303 scoped_ptr<MockTestRemoteCommandFactory> factory( |
304 new MockTestRemoteCommandFactory()); | 304 new MockTestRemoteCommandFactory()); |
305 EXPECT_CALL(*factory, BuildTestCommand()).Times(1); | 305 EXPECT_CALL(*factory, BuildTestCommand()).Times(1); |
306 | 306 |
307 { | 307 { |
308 ScopedMockTimeTaskRunner::ScopedRunner scoped_runner(task_runner_); | 308 ScopedMockTimeTaskRunner::ScopedRunner scoped_runner(task_runner_); |
309 | 309 |
310 // Issue a command before service started. | 310 // Issue a command before service started. |
311 server_->IssueCommand(em::RemoteCommand_Type_COMMAND_ECHO_TEST, | 311 server_->IssueCommand(em::RemoteCommand_Type_COMMAND_ECHO_TEST, |
312 kTestPayload, | 312 kTestPayload, |
313 base::Bind(&ExpectSucceededJob, kTestPayload), false); | 313 base::Bind(&ExpectSucceededJob, kTestPayload), false); |
314 | 314 |
315 // Start the service, run until the command is fetched. | 315 // Start the service, run until the command is fetched. |
316 cloud_policy_client_->ExpectFetchCommands(0u, 1u, | 316 cloud_policy_client_->ExpectFetchCommands(0u, 1u, |
317 scoped_runner.QuitClosure()); | 317 scoped_runner.QuitClosure()); |
318 StartService(factory.Pass()); | 318 StartService(std::move(factory)); |
319 EXPECT_TRUE(remote_commands_service_->FetchRemoteCommands()); | 319 EXPECT_TRUE(remote_commands_service_->FetchRemoteCommands()); |
320 | 320 |
321 scoped_runner.Run(); | 321 scoped_runner.Run(); |
322 } | 322 } |
323 | 323 |
324 // And run again so that the result can be reported. | 324 // And run again so that the result can be reported. |
325 cloud_policy_client_->ExpectFetchCommands(1u, 0u, base::Closure()); | 325 cloud_policy_client_->ExpectFetchCommands(1u, 0u, base::Closure()); |
326 | 326 |
327 FlushAllTasks(); | 327 FlushAllTasks(); |
328 | 328 |
329 EXPECT_EQ(0u, server_->NumberOfCommandsPendingResult()); | 329 EXPECT_EQ(0u, server_->NumberOfCommandsPendingResult()); |
330 } | 330 } |
331 | 331 |
332 // Tests that commands issued after service started will be fetched. | 332 // Tests that commands issued after service started will be fetched. |
333 TEST_F(RemoteCommandsServiceTest, NewCommand) { | 333 TEST_F(RemoteCommandsServiceTest, NewCommand) { |
334 scoped_ptr<MockTestRemoteCommandFactory> factory( | 334 scoped_ptr<MockTestRemoteCommandFactory> factory( |
335 new MockTestRemoteCommandFactory()); | 335 new MockTestRemoteCommandFactory()); |
336 EXPECT_CALL(*factory, BuildTestCommand()).Times(1); | 336 EXPECT_CALL(*factory, BuildTestCommand()).Times(1); |
337 | 337 |
338 StartService(factory.Pass()); | 338 StartService(std::move(factory)); |
339 | 339 |
340 // Set up expectations on fetch commands calls. The first request will fetch | 340 // Set up expectations on fetch commands calls. The first request will fetch |
341 // one command, and the second will fetch none but provide result for the | 341 // one command, and the second will fetch none but provide result for the |
342 // previous command instead. | 342 // previous command instead. |
343 cloud_policy_client_->ExpectFetchCommands(0u, 1u, base::Closure()); | 343 cloud_policy_client_->ExpectFetchCommands(0u, 1u, base::Closure()); |
344 cloud_policy_client_->ExpectFetchCommands(1u, 0u, base::Closure()); | 344 cloud_policy_client_->ExpectFetchCommands(1u, 0u, base::Closure()); |
345 | 345 |
346 // Issue a command and manually start a command fetch. | 346 // Issue a command and manually start a command fetch. |
347 server_->IssueCommand(em::RemoteCommand_Type_COMMAND_ECHO_TEST, kTestPayload, | 347 server_->IssueCommand(em::RemoteCommand_Type_COMMAND_ECHO_TEST, kTestPayload, |
348 base::Bind(&ExpectSucceededJob, kTestPayload), false); | 348 base::Bind(&ExpectSucceededJob, kTestPayload), false); |
349 EXPECT_TRUE(remote_commands_service_->FetchRemoteCommands()); | 349 EXPECT_TRUE(remote_commands_service_->FetchRemoteCommands()); |
350 | 350 |
351 FlushAllTasks(); | 351 FlushAllTasks(); |
352 | 352 |
353 EXPECT_EQ(0u, server_->NumberOfCommandsPendingResult()); | 353 EXPECT_EQ(0u, server_->NumberOfCommandsPendingResult()); |
354 } | 354 } |
355 | 355 |
356 // Tests that commands issued after service started will be fetched, even if | 356 // Tests that commands issued after service started will be fetched, even if |
357 // the command is issued when a fetch request is ongoing. | 357 // the command is issued when a fetch request is ongoing. |
358 TEST_F(RemoteCommandsServiceTest, NewCommandFollwingFetch) { | 358 TEST_F(RemoteCommandsServiceTest, NewCommandFollwingFetch) { |
359 scoped_ptr<MockTestRemoteCommandFactory> factory( | 359 scoped_ptr<MockTestRemoteCommandFactory> factory( |
360 new MockTestRemoteCommandFactory()); | 360 new MockTestRemoteCommandFactory()); |
361 EXPECT_CALL(*factory, BuildTestCommand()).Times(1); | 361 EXPECT_CALL(*factory, BuildTestCommand()).Times(1); |
362 | 362 |
363 StartService(factory.Pass()); | 363 StartService(std::move(factory)); |
364 | 364 |
365 { | 365 { |
366 ScopedMockTimeTaskRunner::ScopedRunner scoped_runner(task_runner_); | 366 ScopedMockTimeTaskRunner::ScopedRunner scoped_runner(task_runner_); |
367 | 367 |
368 // Add a command which will be issued after first fetch. | 368 // Add a command which will be issued after first fetch. |
369 server_->IssueCommand(em::RemoteCommand_Type_COMMAND_ECHO_TEST, | 369 server_->IssueCommand(em::RemoteCommand_Type_COMMAND_ECHO_TEST, |
370 kTestPayload, | 370 kTestPayload, |
371 base::Bind(&ExpectSucceededJob, kTestPayload), true); | 371 base::Bind(&ExpectSucceededJob, kTestPayload), true); |
372 | 372 |
373 cloud_policy_client_->ExpectFetchCommands(0u, 0u, | 373 cloud_policy_client_->ExpectFetchCommands(0u, 0u, |
(...skipping 23 matching lines...) Expand all Loading... |
397 cloud_policy_client_->ExpectFetchCommands(1u, 0u, base::Closure()); | 397 cloud_policy_client_->ExpectFetchCommands(1u, 0u, base::Closure()); |
398 | 398 |
399 // No further fetch request is made, but the new issued command should be | 399 // No further fetch request is made, but the new issued command should be |
400 // fetched and executed. | 400 // fetched and executed. |
401 FlushAllTasks(); | 401 FlushAllTasks(); |
402 | 402 |
403 EXPECT_EQ(0u, server_->NumberOfCommandsPendingResult()); | 403 EXPECT_EQ(0u, server_->NumberOfCommandsPendingResult()); |
404 } | 404 } |
405 | 405 |
406 } // namespace policy | 406 } // namespace policy |
OLD | NEW |