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" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 NativeMessagingTest() | 99 NativeMessagingTest() |
100 : current_channel_(chrome::VersionInfo::CHANNEL_DEV), | 100 : current_channel_(chrome::VersionInfo::CHANNEL_DEV), |
101 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 101 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
102 channel_closed_(false) {} | 102 channel_closed_(false) {} |
103 | 103 |
104 virtual void SetUp() OVERRIDE { | 104 virtual void SetUp() OVERRIDE { |
105 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 105 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
106 } | 106 } |
107 | 107 |
108 virtual void TearDown() OVERRIDE { | 108 virtual void TearDown() OVERRIDE { |
109 if (native_message_process_host_.get()) { | 109 if (native_message_host_.get()) { |
110 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, | 110 BrowserThread::DeleteSoon( |
111 native_message_process_host_.release()); | 111 BrowserThread::IO, FROM_HERE, native_message_host_.release()); |
112 } | 112 } |
113 base::RunLoop().RunUntilIdle(); | 113 base::RunLoop().RunUntilIdle(); |
114 } | 114 } |
115 | 115 |
116 virtual void PostMessageFromNativeProcess( | 116 virtual void PostMessageFromNativeHost(int port_id, |
117 int port_id, | 117 const std::string& message) OVERRIDE { |
118 const std::string& message) OVERRIDE { | |
119 last_message_ = message; | 118 last_message_ = message; |
120 | 119 |
121 // Parse the message. | 120 // Parse the message. |
122 base::Value* parsed = base::JSONReader::Read(message); | 121 base::Value* parsed = base::JSONReader::Read(message); |
123 base::DictionaryValue* dict_value; | 122 base::DictionaryValue* dict_value; |
124 if (parsed && parsed->GetAsDictionary(&dict_value)) { | 123 if (parsed && parsed->GetAsDictionary(&dict_value)) { |
125 last_message_parsed_.reset(dict_value); | 124 last_message_parsed_.reset(dict_value); |
126 } else { | 125 } else { |
127 LOG(ERROR) << "Failed to parse " << message; | 126 LOG(ERROR) << "Failed to parse " << message; |
128 last_message_parsed_.reset(); | 127 last_message_parsed_.reset(); |
(...skipping 28 matching lines...) Expand all Loading... |
157 if (bytes_written < 0 || | 156 if (bytes_written < 0 || |
158 (message_with_header.size() != static_cast<size_t>(bytes_written))) { | 157 (message_with_header.size() != static_cast<size_t>(bytes_written))) { |
159 return base::FilePath(); | 158 return base::FilePath(); |
160 } | 159 } |
161 return filename; | 160 return filename; |
162 } | 161 } |
163 | 162 |
164 base::ScopedTempDir temp_dir_; | 163 base::ScopedTempDir temp_dir_; |
165 // Force the channel to be dev. | 164 // Force the channel to be dev. |
166 ScopedCurrentChannel current_channel_; | 165 ScopedCurrentChannel current_channel_; |
167 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; | 166 scoped_ptr<NativeMessageHost> native_message_host_; |
168 scoped_ptr<base::RunLoop> run_loop_; | 167 scoped_ptr<base::RunLoop> run_loop_; |
169 content::TestBrowserThreadBundle thread_bundle_; | 168 content::TestBrowserThreadBundle thread_bundle_; |
170 std::string last_message_; | 169 std::string last_message_; |
171 scoped_ptr<base::DictionaryValue> last_message_parsed_; | 170 scoped_ptr<base::DictionaryValue> last_message_parsed_; |
172 bool channel_closed_; | 171 bool channel_closed_; |
173 }; | 172 }; |
174 | 173 |
175 // Read a single message from a local file. | 174 // Read a single message from a local file. |
176 TEST_F(NativeMessagingTest, SingleSendMessageRead) { | 175 TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
177 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 176 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
178 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); | 177 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); |
179 ASSERT_FALSE(temp_input_file.empty()); | 178 ASSERT_FALSE(temp_input_file.empty()); |
180 | 179 |
181 scoped_ptr<NativeProcessLauncher> launcher = | 180 scoped_ptr<NativeProcessLauncher> launcher = |
182 FakeLauncher::Create(temp_input_file, temp_output_file).Pass(); | 181 FakeLauncher::Create(temp_input_file, temp_output_file).Pass(); |
183 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( | 182 native_message_host_ = NativeMessageProcessHost::CreateWithLauncher( |
184 AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, "empty_app.py", | 183 AsWeakPtr(), |
185 0, launcher.Pass()); | 184 ScopedTestNativeMessagingHost::kExtensionId, |
186 ASSERT_TRUE(native_message_process_host_.get()); | 185 "empty_app.py", |
| 186 0, |
| 187 launcher.Pass()); |
| 188 ASSERT_TRUE(native_message_host_.get()); |
187 run_loop_.reset(new base::RunLoop()); | 189 run_loop_.reset(new base::RunLoop()); |
188 run_loop_->RunUntilIdle(); | 190 run_loop_->RunUntilIdle(); |
189 | 191 |
190 if (last_message_.empty()) { | 192 if (last_message_.empty()) { |
191 run_loop_.reset(new base::RunLoop()); | 193 run_loop_.reset(new base::RunLoop()); |
| 194 scoped_ptr<NativeMessageProcessHost> native_message_process_host_( |
| 195 static_cast<NativeMessageProcessHost*>(native_message_host_.release())); |
192 native_message_process_host_->ReadNowForTesting(); | 196 native_message_process_host_->ReadNowForTesting(); |
193 run_loop_->Run(); | 197 run_loop_->Run(); |
194 } | 198 } |
195 EXPECT_EQ(kTestMessage, last_message_); | 199 EXPECT_EQ(kTestMessage, last_message_); |
196 } | 200 } |
197 | 201 |
198 // Tests sending a single message. The message should get written to | 202 // Tests sending a single message. The message should get written to |
199 // |temp_file| and should match the contents of single_message_request.msg. | 203 // |temp_file| and should match the contents of single_message_request.msg. |
200 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { | 204 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
201 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 205 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
(...skipping 17 matching lines...) Expand all Loading... |
219 #else // defined(OS_WIN) | 223 #else // defined(OS_WIN) |
220 base::PlatformFile pipe_handles[2]; | 224 base::PlatformFile pipe_handles[2]; |
221 ASSERT_EQ(0, pipe(pipe_handles)); | 225 ASSERT_EQ(0, pipe(pipe_handles)); |
222 read_file = base::File(pipe_handles[0]); | 226 read_file = base::File(pipe_handles[0]); |
223 base::File write_file(pipe_handles[1]); | 227 base::File write_file(pipe_handles[1]); |
224 #endif // !defined(OS_WIN) | 228 #endif // !defined(OS_WIN) |
225 | 229 |
226 scoped_ptr<NativeProcessLauncher> launcher = | 230 scoped_ptr<NativeProcessLauncher> launcher = |
227 FakeLauncher::CreateWithPipeInput(read_file.Pass(), | 231 FakeLauncher::CreateWithPipeInput(read_file.Pass(), |
228 temp_output_file).Pass(); | 232 temp_output_file).Pass(); |
229 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( | 233 native_message_host_ = NativeMessageProcessHost::CreateWithLauncher( |
230 AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, "empty_app.py", | 234 AsWeakPtr(), |
231 0, launcher.Pass()); | 235 ScopedTestNativeMessagingHost::kExtensionId, |
232 ASSERT_TRUE(native_message_process_host_.get()); | 236 "empty_app.py", |
| 237 0, |
| 238 launcher.Pass()); |
| 239 ASSERT_TRUE(native_message_host_.get()); |
233 base::RunLoop().RunUntilIdle(); | 240 base::RunLoop().RunUntilIdle(); |
234 | 241 |
235 native_message_process_host_->Send(kTestMessage); | 242 native_message_host_->Send(kTestMessage); |
236 base::RunLoop().RunUntilIdle(); | 243 base::RunLoop().RunUntilIdle(); |
237 | 244 |
238 std::string output; | 245 std::string output; |
239 base::TimeTicks start_time = base::TimeTicks::Now(); | 246 base::TimeTicks start_time = base::TimeTicks::Now(); |
240 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { | 247 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { |
241 ASSERT_TRUE(base::ReadFileToString(temp_output_file, &output)); | 248 ASSERT_TRUE(base::ReadFileToString(temp_output_file, &output)); |
242 if (!output.empty()) | 249 if (!output.empty()) |
243 break; | 250 break; |
244 base::PlatformThread::YieldCurrentThread(); | 251 base::PlatformThread::YieldCurrentThread(); |
245 } | 252 } |
246 | 253 |
247 EXPECT_EQ(FormatMessage(kTestMessage), output); | 254 EXPECT_EQ(FormatMessage(kTestMessage), output); |
248 } | 255 } |
249 | 256 |
250 // Test send message with a real client. The client just echo's back the text | 257 // Test send message with a real client. The client just echo's back the text |
251 // it received. | 258 // it received. |
252 TEST_F(NativeMessagingTest, EchoConnect) { | 259 TEST_F(NativeMessagingTest, EchoConnect) { |
253 ScopedTestNativeMessagingHost test_host; | 260 ScopedTestNativeMessagingHost test_host; |
254 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(false)); | 261 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(false)); |
255 | 262 |
256 native_message_process_host_ = NativeMessageProcessHost::Create( | 263 native_message_host_ = NativeMessageProcessHost::Create( |
257 NULL, AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, | 264 NULL, |
258 ScopedTestNativeMessagingHost::kHostName, 0, false); | 265 AsWeakPtr(), |
259 ASSERT_TRUE(native_message_process_host_.get()); | 266 ScopedTestNativeMessagingHost::kExtensionId, |
| 267 ScopedTestNativeMessagingHost::kHostName, |
| 268 0, |
| 269 false); |
| 270 ASSERT_TRUE(native_message_host_.get()); |
260 | 271 |
261 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); | 272 native_message_host_->Send("{\"text\": \"Hello.\"}"); |
262 run_loop_.reset(new base::RunLoop()); | 273 run_loop_.reset(new base::RunLoop()); |
263 run_loop_->Run(); | 274 run_loop_->Run(); |
264 ASSERT_FALSE(last_message_.empty()); | 275 ASSERT_FALSE(last_message_.empty()); |
265 ASSERT_TRUE(last_message_parsed_); | 276 ASSERT_TRUE(last_message_parsed_); |
266 | 277 |
267 std::string expected_url = std::string("chrome-extension://") + | 278 std::string expected_url = std::string("chrome-extension://") + |
268 ScopedTestNativeMessagingHost::kExtensionId + "/"; | 279 ScopedTestNativeMessagingHost::kExtensionId + "/"; |
269 int id; | 280 int id; |
270 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); | 281 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); |
271 EXPECT_EQ(1, id); | 282 EXPECT_EQ(1, id); |
272 std::string text; | 283 std::string text; |
273 EXPECT_TRUE(last_message_parsed_->GetString("echo.text", &text)); | 284 EXPECT_TRUE(last_message_parsed_->GetString("echo.text", &text)); |
274 EXPECT_EQ("Hello.", text); | 285 EXPECT_EQ("Hello.", text); |
275 std::string url; | 286 std::string url; |
276 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); | 287 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); |
277 EXPECT_EQ(expected_url, url); | 288 EXPECT_EQ(expected_url, url); |
278 | 289 |
279 native_message_process_host_->Send("{\"foo\": \"bar\"}"); | 290 native_message_host_->Send("{\"foo\": \"bar\"}"); |
280 run_loop_.reset(new base::RunLoop()); | 291 run_loop_.reset(new base::RunLoop()); |
281 run_loop_->Run(); | 292 run_loop_->Run(); |
282 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); | 293 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); |
283 EXPECT_EQ(2, id); | 294 EXPECT_EQ(2, id); |
284 EXPECT_TRUE(last_message_parsed_->GetString("echo.foo", &text)); | 295 EXPECT_TRUE(last_message_parsed_->GetString("echo.foo", &text)); |
285 EXPECT_EQ("bar", text); | 296 EXPECT_EQ("bar", text); |
286 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); | 297 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); |
287 EXPECT_EQ(expected_url, url); | 298 EXPECT_EQ(expected_url, url); |
288 } | 299 } |
289 | 300 |
290 TEST_F(NativeMessagingTest, UserLevel) { | 301 TEST_F(NativeMessagingTest, UserLevel) { |
291 ScopedTestNativeMessagingHost test_host; | 302 ScopedTestNativeMessagingHost test_host; |
292 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(true)); | 303 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(true)); |
293 | 304 |
294 native_message_process_host_ = NativeMessageProcessHost::Create( | 305 native_message_host_ = NativeMessageProcessHost::Create( |
295 NULL, AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, | 306 NULL, |
296 ScopedTestNativeMessagingHost::kHostName, 0, true); | 307 AsWeakPtr(), |
297 ASSERT_TRUE(native_message_process_host_.get()); | 308 ScopedTestNativeMessagingHost::kExtensionId, |
| 309 ScopedTestNativeMessagingHost::kHostName, |
| 310 0, |
| 311 true); |
| 312 ASSERT_TRUE(native_message_host_.get()); |
298 | 313 |
299 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); | 314 native_message_host_->Send("{\"text\": \"Hello.\"}"); |
300 run_loop_.reset(new base::RunLoop()); | 315 run_loop_.reset(new base::RunLoop()); |
301 run_loop_->Run(); | 316 run_loop_->Run(); |
302 ASSERT_FALSE(last_message_.empty()); | 317 ASSERT_FALSE(last_message_.empty()); |
303 ASSERT_TRUE(last_message_parsed_); | 318 ASSERT_TRUE(last_message_parsed_); |
304 } | 319 } |
305 | 320 |
306 TEST_F(NativeMessagingTest, DisallowUserLevel) { | 321 TEST_F(NativeMessagingTest, DisallowUserLevel) { |
307 ScopedTestNativeMessagingHost test_host; | 322 ScopedTestNativeMessagingHost test_host; |
308 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(true)); | 323 ASSERT_NO_FATAL_FAILURE(test_host.RegisterTestHost(true)); |
309 | 324 |
310 native_message_process_host_ = NativeMessageProcessHost::Create( | 325 native_message_host_ = NativeMessageProcessHost::Create( |
311 NULL, AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, | 326 NULL, |
312 ScopedTestNativeMessagingHost::kHostName, 0, false); | 327 AsWeakPtr(), |
313 ASSERT_TRUE(native_message_process_host_.get()); | 328 ScopedTestNativeMessagingHost::kExtensionId, |
| 329 ScopedTestNativeMessagingHost::kHostName, |
| 330 0, |
| 331 false); |
| 332 ASSERT_TRUE(native_message_host_.get()); |
314 run_loop_.reset(new base::RunLoop()); | 333 run_loop_.reset(new base::RunLoop()); |
315 run_loop_->Run(); | 334 run_loop_->Run(); |
316 | 335 |
317 // The host should fail to start. | 336 // The host should fail to start. |
318 ASSERT_TRUE(channel_closed_); | 337 ASSERT_TRUE(channel_closed_); |
319 } | 338 } |
320 | 339 |
321 } // namespace extensions | 340 } // namespace extensions |
OLD | NEW |