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" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/stringize_macros.h" | 14 #include "base/strings/stringize_macros.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "net/base/file_stream.h" | 16 #include "net/base/file_stream.h" |
17 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
18 #include "remoting/base/auto_thread_task_runner.h" | 18 #include "remoting/base/auto_thread_task_runner.h" |
19 #include "remoting/host/chromoting_host_context.h" | 19 #include "remoting/host/chromoting_host_context.h" |
| 20 #include "remoting/host/native_messaging/native_messaging_pipe.h" |
20 #include "remoting/host/native_messaging/pipe_messaging_channel.h" | 21 #include "remoting/host/native_messaging/pipe_messaging_channel.h" |
21 #include "remoting/host/setup/test_util.h" | 22 #include "remoting/host/setup/test_util.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 namespace remoting { | 25 namespace remoting { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 const char kTestAccessCode[] = "888888"; | 29 const char kTestAccessCode[] = "888888"; |
29 const int kTestAccessCodeLifetimeInSeconds = 666; | 30 const int kTestAccessCodeLifetimeInSeconds = 666; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 base::WeakPtr<It2MeHost::Observer> observer, | 72 base::WeakPtr<It2MeHost::Observer> observer, |
72 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, | 73 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, |
73 const std::string& directory_bot_jid) | 74 const std::string& directory_bot_jid) |
74 : It2MeHost(context, | 75 : It2MeHost(context, |
75 task_runner, | 76 task_runner, |
76 observer, | 77 observer, |
77 xmpp_server_config, | 78 xmpp_server_config, |
78 directory_bot_jid) {} | 79 directory_bot_jid) {} |
79 | 80 |
80 // It2MeHost overrides | 81 // It2MeHost overrides |
81 virtual void Connect() OVERRIDE; | 82 virtual void Connect() override; |
82 virtual void Disconnect() OVERRIDE; | 83 virtual void Disconnect() override; |
83 virtual void RequestNatPolicy() OVERRIDE; | 84 virtual void RequestNatPolicy() override; |
84 | 85 |
85 private: | 86 private: |
86 virtual ~MockIt2MeHost() {} | 87 virtual ~MockIt2MeHost() {} |
87 | 88 |
88 void RunSetState(It2MeHostState state); | 89 void RunSetState(It2MeHostState state); |
89 | 90 |
90 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHost); | 91 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHost); |
91 }; | 92 }; |
92 | 93 |
93 void MockIt2MeHost::Connect() { | 94 void MockIt2MeHost::Connect() { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 147 } |
147 | 148 |
148 class MockIt2MeHostFactory : public It2MeHostFactory { | 149 class MockIt2MeHostFactory : public It2MeHostFactory { |
149 public: | 150 public: |
150 MockIt2MeHostFactory() {} | 151 MockIt2MeHostFactory() {} |
151 virtual scoped_refptr<It2MeHost> CreateIt2MeHost( | 152 virtual scoped_refptr<It2MeHost> CreateIt2MeHost( |
152 ChromotingHostContext* context, | 153 ChromotingHostContext* context, |
153 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 154 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
154 base::WeakPtr<It2MeHost::Observer> observer, | 155 base::WeakPtr<It2MeHost::Observer> observer, |
155 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, | 156 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, |
156 const std::string& directory_bot_jid) OVERRIDE { | 157 const std::string& directory_bot_jid) override { |
157 return new MockIt2MeHost( | 158 return new MockIt2MeHost( |
158 context, task_runner, observer, xmpp_server_config, directory_bot_jid); | 159 context, task_runner, observer, xmpp_server_config, directory_bot_jid); |
159 } | 160 } |
160 | 161 |
161 private: | 162 private: |
162 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHostFactory); | 163 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHostFactory); |
163 }; // MockIt2MeHostFactory | 164 }; // MockIt2MeHostFactory |
164 | 165 |
165 class It2MeNativeMessagingHostTest : public testing::Test { | 166 class It2MeNativeMessagingHostTest : public testing::Test { |
166 public: | 167 public: |
167 It2MeNativeMessagingHostTest() {} | 168 It2MeNativeMessagingHostTest() {} |
168 virtual ~It2MeNativeMessagingHostTest() {} | 169 virtual ~It2MeNativeMessagingHostTest() {} |
169 | 170 |
170 virtual void SetUp() OVERRIDE; | 171 virtual void SetUp() override; |
171 virtual void TearDown() OVERRIDE; | 172 virtual void TearDown() override; |
172 | 173 |
173 protected: | 174 protected: |
174 scoped_ptr<base::DictionaryValue> ReadMessageFromOutputPipe(); | 175 scoped_ptr<base::DictionaryValue> ReadMessageFromOutputPipe(); |
175 void WriteMessageToInputPipe(const base::Value& message); | 176 void WriteMessageToInputPipe(const base::Value& message); |
176 | 177 |
177 void VerifyHelloResponse(int request_id); | 178 void VerifyHelloResponse(int request_id); |
178 void VerifyErrorResponse(); | 179 void VerifyErrorResponse(); |
179 void VerifyConnectResponses(int request_id); | 180 void VerifyConnectResponses(int request_id); |
180 void VerifyDisconnectResponses(int request_id); | 181 void VerifyDisconnectResponses(int request_id); |
181 | 182 |
(...skipping 20 matching lines...) Expand all Loading... |
202 | 203 |
203 // Message loop of the test thread. | 204 // Message loop of the test thread. |
204 scoped_ptr<base::MessageLoop> test_message_loop_; | 205 scoped_ptr<base::MessageLoop> test_message_loop_; |
205 scoped_ptr<base::RunLoop> test_run_loop_; | 206 scoped_ptr<base::RunLoop> test_run_loop_; |
206 | 207 |
207 scoped_ptr<base::Thread> host_thread_; | 208 scoped_ptr<base::Thread> host_thread_; |
208 scoped_ptr<base::RunLoop> host_run_loop_; | 209 scoped_ptr<base::RunLoop> host_run_loop_; |
209 | 210 |
210 // Task runner of the host thread. | 211 // Task runner of the host thread. |
211 scoped_refptr<AutoThreadTaskRunner> host_task_runner_; | 212 scoped_refptr<AutoThreadTaskRunner> host_task_runner_; |
212 scoped_ptr<remoting::It2MeNativeMessagingHost> host_; | 213 scoped_ptr<remoting::NativeMessagingPipe> pipe_; |
213 | 214 |
214 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest); | 215 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest); |
215 }; | 216 }; |
216 | 217 |
217 void It2MeNativeMessagingHostTest::SetUp() { | 218 void It2MeNativeMessagingHostTest::SetUp() { |
218 test_message_loop_.reset(new base::MessageLoop()); | 219 test_message_loop_.reset(new base::MessageLoop()); |
219 test_run_loop_.reset(new base::RunLoop()); | 220 test_run_loop_.reset(new base::RunLoop()); |
220 | 221 |
221 // Run the host on a dedicated thread. | 222 // Run the host on a dedicated thread. |
222 host_thread_.reset(new base::Thread("host_thread")); | 223 host_thread_.reset(new base::Thread("host_thread")); |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 | 425 |
425 base::File input_read_file; | 426 base::File input_read_file; |
426 base::File output_write_file; | 427 base::File output_write_file; |
427 | 428 |
428 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); | 429 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); |
429 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); | 430 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); |
430 | 431 |
431 // Creating a native messaging host with a mock It2MeHostFactory. | 432 // Creating a native messaging host with a mock It2MeHostFactory. |
432 scoped_ptr<It2MeHostFactory> factory(new MockIt2MeHostFactory()); | 433 scoped_ptr<It2MeHostFactory> factory(new MockIt2MeHostFactory()); |
433 | 434 |
| 435 pipe_.reset(new NativeMessagingPipe()); |
| 436 |
434 scoped_ptr<extensions::NativeMessagingChannel> channel( | 437 scoped_ptr<extensions::NativeMessagingChannel> channel( |
435 new PipeMessagingChannel(input_read_file.Pass(), | 438 new PipeMessagingChannel(input_read_file.Pass(), |
436 output_write_file.Pass())); | 439 output_write_file.Pass())); |
437 | 440 |
438 host_.reset(new It2MeNativeMessagingHost( | 441 scoped_ptr<extensions::NativeMessageHost> it2me_host( |
439 host_task_runner_, | 442 new It2MeNativeMessagingHost( |
440 channel.Pass(), | 443 host_task_runner_, |
441 factory.Pass())); | 444 factory.Pass())); |
442 host_->Start(base::Bind(&It2MeNativeMessagingHostTest::StopHost, | 445 it2me_host->Start(pipe_.get()); |
| 446 |
| 447 pipe_->Start(it2me_host.Pass(), |
| 448 channel.Pass(), |
| 449 base::Bind(&It2MeNativeMessagingHostTest::StopHost, |
443 base::Unretained(this))); | 450 base::Unretained(this))); |
444 | 451 |
445 // Notify the test that the host has finished starting up. | 452 // Notify the test that the host has finished starting up. |
446 test_message_loop_->message_loop_proxy()->PostTask( | 453 test_message_loop_->message_loop_proxy()->PostTask( |
447 FROM_HERE, test_run_loop_->QuitClosure()); | 454 FROM_HERE, test_run_loop_->QuitClosure()); |
448 } | 455 } |
449 | 456 |
450 void It2MeNativeMessagingHostTest::StopHost() { | 457 void It2MeNativeMessagingHostTest::StopHost() { |
451 DCHECK(host_task_runner_->RunsTasksOnCurrentThread()); | 458 DCHECK(host_task_runner_->RunsTasksOnCurrentThread()); |
452 | 459 |
453 host_.reset(); | 460 pipe_.reset(); |
454 | 461 |
455 // Wait till all shutdown tasks have completed. | 462 // Wait till all shutdown tasks have completed. |
456 base::RunLoop().RunUntilIdle(); | 463 base::RunLoop().RunUntilIdle(); |
457 | 464 |
458 // Trigger a test shutdown via ExitTest(). | 465 // Trigger a test shutdown via ExitTest(). |
459 host_task_runner_ = NULL; | 466 host_task_runner_ = NULL; |
460 } | 467 } |
461 | 468 |
462 void It2MeNativeMessagingHostTest::ExitTest() { | 469 void It2MeNativeMessagingHostTest::ExitTest() { |
463 if (!test_message_loop_->message_loop_proxy()->RunsTasksOnCurrentThread()) { | 470 if (!test_message_loop_->message_loop_proxy()->RunsTasksOnCurrentThread()) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 | 554 |
548 // Verify rejection if type is unrecognized. | 555 // Verify rejection if type is unrecognized. |
549 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { | 556 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { |
550 base::DictionaryValue message; | 557 base::DictionaryValue message; |
551 message.SetString("type", "xxx"); | 558 message.SetString("type", "xxx"); |
552 TestBadRequest(message, true); | 559 TestBadRequest(message, true); |
553 } | 560 } |
554 | 561 |
555 } // namespace remoting | 562 } // namespace remoting |
556 | 563 |
OLD | NEW |