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 #include "util/test/mac/mach_multiprocess.h" | 15 #include "util/test/mac/mach_multiprocess.h" |
16 | 16 |
| 17 #include <unistd.h> |
| 18 |
17 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
18 #include "gtest/gtest.h" | 20 #include "gtest/gtest.h" |
19 #include "util/file/fd_io.h" | 21 #include "util/file/fd_io.h" |
20 #include "util/test/errors.h" | 22 #include "util/test/errors.h" |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 using namespace crashpad; | 26 using namespace crashpad; |
25 using namespace crashpad::test; | 27 using namespace crashpad::test; |
26 | 28 |
27 class TestMachMultiprocess final : public MachMultiprocess { | 29 class TestMachMultiprocess final : public MachMultiprocess { |
28 public: | 30 public: |
29 TestMachMultiprocess() : MachMultiprocess() {} | 31 TestMachMultiprocess() : MachMultiprocess() {} |
30 | 32 |
31 ~TestMachMultiprocess() {} | 33 ~TestMachMultiprocess() {} |
32 | 34 |
33 protected: | 35 protected: |
34 // The base class will have already exercised the Mach ports for IPC and the | 36 // The base class will have already exercised the Mach ports for IPC and the |
35 // child task port. Just make sure that the pipe is set up correctly. | 37 // child task port. Just make sure that the pipe is set up correctly and that |
| 38 // ChildPID() works as expected. |
36 virtual void Parent() override { | 39 virtual void Parent() override { |
37 int fd = PipeFD(); | 40 int read_fd = ReadPipeFD(); |
38 | |
39 char c; | 41 char c; |
40 ssize_t rv = ReadFD(fd, &c, 1); | 42 ssize_t rv = ReadFD(read_fd, &c, 1); |
41 ASSERT_EQ(1, rv) << ErrnoMessage("read"); | 43 ASSERT_EQ(1, rv) << ErrnoMessage("read"); |
42 EXPECT_EQ('M', c); | 44 EXPECT_EQ('M', c); |
43 | 45 |
| 46 pid_t pid; |
| 47 rv = ReadFD(read_fd, &pid, sizeof(pid)); |
| 48 ASSERT_EQ(static_cast<ssize_t>(sizeof(pid)), rv) << ErrnoMessage("read"); |
| 49 EXPECT_EQ(pid, ChildPID()); |
| 50 |
| 51 int write_fd = WritePipeFD(); |
| 52 c = 'm'; |
| 53 rv = WriteFD(write_fd, &c, 1); |
| 54 ASSERT_EQ(1, rv) << ErrnoMessage("write"); |
| 55 |
44 // The child will close its end of the pipe and exit. Make sure that the | 56 // The child will close its end of the pipe and exit. Make sure that the |
45 // parent sees EOF. | 57 // parent sees EOF. |
46 rv = ReadFD(fd, &c, 1); | 58 rv = ReadFD(read_fd, &c, 1); |
47 ASSERT_EQ(0, rv) << ErrnoMessage("read"); | 59 ASSERT_EQ(0, rv) << ErrnoMessage("read"); |
48 } | 60 } |
49 | 61 |
50 virtual void Child() override { | 62 virtual void Child() override { |
51 int fd = PipeFD(); | 63 int write_fd = WritePipeFD(); |
52 | 64 |
53 char c = 'M'; | 65 char c = 'M'; |
54 ssize_t rv = WriteFD(fd, &c, 1); | 66 ssize_t rv = WriteFD(write_fd, &c, 1); |
55 ASSERT_EQ(1, rv) << ErrnoMessage("write"); | 67 ASSERT_EQ(1, rv) << ErrnoMessage("write"); |
| 68 |
| 69 pid_t pid = getpid(); |
| 70 rv = WriteFD(write_fd, &pid, sizeof(pid)); |
| 71 ASSERT_EQ(static_cast<ssize_t>(sizeof(pid)), rv) << ErrnoMessage("write"); |
| 72 |
| 73 int read_fd = ReadPipeFD(); |
| 74 rv = ReadFD(read_fd, &c, 1); |
| 75 ASSERT_EQ(1, rv) << ErrnoMessage("read"); |
| 76 EXPECT_EQ('m', c); |
56 } | 77 } |
57 | 78 |
58 private: | 79 private: |
59 DISALLOW_COPY_AND_ASSIGN(TestMachMultiprocess); | 80 DISALLOW_COPY_AND_ASSIGN(TestMachMultiprocess); |
60 }; | 81 }; |
61 | 82 |
62 TEST(MachMultiprocess, MachMultiprocess) { | 83 TEST(MachMultiprocess, MachMultiprocess) { |
63 TestMachMultiprocess mach_multiprocess; | 84 TestMachMultiprocess mach_multiprocess; |
64 mach_multiprocess.Run(); | 85 mach_multiprocess.Run(); |
65 } | 86 } |
66 | 87 |
67 } // namespace | 88 } // namespace |
OLD | NEW |