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