Chromium Code Reviews| 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 "chrome/utility/importer/firefox_importer_unittest_utils.h" | 5 #include "chrome/utility/importer/firefox_importer_unittest_utils.h" |
| 6 | 6 |
| 7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/posix/global_descriptors.h" | 15 #include "base/posix/global_descriptors.h" |
| 16 #include "base/process/kill.h" | 16 #include "base/process/kill.h" |
| 17 #include "base/process/launch.h" | 17 #include "base/process/launch.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/test/test_timeouts.h" | 20 #include "base/test/test_timeouts.h" |
| 21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
| 22 #include "chrome/common/importer/firefox_importer_utils.h" | 22 #include "chrome/common/importer/firefox_importer_utils.h" |
| 23 #include "content/public/common/mojo_channel_switches.h" | |
| 23 #include "ipc/ipc_channel.h" | 24 #include "ipc/ipc_channel.h" |
| 24 #include "ipc/ipc_descriptors.h" | 25 #include "ipc/ipc_descriptors.h" |
| 25 #include "ipc/ipc_listener.h" | 26 #include "ipc/ipc_listener.h" |
| 26 #include "ipc/ipc_message.h" | 27 #include "ipc/ipc_message.h" |
| 27 #include "ipc/ipc_multiprocess_test.h" | 28 #include "ipc/ipc_multiprocess_test.h" |
| 29 #include "mojo/edk/embedder/embedder.h" | |
| 30 #include "mojo/edk/embedder/platform_channel_pair.h" | |
| 31 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
| 28 #include "testing/multiprocess_func_list.h" | 32 #include "testing/multiprocess_func_list.h" |
| 29 | 33 |
| 30 #define IPC_MESSAGE_IMPL | 34 #define IPC_MESSAGE_IMPL |
| 31 #include "chrome/utility/importer/firefox_importer_unittest_messages_internal.h" | 35 #include "chrome/utility/importer/firefox_importer_unittest_messages_internal.h" |
| 32 | 36 |
| 33 namespace { | 37 namespace { |
| 34 | 38 |
| 35 // Name of IPC Channel to use for Server<-> Child Communications. | |
| 36 const char kTestChannelID[] = "T1"; | |
| 37 | |
| 38 // Launch the child process: | 39 // Launch the child process: |
| 39 // |nss_path| - path to the NSS directory holding the decryption libraries. | 40 // |nss_path| - path to the NSS directory holding the decryption libraries. |
| 40 // |channel| - IPC Channel to use for communication. | 41 // |mojo_handle| - platform handle for Mojo transport. |
| 41 base::Process LaunchNSSDecrypterChildProcess(const base::FilePath& nss_path, | 42 // |mojo_channel_token| - token for creating the IPC channel over Mojo. |
| 42 IPC::Channel* channel) { | 43 base::Process LaunchNSSDecrypterChildProcess( |
| 44 const base::FilePath& nss_path, | |
| 45 mojo::edk::ScopedPlatformHandle mojo_handle, | |
| 46 const std::string& mojo_channel_token) { | |
| 43 base::CommandLine cl(*base::CommandLine::ForCurrentProcess()); | 47 base::CommandLine cl(*base::CommandLine::ForCurrentProcess()); |
| 44 cl.AppendSwitchASCII(switches::kTestChildProcess, "NSSDecrypterChildProcess"); | 48 cl.AppendSwitchASCII(switches::kTestChildProcess, "NSSDecrypterChildProcess"); |
| 49 cl.AppendSwitchASCII(switches::kMojoChannelToken, mojo_channel_token); | |
| 45 | 50 |
| 46 // Set env variable needed for FF encryption libs to load. | 51 // Set env variable needed for FF encryption libs to load. |
| 47 // See "chrome/utility/importer/nss_decryptor_mac.mm" for an explanation of | 52 // See "chrome/utility/importer/nss_decryptor_mac.mm" for an explanation of |
| 48 // why we need this. | 53 // why we need this. |
| 49 base::LaunchOptions options; | 54 base::LaunchOptions options; |
| 50 options.environ["DYLD_FALLBACK_LIBRARY_PATH"] = nss_path.value(); | 55 options.environ["DYLD_FALLBACK_LIBRARY_PATH"] = nss_path.value(); |
| 51 | 56 |
| 52 base::ScopedFD ipcfd(channel->TakeClientFileDescriptor()); | |
| 53 if (!ipcfd.is_valid()) | |
| 54 return base::Process(); | |
| 55 | |
| 56 base::FileHandleMappingVector fds_to_map; | 57 base::FileHandleMappingVector fds_to_map; |
| 57 fds_to_map.push_back(std::pair<int,int>(ipcfd.get(), | 58 fds_to_map.push_back(std::pair<int,int>(mojo_handle.get().handle, |
| 58 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); | 59 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
| 59 | 60 |
| 60 options.fds_to_remap = &fds_to_map; | 61 options.fds_to_remap = &fds_to_map; |
| 61 return base::LaunchProcess(cl.argv(), options); | 62 return base::LaunchProcess(cl.argv(), options); |
| 62 } | 63 } |
| 63 | 64 |
| 64 } // namespace | 65 } // namespace |
| 65 | 66 |
| 66 //----------------------- Server -------------------- | 67 //----------------------- Server -------------------- |
| 67 | 68 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 }; | 135 }; |
| 135 | 136 |
| 136 FFUnitTestDecryptorProxy::FFUnitTestDecryptorProxy() { | 137 FFUnitTestDecryptorProxy::FFUnitTestDecryptorProxy() { |
| 137 } | 138 } |
| 138 | 139 |
| 139 bool FFUnitTestDecryptorProxy::Setup(const base::FilePath& nss_path) { | 140 bool FFUnitTestDecryptorProxy::Setup(const base::FilePath& nss_path) { |
| 140 // Create a new message loop and spawn the child process. | 141 // Create a new message loop and spawn the child process. |
| 141 message_loop_.reset(new base::MessageLoopForIO()); | 142 message_loop_.reset(new base::MessageLoopForIO()); |
| 142 | 143 |
| 143 listener_.reset(new FFDecryptorServerChannelListener()); | 144 listener_.reset(new FFDecryptorServerChannelListener()); |
| 144 channel_ = IPC::Channel::CreateServer(kTestChannelID, listener_.get()); | 145 |
| 146 // Setup IPC channel using ChannelMojo. | |
|
Ilya Sherman
2016/07/07 20:14:24
nit: s/Setup/Set up
Anand Mistry (off Chromium)
2016/07/11 06:59:59
Done.
| |
| 147 const std::string mojo_child_token = mojo::edk::GenerateRandomToken(); | |
| 148 const std::string mojo_channel_token = mojo::edk::GenerateRandomToken(); | |
| 149 mojo::ScopedMessagePipeHandle parent_handle = | |
| 150 mojo::edk::CreateParentMessagePipe(mojo_channel_token, mojo_child_token); | |
| 151 channel_ = IPC::Channel::CreateServer(parent_handle.release(), | |
| 152 listener_.get()); | |
| 145 CHECK(channel_->Connect()); | 153 CHECK(channel_->Connect()); |
| 146 listener_->SetSender(channel_.get()); | 154 listener_->SetSender(channel_.get()); |
| 147 | 155 |
| 148 // Spawn child and set up sync IPC connection. | 156 // Spawn child and set up sync IPC connection. |
| 149 child_process_ = LaunchNSSDecrypterChildProcess(nss_path, channel_.get()); | 157 mojo::edk::PlatformChannelPair channel_pair; |
| 158 child_process_ = LaunchNSSDecrypterChildProcess( | |
| 159 nss_path, channel_pair.PassClientHandle(), mojo_channel_token); | |
| 160 if (child_process_.IsValid()) { | |
| 161 mojo::edk::ChildProcessLaunched(child_process_.Handle(), | |
| 162 channel_pair.PassServerHandle(), | |
| 163 mojo_child_token); | |
| 164 } else { | |
| 165 mojo::edk::ChildProcessLaunchFailed(mojo_child_token); | |
| 166 } | |
| 167 | |
| 150 return child_process_.IsValid(); | 168 return child_process_.IsValid(); |
| 151 } | 169 } |
| 152 | 170 |
| 153 FFUnitTestDecryptorProxy::~FFUnitTestDecryptorProxy() { | 171 FFUnitTestDecryptorProxy::~FFUnitTestDecryptorProxy() { |
| 154 listener_->QuitClient(); | 172 listener_->QuitClient(); |
| 155 channel_->Close(); | 173 channel_->Close(); |
| 156 | 174 |
| 157 if (child_process_.IsValid()) { | 175 if (child_process_.IsValid()) { |
| 158 int exit_code; | 176 int exit_code; |
| 159 child_process_.WaitForExitWithTimeout(base::TimeDelta::FromSeconds(5), | 177 child_process_.WaitForExitWithTimeout(base::TimeDelta::FromSeconds(5), |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 private: | 298 private: |
| 281 NSSDecryptor decryptor_; | 299 NSSDecryptor decryptor_; |
| 282 IPC::Sender* sender_; | 300 IPC::Sender* sender_; |
| 283 }; | 301 }; |
| 284 | 302 |
| 285 // Entry function in child process. | 303 // Entry function in child process. |
| 286 MULTIPROCESS_IPC_TEST_MAIN(NSSDecrypterChildProcess) { | 304 MULTIPROCESS_IPC_TEST_MAIN(NSSDecrypterChildProcess) { |
| 287 base::MessageLoopForIO main_message_loop; | 305 base::MessageLoopForIO main_message_loop; |
| 288 FFDecryptorClientChannelListener listener; | 306 FFDecryptorClientChannelListener listener; |
| 289 | 307 |
| 308 mojo::edk::SetParentPipeHandle( | |
| 309 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( | |
| 310 base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel)))); | |
| 311 mojo::ScopedMessagePipeHandle mojo_handle = | |
| 312 mojo::edk::CreateChildMessagePipe( | |
| 313 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 314 switches::kMojoChannelToken)); | |
| 315 | |
| 290 std::unique_ptr<IPC::Channel> channel = | 316 std::unique_ptr<IPC::Channel> channel = |
| 291 IPC::Channel::CreateClient(kTestChannelID, &listener); | 317 IPC::Channel::CreateClient(mojo_handle.release(), &listener); |
| 292 CHECK(channel->Connect()); | 318 CHECK(channel->Connect()); |
| 293 listener.SetSender(channel.get()); | 319 listener.SetSender(channel.get()); |
| 294 | 320 |
| 295 // run message loop | 321 // run message loop |
| 296 base::RunLoop().Run(); | 322 base::RunLoop().Run(); |
| 297 | 323 |
| 298 return 0; | 324 return 0; |
| 299 } | 325 } |
| OLD | NEW |