OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #ifndef CRASHPAD_UTIL_TEST_MAC_MACH_MULTIPROCESS_H_ |
| 16 #define CRASHPAD_UTIL_TEST_MAC_MACH_MULTIPROCESS_H_ |
| 17 |
| 18 #include <mach/mach.h> |
| 19 #include <unistd.h> |
| 20 |
| 21 #include "base/basictypes.h" |
| 22 |
| 23 namespace crashpad { |
| 24 namespace test { |
| 25 |
| 26 //! \brief Manages a Mach-aware multiprocess test. |
| 27 //! |
| 28 //! These tests are `fork()`-based. The parent process has access to the child |
| 29 //! process’ task port. The parent and child processes are able to communicate |
| 30 //! via Mach IPC, and the child process can also send messages to the parent |
| 31 //! process via a POSIX pipe. |
| 32 //! |
| 33 //! Subclasses are expected to implement the parent and child by overriding the |
| 34 //! appropriate methods. |
| 35 class MachMultiprocess { |
| 36 public: |
| 37 MachMultiprocess(); |
| 38 |
| 39 //! \brief Runs the test. |
| 40 //! |
| 41 //! This method establishes the proper testing environment and calls Parent() |
| 42 //! in the parent process and Child() in the child process. |
| 43 //! |
| 44 //! This method uses gtest assertions to validate the testing environment. If |
| 45 //! the testing environment cannot be set up properly, it is possible that |
| 46 //! Parent() or Child() will not be called. In the parent process, this method |
| 47 //! also waits for the child process to exit after Parent() returns, and |
| 48 //! verifies that it exited cleanly with gtest assertions. |
| 49 void Run(); |
| 50 |
| 51 protected: |
| 52 ~MachMultiprocess(); |
| 53 |
| 54 //! \brief The parent routine. |
| 55 //! |
| 56 //! Test failures should be reported via gtest: `EXPECT_*()`, `ASSERT_*()`, |
| 57 //! `FAIL()`, etc. |
| 58 //! |
| 59 //! This method must not use a `wait()`-family system call to wait for the |
| 60 //! child process to exit, as this is handled by Run(). |
| 61 //! |
| 62 //! Subclasses must implement this method to define how the parent operates. |
| 63 virtual void Parent() = 0; |
| 64 |
| 65 //! \brief The child routine. |
| 66 //! |
| 67 //! Test failures should be reported via gtest: `EXPECT_*()`, `ASSERT_*()`, |
| 68 //! `FAIL()`, etc. |
| 69 //! |
| 70 //! Subclasses must implement this method to define how the child operates. |
| 71 virtual void Child() = 0; |
| 72 |
| 73 //! \brief Returns the child process’ process ID. |
| 74 //! |
| 75 //! This method may only be called by the parent process. |
| 76 pid_t ChildPID() const; |
| 77 |
| 78 //! \brief Returns the pipe’s file descriptor. |
| 79 //! |
| 80 //! This method may be called by either the parent or the child process. In |
| 81 //! the parent process, the pipe is read-only, and in the child process, it is |
| 82 //! write-only. |
| 83 int PipeFD() const; |
| 84 |
| 85 //! \brief Returns a receive right for the local port. |
| 86 //! |
| 87 //! This method may be called by either the parent or the child process. It |
| 88 //! returns a receive right, with a corresponding send right held in the |
| 89 //! opposing process. |
| 90 mach_port_t LocalPort() const; |
| 91 |
| 92 //! \brief Returns a send right for the remote port. |
| 93 //! |
| 94 //! This method may be called by either the parent or the child process. It |
| 95 //! returns a send right, with the corresponding receive right held in the |
| 96 //! opposing process. |
| 97 mach_port_t RemotePort() const; |
| 98 |
| 99 //! \brief Returns a send right for the child’s task port. |
| 100 //! |
| 101 //! This method may only be called by the parent process. |
| 102 mach_port_t ChildTask() const; |
| 103 |
| 104 private: |
| 105 pid_t child_pid_; // valid only in parent |
| 106 int pipe_fd_; // read-only in parent, write-only in child |
| 107 mach_port_t local_port_; // receive right |
| 108 mach_port_t remote_port_; // send right |
| 109 mach_port_t child_task_; // valid only in parent |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(MachMultiprocess); |
| 112 }; |
| 113 |
| 114 } // namespace test |
| 115 } // namespace crashpad |
| 116 |
| 117 #endif // CRASHPAD_UTIL_TEST_MAC_MACH_MULTIPROCESS_H_ |
OLD | NEW |