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/files/scoped_temp_dir.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
11 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
12 #include "base/path_service.h" | 13 #include "base/path_service.h" |
13 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
| 15 #include "base/process_util.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 const char kTestMessage[] = "{\"text\": \"Hello.\"}"; |
| 32 |
29 FilePath GetTestDir() { | 33 FilePath GetTestDir() { |
30 FilePath test_dir; | 34 FilePath test_dir; |
31 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); | 35 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); |
32 test_dir = test_dir.AppendASCII("native_messaging"); | 36 test_dir = test_dir.AppendASCII("native_messaging"); |
33 return test_dir; | 37 return test_dir; |
34 } | 38 } |
35 | 39 |
36 } // namespace | 40 } // namespace |
37 | 41 |
38 namespace extensions { | 42 namespace extensions { |
39 | 43 |
40 class FakeLauncher : public NativeProcessLauncher { | 44 class FakeLauncher : public NativeProcessLauncher { |
41 public: | 45 public: |
42 FakeLauncher(FilePath read_file, FilePath write_file) { | 46 FakeLauncher(FilePath read_file, FilePath write_file) { |
43 read_file_ = base::CreatePlatformFile( | 47 read_file_ = base::CreatePlatformFile( |
44 read_file, | 48 read_file, |
45 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 49 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
46 NULL, NULL); | 50 NULL, NULL); |
47 write_file_ = base::CreatePlatformFile( | 51 write_file_ = base::CreatePlatformFile( |
48 write_file, | 52 write_file, |
49 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | 53 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
50 NULL, NULL); | 54 NULL, NULL); |
51 } | 55 } |
52 | 56 |
53 virtual bool LaunchNativeProcess( | 57 virtual bool LaunchNativeProcess( |
54 const FilePath& path, | 58 const FilePath& path, |
55 base::ProcessHandle* native_process_handle, | 59 base::ProcessHandle* native_process_handle, |
56 NativeMessageProcessHost::FileHandle* read_file, | 60 NativeMessageProcessHost::FileHandle* read_file, |
57 NativeMessageProcessHost::FileHandle* write_file) const OVERRIDE { | 61 NativeMessageProcessHost::FileHandle* write_file) const OVERRIDE { |
58 *native_process_handle = base::kNullProcessHandle; | 62 *native_process_handle = base::GetCurrentProcessHandle(); |
59 *read_file = read_file_; | 63 *read_file = read_file_; |
60 *write_file = write_file_; | 64 *write_file = write_file_; |
61 return true; | 65 return true; |
62 } | 66 } |
63 | 67 |
64 private: | 68 private: |
65 base::PlatformFile read_file_; | 69 base::PlatformFile read_file_; |
66 base::PlatformFile write_file_; | 70 base::PlatformFile write_file_; |
67 }; | 71 }; |
68 | 72 |
69 class NativeMessagingTest : public ::testing::Test, | 73 class NativeMessagingTest : public ::testing::Test, |
70 public NativeMessageProcessHost::Client, | 74 public NativeMessageProcessHost::Client, |
71 public base::SupportsWeakPtr<NativeMessagingTest> { | 75 public base::SupportsWeakPtr<NativeMessagingTest> { |
72 public: | 76 public: |
73 NativeMessagingTest() : current_channel_(chrome::VersionInfo::CHANNEL_DEV) { | 77 NativeMessagingTest() |
| 78 : current_channel_(chrome::VersionInfo::CHANNEL_DEV), |
| 79 native_message_process_host_(NULL) { |
74 } | 80 } |
75 | 81 |
76 virtual void SetUp() { | 82 virtual void SetUp() OVERRIDE { |
77 CommandLine::ForCurrentProcess()->AppendSwitch( | 83 CommandLine::ForCurrentProcess()->AppendSwitch( |
78 switches::kEnableNativeMessaging); | 84 switches::kEnableNativeMessaging); |
| 85 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
79 // Change the user data dir so native apps will be looked for in the test | 86 // Change the user data dir so native apps will be looked for in the test |
80 // directory. | 87 // directory. |
81 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); | 88 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); |
82 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir())); | 89 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir())); |
83 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | 90 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, |
84 &message_loop_)); | 91 &message_loop_)); |
85 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE, | 92 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE, |
86 &message_loop_)); | 93 &message_loop_)); |
87 } | 94 } |
88 | 95 |
89 virtual void TearDown() { | 96 virtual void TearDown() OVERRIDE { |
90 // Change the user data dir back for other tests. | 97 // Change the user data dir back for other tests. |
91 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); | 98 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); |
92 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, | 99 if (native_message_process_host_.get()) { |
93 native_message_process_host_); | 100 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, |
| 101 native_message_process_host_.release()); |
| 102 } |
94 message_loop_.RunUntilIdle(); | 103 message_loop_.RunUntilIdle(); |
95 } | 104 } |
96 | 105 |
97 void PostMessageFromNativeProcess(int port_id, const std::string& message) { | 106 virtual void PostMessageFromNativeProcess( |
| 107 int port_id, |
| 108 const std::string& message) OVERRIDE { |
98 last_posted_message_ = message; | 109 last_posted_message_ = message; |
99 } | 110 } |
100 | 111 |
101 void CloseChannel(int port_id, bool error) { | 112 virtual void CloseChannel(int port_id, bool error) OVERRIDE { |
102 } | |
103 | |
104 void AcquireProcess(NativeMessageProcessHost::ScopedHost process) { | |
105 native_message_process_host_ = process.release(); | |
106 } | 113 } |
107 | 114 |
108 protected: | 115 protected: |
| 116 std::string FormatMessage(const std::string& message) { |
| 117 Pickle pickle; |
| 118 pickle.WriteString(message); |
| 119 return std::string(const_cast<const Pickle*>(&pickle)->payload(), |
| 120 pickle.payload_size()); |
| 121 } |
| 122 |
| 123 FilePath CreateTempFileWithMessage(const std::string& message) { |
| 124 FilePath filename = temp_dir_.path().Append("input"); |
| 125 file_util::CreateTemporaryFile(&filename); |
| 126 std::string message_with_header = FormatMessage(message); |
| 127 EXPECT_TRUE(file_util::WriteFile( |
| 128 filename, message_with_header.data(), message_with_header.size())); |
| 129 return filename; |
| 130 } |
| 131 |
109 // Force the channel to be dev. | 132 // Force the channel to be dev. |
| 133 base::ScopedTempDir temp_dir_; |
110 Feature::ScopedCurrentChannel current_channel_; | 134 Feature::ScopedCurrentChannel current_channel_; |
111 NativeMessageProcessHost* native_message_process_host_; | 135 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; |
112 FilePath user_data_dir_; | 136 FilePath user_data_dir_; |
113 MessageLoopForIO message_loop_; | 137 MessageLoopForIO message_loop_; |
114 scoped_ptr<content::TestBrowserThread> ui_thread_; | 138 scoped_ptr<content::TestBrowserThread> ui_thread_; |
115 scoped_ptr<content::TestBrowserThread> file_thread_; | 139 scoped_ptr<content::TestBrowserThread> file_thread_; |
116 std::string last_posted_message_; | 140 std::string last_posted_message_; |
117 }; | 141 }; |
118 | 142 |
119 // Read a single message from a local file (single_message_response.msg). | 143 // Read a single message from a local file. |
120 TEST_F(NativeMessagingTest, SingleSendMessageRead) { | 144 TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
121 FilePath temp_file; | 145 FilePath temp_output_file = temp_dir_.path().Append("output"); |
122 file_util::CreateTemporaryFile(&temp_file); | 146 FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); |
123 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), | 147 |
124 temp_file); | 148 scoped_ptr<NativeProcessLauncher> launcher( |
125 NativeMessageProcessHost::CreateWithLauncher( | 149 new FakeLauncher(temp_input_file, temp_output_file)); |
126 AsWeakPtr(), "empty_app.py", "{}", 0, | 150 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( |
127 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( | 151 AsWeakPtr(), "empty_app.py", 0, launcher.Pass()); |
128 &NativeMessagingTest::AcquireProcess, AsWeakPtr()), | 152 ASSERT_TRUE(native_message_process_host_.get()); |
129 launcher); | |
130 message_loop_.RunUntilIdle(); | 153 message_loop_.RunUntilIdle(); |
131 ASSERT_TRUE(native_message_process_host_); | 154 |
132 native_message_process_host_->ReadNowForTesting(); | 155 native_message_process_host_->ReadNowForTesting(); |
133 message_loop_.RunUntilIdle(); | 156 message_loop_.RunUntilIdle(); |
134 EXPECT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}"); | 157 EXPECT_EQ(kTestMessage, last_posted_message_); |
135 file_util::Delete(temp_file, false /* non-recursive */); | |
136 } | 158 } |
137 | 159 |
138 // Tests sending a single message. The message should get written to | 160 // Tests sending a single message. The message should get written to |
139 // |temp_file| and should match the contents of single_message_request.msg. | 161 // |temp_file| and should match the contents of single_message_request.msg. |
140 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { | 162 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
141 FilePath temp_file; | 163 FilePath temp_output_file = temp_dir_.path().Append("output"); |
142 file_util::CreateTemporaryFile(&temp_file); | 164 FilePath temp_input_file = CreateTempFileWithMessage(std::string()); |
143 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), | 165 |
144 temp_file); | 166 scoped_ptr<NativeProcessLauncher> launcher( |
145 NativeMessageProcessHost::CreateWithLauncher( | 167 new FakeLauncher(temp_input_file, temp_output_file)); |
146 AsWeakPtr(), "empty_app.py", "{\"text\": \"Hello.\"}", 0, | 168 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( |
147 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( | 169 AsWeakPtr(), "empty_app.py", 0, launcher.Pass()); |
148 &NativeMessagingTest::AcquireProcess, AsWeakPtr()), | 170 ASSERT_TRUE(native_message_process_host_.get()); |
149 launcher); | |
150 message_loop_.RunUntilIdle(); | 171 message_loop_.RunUntilIdle(); |
151 ASSERT_TRUE(native_message_process_host_); | |
152 | 172 |
153 EXPECT_TRUE(file_util::ContentsEqual( | 173 native_message_process_host_->Send(kTestMessage); |
154 temp_file, GetTestDir().AppendASCII("single_message_request.msg"))); | 174 message_loop_.RunUntilIdle(); |
155 | 175 |
156 file_util::Delete(temp_file, false /* non-recursive */); | 176 std::string output; |
| 177 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); |
| 178 EXPECT_EQ(FormatMessage(kTestMessage), output); |
157 } | 179 } |
158 | 180 |
159 // Disabled, see http://crbug.com/159754. | 181 // Disabled, see http://crbug.com/159754. |
160 // Test send message with a real client. The client just echo's back the text | 182 // Test send message with a real client. The client just echo's back the text |
161 // it recieved. | 183 // it recieved. |
162 TEST_F(NativeMessagingTest, DISABLED_EchoConnect) { | 184 TEST_F(NativeMessagingTest, DISABLED_EchoConnect) { |
163 NativeMessageProcessHost::Create( | 185 native_message_process_host_ = NativeMessageProcessHost::Create( |
164 AsWeakPtr(), "echo.py", "{\"text\": \"Hello.\"}", 0, | 186 AsWeakPtr(), "empty_app.py", 0); |
165 NativeMessageProcessHost::TYPE_CONNECT, base::Bind( | 187 ASSERT_TRUE(native_message_process_host_.get()); |
166 &NativeMessagingTest::AcquireProcess, AsWeakPtr())); | |
167 message_loop_.RunUntilIdle(); | 188 message_loop_.RunUntilIdle(); |
168 ASSERT_TRUE(native_message_process_host_); | 189 |
| 190 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); |
| 191 message_loop_.RunUntilIdle(); |
169 | 192 |
170 native_message_process_host_->ReadNowForTesting(); | 193 native_message_process_host_->ReadNowForTesting(); |
171 message_loop_.RunUntilIdle(); | 194 message_loop_.RunUntilIdle(); |
172 EXPECT_EQ(last_posted_message_, | 195 EXPECT_EQ("{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}", |
173 "{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}"); | 196 last_posted_message_); |
174 | 197 |
175 native_message_process_host_->Send("{\"foo\": \"bar\"}"); | 198 native_message_process_host_->Send("{\"foo\": \"bar\"}"); |
176 message_loop_.RunUntilIdle(); | 199 message_loop_.RunUntilIdle(); |
177 native_message_process_host_->ReadNowForTesting(); | 200 native_message_process_host_->ReadNowForTesting(); |
178 message_loop_.RunUntilIdle(); | 201 message_loop_.RunUntilIdle(); |
179 EXPECT_EQ(last_posted_message_, "{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}"); | 202 EXPECT_EQ("{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}", last_posted_message_); |
180 } | 203 } |
181 | 204 |
182 } // namespace extensions | 205 } // namespace extensions |
OLD | NEW |