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