Index: base/debug/proc_maps_unittest.cc |
diff --git a/base/debug/proc_maps_unittest.cc b/base/debug/proc_maps_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a67be6bc9633565d9a77ed80c47951d87d8ccf34 |
--- /dev/null |
+++ b/base/debug/proc_maps_unittest.cc |
@@ -0,0 +1,229 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/debug/proc_maps.h" |
+#include "base/logging.h" |
+#include "base/strings/stringprintf.h" |
+#include "build/build_config.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace base { |
+namespace debug { |
Mark Mentovai
2013/07/02 19:36:57
Ditto on the inner nested namespace.
|
+ |
+#if defined(OS_LINUX) || defined(OS_ANDROID) |
+ |
+TEST(ProcMapsTest, NoSpaces) { |
+ static const char kNoSpaces[] = |
+ "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat\n"; |
+ |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_TRUE(ParseProcMaps(kNoSpaces, ®ions)); |
+ ASSERT_EQ(1u, regions.size()); |
+ |
+ EXPECT_EQ(0x00400000u, regions[0].start); |
+ EXPECT_EQ(0x0040b000u, regions[0].end); |
+ EXPECT_EQ(0x00002200u, regions[0].offset); |
+ EXPECT_EQ(FilePath("/bin/cat").value(), regions[0].path.value()); |
+} |
+ |
+TEST(ProcMapsTest, Spaces) { |
+ static const char kSpaces[] = |
+ "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/space cat\n"; |
+ |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_TRUE(ParseProcMaps(kSpaces, ®ions)); |
+ ASSERT_EQ(1u, regions.size()); |
+ |
+ EXPECT_EQ(0x00400000u, regions[0].start); |
+ EXPECT_EQ(0x0040b000u, regions[0].end); |
+ EXPECT_EQ(0x00002200u, regions[0].offset); |
+ EXPECT_EQ(FilePath("/bin/space cat").value(), regions[0].path.value()); |
+} |
+ |
+TEST(ProcMapsTest, NoPath) { |
+ static const char kNoPath[] = |
+ "00400000-0040b000 rw-p 00000000 00:00 0 \n"; |
+ |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_TRUE(ParseProcMaps(kNoPath, ®ions)); |
+ ASSERT_EQ(1u, regions.size()); |
+ |
+ EXPECT_EQ(0x00400000u, regions[0].start); |
+ EXPECT_EQ(0x0040b000u, regions[0].end); |
+ EXPECT_EQ(0x00000000u, regions[0].offset); |
+ EXPECT_EQ(FilePath().value(), regions[0].path.value()); |
+} |
+ |
+TEST(ProcMapsTest, Heap) { |
+ static const char kHeap[] = |
+ "022ac000-022cd000 rw-p 00000000 00:00 0 [heap]\n"; |
+ |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_TRUE(ParseProcMaps(kHeap, ®ions)); |
+ ASSERT_EQ(1u, regions.size()); |
+ |
+ EXPECT_EQ(0x022ac000u, regions[0].start); |
+ EXPECT_EQ(0x022cd000u, regions[0].end); |
+ EXPECT_EQ(0x00000000u, regions[0].offset); |
+ EXPECT_EQ(FilePath("[heap]").value(), regions[0].path.value()); |
+} |
+ |
+#if defined(ARCH_CPU_32_BITS) |
+TEST(ProcMapsTest, Stack32) { |
+ static const char kStack[] = |
+ "beb04000-beb25000 rw-p 00000000 00:00 0 [stack]\n"; |
+ |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_TRUE(ParseProcMaps(kStack, ®ions)); |
+ ASSERT_EQ(1u, regions.size()); |
+ |
+ EXPECT_EQ(0xbeb04000u, regions[0].start); |
+ EXPECT_EQ(0xbeb25000u, regions[0].end); |
+ EXPECT_EQ(0x00000000u, regions[0].offset); |
+ EXPECT_EQ(FilePath("[stack]").value(), regions[0].path.value()); |
+} |
+#endif |
+ |
+#if defined(ARCH_CPU_64_BITS) |
Mark Mentovai
2013/07/02 19:36:57
#elif makes it clear that this is the same test bu
scherkus (not reviewing)
2013/07/02 21:43:01
Done.
|
+TEST(ProcMapsTest, Stack64) { |
+ static const char kStack[] = |
+ "7fff69c5b000-7fff69c7d000 rw-p 00000000 00:00 0 [stack]\n"; |
+ |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_TRUE(ParseProcMaps(kStack, ®ions)); |
+ ASSERT_EQ(1u, regions.size()); |
+ |
+ EXPECT_EQ(0x7fff69c5b000u, regions[0].start); |
+ EXPECT_EQ(0x7fff69c7d000u, regions[0].end); |
+ EXPECT_EQ(0x00000000u, regions[0].offset); |
+ EXPECT_EQ(FilePath("[stack]").value(), regions[0].path.value()); |
+} |
+#endif |
+ |
+TEST(ProcMapsTest, Multiple) { |
+ static const char kMultiple[] = |
+ "00400000-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n" |
+ "0060a000-0060b000 r--p 0000a000 fc:00 794418 /bin/cat\n" |
+ "0060b000-0060c000 rw-p 0000b000 fc:00 794418 /bin/cat\n"; |
+ |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_TRUE(ParseProcMaps(kMultiple, ®ions)); |
+ ASSERT_EQ(3u, regions.size()); |
+ |
+ EXPECT_EQ(0x00400000u, regions[0].start); |
+ EXPECT_EQ(0x0040b000u, regions[0].end); |
+ EXPECT_EQ(0x00000000u, regions[0].offset); |
+ EXPECT_EQ(FilePath("/bin/cat").value(), regions[0].path.value()); |
+ |
+ EXPECT_EQ(0x0060a000u, regions[1].start); |
+ EXPECT_EQ(0x0060b000u, regions[1].end); |
+ EXPECT_EQ(0x0000a000u, regions[1].offset); |
+ EXPECT_EQ(FilePath("/bin/cat").value(), regions[1].path.value()); |
+ |
+ EXPECT_EQ(0x0060b000u, regions[2].start); |
+ EXPECT_EQ(0x0060c000u, regions[2].end); |
+ EXPECT_EQ(0x0000b000u, regions[2].offset); |
+ EXPECT_EQ(FilePath("/bin/cat").value(), regions[2].path.value()); |
+} |
+ |
+TEST(ProcMapsTest, Permissions) { |
+ static struct { |
+ const char* input; |
+ int permissions; |
Mark Mentovai
2013/07/02 19:36:57
You can call this a uint8 to match what it is in t
scherkus (not reviewing)
2013/07/02 21:43:01
Done.
|
+ } kTestCases[] = { |
+ {"00400000-0040b000 ---s 00000000 fc:00 794418 /bin/cat\n", 0}, |
+ {"00400000-0040b000 r--s 00000000 fc:00 794418 /bin/cat\n", |
+ MappedMemoryRegion::READ}, |
+ {"00400000-0040b000 -w-s 00000000 fc:00 794418 /bin/cat\n", |
+ MappedMemoryRegion::WRITE}, |
+ {"00400000-0040b000 --xs 00000000 fc:00 794418 /bin/cat\n", |
+ MappedMemoryRegion::EXECUTE}, |
+ {"00400000-0040b000 rwxs 00000000 fc:00 794418 /bin/cat\n", |
+ MappedMemoryRegion::READ | MappedMemoryRegion::WRITE | |
+ MappedMemoryRegion::EXECUTE}, |
+ {"00400000-0040b000 ---p 00000000 fc:00 794418 /bin/cat\n", |
+ MappedMemoryRegion::PRIVATE}, |
+ {"00400000-0040b000 r--p 00000000 fc:00 794418 /bin/cat\n", |
+ MappedMemoryRegion::READ | MappedMemoryRegion::PRIVATE}, |
+ {"00400000-0040b000 -w-p 00000000 fc:00 794418 /bin/cat\n", |
+ MappedMemoryRegion::WRITE | MappedMemoryRegion::PRIVATE}, |
+ {"00400000-0040b000 --xp 00000000 fc:00 794418 /bin/cat\n", |
+ MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE}, |
+ {"00400000-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n", |
+ MappedMemoryRegion::READ | MappedMemoryRegion::WRITE | |
+ MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE}, |
+ { NULL, 0 }, |
+ }; |
+ |
+ for (size_t i = 0; kTestCases[i].input != NULL; ++i) { |
Mark Mentovai
2013/07/02 19:36:57
Why not just use arraysize (like you did for the l
scherkus (not reviewing)
2013/07/02 21:43:01
arraysize() doesn't work with anonymous struct typ
Mark Mentovai
2013/07/02 22:21:35
scherkus wrote:
scherkus (not reviewing)
2013/07/02 23:07:47
The More You Know(tm)
Done. (I'm an arraysize fan
|
+ SCOPED_TRACE( |
+ base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i].input)); |
+ |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_TRUE(ParseProcMaps(kTestCases[i].input, ®ions)); |
Mark Mentovai
2013/07/02 19:36:57
On one hand, I think these should be EXPECT, becau
scherkus (not reviewing)
2013/07/02 21:43:01
agreed -- changed to EXPECT + added check to verif
|
+ ASSERT_EQ(1u, regions.size()); |
+ EXPECT_EQ(kTestCases[i].permissions, regions[0].permissions); |
+ } |
+} |
+ |
+TEST(ProcMapsTest, ReadProcMaps) { |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_TRUE(ParseProcMaps(ReadProcMaps(), ®ions)); |
Mark Mentovai
2013/07/02 19:36:57
I’d break this into two checked operations so that
scherkus (not reviewing)
2013/07/02 21:43:01
Done.
|
+ ASSERT_FALSE(regions.empty()); |
+ |
+ // We should be find some mention of base_unittests in one of the paths. |
+ bool found_base_unittests = false; |
+ for (size_t i = 0; i < regions.size(); ++i) { |
+ if (regions[i].path.value().find("base_unittests") != std::string::npos) { |
Mark Mentovai
2013/07/02 19:36:57
There’s got to be a better way to figure out your
scherkus (not reviewing)
2013/07/02 21:43:01
hrmm... while this will work for linux it's tricki
Mark Mentovai
2013/07/02 22:21:35
scherkus wrote:
scherkus (not reviewing)
2013/07/02 23:07:47
Looks like PathService w/ FILE_EXE does a readlink
|
+ found_base_unittests = true; |
+ break; |
+ } |
+ } |
+ |
+ EXPECT_TRUE(found_base_unittests); |
+} |
+ |
+TEST(ProcMapsTest, MissingFields) { |
+ static const char* kTestCases[] = { |
+ "00400000\n", // Missing end + beyond. |
+ "00400000-0040b000\n", // Missing perms + beyond. |
+ "00400000-0040b000 r-xp\n", // Missing offset + beyond. |
+ "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond. |
+ "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond. |
+ "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms. |
+ "00400000-0040b000 r-xp fc:00 794418 /bin/cat\n", // Missing offset. |
+ "00400000-0040b000 r-xp 00000000 fc:00 /bin/cat\n", // Missing inode. |
+ "00400000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing end. |
+ "-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing start. |
+ "00400000-0040b000 r-xp 00000000 794418 /bin/cat\n", // Missing device. |
+ }; |
+ |
+ for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
+ SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i])); |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); |
Mark Mentovai
2013/07/02 19:36:57
This one should be EXPECT, and so should line 220.
scherkus (not reviewing)
2013/07/02 21:43:01
Done.
|
+ } |
+} |
+ |
+TEST(ProcMapsTest, InvalidInput) { |
+ static const char* kTestCases[] = { |
+ "thisisal-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n", |
+ "0040000d-linvalid rwxp 00000000 fc:00 794418 /bin/cat\n", |
+ "00400000-0040b000 inpu 00000000 fc:00 794418 /bin/cat\n", |
+ "00400000-0040b000 rwxp tforproc fc:00 794418 /bin/cat\n", |
+ "00400000-0040b000 rwxp 00000000 ma:ps 794418 /bin/cat\n", |
+ "00400000-0040b000 rwxp 00000000 fc:00 parse! /bin/cat\n", |
+ }; |
+ |
+ for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
+ SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i])); |
+ std::vector<MappedMemoryRegion> regions; |
+ ASSERT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); |
+ } |
+} |
+ |
+#endif |
+ |
+} // namespace debug |
+} // namespace base |
Mark Mentovai
2013/07/02 19:36:57
This is a really good and comprehensive test, nice
|