Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/processor/proc_maps_linux_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698