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