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/multiprocess.h" | 15 #include "util/test/multiprocess.h" |
16 | 16 |
17 #include <stdlib.h> | 17 #include <stdlib.h> |
18 #include <sys/signal.h> | 18 #include <sys/signal.h> |
19 #include <unistd.h> | 19 #include <unistd.h> |
20 | 20 |
21 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
22 #include "gtest/gtest.h" | 22 #include "gtest/gtest.h" |
23 #include "util/file/fd_io.h" | 23 #include "util/file/fd_io.h" |
24 #include "util/test/errors.h" | |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 using namespace crashpad; | 27 using namespace crashpad; |
29 using namespace crashpad::test; | 28 using namespace crashpad::test; |
30 | 29 |
31 class TestMultiprocess final : public Multiprocess { | 30 class TestMultiprocess final : public Multiprocess { |
32 public: | 31 public: |
33 TestMultiprocess() : Multiprocess() {} | 32 TestMultiprocess() : Multiprocess() {} |
34 | 33 |
35 ~TestMultiprocess() {} | 34 ~TestMultiprocess() {} |
36 | 35 |
37 private: | 36 private: |
38 virtual void MultiprocessParent() override { | 37 virtual void MultiprocessParent() override { |
39 int read_fd = ReadPipeFD(); | 38 int read_fd = ReadPipeFD(); |
40 char c; | 39 char c; |
41 ssize_t rv = ReadFD(read_fd, &c, 1); | 40 CheckedReadFD(read_fd, &c, 1); |
42 ASSERT_EQ(1, rv) << ErrnoMessage("read"); | |
43 EXPECT_EQ('M', c); | 41 EXPECT_EQ('M', c); |
44 | 42 |
45 pid_t pid; | 43 pid_t pid; |
46 rv = ReadFD(read_fd, &pid, sizeof(pid)); | 44 CheckedReadFD(read_fd, &pid, sizeof(pid)); |
47 ASSERT_EQ(static_cast<ssize_t>(sizeof(pid)), rv) << ErrnoMessage("read"); | |
48 EXPECT_EQ(pid, ChildPID()); | 45 EXPECT_EQ(pid, ChildPID()); |
49 | 46 |
50 int write_fd = WritePipeFD(); | |
51 c = 'm'; | 47 c = 'm'; |
52 rv = WriteFD(write_fd, &c, 1); | 48 CheckedWriteFD(WritePipeFD(), &c, 1); |
53 ASSERT_EQ(1, rv) << ErrnoMessage("write"); | |
54 | 49 |
55 // The child will close its end of the pipe and exit. Make sure that the | 50 // The child will close its end of the pipe and exit. Make sure that the |
56 // parent sees EOF. | 51 // parent sees EOF. |
57 rv = ReadFD(read_fd, &c, 1); | 52 CheckedReadFDAtEOF(read_fd); |
58 ASSERT_EQ(0, rv) << ErrnoMessage("read"); | |
59 } | 53 } |
60 | 54 |
61 virtual void MultiprocessChild() override { | 55 virtual void MultiprocessChild() override { |
62 int write_fd = WritePipeFD(); | 56 int write_fd = WritePipeFD(); |
63 | 57 |
64 char c = 'M'; | 58 char c = 'M'; |
65 ssize_t rv = WriteFD(write_fd, &c, 1); | 59 CheckedWriteFD(write_fd, &c, 1); |
66 ASSERT_EQ(1, rv) << ErrnoMessage("write"); | |
67 | 60 |
68 pid_t pid = getpid(); | 61 pid_t pid = getpid(); |
69 rv = WriteFD(write_fd, &pid, sizeof(pid)); | 62 CheckedWriteFD(write_fd, &pid, sizeof(pid)); |
70 ASSERT_EQ(static_cast<ssize_t>(sizeof(pid)), rv) << ErrnoMessage("write"); | |
71 | 63 |
72 int read_fd = ReadPipeFD(); | 64 CheckedReadFD(ReadPipeFD(), &c, 1); |
73 rv = ReadFD(read_fd, &c, 1); | |
74 ASSERT_EQ(1, rv) << ErrnoMessage("read"); | |
75 EXPECT_EQ('m', c); | 65 EXPECT_EQ('m', c); |
76 } | 66 } |
77 | 67 |
78 DISALLOW_COPY_AND_ASSIGN(TestMultiprocess); | 68 DISALLOW_COPY_AND_ASSIGN(TestMultiprocess); |
79 }; | 69 }; |
80 | 70 |
81 TEST(Multiprocess, Multiprocess) { | 71 TEST(Multiprocess, Multiprocess) { |
82 TestMultiprocess multiprocess; | 72 TestMultiprocess multiprocess; |
83 multiprocess.Run(); | 73 multiprocess.Run(); |
84 } | 74 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 TestMultiprocessUnclean multiprocess(TestMultiprocessUnclean::kExit2); | 129 TestMultiprocessUnclean multiprocess(TestMultiprocessUnclean::kExit2); |
140 multiprocess.Run(); | 130 multiprocess.Run(); |
141 } | 131 } |
142 | 132 |
143 TEST(Multiprocess, MultiprocessAbortSignal) { | 133 TEST(Multiprocess, MultiprocessAbortSignal) { |
144 TestMultiprocessUnclean multiprocess(TestMultiprocessUnclean::kAbort); | 134 TestMultiprocessUnclean multiprocess(TestMultiprocessUnclean::kAbort); |
145 multiprocess.Run(); | 135 multiprocess.Run(); |
146 } | 136 } |
147 | 137 |
148 } // namespace | 138 } // namespace |
OLD | NEW |