| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef CRASHPAD_UTIL_TEST_MAC_MACH_MULTIPROCESS_H_ | 15 #ifndef CRASHPAD_UTIL_TEST_MULTIPROCESS_H_ |
| 16 #define CRASHPAD_UTIL_TEST_MAC_MACH_MULTIPROCESS_H_ | 16 #define CRASHPAD_UTIL_TEST_MULTIPROCESS_H_ |
| 17 | 17 |
| 18 #include <mach/mach.h> | |
| 19 #include <unistd.h> | 18 #include <unistd.h> |
| 20 | 19 |
| 21 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
| 22 | 21 |
| 23 namespace crashpad { | 22 namespace crashpad { |
| 24 namespace test { | 23 namespace test { |
| 25 | 24 |
| 26 namespace internal { | 25 namespace internal { |
| 27 struct MachMultiprocessInfo; | 26 struct MultiprocessInfo; |
| 28 } // namespace internal | 27 }; |
| 29 | 28 |
| 30 //! \brief Manages a Mach-aware multiprocess test. | 29 //! \brief Manages a multiprocess test. |
| 31 //! | 30 //! |
| 32 //! These tests are `fork()`-based. The parent process has access to the child | 31 //! These tests are `fork()`-based. The parent and child processes are able to |
| 33 //! process’ task port. The parent and child processes are able to communicate | 32 //! communicate via a pair of POSIX pipes. |
| 34 //! via Mach IPC, and via a pair of POSIX pipes. | |
| 35 //! | 33 //! |
| 36 //! Subclasses are expected to implement the parent and child by overriding the | 34 //! Subclasses are expected to implement the parent and child by overriding the |
| 37 //! appropriate methods. | 35 //! appropriate methods. |
| 38 class MachMultiprocess { | 36 class Multiprocess { |
| 39 public: | 37 public: |
| 40 MachMultiprocess(); | 38 Multiprocess(); |
| 41 | 39 |
| 42 //! \brief Runs the test. | 40 //! \brief Runs the test. |
| 43 //! | 41 //! |
| 44 //! This method establishes the proper testing environment and calls | 42 //! This method establishes the proper testing environment by calling |
| 45 //! RunParent() in the parent process and RunChild() in the child process. | 43 //! PreFork(), then calls `fork()`. In the parent process, it calls |
| 44 //! RunParent(), and in the child process, it calls RunChild(). |
| 46 //! | 45 //! |
| 47 //! This method uses gtest assertions to validate the testing environment. If | 46 //! This method uses gtest assertions to validate the testing environment. If |
| 48 //! the testing environment cannot be set up properly, it is possible that | 47 //! the testing environment cannot be set up properly, it is possible that |
| 49 //! Parent() or Child() will not be called. In the parent process, this method | 48 //! MultiprocessParent() or MultiprocessChild() will not be called. In the |
| 50 //! also waits for the child process to exit after Parent() returns, and | 49 //! parent process, this method also waits for the child process to exit after |
| 51 //! verifies that it exited cleanly with gtest assertions. | 50 //! MultiprocessParent() returns, and verifies that it exited cleanly without |
| 51 //! raising any gtest assertions. |
| 52 void Run(); | 52 void Run(); |
| 53 | 53 |
| 54 protected: | 54 protected: |
| 55 ~MachMultiprocess(); | 55 ~Multiprocess(); |
| 56 | 56 |
| 57 //! \brief The subclass-provided parent routine. | 57 //! \brief Establishes the proper testing environment prior to forking. |
| 58 //! | 58 //! |
| 59 //! Test failures should be reported via gtest: `EXPECT_*()`, `ASSERT_*()`, | 59 //! Subclasses that solely implement a test should not need to override this |
| 60 //! `FAIL()`, etc. | 60 //! method. Subclasses that do not implement tests but instead implement |
| 61 //! additional testing features on top of this class may override this method |
| 62 //! provided that they call the superclass’ implementation first as follows: |
| 61 //! | 63 //! |
| 62 //! This method must not use a `wait()`-family system call to wait for the | 64 //! \code |
| 63 //! child process to exit, as this is handled by RunParent(). | 65 //! virtual void PreFork() override { |
| 66 //! Multiprocess::PreFork(); |
| 67 //! if (testing::Test::HasFatalAssertions()) { |
| 68 //! return; |
| 69 //! } |
| 64 //! | 70 //! |
| 65 //! Subclasses must implement this method to define how the parent operates. | 71 //! // Place subclass-specific pre-fork code here. |
| 66 virtual void Parent() = 0; | 72 //! } |
| 67 | 73 //! \endcode |
| 68 //! \brief The subclass-provided child routine. | |
| 69 //! | 74 //! |
| 70 //! Test failures should be reported via gtest: `EXPECT_*()`, `ASSERT_*()`, | 75 //! Subclass implementations may signal failure by raising their own fatal |
| 71 //! `FAIL()`, etc. | 76 //! gtest assertions. |
| 72 //! | 77 virtual void PreFork(); |
| 73 //! Subclasses must implement this method to define how the child operates. | |
| 74 virtual void Child() = 0; | |
| 75 | 78 |
| 76 //! \brief Returns the child process’ process ID. | 79 //! \brief Returns the child process’ process ID. |
| 77 //! | 80 //! |
| 78 //! This method may only be called by the parent process. | 81 //! This method may only be called by the parent process. |
| 79 pid_t ChildPID() const; | 82 pid_t ChildPID() const; |
| 80 | 83 |
| 81 //! \brief Returns the read pipe’s file descriptor. | 84 //! \brief Returns the read pipe’s file descriptor. |
| 82 //! | 85 //! |
| 83 //! This method may be called by either the parent or the child process. | 86 //! This method may be called by either the parent or the child process. |
| 84 //! Anything written to the write pipe in the partner process will appear | 87 //! Anything written to the write pipe in the partner process will appear |
| 85 //! on the this file descriptor in this process. | 88 //! on the this file descriptor in this process. |
| 86 int ReadPipeFD() const; | 89 int ReadPipeFD() const; |
| 87 | 90 |
| 88 //! \brief Returns the write pipe’s file descriptor. | 91 //! \brief Returns the write pipe’s file descriptor. |
| 89 //! | 92 //! |
| 90 //! This method may be called by either the parent or the child process. | 93 //! This method may be called by either the parent or the child process. |
| 91 //! Anything written to this file descriptor in this process will appear on | 94 //! Anything written to this file descriptor in this process will appear on |
| 92 //! the read pipe in the partner process. | 95 //! the read pipe in the partner process. |
| 93 int WritePipeFD() const; | 96 int WritePipeFD() const; |
| 94 | 97 |
| 95 //! \brief Returns a receive right for the local port. | |
| 96 //! | |
| 97 //! This method may be called by either the parent or the child process. It | |
| 98 //! returns a receive right, with a corresponding send right held in the | |
| 99 //! opposing process. | |
| 100 mach_port_t LocalPort() const; | |
| 101 | |
| 102 //! \brief Returns a send right for the remote port. | |
| 103 //! | |
| 104 //! This method may be called by either the parent or the child process. It | |
| 105 //! returns a send right, with the corresponding receive right held in the | |
| 106 //! opposing process. | |
| 107 mach_port_t RemotePort() const; | |
| 108 | |
| 109 //! \brief Returns a send right for the child’s task port. | |
| 110 //! | |
| 111 //! This method may only be called by the parent process. | |
| 112 mach_port_t ChildTask() const; | |
| 113 | |
| 114 private: | 98 private: |
| 115 //! \brief Runs the parent side of the test. | 99 //! \brief Runs the parent side of the test. |
| 116 //! | 100 //! |
| 117 //! This method establishes the parent’s environment, performs the handshake | 101 //! This method establishes the parent’s environment and calls |
| 118 //! with the child, calls Parent(), and waits for the child process to exit. | 102 //! MultiprocessParent(). |
| 119 //! Assuming that the environment can be set up correctly and the child exits | |
| 120 //! successfully, the test will pass. | |
| 121 void RunParent(); | 103 void RunParent(); |
| 122 | 104 |
| 123 //! \brief Runs the child side of the test. | 105 //! \brief Runs the child side of the test. |
| 124 //! | 106 //! |
| 125 //! This method establishes the child’s environment, performs the handshake | 107 //! This method establishes the child’s environment, calls |
| 126 //! with the parent, calls Child(), and exits cleanly. However, if any failure | 108 //! MultiprocessChild(), and exits cleanly. However, if any failure (via fatal |
| 127 //! (via fatal or nonfatal gtest assertion) is detected, the child will exit | 109 //! or nonfatal gtest assertion) is detected, the child will exit with a |
| 128 //! with a failure status. | 110 //! failure status. |
| 129 void RunChild(); | 111 void RunChild(); |
| 130 | 112 |
| 131 internal::MachMultiprocessInfo* info_; | 113 //! \brief The subclass-provided parent routine. |
| 114 //! |
| 115 //! Test failures should be reported via gtest: `EXPECT_*()`, `ASSERT_*()`, |
| 116 //! `FAIL()`, etc. |
| 117 //! |
| 118 //! This method must not use a `wait()`-family system call to wait for the |
| 119 //! child process to exit, as this is handled by this class. |
| 120 //! |
| 121 //! Subclasses must implement this method to define how the parent operates. |
| 122 virtual void MultiprocessParent() = 0; |
| 132 | 123 |
| 133 DISALLOW_COPY_AND_ASSIGN(MachMultiprocess); | 124 //! \brief The subclass-provided child routine. |
| 125 //! |
| 126 //! Test failures should be reported via gtest: `EXPECT_*()`, `ASSERT_*()`, |
| 127 //! `FAIL()`, etc. |
| 128 //! |
| 129 //! Subclasses must implement this method to define how the child operates. |
| 130 virtual void MultiprocessChild() = 0; |
| 131 |
| 132 internal::MultiprocessInfo* info_; |
| 133 |
| 134 DISALLOW_COPY_AND_ASSIGN(Multiprocess); |
| 134 }; | 135 }; |
| 135 | 136 |
| 136 } // namespace test | 137 } // namespace test |
| 137 } // namespace crashpad | 138 } // namespace crashpad |
| 138 | 139 |
| 139 #endif // CRASHPAD_UTIL_TEST_MAC_MACH_MULTIPROCESS_H_ | 140 #endif // CRASHPAD_UTIL_TEST_MULTIPROCESS_H_ |
| OLD | NEW |