| 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_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/json/json_reader.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 14 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
| 15 #include "base/process_util.h" | 16 #include "base/process_util.h" |
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 17 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
| 18 #include "base/test/test_timeouts.h" | 19 #include "base/test/test_timeouts.h" |
| 19 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 if (native_message_process_host_.get()) { | 104 if (native_message_process_host_.get()) { |
| 104 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, | 105 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
| 105 native_message_process_host_.release()); | 106 native_message_process_host_.release()); |
| 106 } | 107 } |
| 107 message_loop_.RunUntilIdle(); | 108 message_loop_.RunUntilIdle(); |
| 108 } | 109 } |
| 109 | 110 |
| 110 virtual void PostMessageFromNativeProcess( | 111 virtual void PostMessageFromNativeProcess( |
| 111 int port_id, | 112 int port_id, |
| 112 const std::string& message) OVERRIDE { | 113 const std::string& message) OVERRIDE { |
| 113 last_posted_message_ = message; | 114 last_message_ = message; |
| 115 |
| 116 // Parse the message. |
| 117 base::Value* parsed = base::JSONReader::Read(message); |
| 118 base::DictionaryValue* dict_value; |
| 119 if (parsed && parsed->GetAsDictionary(&dict_value)) { |
| 120 last_message_parsed_.reset(dict_value); |
| 121 } else { |
| 122 LOG(ERROR) << "Failed to parse " << message; |
| 123 last_message_parsed_.reset(); |
| 124 delete parsed; |
| 125 } |
| 126 |
| 114 if (read_message_run_loop_) | 127 if (read_message_run_loop_) |
| 115 read_message_run_loop_->Quit(); | 128 read_message_run_loop_->Quit(); |
| 116 } | 129 } |
| 117 | 130 |
| 118 virtual void CloseChannel(int port_id, bool error) OVERRIDE { | 131 virtual void CloseChannel(int port_id, bool error) OVERRIDE { |
| 119 } | 132 } |
| 120 | 133 |
| 121 protected: | 134 protected: |
| 122 std::string FormatMessage(const std::string& message) { | 135 std::string FormatMessage(const std::string& message) { |
| 123 Pickle pickle; | 136 Pickle pickle; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 137 | 150 |
| 138 // Force the channel to be dev. | 151 // Force the channel to be dev. |
| 139 base::ScopedTempDir temp_dir_; | 152 base::ScopedTempDir temp_dir_; |
| 140 Feature::ScopedCurrentChannel current_channel_; | 153 Feature::ScopedCurrentChannel current_channel_; |
| 141 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; | 154 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; |
| 142 base::FilePath user_data_dir_; | 155 base::FilePath user_data_dir_; |
| 143 MessageLoopForIO message_loop_; | 156 MessageLoopForIO message_loop_; |
| 144 scoped_ptr<base::RunLoop> read_message_run_loop_; | 157 scoped_ptr<base::RunLoop> read_message_run_loop_; |
| 145 scoped_ptr<content::TestBrowserThread> ui_thread_; | 158 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 146 scoped_ptr<content::TestBrowserThread> io_thread_; | 159 scoped_ptr<content::TestBrowserThread> io_thread_; |
| 147 std::string last_posted_message_; | 160 std::string last_message_; |
| 161 scoped_ptr<base::DictionaryValue> last_message_parsed_; |
| 148 }; | 162 }; |
| 149 | 163 |
| 150 // Read a single message from a local file. | 164 // Read a single message from a local file. |
| 151 TEST_F(NativeMessagingTest, SingleSendMessageRead) { | 165 TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
| 152 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 166 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
| 153 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); | 167 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); |
| 154 | 168 |
| 155 scoped_ptr<NativeProcessLauncher> launcher( | 169 scoped_ptr<NativeProcessLauncher> launcher( |
| 156 new FakeLauncher(temp_input_file, temp_output_file)); | 170 new FakeLauncher(temp_input_file, temp_output_file)); |
| 157 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( | 171 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( |
| 158 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py", | 172 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py", |
| 159 0, launcher.Pass()); | 173 0, launcher.Pass()); |
| 160 ASSERT_TRUE(native_message_process_host_.get()); | 174 ASSERT_TRUE(native_message_process_host_.get()); |
| 161 read_message_run_loop_.reset(new base::RunLoop()); | 175 read_message_run_loop_.reset(new base::RunLoop()); |
| 162 read_message_run_loop_->RunUntilIdle(); | 176 read_message_run_loop_->RunUntilIdle(); |
| 163 | 177 |
| 164 if (last_posted_message_.empty()) { | 178 if (last_message_.empty()) { |
| 165 read_message_run_loop_.reset(new base::RunLoop()); | 179 read_message_run_loop_.reset(new base::RunLoop()); |
| 166 native_message_process_host_->ReadNowForTesting(); | 180 native_message_process_host_->ReadNowForTesting(); |
| 167 read_message_run_loop_->Run(); | 181 read_message_run_loop_->Run(); |
| 168 } | 182 } |
| 169 EXPECT_EQ(kTestMessage, last_posted_message_); | 183 EXPECT_EQ(kTestMessage, last_message_); |
| 170 } | 184 } |
| 171 | 185 |
| 172 // Tests sending a single message. The message should get written to | 186 // Tests sending a single message. The message should get written to |
| 173 // |temp_file| and should match the contents of single_message_request.msg. | 187 // |temp_file| and should match the contents of single_message_request.msg. |
| 174 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { | 188 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
| 175 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 189 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
| 176 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string()); | 190 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string()); |
| 177 | 191 |
| 178 scoped_ptr<NativeProcessLauncher> launcher( | 192 scoped_ptr<NativeProcessLauncher> launcher( |
| 179 new FakeLauncher(temp_input_file, temp_output_file)); | 193 new FakeLauncher(temp_input_file, temp_output_file)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 192 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); | 206 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); |
| 193 if (!output.empty()) | 207 if (!output.empty()) |
| 194 break; | 208 break; |
| 195 base::PlatformThread::YieldCurrentThread(); | 209 base::PlatformThread::YieldCurrentThread(); |
| 196 } | 210 } |
| 197 | 211 |
| 198 EXPECT_EQ(FormatMessage(kTestMessage), output); | 212 EXPECT_EQ(FormatMessage(kTestMessage), output); |
| 199 } | 213 } |
| 200 | 214 |
| 201 // Test send message with a real client. The client just echo's back the text | 215 // Test send message with a real client. The client just echo's back the text |
| 202 // it recieved. | 216 // it received. |
| 203 TEST_F(NativeMessagingTest, EchoConnect) { | 217 TEST_F(NativeMessagingTest, EchoConnect) { |
| 204 base::ScopedTempDir temp_dir; | 218 base::ScopedTempDir temp_dir; |
| 205 base::FilePath manifest_path = temp_dir.path().AppendASCII( | 219 base::FilePath manifest_path = temp_dir.path().AppendASCII( |
| 206 std::string(kTestNativeMessagingHostName) + ".json"); | 220 std::string(kTestNativeMessagingHostName) + ".json"); |
| 207 ASSERT_NO_FATAL_FAILURE(CreateTestNativeHostManifest(manifest_path)); | 221 ASSERT_NO_FATAL_FAILURE(CreateTestNativeHostManifest(manifest_path)); |
| 208 | 222 |
| 209 std::string hosts_option = base::StringPrintf( | 223 std::string hosts_option = base::StringPrintf( |
| 210 "%s=%s", extensions::kTestNativeMessagingHostName, | 224 "%s=%s", extensions::kTestNativeMessagingHostName, |
| 211 manifest_path.AsUTF8Unsafe().c_str()); | 225 manifest_path.AsUTF8Unsafe().c_str()); |
| 212 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 226 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 213 switches::kNativeMessagingHosts, hosts_option); | 227 switches::kNativeMessagingHosts, hosts_option); |
| 214 | 228 |
| 215 native_message_process_host_ = NativeMessageProcessHost::Create( | 229 native_message_process_host_ = NativeMessageProcessHost::Create( |
| 216 AsWeakPtr(), kTestNativeMessagingExtensionId, | 230 AsWeakPtr(), kTestNativeMessagingExtensionId, |
| 217 kTestNativeMessagingHostName, 0); | 231 kTestNativeMessagingHostName, 0); |
| 218 ASSERT_TRUE(native_message_process_host_.get()); | 232 ASSERT_TRUE(native_message_process_host_.get()); |
| 219 | 233 |
| 220 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); | 234 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); |
| 221 read_message_run_loop_.reset(new base::RunLoop()); | 235 read_message_run_loop_.reset(new base::RunLoop()); |
| 222 read_message_run_loop_->Run(); | 236 read_message_run_loop_->Run(); |
| 223 EXPECT_EQ("{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}", | 237 ASSERT_FALSE(last_message_.empty()); |
| 224 last_posted_message_); | 238 ASSERT_TRUE(last_message_parsed_); |
| 239 |
| 240 std::string expected_url = std::string("chrome-extension://") + |
| 241 kTestNativeMessagingExtensionId + "/"; |
| 242 int id; |
| 243 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); |
| 244 EXPECT_EQ(1, id); |
| 245 std::string text; |
| 246 EXPECT_TRUE(last_message_parsed_->GetString("echo.text", &text)); |
| 247 EXPECT_EQ("Hello.", text); |
| 248 std::string url; |
| 249 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); |
| 250 EXPECT_EQ(expected_url, url); |
| 251 |
| 225 | 252 |
| 226 native_message_process_host_->Send("{\"foo\": \"bar\"}"); | 253 native_message_process_host_->Send("{\"foo\": \"bar\"}"); |
| 227 read_message_run_loop_.reset(new base::RunLoop()); | 254 read_message_run_loop_.reset(new base::RunLoop()); |
| 228 read_message_run_loop_->Run(); | 255 read_message_run_loop_->Run(); |
| 229 EXPECT_EQ("{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}", last_posted_message_); | 256 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); |
| 257 EXPECT_EQ(2, id); |
| 258 EXPECT_TRUE(last_message_parsed_->GetString("echo.foo", &text)); |
| 259 EXPECT_EQ("bar", text); |
| 260 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); |
| 261 EXPECT_EQ(expected_url, url); |
| 230 } | 262 } |
| 231 | 263 |
| 232 } // namespace extensions | 264 } // namespace extensions |
| OLD | NEW |