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