| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "base/threading/platform_thread.h" |
| 14 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 17 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| 16 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 18 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| 17 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/chrome_version_info.h" | 21 #include "chrome/common/chrome_version_info.h" |
| 20 #include "chrome/common/extensions/features/feature.h" | 22 #include "chrome/common/extensions/features/feature.h" |
| 21 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 26 |
| 25 using content::BrowserThread; | 27 using content::BrowserThread; |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 31 #if defined(OS_WIN) |
| 32 const char kEmptyAppName[] = "empty_app.bat"; |
| 33 const char kEchoAppName[] = "echo.bat"; |
| 34 #else |
| 35 const char kEmptyAppName[] = "empty_app.py"; |
| 36 const char kEchoAppName[] = "echo.py"; |
| 37 #endif // defined(OS_WIN) |
| 38 |
| 29 FilePath GetTestDir() { | 39 FilePath GetTestDir() { |
| 30 FilePath test_dir; | 40 FilePath test_dir; |
| 31 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); | 41 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); |
| 32 test_dir = test_dir.AppendASCII("native_messaging"); | 42 test_dir = test_dir.AppendASCII("native_messaging"); |
| 33 return test_dir; | 43 return test_dir; |
| 34 } | 44 } |
| 35 | 45 |
| 36 } // namespace | 46 } // namespace |
| 37 | 47 |
| 38 namespace extensions { | 48 namespace extensions { |
| 39 | 49 |
| 40 class FakeLauncher : public NativeProcessLauncher { | 50 class FakeLauncher : public NativeProcessLauncher { |
| 41 public: | 51 public: |
| 42 FakeLauncher(FilePath read_file, FilePath write_file) { | 52 FakeLauncher(FilePath read_file, FilePath write_file) { |
| 53 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_ASYNC; |
| 43 read_file_ = base::CreatePlatformFile( | 54 read_file_ = base::CreatePlatformFile( |
| 44 read_file, | 55 read_file, |
| 45 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 56 flags | base::PLATFORM_FILE_READ, |
| 46 NULL, NULL); | 57 NULL, NULL); |
| 47 write_file_ = base::CreatePlatformFile( | 58 write_file_ = base::CreatePlatformFile( |
| 48 write_file, | 59 write_file, |
| 49 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | 60 flags | base::PLATFORM_FILE_WRITE, |
| 50 NULL, NULL); | 61 NULL, NULL); |
| 51 } | 62 } |
| 52 | 63 |
| 53 virtual bool LaunchNativeProcess( | 64 virtual bool LaunchNativeProcess( |
| 54 const FilePath& path, | 65 const FilePath& path, |
| 55 base::ProcessHandle* native_process_handle, | 66 base::ProcessHandle* native_process_handle, |
| 56 NativeMessageProcessHost::FileHandle* read_file, | 67 NativeMessageProcessHost::FileHandle* read_file, |
| 57 NativeMessageProcessHost::FileHandle* write_file) const OVERRIDE { | 68 NativeMessageProcessHost::FileHandle* write_file) const OVERRIDE { |
| 58 *native_process_handle = base::kNullProcessHandle; | 69 *native_process_handle = base::kNullProcessHandle; |
| 59 *read_file = read_file_; | 70 *read_file = read_file_; |
| 60 *write_file = write_file_; | 71 *write_file = write_file_; |
| 61 return true; | 72 return true; |
| 62 } | 73 } |
| 63 | 74 |
| 64 private: | 75 private: |
| 65 base::PlatformFile read_file_; | 76 base::PlatformFile read_file_; |
| 66 base::PlatformFile write_file_; | 77 base::PlatformFile write_file_; |
| 67 }; | 78 }; |
| 68 | 79 |
| 69 class NativeMessagingTest : public ::testing::Test, | 80 class NativeMessagingTest : public ::testing::Test, |
| 70 public NativeMessageProcessHost::Client, | 81 public NativeMessageProcessHost::Client, |
| 71 public base::SupportsWeakPtr<NativeMessagingTest> { | 82 public base::SupportsWeakPtr<NativeMessagingTest> { |
| 72 public: | 83 public: |
| 73 NativeMessagingTest() : current_channel_(chrome::VersionInfo::CHANNEL_DEV) { | 84 NativeMessagingTest() : current_channel_(chrome::VersionInfo::CHANNEL_DEV), |
| 85 run_loop_(NULL) { |
| 74 } | 86 } |
| 75 | 87 |
| 76 virtual void SetUp() { | 88 virtual void SetUp() { |
| 77 CommandLine::ForCurrentProcess()->AppendSwitch( | 89 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 78 switches::kEnableNativeMessaging); | 90 switches::kEnableNativeMessaging); |
| 79 // Change the user data dir so native apps will be looked for in the test | 91 // Change the user data dir so native apps will be looked for in the test |
| 80 // directory. | 92 // directory. |
| 81 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); | 93 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); |
| 82 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir())); | 94 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir())); |
| 83 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | 95 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, |
| 84 &message_loop_)); | 96 &message_loop_)); |
| 85 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE, | 97 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE, |
| 86 &message_loop_)); | 98 &message_loop_)); |
| 99 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO, |
| 100 &message_loop_)); |
| 87 } | 101 } |
| 88 | 102 |
| 89 virtual void TearDown() { | 103 virtual void TearDown() { |
| 90 // Change the user data dir back for other tests. | 104 // Change the user data dir back for other tests. |
| 91 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); | 105 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); |
| 92 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, | |
| 93 native_message_process_host_); | |
| 94 message_loop_.RunAllPending(); | |
| 95 } | 106 } |
| 96 | 107 |
| 97 void PostMessageFromNativeProcess(int port_id, const std::string& message) { | 108 void PostMessageFromNativeProcess(int port_id, const std::string& message) { |
| 98 last_posted_message_ = message; | 109 last_posted_message_ = message; |
| 110 if (run_loop_) |
| 111 run_loop_->Quit(); |
| 99 } | 112 } |
| 100 | 113 |
| 101 void CloseChannel(int port_id, bool error) { | 114 void CloseChannel(int port_id, bool error) { |
| 102 } | 115 } |
| 103 | 116 |
| 104 void AcquireProcess(NativeMessageProcessHost::ScopedHost process) { | 117 void AcquireProcess(NativeMessageProcessHost::ScopedHost process) { |
| 105 native_message_process_host_ = process.release(); | 118 native_message_process_host_.swap(process); |
| 106 } | 119 } |
| 107 | 120 |
| 108 protected: | 121 protected: |
| 109 // Force the channel to be dev. | 122 // Force the channel to be dev. |
| 110 Feature::ScopedCurrentChannel current_channel_; | 123 Feature::ScopedCurrentChannel current_channel_; |
| 111 NativeMessageProcessHost* native_message_process_host_; | 124 NativeMessageProcessHost::ScopedHost native_message_process_host_; |
| 112 FilePath user_data_dir_; | 125 FilePath user_data_dir_; |
| 113 MessageLoopForIO message_loop_; | 126 MessageLoopForIO message_loop_; |
| 114 scoped_ptr<content::TestBrowserThread> ui_thread_; | 127 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 115 scoped_ptr<content::TestBrowserThread> file_thread_; | 128 scoped_ptr<content::TestBrowserThread> file_thread_; |
| 129 scoped_ptr<content::TestBrowserThread> io_thread_; |
| 116 std::string last_posted_message_; | 130 std::string last_posted_message_; |
| 131 base::RunLoop* run_loop_; |
| 117 }; | 132 }; |
| 118 | 133 |
| 119 // Read a single message from a local file (single_message_response.msg). | 134 // Read a single message from a local file (single_message_response.msg). |
| 120 TEST_F(NativeMessagingTest, SingleSendMessageRead) { | 135 TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
| 121 FilePath temp_file; | 136 FilePath temp_file; |
| 122 file_util::CreateTemporaryFile(&temp_file); | 137 file_util::CreateTemporaryFile(&temp_file); |
| 123 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), | |
| 124 temp_file); | |
| 125 NativeMessageProcessHost::CreateWithLauncher( | 138 NativeMessageProcessHost::CreateWithLauncher( |
| 126 AsWeakPtr(), "empty_app.py", "{}", 0, | 139 AsWeakPtr(), kEmptyAppName, "{}", 0, |
| 127 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( | 140 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( |
| 128 &NativeMessagingTest::AcquireProcess, AsWeakPtr()), | 141 &NativeMessagingTest::AcquireProcess, AsWeakPtr()), |
| 129 launcher); | 142 scoped_ptr<NativeProcessLauncher>(new FakeLauncher( |
| 143 GetTestDir().AppendASCII("single_message_response.msg"), temp_file))); |
| 130 message_loop_.RunAllPending(); | 144 message_loop_.RunAllPending(); |
| 131 ASSERT_TRUE(native_message_process_host_); | 145 ASSERT_TRUE(native_message_process_host_.get()); |
| 146 // The process host is directly connected to files, there is no need to wait |
| 147 // for the message. |
| 132 native_message_process_host_->ReadNowForTesting(); | 148 native_message_process_host_->ReadNowForTesting(); |
| 133 message_loop_.RunAllPending(); | 149 message_loop_.RunAllPending(); |
| 134 EXPECT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}"); | 150 EXPECT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}"); |
| 135 file_util::Delete(temp_file, false /* non-recursive */); | 151 file_util::Delete(temp_file, false /* non-recursive */); |
| 136 } | 152 } |
| 137 | 153 |
| 138 // Tests sending a single message. The message should get written to | 154 // Tests sending a single message. The message should get written to |
| 139 // |temp_file| and should match the contents of single_message_request.msg. | 155 // |temp_file| and should match the contents of single_message_request.msg. |
| 140 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { | 156 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
| 141 FilePath temp_file; | 157 FilePath temp_file; |
| 142 file_util::CreateTemporaryFile(&temp_file); | 158 file_util::CreateTemporaryFile(&temp_file); |
| 143 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), | |
| 144 temp_file); | |
| 145 NativeMessageProcessHost::CreateWithLauncher( | 159 NativeMessageProcessHost::CreateWithLauncher( |
| 146 AsWeakPtr(), "empty_app.py", "{\"text\": \"Hello.\"}", 0, | 160 AsWeakPtr(), kEmptyAppName, "{\"text\": \"Hello.\"}", 0, |
| 147 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( | 161 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( |
| 148 &NativeMessagingTest::AcquireProcess, AsWeakPtr()), | 162 &NativeMessagingTest::AcquireProcess, AsWeakPtr()), |
| 149 launcher); | 163 scoped_ptr<NativeProcessLauncher>(new FakeLauncher( |
| 164 GetTestDir().AppendASCII("single_message_response.msg"), temp_file))); |
| 150 message_loop_.RunAllPending(); | 165 message_loop_.RunAllPending(); |
| 151 ASSERT_TRUE(native_message_process_host_); | 166 ASSERT_TRUE(native_message_process_host_.get()); |
| 152 | |
| 153 EXPECT_TRUE(file_util::ContentsEqual( | 167 EXPECT_TRUE(file_util::ContentsEqual( |
| 154 temp_file, GetTestDir().AppendASCII("single_message_request.msg"))); | 168 temp_file, GetTestDir().AppendASCII("single_message_request.msg"))); |
| 155 | |
| 156 file_util::Delete(temp_file, false /* non-recursive */); | 169 file_util::Delete(temp_file, false /* non-recursive */); |
| 157 } | 170 } |
| 158 | 171 |
| 159 // Test send message with a real client. The client just echo's back the text | 172 // Test connecting with a real client. The client just echo's back the text |
| 160 // it recieved. | 173 // it recieves. |
| 161 TEST_F(NativeMessagingTest, EchoConnect) { | 174 TEST_F(NativeMessagingTest, EchoConnect) { |
| 162 NativeMessageProcessHost::Create( | 175 NativeMessageProcessHost::Create( |
| 163 AsWeakPtr(), "echo.py", "{\"text\": \"Hello.\"}", 0, | 176 AsWeakPtr(), kEchoAppName, "{\"text\": \"Hello.\"}", 0, |
| 164 NativeMessageProcessHost::TYPE_CONNECT, base::Bind( | 177 NativeMessageProcessHost::TYPE_CONNECT, base::Bind( |
| 165 &NativeMessagingTest::AcquireProcess, AsWeakPtr())); | 178 &NativeMessagingTest::AcquireProcess, AsWeakPtr())); |
| 166 message_loop_.RunAllPending(); | 179 message_loop_.RunAllPending(); |
| 167 ASSERT_TRUE(native_message_process_host_); | 180 ASSERT_TRUE(native_message_process_host_.get()); |
| 168 | 181 |
| 169 native_message_process_host_->ReadNowForTesting(); | 182 base::RunLoop run_loop_1; |
| 170 message_loop_.RunAllPending(); | 183 run_loop_ = &run_loop_1; |
| 184 run_loop_1.Run(); |
| 171 EXPECT_EQ(last_posted_message_, | 185 EXPECT_EQ(last_posted_message_, |
| 172 "{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}"); | 186 "{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}"); |
| 173 | 187 |
| 174 native_message_process_host_->Send("{\"foo\": \"bar\"}"); | 188 native_message_process_host_->Send("{\"foo\": \"bar\"}"); |
| 175 message_loop_.RunAllPending(); | 189 base::RunLoop run_loop_2; |
| 176 native_message_process_host_->ReadNowForTesting(); | 190 run_loop_ = &run_loop_2; |
| 177 message_loop_.RunAllPending(); | 191 run_loop_2.Run(); |
| 178 EXPECT_EQ(last_posted_message_, "{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}"); | 192 EXPECT_EQ(last_posted_message_, "{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}"); |
| 179 } | 193 } |
| 180 | 194 |
| 181 } // namespace extensions | 195 } // namespace extensions |
| OLD | NEW |