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 #include "util/win/command_line.h" | |
18 | 19 |
19 namespace crashpad { | 20 namespace crashpad { |
20 namespace test { | 21 namespace test { |
21 | 22 |
22 ChildLauncher::ChildLauncher(const std::wstring& executable, | 23 ChildLauncher::ChildLauncher(const std::wstring& executable, |
23 const std::wstring& command_line) | 24 const std::wstring& command_line) |
24 : executable_(executable), | 25 : executable_(executable), |
25 command_line_(command_line), | 26 command_line_(command_line), |
26 process_handle_(), | 27 process_handle_(), |
27 main_thread_handle_(), | 28 main_thread_handle_(), |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 | 63 |
63 STARTUPINFO startup_info = {0}; | 64 STARTUPINFO startup_info = {0}; |
64 startup_info.cb = sizeof(startup_info); | 65 startup_info.cb = sizeof(startup_info); |
65 startup_info.hStdInput = read_handle.get(); | 66 startup_info.hStdInput = read_handle.get(); |
66 startup_info.hStdOutput = write_handle.get(); | 67 startup_info.hStdOutput = write_handle.get(); |
67 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); | 68 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
68 startup_info.dwFlags = STARTF_USESTDHANDLES; | 69 startup_info.dwFlags = STARTF_USESTDHANDLES; |
69 PROCESS_INFORMATION process_information; | 70 PROCESS_INFORMATION process_information; |
70 std::wstring command_line; | 71 std::wstring command_line; |
71 AppendCommandLineArgument(executable_, &command_line); | 72 AppendCommandLineArgument(executable_, &command_line); |
72 command_line += L" "; | 73 command_line += L" "; |
scottmg
2015/10/30 23:16:31
Hmm, this is unfortunate. Oh well.
| |
73 command_line += command_line_; | 74 command_line += command_line_; |
74 ASSERT_TRUE(CreateProcess(executable_.c_str(), | 75 ASSERT_TRUE(CreateProcess(executable_.c_str(), |
75 &command_line[0], | 76 &command_line[0], |
76 nullptr, | 77 nullptr, |
77 nullptr, | 78 nullptr, |
78 true, | 79 true, |
79 0, | 80 0, |
80 nullptr, | 81 nullptr, |
81 nullptr, | 82 nullptr, |
82 &startup_info, | 83 &startup_info, |
83 &process_information)); | 84 &process_information)); |
84 // Take ownership of the two process handles returned. | 85 // Take ownership of the two process handles returned. |
85 main_thread_handle_.reset(process_information.hThread); | 86 main_thread_handle_.reset(process_information.hThread); |
86 process_handle_.reset(process_information.hProcess); | 87 process_handle_.reset(process_information.hProcess); |
87 } | 88 } |
88 | 89 |
89 DWORD ChildLauncher::WaitForExit() { | 90 DWORD ChildLauncher::WaitForExit() { |
90 EXPECT_TRUE(process_handle_.is_valid()); | 91 EXPECT_TRUE(process_handle_.is_valid()); |
91 EXPECT_EQ(WAIT_OBJECT_0, | 92 EXPECT_EQ(WAIT_OBJECT_0, |
92 WaitForSingleObject(process_handle_.get(), INFINITE)); | 93 WaitForSingleObject(process_handle_.get(), INFINITE)); |
93 DWORD exit_code = 0; | 94 DWORD exit_code = 0; |
94 EXPECT_TRUE(GetExitCodeProcess(process_handle_.get(), &exit_code)); | 95 EXPECT_TRUE(GetExitCodeProcess(process_handle_.get(), &exit_code)); |
95 process_handle_.reset(); | 96 process_handle_.reset(); |
96 return exit_code; | 97 return exit_code; |
97 } | 98 } |
98 | 99 |
99 // Ref: http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/ everyone-quotes-arguments-the-wrong-way.aspx | |
100 void AppendCommandLineArgument(const std::wstring& argument, | |
101 std::wstring* command_line) { | |
102 // Don't bother quoting if unnecessary. | |
103 if (!argument.empty() && | |
104 argument.find_first_of(L" \t\n\v\"") == std::wstring::npos) { | |
105 command_line->append(argument); | |
106 } else { | |
107 command_line->push_back(L'"'); | |
108 for (std::wstring::const_iterator i = argument.begin();; ++i) { | |
109 size_t backslash_count = 0; | |
110 while (i != argument.end() && *i == L'\\') { | |
111 ++i; | |
112 ++backslash_count; | |
113 } | |
114 if (i == argument.end()) { | |
115 // Escape all backslashes, but let the terminating double quotation mark | |
116 // we add below be interpreted as a metacharacter. | |
117 command_line->append(backslash_count * 2, L'\\'); | |
118 break; | |
119 } else if (*i == L'"') { | |
120 // Escape all backslashes and the following double quotation mark. | |
121 command_line->append(backslash_count * 2 + 1, L'\\'); | |
122 command_line->push_back(*i); | |
123 } else { | |
124 // Backslashes aren't special here. | |
125 command_line->append(backslash_count, L'\\'); | |
126 command_line->push_back(*i); | |
127 } | |
128 } | |
129 command_line->push_back(L'"'); | |
130 } | |
131 } | |
132 | |
133 } // namespace test | 100 } // namespace test |
134 } // namespace crashpad | 101 } // namespace crashpad |
OLD | NEW |