| OLD | NEW |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "test/win/child_launcher.h" | 15 #include "test/win/child_launcher.h" |
| 16 | 16 |
| 17 #include "gtest/gtest.h" | 17 #include "gtest/gtest.h" |
| 18 | 18 |
| 19 namespace crashpad { | 19 namespace crashpad { |
| 20 namespace test { | 20 namespace test { |
| 21 | 21 |
| 22 ChildLauncher::ChildLauncher(const std::wstring& executable, | 22 ChildLauncher::ChildLauncher(const std::wstring& executable, |
| 23 const std::wstring& command_line) | 23 const std::wstring& command_line) |
| 24 : executable_(executable), | 24 : executable_(executable), |
| 25 command_line_(command_line), | 25 command_line_(command_line), |
| 26 process_handle_(), | 26 process_handle_(), |
| 27 main_thread_handle_(), | 27 main_thread_handle_(), |
| 28 stdout_read_handle_() { | 28 stdout_read_handle_(), |
| 29 stdin_write_handle_() { |
| 29 } | 30 } |
| 30 | 31 |
| 31 ChildLauncher::~ChildLauncher() { | 32 ChildLauncher::~ChildLauncher() { |
| 32 EXPECT_EQ(WAIT_OBJECT_0, | 33 if (process_handle_.is_valid()) |
| 33 WaitForSingleObject(process_handle_.get(), INFINITE)); | 34 WaitForExit(); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void ChildLauncher::Start() { | 37 void ChildLauncher::Start() { |
| 37 ASSERT_FALSE(process_handle_.is_valid()); | 38 ASSERT_FALSE(process_handle_.is_valid()); |
| 38 ASSERT_FALSE(main_thread_handle_.is_valid()); | 39 ASSERT_FALSE(main_thread_handle_.is_valid()); |
| 39 ASSERT_FALSE(stdout_read_handle_.is_valid()); | 40 ASSERT_FALSE(stdout_read_handle_.is_valid()); |
| 40 | 41 |
| 41 // Create a pipe for the stdout of the child. | 42 // Create pipes for the stdin/stdout of the child. |
| 42 SECURITY_ATTRIBUTES security_attributes = {0}; | 43 SECURITY_ATTRIBUTES security_attributes = {0}; |
| 43 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); | 44 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 44 security_attributes.bInheritHandle = true; | 45 security_attributes.bInheritHandle = true; |
| 46 |
| 45 HANDLE stdout_read; | 47 HANDLE stdout_read; |
| 46 HANDLE stdout_write; | 48 HANDLE stdout_write; |
| 47 ASSERT_TRUE(CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0)); | 49 ASSERT_TRUE(CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0)); |
| 48 stdout_read_handle_.reset(stdout_read); | 50 stdout_read_handle_.reset(stdout_read); |
| 49 ScopedFileHANDLE write_handle(stdout_write); | 51 ScopedFileHANDLE write_handle(stdout_write); |
| 50 ASSERT_TRUE( | 52 ASSERT_TRUE( |
| 51 SetHandleInformation(stdout_read_handle_.get(), HANDLE_FLAG_INHERIT, 0)); | 53 SetHandleInformation(stdout_read_handle_.get(), HANDLE_FLAG_INHERIT, 0)); |
| 52 | 54 |
| 55 HANDLE stdin_read; |
| 56 HANDLE stdin_write; |
| 57 ASSERT_TRUE(CreatePipe(&stdin_read, &stdin_write, &security_attributes, 0)); |
| 58 stdin_write_handle_.reset(stdin_write); |
| 59 ScopedFileHANDLE read_handle(stdin_read); |
| 60 ASSERT_TRUE( |
| 61 SetHandleInformation(stdin_write_handle_.get(), HANDLE_FLAG_INHERIT, 0)); |
| 62 |
| 53 STARTUPINFO startup_info = {0}; | 63 STARTUPINFO startup_info = {0}; |
| 54 startup_info.cb = sizeof(startup_info); | 64 startup_info.cb = sizeof(startup_info); |
| 55 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | 65 startup_info.hStdInput = read_handle.get(); |
| 56 startup_info.hStdOutput = write_handle.get(); | 66 startup_info.hStdOutput = write_handle.get(); |
| 57 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); | 67 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| 58 startup_info.dwFlags = STARTF_USESTDHANDLES; | 68 startup_info.dwFlags = STARTF_USESTDHANDLES; |
| 59 PROCESS_INFORMATION process_information; | 69 PROCESS_INFORMATION process_information; |
| 60 std::wstring command_line; | 70 std::wstring command_line; |
| 61 AppendCommandLineArgument(executable_, &command_line); | 71 AppendCommandLineArgument(executable_, &command_line); |
| 62 command_line += L" "; | 72 command_line += L" "; |
| 63 command_line += command_line_; | 73 command_line += command_line_; |
| 64 ASSERT_TRUE(CreateProcess(executable_.c_str(), | 74 ASSERT_TRUE(CreateProcess(executable_.c_str(), |
| 65 &command_line[0], | 75 &command_line[0], |
| 66 nullptr, | 76 nullptr, |
| 67 nullptr, | 77 nullptr, |
| 68 true, | 78 true, |
| 69 0, | 79 0, |
| 70 nullptr, | 80 nullptr, |
| 71 nullptr, | 81 nullptr, |
| 72 &startup_info, | 82 &startup_info, |
| 73 &process_information)); | 83 &process_information)); |
| 74 // Take ownership of the two process handles returned. | 84 // Take ownership of the two process handles returned. |
| 75 main_thread_handle_.reset(process_information.hThread); | 85 main_thread_handle_.reset(process_information.hThread); |
| 76 process_handle_.reset(process_information.hProcess); | 86 process_handle_.reset(process_information.hProcess); |
| 77 } | 87 } |
| 78 | 88 |
| 89 DWORD ChildLauncher::WaitForExit() { |
| 90 EXPECT_TRUE(process_handle_.is_valid()); |
| 91 EXPECT_EQ(WAIT_OBJECT_0, |
| 92 WaitForSingleObject(process_handle_.get(), INFINITE)); |
| 93 DWORD exit_code = 0; |
| 94 EXPECT_TRUE(GetExitCodeProcess(process_handle_.get(), &exit_code)); |
| 95 process_handle_.reset(); |
| 96 return exit_code; |
| 97 } |
| 98 |
| 79 // Ref: http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/
everyone-quotes-arguments-the-wrong-way.aspx | 99 // Ref: http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/
everyone-quotes-arguments-the-wrong-way.aspx |
| 80 void AppendCommandLineArgument(const std::wstring& argument, | 100 void AppendCommandLineArgument(const std::wstring& argument, |
| 81 std::wstring* command_line) { | 101 std::wstring* command_line) { |
| 82 // Don't bother quoting if unnecessary. | 102 // Don't bother quoting if unnecessary. |
| 83 if (!argument.empty() && | 103 if (!argument.empty() && |
| 84 argument.find_first_of(L" \t\n\v\"") == std::wstring::npos) { | 104 argument.find_first_of(L" \t\n\v\"") == std::wstring::npos) { |
| 85 command_line->append(argument); | 105 command_line->append(argument); |
| 86 } else { | 106 } else { |
| 87 command_line->push_back(L'"'); | 107 command_line->push_back(L'"'); |
| 88 for (std::wstring::const_iterator i = argument.begin();; ++i) { | 108 for (std::wstring::const_iterator i = argument.begin();; ++i) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 105 command_line->append(backslash_count, L'\\'); | 125 command_line->append(backslash_count, L'\\'); |
| 106 command_line->push_back(*i); | 126 command_line->push_back(*i); |
| 107 } | 127 } |
| 108 } | 128 } |
| 109 command_line->push_back(L'"'); | 129 command_line->push_back(L'"'); |
| 110 } | 130 } |
| 111 } | 131 } |
| 112 | 132 |
| 113 } // namespace test | 133 } // namespace test |
| 114 } // namespace crashpad | 134 } // namespace crashpad |
| OLD | NEW |