Yavor Goulishev | 9c08e84 | 2020-04-29 14:03:33 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Crashpad Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (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 |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "test/multiprocess_exec.h" |
| 16 | |
| 17 | #include <map> |
| 18 | |
| 19 | #include "base/logging.h" |
| 20 | #include "base/strings/utf_string_conversions.h" |
| 21 | #include "test/main_arguments.h" |
| 22 | #include "util/stdlib/map_insert.h" |
| 23 | #include "test/test_paths.h" |
| 24 | |
| 25 | namespace crashpad { |
| 26 | namespace test { |
| 27 | |
| 28 | namespace internal { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | std::map<std::string, int(*)()>* GetMultiprocessFunctionMap() { |
| 33 | static auto* map = new std::map<std::string, int(*)()>(); |
| 34 | return map; |
| 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
| 39 | AppendMultiprocessTest::AppendMultiprocessTest(const std::string& test_name, |
| 40 | int (*main_function_pointer)()) { |
| 41 | CHECK(MapInsertOrReplace( |
| 42 | GetMultiprocessFunctionMap(), test_name, main_function_pointer, nullptr)) |
| 43 | << test_name << " already registered"; |
| 44 | } |
| 45 | |
| 46 | int CheckedInvokeMultiprocessChild(const std::string& test_name) { |
| 47 | const auto* functions = internal::GetMultiprocessFunctionMap(); |
| 48 | auto it = functions->find(test_name); |
| 49 | CHECK(it != functions->end()) |
| 50 | << "child main " << test_name << " not registered"; |
| 51 | return (*it->second)(); |
| 52 | } |
| 53 | |
| 54 | } // namespace internal |
| 55 | |
| 56 | void MultiprocessExec::SetChildTestMainFunction( |
| 57 | const std::string& function_name) { |
| 58 | std::vector<std::string> rest(GetMainArguments().begin() + 1, |
| 59 | GetMainArguments().end()); |
| 60 | rest.push_back(internal::kChildTestFunction + function_name); |
| 61 | |
| 62 | #if defined(OS_WIN) |
| 63 | // Instead of using argv[0] on Windows, use the actual binary name. This is |
| 64 | // necessary because if originally the test isn't run with ".exe" on the |
| 65 | // command line, then argv[0] also won't include ".exe". This argument is used |
| 66 | // as the lpApplicationName argument to CreateProcess(), and so will fail. |
| 67 | SetChildCommand(TestPaths::Executable(), &rest); |
| 68 | #else |
| 69 | SetChildCommand(base::FilePath(GetMainArguments()[0]), &rest); |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | } // namespace test |
| 74 | } // namespace crashpad |