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 "breakpad_googletest_includes.h" |
| 6 #include "google_breakpad/processor/proc_maps_linux.h" |
| 7 |
| 8 namespace { |
| 9 |
| 10 TEST(ProcMapsTest, Empty) { |
| 11 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 12 EXPECT_TRUE(ParseProcMaps("", ®ions)); |
| 13 EXPECT_EQ(0u, regions.size()); |
| 14 } |
| 15 |
| 16 TEST(ProcMapsTest, NoSpaces) { |
| 17 static const char kNoSpaces[] = |
| 18 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat\n"; |
| 19 |
| 20 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 21 ASSERT_TRUE(ParseProcMaps(kNoSpaces, ®ions)); |
| 22 ASSERT_EQ(1u, regions.size()); |
| 23 |
| 24 EXPECT_EQ(0x00400000u, regions[0].start); |
| 25 EXPECT_EQ(0x0040b000u, regions[0].end); |
| 26 EXPECT_EQ(0x00002200u, regions[0].offset); |
| 27 EXPECT_EQ("/bin/cat", regions[0].path); |
| 28 } |
| 29 |
| 30 TEST(ProcMapsTest, Spaces) { |
| 31 static const char kSpaces[] = |
| 32 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/space cat\n"; |
| 33 |
| 34 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 35 ASSERT_TRUE(ParseProcMaps(kSpaces, ®ions)); |
| 36 ASSERT_EQ(1u, regions.size()); |
| 37 |
| 38 EXPECT_EQ(0x00400000u, regions[0].start); |
| 39 EXPECT_EQ(0x0040b000u, regions[0].end); |
| 40 EXPECT_EQ(0x00002200u, regions[0].offset); |
| 41 EXPECT_EQ("/bin/space cat", regions[0].path); |
| 42 } |
| 43 |
| 44 TEST(ProcMapsTest, NoNewline) { |
| 45 static const char kNoSpaces[] = |
| 46 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat"; |
| 47 |
| 48 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 49 ASSERT_FALSE(ParseProcMaps(kNoSpaces, ®ions)); |
| 50 } |
| 51 |
| 52 TEST(ProcMapsTest, NoPath) { |
| 53 static const char kNoPath[] = |
| 54 "00400000-0040b000 rw-p 00000000 00:00 0 \n"; |
| 55 |
| 56 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 57 ASSERT_TRUE(ParseProcMaps(kNoPath, ®ions)); |
| 58 ASSERT_EQ(1u, regions.size()); |
| 59 |
| 60 EXPECT_EQ(0x00400000u, regions[0].start); |
| 61 EXPECT_EQ(0x0040b000u, regions[0].end); |
| 62 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 63 EXPECT_EQ("", regions[0].path); |
| 64 } |
| 65 |
| 66 TEST(ProcMapsTest, Heap) { |
| 67 static const char kHeap[] = |
| 68 "022ac000-022cd000 rw-p 00000000 00:00 0 [heap]\n"; |
| 69 |
| 70 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 71 ASSERT_TRUE(ParseProcMaps(kHeap, ®ions)); |
| 72 ASSERT_EQ(1u, regions.size()); |
| 73 |
| 74 EXPECT_EQ(0x022ac000u, regions[0].start); |
| 75 EXPECT_EQ(0x022cd000u, regions[0].end); |
| 76 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 77 EXPECT_EQ("[heap]", regions[0].path); |
| 78 } |
| 79 |
| 80 #if defined(ARCH_CPU_32_BITS) |
| 81 TEST(ProcMapsTest, Stack32) { |
| 82 static const char kStack[] = |
| 83 "beb04000-beb25000 rw-p 00000000 00:00 0 [stack]\n"; |
| 84 |
| 85 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 86 ASSERT_TRUE(ParseProcMaps(kStack, ®ions)); |
| 87 ASSERT_EQ(1u, regions.size()); |
| 88 |
| 89 EXPECT_EQ(0xbeb04000u, regions[0].start); |
| 90 EXPECT_EQ(0xbeb25000u, regions[0].end); |
| 91 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 92 EXPECT_EQ("[stack]", regions[0].path); |
| 93 } |
| 94 #elif defined(ARCH_CPU_64_BITS) |
| 95 TEST(ProcMapsTest, Stack64) { |
| 96 static const char kStack[] = |
| 97 "7fff69c5b000-7fff69c7d000 rw-p 00000000 00:00 0 [stack]\n"; |
| 98 |
| 99 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 100 ASSERT_TRUE(ParseProcMaps(kStack, ®ions)); |
| 101 ASSERT_EQ(1u, regions.size()); |
| 102 |
| 103 EXPECT_EQ(0x7fff69c5b000u, regions[0].start); |
| 104 EXPECT_EQ(0x7fff69c7d000u, regions[0].end); |
| 105 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 106 EXPECT_EQ("[stack]", regions[0].path); |
| 107 } |
| 108 #endif |
| 109 |
| 110 TEST(ProcMapsTest, Multiple) { |
| 111 static const char kMultiple[] = |
| 112 "00400000-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n" |
| 113 "0060a000-0060b000 r--p 0000a000 fc:00 794418 /bin/cat\n" |
| 114 "0060b000-0060c000 rw-p 0000b000 fc:00 794418 /bin/cat\n"; |
| 115 |
| 116 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 117 ASSERT_TRUE(ParseProcMaps(kMultiple, ®ions)); |
| 118 ASSERT_EQ(3u, regions.size()); |
| 119 |
| 120 EXPECT_EQ(0x00400000u, regions[0].start); |
| 121 EXPECT_EQ(0x0040b000u, regions[0].end); |
| 122 EXPECT_EQ(0x00000000u, regions[0].offset); |
| 123 EXPECT_EQ("/bin/cat", regions[0].path); |
| 124 |
| 125 EXPECT_EQ(0x0060a000u, regions[1].start); |
| 126 EXPECT_EQ(0x0060b000u, regions[1].end); |
| 127 EXPECT_EQ(0x0000a000u, regions[1].offset); |
| 128 EXPECT_EQ("/bin/cat", regions[1].path); |
| 129 |
| 130 EXPECT_EQ(0x0060b000u, regions[2].start); |
| 131 EXPECT_EQ(0x0060c000u, regions[2].end); |
| 132 EXPECT_EQ(0x0000b000u, regions[2].offset); |
| 133 EXPECT_EQ("/bin/cat", regions[2].path); |
| 134 } |
| 135 |
| 136 TEST(ProcMapsTest, Permissions) { |
| 137 static struct { |
| 138 const char* input; |
| 139 uint8_t permissions; |
| 140 } kTestCases[] = { |
| 141 {"00400000-0040b000 ---s 00000000 fc:00 794418 /bin/cat\n", 0}, |
| 142 {"00400000-0040b000 ---S 00000000 fc:00 794418 /bin/cat\n", 0}, |
| 143 {"00400000-0040b000 r--s 00000000 fc:00 794418 /bin/cat\n", |
| 144 google_breakpad::MappedMemoryRegion::READ}, |
| 145 {"00400000-0040b000 -w-s 00000000 fc:00 794418 /bin/cat\n", |
| 146 google_breakpad::MappedMemoryRegion::WRITE}, |
| 147 {"00400000-0040b000 --xs 00000000 fc:00 794418 /bin/cat\n", |
| 148 google_breakpad::MappedMemoryRegion::EXECUTE}, |
| 149 {"00400000-0040b000 rwxs 00000000 fc:00 794418 /bin/cat\n", |
| 150 google_breakpad::MappedMemoryRegion::READ |
| 151 | google_breakpad::MappedMemoryRegion::WRITE |
| 152 | google_breakpad::MappedMemoryRegion::EXECUTE}, |
| 153 {"00400000-0040b000 ---p 00000000 fc:00 794418 /bin/cat\n", |
| 154 google_breakpad::MappedMemoryRegion::PRIVATE}, |
| 155 {"00400000-0040b000 r--p 00000000 fc:00 794418 /bin/cat\n", |
| 156 google_breakpad::MappedMemoryRegion::READ |
| 157 | google_breakpad::MappedMemoryRegion::PRIVATE}, |
| 158 {"00400000-0040b000 -w-p 00000000 fc:00 794418 /bin/cat\n", |
| 159 google_breakpad::MappedMemoryRegion::WRITE |
| 160 | google_breakpad::MappedMemoryRegion::PRIVATE}, |
| 161 {"00400000-0040b000 --xp 00000000 fc:00 794418 /bin/cat\n", |
| 162 google_breakpad::MappedMemoryRegion::EXECUTE |
| 163 | google_breakpad::MappedMemoryRegion::PRIVATE}, |
| 164 {"00400000-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n", |
| 165 google_breakpad::MappedMemoryRegion::READ |
| 166 | google_breakpad::MappedMemoryRegion::WRITE |
| 167 | google_breakpad::MappedMemoryRegion::EXECUTE |
| 168 | google_breakpad::MappedMemoryRegion::PRIVATE}, |
| 169 }; |
| 170 |
| 171 for (size_t i = 0; i < sizeof(kTestCases) / sizeof(kTestCases[0]); ++i) { |
| 172 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 173 EXPECT_TRUE(ParseProcMaps(kTestCases[i].input, ®ions)); |
| 174 EXPECT_EQ(1u, regions.size()); |
| 175 if (regions.empty()) |
| 176 continue; |
| 177 EXPECT_EQ(kTestCases[i].permissions, regions[0].permissions); |
| 178 } |
| 179 } |
| 180 |
| 181 TEST(ProcMapsTest, MissingFields) { |
| 182 static const char* kTestCases[] = { |
| 183 "00400000\n", // Missing end + beyond. |
| 184 "00400000-0040b000\n", // Missing perms + beyond. |
| 185 "00400000-0040b000 r-xp\n", // Missing offset + beyond. |
| 186 "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond. |
| 187 "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond. |
| 188 "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms. |
| 189 "00400000-0040b000 r-xp fc:00 794418 /bin/cat\n", // Missing offset. |
| 190 "00400000-0040b000 r-xp 00000000 fc:00 /bin/cat\n", // Missing inode. |
| 191 "00400000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing end. |
| 192 "-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing start. |
| 193 "00400000-0040b000 r-xp 00000000 794418 /bin/cat\n", // Missing device. |
| 194 }; |
| 195 |
| 196 for (size_t i = 0; i < sizeof(kTestCases) / sizeof(kTestCases[0]); ++i) { |
| 197 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 198 EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); |
| 199 } |
| 200 } |
| 201 |
| 202 TEST(ProcMapsTest, InvalidInput) { |
| 203 static const char* kTestCases[] = { |
| 204 "thisisal-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n", |
| 205 "0040000d-linvalid rwxp 00000000 fc:00 794418 /bin/cat\n", |
| 206 "00400000-0040b000 inpu 00000000 fc:00 794418 /bin/cat\n", |
| 207 "00400000-0040b000 rwxp tforproc fc:00 794418 /bin/cat\n", |
| 208 "00400000-0040b000 rwxp 00000000 ma:ps 794418 /bin/cat\n", |
| 209 "00400000-0040b000 rwxp 00000000 fc:00 parse! /bin/cat\n", |
| 210 }; |
| 211 |
| 212 for (size_t i = 0; i < sizeof(kTestCases) / sizeof(kTestCases[0]); ++i) { |
| 213 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 214 EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); |
| 215 } |
| 216 } |
| 217 |
| 218 TEST(ProcMapsTest, ParseProcMapsEmptyString) { |
| 219 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 220 EXPECT_TRUE(ParseProcMaps("", ®ions)); |
| 221 EXPECT_EQ(0ULL, regions.size()); |
| 222 } |
| 223 |
| 224 // Testing a couple of remotely possible weird things in the input: |
| 225 // - Line ending with \r\n or \n\r. |
| 226 // - File name contains quotes. |
| 227 // - File name has whitespaces. |
| 228 TEST(ProcMapsTest, ParseProcMapsWeirdCorrectInput) { |
| 229 std::vector<google_breakpad::MappedMemoryRegion> regions; |
| 230 const std::string kContents = |
| 231 "00400000-0040b000 r-xp 00000000 fc:00 2106562 " |
| 232 " /bin/cat\r\n" |
| 233 "7f53b7dad000-7f53b7f62000 r-xp 00000000 fc:00 263011 " |
| 234 " /lib/x86_64-linux-gnu/libc-2.15.so\n\r" |
| 235 "7f53b816d000-7f53b818f000 r-xp 00000000 fc:00 264284 " |
| 236 " /lib/x86_64-linux-gnu/ld-2.15.so\n" |
| 237 "7fff9c7ff000-7fff9c800000 r-xp 00000000 00:00 0 " |
| 238 " \"vd so\"\n" |
| 239 "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 " |
| 240 " [vsys call]\n"; |
| 241 EXPECT_TRUE(ParseProcMaps(kContents, ®ions)); |
| 242 EXPECT_EQ(5ULL, regions.size()); |
| 243 EXPECT_EQ("/bin/cat", regions[0].path); |
| 244 EXPECT_EQ("/lib/x86_64-linux-gnu/libc-2.15.so", regions[1].path); |
| 245 EXPECT_EQ("/lib/x86_64-linux-gnu/ld-2.15.so", regions[2].path); |
| 246 EXPECT_EQ("\"vd so\"", regions[3].path); |
| 247 EXPECT_EQ("[vsys call]", regions[4].path); |
| 248 } |
| 249 |
| 250 } // namespace |
OLD | NEW |