Chromium Code Reviews| Index: util/mac/process_reader_test.cc |
| diff --git a/util/mac/process_reader_test.cc b/util/mac/process_reader_test.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..83361b01b5dd4a6dcd8855e404dfa71264732afe |
| --- /dev/null |
| +++ b/util/mac/process_reader_test.cc |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2014 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#include "util/mac/process_reader.h" |
| + |
| +#include <string.h> |
| + |
| +#include <string> |
| + |
| +#include "base/posix/eintr_wrapper.h" |
| +#include "build/build_config.h" |
| +#include "gtest/gtest.h" |
| +#include "util/file/fd_io.h" |
| +#include "util/test/mac/mach_multiprocess.h" |
| +#include "util/test/errors.h" |
| + |
| +namespace { |
| + |
| +using namespace crashpad; |
| +using namespace crashpad::test; |
| + |
| +TEST(ProcessReader, Self) { |
| + ProcessReader process_reader; |
| + ASSERT_TRUE(process_reader.Initialize(mach_task_self())); |
| + |
| +#if !defined(ARCH_CPU_64_BITS) |
| + EXPECT_FALSE(process_reader.Is64Bit()); |
| +#else |
| + EXPECT_TRUE(process_reader.Is64Bit()); |
| +#endif |
| + |
| + EXPECT_EQ(getpid(), process_reader.ProcessID()); |
| + EXPECT_EQ(getppid(), process_reader.ParentProcessID()); |
| + |
| + const char kTestMemory[] = "Some test memory"; |
| + char buffer[arraysize(kTestMemory)]; |
| + ASSERT_TRUE(process_reader.Memory()->Read( |
| + reinterpret_cast<mach_vm_address_t>(kTestMemory), |
| + sizeof(kTestMemory), |
| + &buffer)); |
| + EXPECT_STREQ(kTestMemory, buffer); |
| +} |
| + |
| +class ProcessReaderChild final : public MachMultiprocess { |
| + public: |
| + ProcessReaderChild() : MachMultiprocess() {} |
| + |
| + ~ProcessReaderChild() {} |
| + |
| + protected: |
| + void Parent() override { |
| + ProcessReader process_reader; |
| + ASSERT_TRUE(process_reader.Initialize(ChildTask())); |
| + |
| +#if !defined(ARCH_CPU_64_BITS) |
| + EXPECT_FALSE(process_reader.Is64Bit()); |
| +#else |
| + EXPECT_TRUE(process_reader.Is64Bit()); |
| +#endif |
| + |
| + EXPECT_EQ(getpid(), process_reader.ParentProcessID()); |
| + |
| + int fd = PipeFD(); |
| + |
| + pid_t pid; |
| + 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:
|
| + ASSERT_EQ(static_cast<ssize_t>(sizeof(pid)), rv) << ErrnoMessage("read"); |
| + |
| + EXPECT_EQ(pid, process_reader.ProcessID()); |
| + |
| + mach_vm_address_t address; |
| + rv = ReadFD(fd, &address, sizeof(address)); |
| + ASSERT_EQ(static_cast<ssize_t>(sizeof(address)), rv) |
| + << ErrnoMessage("read"); |
| + |
| + std::string read_string; |
| + ASSERT_TRUE(process_reader.Memory()->ReadCString(address, &read_string)); |
| + EXPECT_EQ("Read me from another process", read_string); |
| + } |
| + |
| + void Child() override { |
| + int fd = PipeFD(); |
| + |
| + pid_t pid = getpid(); |
| + ssize_t rv = WriteFD(fd, &pid, sizeof(pid)); |
| + ASSERT_EQ(static_cast<ssize_t>(sizeof(pid)), rv) << ErrnoMessage("write"); |
| + |
| + 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
|
| + mach_vm_address_t address = |
| + reinterpret_cast<mach_vm_address_t>(kTestMemory); |
| + rv = WriteFD(fd, &address, sizeof(address)); |
| + ASSERT_EQ(static_cast<ssize_t>(sizeof(address)), rv) |
| + << ErrnoMessage("write"); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ProcessReaderChild); |
| +}; |
| + |
| +TEST(ProcessReader, Child) { |
| + ProcessReaderChild process_reader; |
| + process_reader.Run(); |
| +} |
|
Robert Sesek
2014/08/21 15:26:54
No test for threads of another process?
|
| + |
| +} // namespace |