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