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/files/file.h" | 6 #include "base/files/file.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_file.h" | 9 #include "base/files/scoped_file.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
14 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
15 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/test/test_timeouts.h" | 18 #include "base/test/test_timeouts.h" |
18 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
19 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
21 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 22 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
22 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" | 23 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 base::kNullProcessHandle, | 86 base::kNullProcessHandle, |
86 read_file_.Pass(), write_file_.Pass()); | 87 read_file_.Pass(), write_file_.Pass()); |
87 } | 88 } |
88 | 89 |
89 private: | 90 private: |
90 mutable base::File read_file_; | 91 mutable base::File read_file_; |
91 mutable base::File write_file_; | 92 mutable base::File write_file_; |
92 }; | 93 }; |
93 | 94 |
94 class NativeMessagingTest : public ::testing::Test, | 95 class NativeMessagingTest : public ::testing::Test, |
95 public NativeMessageHost::Client, | 96 public NativeMessageProcessHost::Client, |
96 public base::SupportsWeakPtr<NativeMessagingTest> { | 97 public base::SupportsWeakPtr<NativeMessagingTest> { |
97 protected: | 98 protected: |
98 NativeMessagingTest() | 99 NativeMessagingTest() |
99 : current_channel_(chrome::VersionInfo::CHANNEL_DEV), | 100 : current_channel_(chrome::VersionInfo::CHANNEL_DEV), |
100 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 101 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
101 channel_closed_(false) {} | 102 channel_closed_(false) {} |
102 | 103 |
103 virtual void SetUp() override { | 104 virtual void SetUp() override { |
104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 105 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
105 } | 106 } |
106 | 107 |
107 virtual void TearDown() override { | 108 virtual void TearDown() override { |
108 if (native_message_host_.get()) { | 109 if (native_message_process_host_.get()) { |
109 BrowserThread::DeleteSoon( | 110 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
110 BrowserThread::IO, FROM_HERE, native_message_host_.release()); | 111 native_message_process_host_.release()); |
111 } | 112 } |
112 base::RunLoop().RunUntilIdle(); | 113 base::RunLoop().RunUntilIdle(); |
113 } | 114 } |
114 | 115 |
115 virtual void PostMessageFromNativeHost(const std::string& message) override { | 116 virtual void PostMessageFromNativeProcess( |
| 117 int port_id, |
| 118 const std::string& message) override { |
116 last_message_ = message; | 119 last_message_ = message; |
117 | 120 |
118 // Parse the message. | 121 // Parse the message. |
119 base::Value* parsed = base::JSONReader::Read(message); | 122 base::Value* parsed = base::JSONReader::Read(message); |
120 base::DictionaryValue* dict_value; | 123 base::DictionaryValue* dict_value; |
121 if (parsed && parsed->GetAsDictionary(&dict_value)) { | 124 if (parsed && parsed->GetAsDictionary(&dict_value)) { |
122 last_message_parsed_.reset(dict_value); | 125 last_message_parsed_.reset(dict_value); |
123 } else { | 126 } else { |
124 LOG(ERROR) << "Failed to parse " << message; | 127 LOG(ERROR) << "Failed to parse " << message; |
125 last_message_parsed_.reset(); | 128 last_message_parsed_.reset(); |
126 delete parsed; | 129 delete parsed; |
127 } | 130 } |
128 | 131 |
129 if (run_loop_) | 132 if (run_loop_) |
130 run_loop_->Quit(); | 133 run_loop_->Quit(); |
131 } | 134 } |
132 | 135 |
133 virtual void CloseChannel(const std::string& error_message) override { | 136 virtual void CloseChannel(int port_id, |
| 137 const std::string& error_message) override { |
134 channel_closed_ = true; | 138 channel_closed_ = true; |
135 if (run_loop_) | 139 if (run_loop_) |
136 run_loop_->Quit(); | 140 run_loop_->Quit(); |
137 } | 141 } |
138 | 142 |
139 protected: | 143 protected: |
140 std::string FormatMessage(const std::string& message) { | 144 std::string FormatMessage(const std::string& message) { |
141 uint32_t length = message.length(); | 145 uint32_t length = message.length(); |
142 return std::string(reinterpret_cast<char*>(&length), 4).append(message); | 146 return std::string(reinterpret_cast<char*>(&length), 4).append(message); |
143 } | 147 } |
144 | 148 |
145 base::FilePath CreateTempFileWithMessage(const std::string& message) { | 149 base::FilePath CreateTempFileWithMessage(const std::string& message) { |
146 base::FilePath filename; | 150 base::FilePath filename; |
147 if (!base::CreateTemporaryFileInDir(temp_dir_.path(), &filename)) | 151 if (!base::CreateTemporaryFileInDir(temp_dir_.path(), &filename)) |
148 return base::FilePath(); | 152 return base::FilePath(); |
149 | 153 |
150 std::string message_with_header = FormatMessage(message); | 154 std::string message_with_header = FormatMessage(message); |
151 int bytes_written = base::WriteFile( | 155 int bytes_written = base::WriteFile( |
152 filename, message_with_header.data(), message_with_header.size()); | 156 filename, message_with_header.data(), message_with_header.size()); |
153 if (bytes_written < 0 || | 157 if (bytes_written < 0 || |
154 (message_with_header.size() != static_cast<size_t>(bytes_written))) { | 158 (message_with_header.size() != static_cast<size_t>(bytes_written))) { |
155 return base::FilePath(); | 159 return base::FilePath(); |
156 } | 160 } |
157 return filename; | 161 return filename; |
158 } | 162 } |
159 | 163 |
160 base::ScopedTempDir temp_dir_; | 164 base::ScopedTempDir temp_dir_; |
161 // Force the channel to be dev. | 165 // Force the channel to be dev. |
162 ScopedCurrentChannel current_channel_; | 166 ScopedCurrentChannel current_channel_; |
163 scoped_ptr<NativeMessageHost> native_message_host_; | 167 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; |
164 scoped_ptr<base::RunLoop> run_loop_; | 168 scoped_ptr<base::RunLoop> run_loop_; |
165 content::TestBrowserThreadBundle thread_bundle_; | 169 content::TestBrowserThreadBundle thread_bundle_; |
166 std::string last_message_; | 170 std::string last_message_; |
167 scoped_ptr<base::DictionaryValue> last_message_parsed_; | 171 scoped_ptr<base::DictionaryValue> last_message_parsed_; |
168 bool channel_closed_; | 172 bool channel_closed_; |
169 }; | 173 }; |
170 | 174 |
171 // Read a single message from a local file. | 175 // Read a single message from a local file. |
172 TEST_F(NativeMessagingTest, SingleSendMessageRead) { | 176 TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
173 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 177 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
174 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); | 178 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); |
175 ASSERT_FALSE(temp_input_file.empty()); | 179 ASSERT_FALSE(temp_input_file.empty()); |
176 | 180 |
177 scoped_ptr<NativeProcessLauncher> launcher = | 181 scoped_ptr<NativeProcessLauncher> launcher = |
178 FakeLauncher::Create(temp_input_file, temp_output_file).Pass(); | 182 FakeLauncher::Create(temp_input_file, temp_output_file).Pass(); |
179 native_message_host_ = NativeMessageProcessHost::CreateWithLauncher( | 183 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( |
180 ScopedTestNativeMessagingHost::kExtensionId, | 184 AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, "empty_app.py", |
181 "empty_app.py", | 185 0, launcher.Pass()); |
182 launcher.Pass()); | 186 ASSERT_TRUE(native_message_process_host_.get()); |
183 native_message_host_->Start(this); | |
184 ASSERT_TRUE(native_message_host_.get()); | |
185 run_loop_.reset(new base::RunLoop()); | 187 run_loop_.reset(new base::RunLoop()); |
186 run_loop_->RunUntilIdle(); | 188 run_loop_->RunUntilIdle(); |
187 | 189 |
188 if (last_message_.empty()) { | 190 if (last_message_.empty()) { |
189 run_loop_.reset(new base::RunLoop()); | 191 run_loop_.reset(new base::RunLoop()); |
190 scoped_ptr<NativeMessageProcessHost> native_message_process_host_( | |
191 static_cast<NativeMessageProcessHost*>(native_message_host_.release())); | |
192 native_message_process_host_->ReadNowForTesting(); | 192 native_message_process_host_->ReadNowForTesting(); |
193 run_loop_->Run(); | 193 run_loop_->Run(); |
194 } | 194 } |
195 EXPECT_EQ(kTestMessage, last_message_); | 195 EXPECT_EQ(kTestMessage, last_message_); |
196 } | 196 } |
197 | 197 |
198 // Tests sending a single message. The message should get written to | 198 // Tests sending a single message. The message should get written to |
199 // |temp_file| and should match the contents of single_message_request.msg. | 199 // |temp_file| and should match the contents of single_message_request.msg. |
200 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { | 200 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
201 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 201 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
(...skipping 17 matching lines...) Expand all Loading... |
219 #else // defined(OS_WIN) | 219 #else // defined(OS_WIN) |
220 base::PlatformFile pipe_handles[2]; | 220 base::PlatformFile pipe_handles[2]; |
221 ASSERT_EQ(0, pipe(pipe_handles)); | 221 ASSERT_EQ(0, pipe(pipe_handles)); |
222 read_file = base::File(pipe_handles[0]); | 222 read_file = base::File(pipe_handles[0]); |
223 base::File write_file(pipe_handles[1]); | 223 base::File write_file(pipe_handles[1]); |
224 #endif // !defined(OS_WIN) | 224 #endif // !defined(OS_WIN) |
225 | 225 |
226 scoped_ptr<NativeProcessLauncher> launcher = | 226 scoped_ptr<NativeProcessLauncher> launcher = |
227 FakeLauncher::CreateWithPipeInput(read_file.Pass(), | 227 FakeLauncher::CreateWithPipeInput(read_file.Pass(), |
228 temp_output_file).Pass(); | 228 temp_output_file).Pass(); |
229 native_message_host_ = NativeMessageProcessHost::CreateWithLauncher( | 229 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( |
230 ScopedTestNativeMessagingHost::kExtensionId, | 230 AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, "empty_app.py", |
231 "empty_app.py", | 231 0, launcher.Pass()); |
232 launcher.Pass()); | 232 ASSERT_TRUE(native_message_process_host_.get()); |
233 native_message_host_->Start(this); | |
234 ASSERT_TRUE(native_message_host_.get()); | |
235 base::RunLoop().RunUntilIdle(); | 233 base::RunLoop().RunUntilIdle(); |
236 | 234 |
237 native_message_host_->OnMessage(kTestMessage); | 235 native_message_process_host_->Send(kTestMessage); |
238 base::RunLoop().RunUntilIdle(); | 236 base::RunLoop().RunUntilIdle(); |
239 | 237 |
240 std::string output; | 238 std::string output; |
241 base::TimeTicks start_time = base::TimeTicks::Now(); | 239 base::TimeTicks start_time = base::TimeTicks::Now(); |
242 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { | 240 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { |
243 ASSERT_TRUE(base::ReadFileToString(temp_output_file, &output)); | 241 ASSERT_TRUE(base::ReadFileToString(temp_output_file, &output)); |
244 if (!output.empty()) | 242 if (!output.empty()) |
245 break; | 243 break; |
246 base::PlatformThread::YieldCurrentThread(); | 244 base::PlatformThread::YieldCurrentThread(); |
247 } | 245 } |
248 | 246 |
249 EXPECT_EQ(FormatMessage(kTestMessage), output); | 247 EXPECT_EQ(FormatMessage(kTestMessage), output); |
250 } | 248 } |
251 | 249 |
252 // Test send message with a real client. The client just echo's back the text | 250 // Test send message with a real client. The client just echo's back the text |
253 // it received. | 251 // it received. |
254 TEST_F(NativeMessagingTest, EchoConnect) { | 252 TEST_F(NativeMessagingTest, EchoConnect) { |
255 ScopedTestNativeMessagingHost test_host; | 253 ScopedTestNativeMessagingHost test_host; |
256 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(false)); | 254 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(false)); |
257 std::string error_message; | |
258 native_message_host_ = NativeMessageProcessHost::Create( | |
259 NULL, | |
260 ScopedTestNativeMessagingHost::kExtensionId, | |
261 ScopedTestNativeMessagingHost::kHostName, | |
262 false, | |
263 &error_message); | |
264 native_message_host_->Start(this); | |
265 ASSERT_TRUE(native_message_host_.get()); | |
266 | 255 |
267 native_message_host_->OnMessage("{\"text\": \"Hello.\"}"); | 256 native_message_process_host_ = NativeMessageProcessHost::Create( |
| 257 NULL, AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, |
| 258 ScopedTestNativeMessagingHost::kHostName, 0, false); |
| 259 ASSERT_TRUE(native_message_process_host_.get()); |
| 260 |
| 261 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); |
268 run_loop_.reset(new base::RunLoop()); | 262 run_loop_.reset(new base::RunLoop()); |
269 run_loop_->Run(); | 263 run_loop_->Run(); |
270 ASSERT_FALSE(last_message_.empty()); | 264 ASSERT_FALSE(last_message_.empty()); |
271 ASSERT_TRUE(last_message_parsed_); | 265 ASSERT_TRUE(last_message_parsed_); |
272 | 266 |
273 std::string expected_url = std::string("chrome-extension://") + | 267 std::string expected_url = std::string("chrome-extension://") + |
274 ScopedTestNativeMessagingHost::kExtensionId + "/"; | 268 ScopedTestNativeMessagingHost::kExtensionId + "/"; |
275 int id; | 269 int id; |
276 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); | 270 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); |
277 EXPECT_EQ(1, id); | 271 EXPECT_EQ(1, id); |
278 std::string text; | 272 std::string text; |
279 EXPECT_TRUE(last_message_parsed_->GetString("echo.text", &text)); | 273 EXPECT_TRUE(last_message_parsed_->GetString("echo.text", &text)); |
280 EXPECT_EQ("Hello.", text); | 274 EXPECT_EQ("Hello.", text); |
281 std::string url; | 275 std::string url; |
282 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); | 276 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); |
283 EXPECT_EQ(expected_url, url); | 277 EXPECT_EQ(expected_url, url); |
284 | 278 |
285 native_message_host_->OnMessage("{\"foo\": \"bar\"}"); | 279 native_message_process_host_->Send("{\"foo\": \"bar\"}"); |
286 run_loop_.reset(new base::RunLoop()); | 280 run_loop_.reset(new base::RunLoop()); |
287 run_loop_->Run(); | 281 run_loop_->Run(); |
288 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); | 282 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); |
289 EXPECT_EQ(2, id); | 283 EXPECT_EQ(2, id); |
290 EXPECT_TRUE(last_message_parsed_->GetString("echo.foo", &text)); | 284 EXPECT_TRUE(last_message_parsed_->GetString("echo.foo", &text)); |
291 EXPECT_EQ("bar", text); | 285 EXPECT_EQ("bar", text); |
292 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); | 286 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); |
293 EXPECT_EQ(expected_url, url); | 287 EXPECT_EQ(expected_url, url); |
294 } | 288 } |
295 | 289 |
296 TEST_F(NativeMessagingTest, UserLevel) { | 290 TEST_F(NativeMessagingTest, UserLevel) { |
297 ScopedTestNativeMessagingHost test_host; | 291 ScopedTestNativeMessagingHost test_host; |
298 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(true)); | 292 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(true)); |
299 | 293 |
300 std::string error_message; | 294 native_message_process_host_ = NativeMessageProcessHost::Create( |
301 native_message_host_ = NativeMessageProcessHost::Create( | 295 NULL, AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, |
302 NULL, | 296 ScopedTestNativeMessagingHost::kHostName, 0, true); |
303 ScopedTestNativeMessagingHost::kExtensionId, | 297 ASSERT_TRUE(native_message_process_host_.get()); |
304 ScopedTestNativeMessagingHost::kHostName, | |
305 true, | |
306 &error_message); | |
307 native_message_host_->Start(this); | |
308 ASSERT_TRUE(native_message_host_.get()); | |
309 | 298 |
310 native_message_host_->OnMessage("{\"text\": \"Hello.\"}"); | 299 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); |
311 run_loop_.reset(new base::RunLoop()); | 300 run_loop_.reset(new base::RunLoop()); |
312 run_loop_->Run(); | 301 run_loop_->Run(); |
313 ASSERT_FALSE(last_message_.empty()); | 302 ASSERT_FALSE(last_message_.empty()); |
314 ASSERT_TRUE(last_message_parsed_); | 303 ASSERT_TRUE(last_message_parsed_); |
315 } | 304 } |
316 | 305 |
317 TEST_F(NativeMessagingTest, DisallowUserLevel) { | 306 TEST_F(NativeMessagingTest, DisallowUserLevel) { |
318 ScopedTestNativeMessagingHost test_host; | 307 ScopedTestNativeMessagingHost test_host; |
319 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(true)); | 308 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(true)); |
320 | 309 |
321 std::string error_message; | 310 native_message_process_host_ = NativeMessageProcessHost::Create( |
322 native_message_host_ = NativeMessageProcessHost::Create( | 311 NULL, AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, |
323 NULL, | 312 ScopedTestNativeMessagingHost::kHostName, 0, false); |
324 ScopedTestNativeMessagingHost::kExtensionId, | 313 ASSERT_TRUE(native_message_process_host_.get()); |
325 ScopedTestNativeMessagingHost::kHostName, | |
326 false, | |
327 &error_message); | |
328 native_message_host_->Start(this); | |
329 ASSERT_TRUE(native_message_host_.get()); | |
330 run_loop_.reset(new base::RunLoop()); | 314 run_loop_.reset(new base::RunLoop()); |
331 run_loop_->Run(); | 315 run_loop_->Run(); |
332 | 316 |
333 // The host should fail to start. | 317 // The host should fail to start. |
334 ASSERT_TRUE(channel_closed_); | 318 ASSERT_TRUE(channel_closed_); |
335 } | 319 } |
336 | 320 |
337 } // namespace extensions | 321 } // namespace extensions |
OLD | NEW |