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 #include "util/mac/process_reader.h" | |
16 | |
17 #include <string.h> | |
18 | |
19 #include <string> | |
20 | |
21 #include "base/posix/eintr_wrapper.h" | |
22 #include "build/build_config.h" | |
23 #include "gtest/gtest.h" | |
24 #include "util/file/fd_io.h" | |
25 #include "util/test/mac/mach_multiprocess.h" | |
26 #include "util/test/errors.h" | |
27 | |
28 namespace { | |
29 | |
30 using namespace crashpad; | |
31 using namespace crashpad::test; | |
32 | |
33 TEST(ProcessReader, Self) { | |
34 ProcessReader process_reader; | |
35 ASSERT_TRUE(process_reader.Initialize(mach_task_self())); | |
36 | |
37 #if !defined(ARCH_CPU_64_BITS) | |
38 EXPECT_FALSE(process_reader.Is64Bit()); | |
39 #else | |
40 EXPECT_TRUE(process_reader.Is64Bit()); | |
41 #endif | |
42 | |
43 EXPECT_EQ(getpid(), process_reader.ProcessID()); | |
44 EXPECT_EQ(getppid(), process_reader.ParentProcessID()); | |
45 | |
46 const char kTestMemory[] = "Some test memory"; | |
47 char buffer[arraysize(kTestMemory)]; | |
48 ASSERT_TRUE(process_reader.Memory()->Read( | |
49 reinterpret_cast<mach_vm_address_t>(kTestMemory), | |
50 sizeof(kTestMemory), | |
51 &buffer)); | |
52 EXPECT_STREQ(kTestMemory, buffer); | |
53 } | |
54 | |
55 class ProcessReaderChild final : public MachMultiprocess { | |
56 public: | |
57 ProcessReaderChild() : MachMultiprocess() {} | |
58 | |
59 ~ProcessReaderChild() {} | |
60 | |
61 protected: | |
62 void Parent() override { | |
63 ProcessReader process_reader; | |
64 ASSERT_TRUE(process_reader.Initialize(ChildTask())); | |
65 | |
66 #if !defined(ARCH_CPU_64_BITS) | |
67 EXPECT_FALSE(process_reader.Is64Bit()); | |
68 #else | |
69 EXPECT_TRUE(process_reader.Is64Bit()); | |
70 #endif | |
71 | |
72 EXPECT_EQ(getpid(), process_reader.ParentProcessID()); | |
73 | |
74 int fd = PipeFD(); | |
75 | |
76 pid_t pid; | |
77 ssize_t rv = ReadFD(fd, &pid, sizeof(pid)); | |
Robert Sesek
2014/08/21 15:26:54
Couldn't you rely on MachMultiprocess::ChildPid()?
Mark Mentovai
2014/08/21 21:40:52
rsesek wrote:
| |
78 ASSERT_EQ(static_cast<ssize_t>(sizeof(pid)), rv) << ErrnoMessage("read"); | |
79 | |
80 EXPECT_EQ(pid, process_reader.ProcessID()); | |
81 | |
82 mach_vm_address_t address; | |
83 rv = ReadFD(fd, &address, sizeof(address)); | |
84 ASSERT_EQ(static_cast<ssize_t>(sizeof(address)), rv) | |
85 << ErrnoMessage("read"); | |
86 | |
87 std::string read_string; | |
88 ASSERT_TRUE(process_reader.Memory()->ReadCString(address, &read_string)); | |
89 EXPECT_EQ("Read me from another process", read_string); | |
90 } | |
91 | |
92 void Child() override { | |
93 int fd = PipeFD(); | |
94 | |
95 pid_t pid = getpid(); | |
96 ssize_t rv = WriteFD(fd, &pid, sizeof(pid)); | |
97 ASSERT_EQ(static_cast<ssize_t>(sizeof(pid)), rv) << ErrnoMessage("write"); | |
98 | |
99 const char kTestMemory[] = "Read me from another process"; | |
Robert Sesek
2014/08/21 15:26:54
Make this a static member that can be used by both
| |
100 mach_vm_address_t address = | |
101 reinterpret_cast<mach_vm_address_t>(kTestMemory); | |
102 rv = WriteFD(fd, &address, sizeof(address)); | |
103 ASSERT_EQ(static_cast<ssize_t>(sizeof(address)), rv) | |
104 << ErrnoMessage("write"); | |
105 } | |
106 | |
107 private: | |
108 DISALLOW_COPY_AND_ASSIGN(ProcessReaderChild); | |
109 }; | |
110 | |
111 TEST(ProcessReader, Child) { | |
112 ProcessReaderChild process_reader; | |
113 process_reader.Run(); | |
114 } | |
Robert Sesek
2014/08/21 15:26:54
No test for threads of another process?
| |
115 | |
116 } // namespace | |
OLD | NEW |