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