OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <inttypes.h> |
| 6 |
| 7 #include "base/debug/proc_maps.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" |
| 10 #include "build/build_config.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace base { |
| 14 namespace debug { |
| 15 |
| 16 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 17 |
| 18 TEST(ProcMapsTest, NoSpaces) { |
| 19 static const char kNoSpaces[] = |
| 20 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat\n"; |
| 21 |
| 22 std::vector<MappedMemoryRegion> regions; |
| 23 ASSERT_TRUE(ParseProcMaps(kNoSpaces, ®ions)); |
| 24 ASSERT_EQ(1u, regions.size()); |
| 25 |
| 26 EXPECT_EQ(0x00400000u, regions[0].start); |
| 27 EXPECT_EQ(0x0040b000u, regions[0].end); |
| 28 EXPECT_EQ(0x00002200u, regions[0].offset); |
| 29 EXPECT_EQ(FilePath("/bin/cat").value(), regions[0].path.value()); |
| 30 } |
| 31 |
| 32 TEST(ProcMapsTest, Spaces) { |
| 33 static const char kSpaces[] = |
| 34 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/space cat\n"; |
| 35 |
| 36 std::vector<MappedMemoryRegion> regions; |
| 37 ASSERT_TRUE(ParseProcMaps(kSpaces, ®ions)); |
| 38 ASSERT_EQ(1u, regions.size()); |
| 39 |
| 40 EXPECT_EQ(0x00400000u, regions[0].start); |
| 41 EXPECT_EQ(0x0040b000u, regions[0].end); |
| 42 EXPECT_EQ(0x00002200u, regions[0].offset); |
| 43 EXPECT_EQ(FilePath("/bin/space cat").value(), regions[0].path.value()); |
| 44 } |
| 45 |
| 46 TEST(ProcMapsTest, NoPath) { |
| 47 static const char kNoPath[] = |
| 48 "00400000-0040b000 rw-p 00000000 00:00 0 \n"; |
| 49 |
| 50 std::vector<MappedMemoryRegion> regions; |
| 51 ASSERT_TRUE(ParseProcMaps(kNoPath, ®ions)); |
| 52 ASSERT_EQ(1u, regions.size()); |
| 53 |
| 54 EXPECT_EQ(0x00400000u, regions[0].start); |
| 55 EXPECT_EQ(0x0040b000u, regions[0].end); |
| 56 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 57 EXPECT_EQ(FilePath().value(), regions[0].path.value()); |
| 58 } |
| 59 |
| 60 TEST(ProcMapsTest, Heap) { |
| 61 static const char kHeap[] = |
| 62 "022ac000-022cd000 rw-p 00000000 00:00 0 [heap]\n"; |
| 63 |
| 64 std::vector<MappedMemoryRegion> regions; |
| 65 ASSERT_TRUE(ParseProcMaps(kHeap, ®ions)); |
| 66 ASSERT_EQ(1u, regions.size()); |
| 67 |
| 68 EXPECT_EQ(0x022ac000u, regions[0].start); |
| 69 EXPECT_EQ(0x022cd000u, regions[0].end); |
| 70 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 71 EXPECT_EQ(FilePath("[heap]").value(), regions[0].path.value()); |
| 72 } |
| 73 |
| 74 #if defined(ARCH_CPU_32_BITS) |
| 75 TEST(ProcMapsTest, Stack32) { |
| 76 static const char kStack[] = |
| 77 "beb04000-beb25000 rw-p 00000000 00:00 0 [stack]\n"; |
| 78 |
| 79 std::vector<MappedMemoryRegion> regions; |
| 80 ASSERT_TRUE(ParseProcMaps(kStack, ®ions)); |
| 81 ASSERT_EQ(1u, regions.size()); |
| 82 |
| 83 EXPECT_EQ(0xbeb04000u, regions[0].start); |
| 84 EXPECT_EQ(0xbeb25000u, regions[0].end); |
| 85 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 86 EXPECT_EQ(FilePath("[stack]").value(), regions[0].path.value()); |
| 87 } |
| 88 #endif |
| 89 |
| 90 #if defined(ARCH_CPU_64_BITS) |
| 91 TEST(ProcMapsTest, Stack64) { |
| 92 static const char kStack[] = |
| 93 "7fff69c5b000-7fff69c7d000 rw-p 00000000 00:00 0 [stack]\n"; |
| 94 |
| 95 std::vector<MappedMemoryRegion> regions; |
| 96 ASSERT_TRUE(ParseProcMaps(kStack, ®ions)); |
| 97 ASSERT_EQ(1u, regions.size()); |
| 98 |
| 99 EXPECT_EQ(0x7fff69c5b000u, regions[0].start); |
| 100 EXPECT_EQ(0x7fff69c7d000u, regions[0].end); |
| 101 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 102 EXPECT_EQ(FilePath("[stack]").value(), regions[0].path.value()); |
| 103 } |
| 104 #endif |
| 105 |
| 106 TEST(ProcMapsTest, Multiple) { |
| 107 static const char kMultiple[] = |
| 108 "00400000-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n" |
| 109 "0060a000-0060b000 r--p 0000a000 fc:00 794418 /bin/cat\n" |
| 110 "0060b000-0060c000 rw-p 0000b000 fc:00 794418 /bin/cat\n"; |
| 111 |
| 112 std::vector<MappedMemoryRegion> regions; |
| 113 ASSERT_TRUE(ParseProcMaps(kMultiple, ®ions)); |
| 114 ASSERT_EQ(3u, regions.size()); |
| 115 |
| 116 EXPECT_EQ(0x00400000u, regions[0].start); |
| 117 EXPECT_EQ(0x0040b000u, regions[0].end); |
| 118 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 119 EXPECT_EQ(FilePath("/bin/cat").value(), regions[0].path.value()); |
| 120 |
| 121 EXPECT_EQ(0x0060a000u, regions[1].start); |
| 122 EXPECT_EQ(0x0060b000u, regions[1].end); |
| 123 EXPECT_EQ(0x0000a000u, regions[1].offset); |
| 124 EXPECT_EQ(FilePath("/bin/cat").value(), regions[1].path.value()); |
| 125 |
| 126 EXPECT_EQ(0x0060b000u, regions[2].start); |
| 127 EXPECT_EQ(0x0060c000u, regions[2].end); |
| 128 EXPECT_EQ(0x0000b000u, regions[2].offset); |
| 129 EXPECT_EQ(FilePath("/bin/cat").value(), regions[2].path.value()); |
| 130 } |
| 131 |
| 132 TEST(ProcMapsTest, Permissions) { |
| 133 static struct { |
| 134 const char* input; |
| 135 int permissions; |
| 136 } kTestCases[] = { |
| 137 {"00400000-0040b000 ---s 00000000 fc:00 794418 /bin/cat\n", 0}, |
| 138 {"00400000-0040b000 r--s 00000000 fc:00 794418 /bin/cat\n", |
| 139 MappedMemoryRegion::READ}, |
| 140 {"00400000-0040b000 -w-s 00000000 fc:00 794418 /bin/cat\n", |
| 141 MappedMemoryRegion::WRITE}, |
| 142 {"00400000-0040b000 --xs 00000000 fc:00 794418 /bin/cat\n", |
| 143 MappedMemoryRegion::EXECUTE}, |
| 144 {"00400000-0040b000 rwxs 00000000 fc:00 794418 /bin/cat\n", |
| 145 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE | |
| 146 MappedMemoryRegion::EXECUTE}, |
| 147 {"00400000-0040b000 ---p 00000000 fc:00 794418 /bin/cat\n", |
| 148 MappedMemoryRegion::PRIVATE}, |
| 149 {"00400000-0040b000 r--p 00000000 fc:00 794418 /bin/cat\n", |
| 150 MappedMemoryRegion::READ | MappedMemoryRegion::PRIVATE}, |
| 151 {"00400000-0040b000 -w-p 00000000 fc:00 794418 /bin/cat\n", |
| 152 MappedMemoryRegion::WRITE | MappedMemoryRegion::PRIVATE}, |
| 153 {"00400000-0040b000 --xp 00000000 fc:00 794418 /bin/cat\n", |
| 154 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE}, |
| 155 {"00400000-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n", |
| 156 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE | |
| 157 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE}, |
| 158 { NULL, 0 }, |
| 159 }; |
| 160 |
| 161 for (size_t i = 0; kTestCases[i].input != NULL; ++i) { |
| 162 SCOPED_TRACE( |
| 163 base::StringPrintf("kTestCases[%d] = %s", i, kTestCases[i].input)); |
| 164 |
| 165 std::vector<MappedMemoryRegion> regions; |
| 166 ASSERT_TRUE(ParseProcMaps(kTestCases[i].input, ®ions)); |
| 167 ASSERT_EQ(1u, regions.size()); |
| 168 EXPECT_EQ(kTestCases[i].permissions, regions[0].permissions); |
| 169 } |
| 170 } |
| 171 |
| 172 TEST(ProcMapsTest, ReadProcMaps) { |
| 173 std::vector<MappedMemoryRegion> regions; |
| 174 ASSERT_TRUE(ParseProcMaps(ReadProcMaps(), ®ions)); |
| 175 ASSERT_FALSE(regions.empty()); |
| 176 |
| 177 // We should be find some mention of base_unittests in one of the paths. |
| 178 bool found_base_unittests = false; |
| 179 for (size_t i = 0; i < regions.size(); ++i) { |
| 180 if (regions[i].path.value().find("base_unittests") != std::string::npos) { |
| 181 found_base_unittests = true; |
| 182 break; |
| 183 } |
| 184 } |
| 185 |
| 186 EXPECT_TRUE(found_base_unittests); |
| 187 } |
| 188 |
| 189 TEST(ProcMapsTest, MissingFields) { |
| 190 static const char* kTestCases[] = { |
| 191 "00400000\n", // Missing end + beyond. |
| 192 "00400000-0040b000\n", // Missing perms + beyond. |
| 193 "00400000-0040b000 r-xp\n", // Missing offset + beyond. |
| 194 "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond. |
| 195 "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond. |
| 196 "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms. |
| 197 "00400000-0040b000 r-xp fc:00 794418 /bin/cat\n", // Missing offset. |
| 198 "00400000-0040b000 r-xp 00000000 fc:00 /bin/cat\n", // Missing inode. |
| 199 "00400000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing end. |
| 200 "-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing start. |
| 201 "00400000-0040b000 r-xp 00000000 794418 /bin/cat\n", // Missing device. |
| 202 }; |
| 203 |
| 204 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 205 SCOPED_TRACE(base::StringPrintf("kTestCases[%d] = %s", i, kTestCases[i])); |
| 206 std::vector<MappedMemoryRegion> regions; |
| 207 ASSERT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); |
| 208 } |
| 209 } |
| 210 |
| 211 TEST(ProcMapsTest, InvalidInput) { |
| 212 static const char* kTestCases[] = { |
| 213 "thisisal-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n", |
| 214 "0040000d-linvalid rwxp 00000000 fc:00 794418 /bin/cat\n", |
| 215 "00400000-0040b000 inpu 00000000 fc:00 794418 /bin/cat\n", |
| 216 "00400000-0040b000 rwxp tforproc fc:00 794418 /bin/cat\n", |
| 217 "00400000-0040b000 rwxp 00000000 ma:ps 794418 /bin/cat\n", |
| 218 "00400000-0040b000 rwxp 00000000 fc:00 parse! /bin/cat\n", |
| 219 }; |
| 220 |
| 221 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 222 SCOPED_TRACE(base::StringPrintf("kTestCases[%d] = %s", i, kTestCases[i])); |
| 223 std::vector<MappedMemoryRegion> regions; |
| 224 ASSERT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); |
| 225 } |
| 226 } |
| 227 |
| 228 #endif |
| 229 |
| 230 } // namespace debug |
| 231 } // namespace base |
OLD | NEW |