OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "remoting/host/it2me/it2me_native_messaging_host.h" | 5 #include "remoting/host/it2me/it2me_native_messaging_host.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 } // namespace | 68 } // namespace |
69 | 69 |
70 class MockIt2MeHost : public It2MeHost { | 70 class MockIt2MeHost : public It2MeHost { |
71 public: | 71 public: |
72 MockIt2MeHost(scoped_ptr<ChromotingHostContext> context, | 72 MockIt2MeHost(scoped_ptr<ChromotingHostContext> context, |
73 scoped_ptr<PolicyWatcher> policy_watcher, | 73 scoped_ptr<PolicyWatcher> policy_watcher, |
74 base::WeakPtr<It2MeHost::Observer> observer, | 74 base::WeakPtr<It2MeHost::Observer> observer, |
75 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, | 75 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, |
76 const std::string& directory_bot_jid) | 76 const std::string& directory_bot_jid) |
77 : It2MeHost(context.Pass(), | 77 : It2MeHost(std::move(context), |
78 policy_watcher.Pass(), | 78 std::move(policy_watcher), |
79 nullptr, | 79 nullptr, |
80 observer, | 80 observer, |
81 xmpp_server_config, | 81 xmpp_server_config, |
82 directory_bot_jid) {} | 82 directory_bot_jid) {} |
83 | 83 |
84 // It2MeHost overrides | 84 // It2MeHost overrides |
85 void Connect() override; | 85 void Connect() override; |
86 void Disconnect() override; | 86 void Disconnect() override; |
87 void RequestNatPolicy() override; | 87 void RequestNatPolicy() override; |
88 | 88 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 149 } |
150 | 150 |
151 class MockIt2MeHostFactory : public It2MeHostFactory { | 151 class MockIt2MeHostFactory : public It2MeHostFactory { |
152 public: | 152 public: |
153 MockIt2MeHostFactory() : It2MeHostFactory() {} | 153 MockIt2MeHostFactory() : It2MeHostFactory() {} |
154 scoped_refptr<It2MeHost> CreateIt2MeHost( | 154 scoped_refptr<It2MeHost> CreateIt2MeHost( |
155 scoped_ptr<ChromotingHostContext> context, | 155 scoped_ptr<ChromotingHostContext> context, |
156 base::WeakPtr<It2MeHost::Observer> observer, | 156 base::WeakPtr<It2MeHost::Observer> observer, |
157 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, | 157 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, |
158 const std::string& directory_bot_jid) override { | 158 const std::string& directory_bot_jid) override { |
159 return new MockIt2MeHost(context.Pass(), nullptr, observer, | 159 return new MockIt2MeHost(std::move(context), nullptr, observer, |
160 xmpp_server_config, directory_bot_jid); | 160 xmpp_server_config, directory_bot_jid); |
161 } | 161 } |
162 | 162 |
163 private: | 163 private: |
164 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHostFactory); | 164 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHostFactory); |
165 }; // MockIt2MeHostFactory | 165 }; // MockIt2MeHostFactory |
166 | 166 |
167 class It2MeNativeMessagingHostTest : public testing::Test { | 167 class It2MeNativeMessagingHostTest : public testing::Test { |
168 public: | 168 public: |
169 It2MeNativeMessagingHostTest() {} | 169 It2MeNativeMessagingHostTest() {} |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 base::JSONWriter::Write(message, &message_json); | 302 base::JSONWriter::Write(message, &message_json); |
303 | 303 |
304 uint32 length = message_json.length(); | 304 uint32 length = message_json.length(); |
305 input_write_file_.WriteAtCurrentPos(reinterpret_cast<char*>(&length), | 305 input_write_file_.WriteAtCurrentPos(reinterpret_cast<char*>(&length), |
306 sizeof(length)); | 306 sizeof(length)); |
307 input_write_file_.WriteAtCurrentPos(message_json.data(), length); | 307 input_write_file_.WriteAtCurrentPos(message_json.data(), length); |
308 } | 308 } |
309 | 309 |
310 void It2MeNativeMessagingHostTest::VerifyHelloResponse(int request_id) { | 310 void It2MeNativeMessagingHostTest::VerifyHelloResponse(int request_id) { |
311 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); | 311 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); |
312 VerifyCommonProperties(response.Pass(), "helloResponse", request_id); | 312 VerifyCommonProperties(std::move(response), "helloResponse", request_id); |
313 } | 313 } |
314 | 314 |
315 void It2MeNativeMessagingHostTest::VerifyErrorResponse() { | 315 void It2MeNativeMessagingHostTest::VerifyErrorResponse() { |
316 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); | 316 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); |
317 VerifyStringProperty(response.Pass(), "type", "error"); | 317 VerifyStringProperty(std::move(response), "type", "error"); |
318 } | 318 } |
319 | 319 |
320 void It2MeNativeMessagingHostTest::VerifyConnectResponses(int request_id) { | 320 void It2MeNativeMessagingHostTest::VerifyConnectResponses(int request_id) { |
321 bool connect_response_received = false; | 321 bool connect_response_received = false; |
322 bool starting_received = false; | 322 bool starting_received = false; |
323 bool requestedAccessCode_received = false; | 323 bool requestedAccessCode_received = false; |
324 bool receivedAccessCode_received = false; | 324 bool receivedAccessCode_received = false; |
325 bool connected_received = false; | 325 bool connected_received = false; |
326 | 326 |
327 // We expect a total of 5 messages: 1 connectResponse and 4 hostStateChanged. | 327 // We expect a total of 5 messages: 1 connectResponse and 4 hostStateChanged. |
328 for (int i = 0; i < 5; ++i) { | 328 for (int i = 0; i < 5; ++i) { |
329 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); | 329 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); |
330 ASSERT_TRUE(response); | 330 ASSERT_TRUE(response); |
331 | 331 |
332 std::string type; | 332 std::string type; |
333 ASSERT_TRUE(response->GetString("type", &type)); | 333 ASSERT_TRUE(response->GetString("type", &type)); |
334 | 334 |
335 if (type == "connectResponse") { | 335 if (type == "connectResponse") { |
336 EXPECT_FALSE(connect_response_received); | 336 EXPECT_FALSE(connect_response_received); |
337 connect_response_received = true; | 337 connect_response_received = true; |
338 VerifyId(response.Pass(), request_id); | 338 VerifyId(std::move(response), request_id); |
339 } else if (type == "hostStateChanged") { | 339 } else if (type == "hostStateChanged") { |
340 std::string state; | 340 std::string state; |
341 ASSERT_TRUE(response->GetString("state", &state)); | 341 ASSERT_TRUE(response->GetString("state", &state)); |
342 | 342 |
343 std::string value; | 343 std::string value; |
344 if (state == It2MeNativeMessagingHost::HostStateToString(kStarting)) { | 344 if (state == It2MeNativeMessagingHost::HostStateToString(kStarting)) { |
345 EXPECT_FALSE(starting_received); | 345 EXPECT_FALSE(starting_received); |
346 starting_received = true; | 346 starting_received = true; |
347 } else if (state == It2MeNativeMessagingHost::HostStateToString( | 347 } else if (state == It2MeNativeMessagingHost::HostStateToString( |
348 kRequestedAccessCode)) { | 348 kRequestedAccessCode)) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 for (int i = 0; i < 2; ++i) { | 384 for (int i = 0; i < 2; ++i) { |
385 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); | 385 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); |
386 ASSERT_TRUE(response); | 386 ASSERT_TRUE(response); |
387 | 387 |
388 std::string type; | 388 std::string type; |
389 ASSERT_TRUE(response->GetString("type", &type)); | 389 ASSERT_TRUE(response->GetString("type", &type)); |
390 | 390 |
391 if (type == "disconnectResponse") { | 391 if (type == "disconnectResponse") { |
392 EXPECT_FALSE(disconnect_response_received); | 392 EXPECT_FALSE(disconnect_response_received); |
393 disconnect_response_received = true; | 393 disconnect_response_received = true; |
394 VerifyId(response.Pass(), request_id); | 394 VerifyId(std::move(response), request_id); |
395 } else if (type == "hostStateChanged") { | 395 } else if (type == "hostStateChanged") { |
396 std::string state; | 396 std::string state; |
397 ASSERT_TRUE(response->GetString("state", &state)); | 397 ASSERT_TRUE(response->GetString("state", &state)); |
398 if (state == It2MeNativeMessagingHost::HostStateToString(kDisconnected)) { | 398 if (state == It2MeNativeMessagingHost::HostStateToString(kDisconnected)) { |
399 EXPECT_FALSE(disconnected_received); | 399 EXPECT_FALSE(disconnected_received); |
400 disconnected_received = true; | 400 disconnected_received = true; |
401 } else { | 401 } else { |
402 ADD_FAILURE() << "Unexpected host state: " << state; | 402 ADD_FAILURE() << "Unexpected host state: " << state; |
403 } | 403 } |
404 } else { | 404 } else { |
(...skipping 26 matching lines...) Expand all Loading... |
431 | 431 |
432 base::File input_read_file; | 432 base::File input_read_file; |
433 base::File output_write_file; | 433 base::File output_write_file; |
434 | 434 |
435 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); | 435 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); |
436 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); | 436 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); |
437 | 437 |
438 pipe_.reset(new NativeMessagingPipe()); | 438 pipe_.reset(new NativeMessagingPipe()); |
439 | 439 |
440 scoped_ptr<extensions::NativeMessagingChannel> channel( | 440 scoped_ptr<extensions::NativeMessagingChannel> channel( |
441 new PipeMessagingChannel(input_read_file.Pass(), | 441 new PipeMessagingChannel(std::move(input_read_file), |
442 output_write_file.Pass())); | 442 std::move(output_write_file))); |
443 | 443 |
444 // Creating a native messaging host with a mock It2MeHostFactory. | 444 // Creating a native messaging host with a mock It2MeHostFactory. |
445 scoped_ptr<extensions::NativeMessageHost> it2me_host( | 445 scoped_ptr<extensions::NativeMessageHost> it2me_host( |
446 new It2MeNativeMessagingHost( | 446 new It2MeNativeMessagingHost( |
447 ChromotingHostContext::Create(host_task_runner_), | 447 ChromotingHostContext::Create(host_task_runner_), |
448 make_scoped_ptr(new MockIt2MeHostFactory()))); | 448 make_scoped_ptr(new MockIt2MeHostFactory()))); |
449 it2me_host->Start(pipe_.get()); | 449 it2me_host->Start(pipe_.get()); |
450 | 450 |
451 pipe_->Start(it2me_host.Pass(), channel.Pass()); | 451 pipe_->Start(std::move(it2me_host), std::move(channel)); |
452 | 452 |
453 // Notify the test that the host has finished starting up. | 453 // Notify the test that the host has finished starting up. |
454 test_message_loop_->task_runner()->PostTask( | 454 test_message_loop_->task_runner()->PostTask( |
455 FROM_HERE, test_run_loop_->QuitClosure()); | 455 FROM_HERE, test_run_loop_->QuitClosure()); |
456 } | 456 } |
457 | 457 |
458 void It2MeNativeMessagingHostTest::ExitTest() { | 458 void It2MeNativeMessagingHostTest::ExitTest() { |
459 if (!test_message_loop_->task_runner()->RunsTasksOnCurrentThread()) { | 459 if (!test_message_loop_->task_runner()->RunsTasksOnCurrentThread()) { |
460 test_message_loop_->task_runner()->PostTask( | 460 test_message_loop_->task_runner()->PostTask( |
461 FROM_HERE, | 461 FROM_HERE, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 } | 542 } |
543 | 543 |
544 // Verify rejection if type is unrecognized. | 544 // Verify rejection if type is unrecognized. |
545 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { | 545 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { |
546 base::DictionaryValue message; | 546 base::DictionaryValue message; |
547 message.SetString("type", "xxx"); | 547 message.SetString("type", "xxx"); |
548 TestBadRequest(message, true); | 548 TestBadRequest(message, true); |
549 } | 549 } |
550 | 550 |
551 } // namespace remoting | 551 } // namespace remoting |
OLD | NEW |