blob: d9c9a5b1ba993447444cafd044b716d6820c7c0f [file] [log] [blame]
Steve Fung6c34c252015-08-20 00:27:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Chris Sosae4a86032010-06-16 17:08:34 -070016
Steve Fung129bea52015-07-23 13:11:15 -070017#include "user_collector.h"
Ben Chan7e776902014-06-18 13:19:51 -070018
Ben Chan6e709a12012-02-29 12:10:44 -080019#include <elf.h>
Steve Fung8bafb3d2015-08-07 13:22:46 -070020#include <sys/cdefs.h> // For __WORDSIZE
Ken Mixter777484c2010-07-23 16:22:44 -070021#include <unistd.h>
Chris Sosae4a86032010-06-16 17:08:34 -070022
Ben Chanab6cc902014-09-05 08:21:06 -070023#include <base/files/file_util.h>
Ben Chan7e776902014-06-18 13:19:51 -070024#include <base/files/scoped_temp_dir.h>
25#include <base/strings/string_split.h>
Alex Vakulenko74dc6242015-10-13 09:23:34 -070026#include <brillo/syslog_logging.h>
Steve Fung6e139522015-02-05 14:54:16 -080027#include <gmock/gmock.h>
Ben Chan7e776902014-06-18 13:19:51 -070028#include <gtest/gtest.h>
Chris Sosae4a86032010-06-16 17:08:34 -070029
Simon Que9f90aca2013-02-19 17:19:52 -080030using base::FilePath;
Alex Vakulenko74dc6242015-10-13 09:23:34 -070031using brillo::FindLog;
Ken Mixtera3249322011-03-03 08:47:38 -080032
Daniel Eratd257ea12015-01-28 10:23:28 -070033namespace {
34
35int s_crashes = 0;
36bool s_metrics = false;
37
38const char kFilePath[] = "/my/path";
39
Chris Sosae4a86032010-06-16 17:08:34 -070040void CountCrash() {
41 ++s_crashes;
42}
43
44bool IsMetrics() {
45 return s_metrics;
46}
47
Daniel Eratd257ea12015-01-28 10:23:28 -070048} // namespace
49
Steve Fung6e139522015-02-05 14:54:16 -080050class UserCollectorMock : public UserCollector {
51 public:
52 MOCK_METHOD0(SetUpDBus, void());
53};
54
Chris Sosae4a86032010-06-16 17:08:34 -070055class UserCollectorTest : public ::testing::Test {
56 void SetUp() {
57 s_crashes = 0;
Steve Fung6e139522015-02-05 14:54:16 -080058
59 EXPECT_CALL(collector_, SetUpDBus()).WillRepeatedly(testing::Return());
60
Chris Sosae4a86032010-06-16 17:08:34 -070061 collector_.Initialize(CountCrash,
62 kFilePath,
63 IsMetrics,
Steve Fungd6169a22014-08-11 15:52:23 -070064 false,
65 false,
66 false,
67 "");
Steve Fung78fcf662016-01-20 01:45:11 -080068
69 EXPECT_TRUE(test_dir_.CreateUniqueTempDir());
70
71 mkdir(test_dir_.path().Append("test").value().c_str(), 0777);
Ken Mixter777484c2010-07-23 16:22:44 -070072 pid_ = getpid();
Alex Vakulenko74dc6242015-10-13 09:23:34 -070073 brillo::ClearLog();
Chris Sosae4a86032010-06-16 17:08:34 -070074 }
Ben Chanf13bb582012-01-06 08:22:07 -080075
Chris Sosae4a86032010-06-16 17:08:34 -070076 protected:
Ken Mixter2953c3a2010-10-18 14:42:20 -070077 void ExpectFileEquals(const char *golden,
Lei Zhang9b1f3002014-04-24 02:10:57 -070078 const FilePath &file_path) {
Ken Mixter2953c3a2010-10-18 14:42:20 -070079 std::string contents;
Lei Zhang9b1f3002014-04-24 02:10:57 -070080 EXPECT_TRUE(base::ReadFileToString(file_path, &contents));
Ken Mixter2953c3a2010-10-18 14:42:20 -070081 EXPECT_EQ(golden, contents);
82 }
83
Ben Chanf13bb582012-01-06 08:22:07 -080084 std::vector<std::string> SplitLines(const std::string &lines) const {
Alex Vakulenkoea05ff92016-01-20 07:53:57 -080085 return base::SplitString(lines, "\n", base::TRIM_WHITESPACE,
86 base::SPLIT_WANT_ALL);
Ben Chanf13bb582012-01-06 08:22:07 -080087 }
88
Steve Fung6e139522015-02-05 14:54:16 -080089 UserCollectorMock collector_;
Ken Mixter777484c2010-07-23 16:22:44 -070090 pid_t pid_;
Steve Fung78fcf662016-01-20 01:45:11 -080091 base::ScopedTempDir test_dir_;
Chris Sosae4a86032010-06-16 17:08:34 -070092};
93
Ken Mixter1b8fe012011-01-25 13:33:05 -080094TEST_F(UserCollectorTest, ParseCrashAttributes) {
95 pid_t pid;
96 int signal;
Michael Krebs1c57e9e2012-09-25 18:03:13 -070097 uid_t uid;
Steve Fung773fd3c2015-10-09 17:01:35 -070098 gid_t gid;
Ken Mixter1b8fe012011-01-25 13:33:05 -080099 std::string exec_name;
Steve Fung773fd3c2015-10-09 17:01:35 -0700100 EXPECT_TRUE(collector_.ParseCrashAttributes("123456:11:1000:2000:foobar",
101 &pid, &signal, &uid, &gid, &exec_name));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800102 EXPECT_EQ(123456, pid);
103 EXPECT_EQ(11, signal);
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700104 EXPECT_EQ(1000, uid);
Steve Fung773fd3c2015-10-09 17:01:35 -0700105 EXPECT_EQ(2000, gid);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800106 EXPECT_EQ("foobar", exec_name);
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700107 EXPECT_TRUE(collector_.ParseCrashAttributes("4321:6:barfoo",
Steve Fung773fd3c2015-10-09 17:01:35 -0700108 &pid, &signal, &uid, &gid, &exec_name));
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700109 EXPECT_EQ(4321, pid);
110 EXPECT_EQ(6, signal);
111 EXPECT_EQ(-1, uid);
Steve Fung773fd3c2015-10-09 17:01:35 -0700112 EXPECT_EQ(-1, gid);
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700113 EXPECT_EQ("barfoo", exec_name);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800114
115 EXPECT_FALSE(collector_.ParseCrashAttributes("123456:11",
Steve Fung773fd3c2015-10-09 17:01:35 -0700116 &pid, &signal, &uid, &gid, &exec_name));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800117
118 EXPECT_TRUE(collector_.ParseCrashAttributes("123456:11:exec:extra",
Steve Fung773fd3c2015-10-09 17:01:35 -0700119 &pid, &signal, &uid, &gid, &exec_name));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800120 EXPECT_EQ("exec:extra", exec_name);
121
122 EXPECT_FALSE(collector_.ParseCrashAttributes("12345p:11:foobar",
Steve Fung773fd3c2015-10-09 17:01:35 -0700123 &pid, &signal, &uid, &gid, &exec_name));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800124
125 EXPECT_FALSE(collector_.ParseCrashAttributes("123456:1 :foobar",
Steve Fung773fd3c2015-10-09 17:01:35 -0700126 &pid, &signal, &uid, &gid, &exec_name));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800127
128 EXPECT_FALSE(collector_.ParseCrashAttributes("123456::foobar",
Steve Fung773fd3c2015-10-09 17:01:35 -0700129 &pid, &signal, &uid, &gid, &exec_name));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800130}
131
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700132TEST_F(UserCollectorTest, ShouldDumpDeveloperImageOverridesConsent) {
133 std::string reason;
Steve Fungc8b74142015-08-05 15:45:20 -0700134 EXPECT_TRUE(collector_.ShouldDump(false, true, &reason));
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700135 EXPECT_EQ("developer build - not testing - always dumping", reason);
136
137 // When running a crash test, behave as normal.
Steve Fungc8b74142015-08-05 15:45:20 -0700138 EXPECT_FALSE(collector_.ShouldDump(false, false, &reason));
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700139 EXPECT_EQ("ignoring - no consent", reason);
140}
141
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700142TEST_F(UserCollectorTest, ShouldDumpUseConsentProductionImage) {
143 std::string result;
Steve Fungc8b74142015-08-05 15:45:20 -0700144 EXPECT_FALSE(collector_.ShouldDump(false, false, &result));
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700145 EXPECT_EQ("ignoring - no consent", result);
146
Steve Fungc8b74142015-08-05 15:45:20 -0700147 EXPECT_TRUE(collector_.ShouldDump(true, false, &result));
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700148 EXPECT_EQ("handling", result);
149}
150
151TEST_F(UserCollectorTest, HandleCrashWithoutConsent) {
Chris Sosae4a86032010-06-16 17:08:34 -0700152 s_metrics = false;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800153 collector_.HandleCrash("20:10:ignored", "foobar");
Ken Mixtera3249322011-03-03 08:47:38 -0800154 EXPECT_TRUE(FindLog(
155 "Received crash notification for foobar[20] sig 10"));
Chris Sosae4a86032010-06-16 17:08:34 -0700156 ASSERT_EQ(s_crashes, 0);
157}
158
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700159TEST_F(UserCollectorTest, HandleNonChromeCrashWithConsent) {
Ken Mixter2105b492010-11-09 16:14:38 -0800160 s_metrics = true;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800161 collector_.HandleCrash("5:2:ignored", "chromeos-wm");
Ken Mixtera3249322011-03-03 08:47:38 -0800162 EXPECT_TRUE(FindLog(
163 "Received crash notification for chromeos-wm[5] sig 2"));
Ken Mixter2105b492010-11-09 16:14:38 -0800164 ASSERT_EQ(s_crashes, 1);
165}
166
Ken Mixter777484c2010-07-23 16:22:44 -0700167TEST_F(UserCollectorTest, GetProcessPath) {
168 FilePath path = collector_.GetProcessPath(100);
169 ASSERT_EQ("/proc/100", path.value());
170}
171
172TEST_F(UserCollectorTest, GetSymlinkTarget) {
173 FilePath result;
174 ASSERT_FALSE(collector_.GetSymlinkTarget(FilePath("/does_not_exist"),
175 &result));
Ken Mixtera3249322011-03-03 08:47:38 -0800176 ASSERT_TRUE(FindLog(
177 "Readlink failed on /does_not_exist with 2"));
Steve Fung78fcf662016-01-20 01:45:11 -0800178 std::string long_link = test_dir_.path().value();
Ken Mixter777484c2010-07-23 16:22:44 -0700179 for (int i = 0; i < 50; ++i)
180 long_link += "0123456789";
181 long_link += "/gold";
182
183 for (size_t len = 1; len <= long_link.size(); ++len) {
184 std::string this_link;
Steve Fung78fcf662016-01-20 01:45:11 -0800185 static const char* kLink =
186 test_dir_.path().Append("test/this_link").value().c_str();
Ken Mixter777484c2010-07-23 16:22:44 -0700187 this_link.assign(long_link.c_str(), len);
188 ASSERT_EQ(len, this_link.size());
189 unlink(kLink);
190 ASSERT_EQ(0, symlink(this_link.c_str(), kLink));
191 ASSERT_TRUE(collector_.GetSymlinkTarget(FilePath(kLink), &result));
192 ASSERT_EQ(this_link, result.value());
193 }
194}
195
Ken Mixterd49d3622011-02-09 18:23:00 -0800196TEST_F(UserCollectorTest, GetExecutableBaseNameFromPid) {
197 std::string base_name;
198 EXPECT_FALSE(collector_.GetExecutableBaseNameFromPid(0, &base_name));
Ken Mixtera3249322011-03-03 08:47:38 -0800199 EXPECT_TRUE(FindLog(
200 "Readlink failed on /proc/0/exe with 2"));
201 EXPECT_TRUE(FindLog(
202 "GetSymlinkTarget failed - Path /proc/0 DirectoryExists: 0"));
203 EXPECT_TRUE(FindLog("stat /proc/0/exe failed: -1 2"));
Ken Mixterd49d3622011-02-09 18:23:00 -0800204
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700205 brillo::ClearLog();
Ken Mixterd49d3622011-02-09 18:23:00 -0800206 pid_t my_pid = getpid();
207 EXPECT_TRUE(collector_.GetExecutableBaseNameFromPid(my_pid, &base_name));
Ken Mixtera3249322011-03-03 08:47:38 -0800208 EXPECT_FALSE(FindLog("Readlink failed"));
Steve Fung8ed101b2015-12-02 16:18:07 -0800209 EXPECT_EQ("crash_reporter_tests", base_name);
Ken Mixterd49d3622011-02-09 18:23:00 -0800210}
211
Ben Chanf13bb582012-01-06 08:22:07 -0800212TEST_F(UserCollectorTest, GetFirstLineWithPrefix) {
213 std::vector<std::string> lines;
214 std::string line;
215
216 EXPECT_FALSE(collector_.GetFirstLineWithPrefix(lines, "Name:", &line));
217 EXPECT_EQ("", line);
218
219 lines.push_back("Name:\tls");
220 lines.push_back("State:\tR (running)");
221 lines.push_back(" Foo:\t1000");
222
223 line.clear();
224 EXPECT_TRUE(collector_.GetFirstLineWithPrefix(lines, "Name:", &line));
225 EXPECT_EQ(lines[0], line);
226
227 line.clear();
228 EXPECT_TRUE(collector_.GetFirstLineWithPrefix(lines, "State:", &line));
229 EXPECT_EQ(lines[1], line);
230
231 line.clear();
232 EXPECT_FALSE(collector_.GetFirstLineWithPrefix(lines, "Foo:", &line));
233 EXPECT_EQ("", line);
234
235 line.clear();
236 EXPECT_TRUE(collector_.GetFirstLineWithPrefix(lines, " Foo:", &line));
237 EXPECT_EQ(lines[2], line);
238
239 line.clear();
240 EXPECT_FALSE(collector_.GetFirstLineWithPrefix(lines, "Bar:", &line));
241 EXPECT_EQ("", line);
242}
243
Ken Mixter777484c2010-07-23 16:22:44 -0700244TEST_F(UserCollectorTest, GetIdFromStatus) {
245 int id = 1;
246 EXPECT_FALSE(collector_.GetIdFromStatus(UserCollector::kUserId,
247 UserCollector::kIdEffective,
Ben Chanf13bb582012-01-06 08:22:07 -0800248 SplitLines("nothing here"),
Ken Mixter777484c2010-07-23 16:22:44 -0700249 &id));
250 EXPECT_EQ(id, 1);
251
252 // Not enough parameters.
253 EXPECT_FALSE(collector_.GetIdFromStatus(UserCollector::kUserId,
254 UserCollector::kIdReal,
Ben Chanf13bb582012-01-06 08:22:07 -0800255 SplitLines("line 1\nUid:\t1\n"),
256 &id));
Ken Mixter777484c2010-07-23 16:22:44 -0700257
Ben Chanf13bb582012-01-06 08:22:07 -0800258 const std::vector<std::string> valid_contents =
259 SplitLines("\nUid:\t1\t2\t3\t4\nGid:\t5\t6\t7\t8\n");
Ken Mixter777484c2010-07-23 16:22:44 -0700260 EXPECT_TRUE(collector_.GetIdFromStatus(UserCollector::kUserId,
261 UserCollector::kIdReal,
262 valid_contents,
263 &id));
264 EXPECT_EQ(1, id);
265
266 EXPECT_TRUE(collector_.GetIdFromStatus(UserCollector::kUserId,
267 UserCollector::kIdEffective,
268 valid_contents,
269 &id));
270 EXPECT_EQ(2, id);
271
272 EXPECT_TRUE(collector_.GetIdFromStatus(UserCollector::kUserId,
273 UserCollector::kIdFileSystem,
274 valid_contents,
275 &id));
276 EXPECT_EQ(4, id);
277
278 EXPECT_TRUE(collector_.GetIdFromStatus(UserCollector::kGroupId,
279 UserCollector::kIdEffective,
280 valid_contents,
281 &id));
282 EXPECT_EQ(6, id);
283
284 EXPECT_TRUE(collector_.GetIdFromStatus(UserCollector::kGroupId,
285 UserCollector::kIdSet,
286 valid_contents,
287 &id));
288 EXPECT_EQ(7, id);
289
290 EXPECT_FALSE(collector_.GetIdFromStatus(UserCollector::kGroupId,
291 UserCollector::IdKind(5),
292 valid_contents,
293 &id));
294 EXPECT_FALSE(collector_.GetIdFromStatus(UserCollector::kGroupId,
295 UserCollector::IdKind(-1),
296 valid_contents,
297 &id));
298
299 // Fail if junk after number
300 EXPECT_FALSE(collector_.GetIdFromStatus(UserCollector::kUserId,
301 UserCollector::kIdReal,
Ben Chanf13bb582012-01-06 08:22:07 -0800302 SplitLines("Uid:\t1f\t2\t3\t4\n"),
Ken Mixter777484c2010-07-23 16:22:44 -0700303 &id));
304 EXPECT_TRUE(collector_.GetIdFromStatus(UserCollector::kUserId,
305 UserCollector::kIdReal,
Ben Chanf13bb582012-01-06 08:22:07 -0800306 SplitLines("Uid:\t1\t2\t3\t4\n"),
Ken Mixter777484c2010-07-23 16:22:44 -0700307 &id));
308 EXPECT_EQ(1, id);
309
310 // Fail if more than 4 numbers.
311 EXPECT_FALSE(collector_.GetIdFromStatus(UserCollector::kUserId,
312 UserCollector::kIdReal,
Ben Chanf13bb582012-01-06 08:22:07 -0800313 SplitLines("Uid:\t1\t2\t3\t4\t5\n"),
Ken Mixter777484c2010-07-23 16:22:44 -0700314 &id));
315}
316
Ben Chanf13bb582012-01-06 08:22:07 -0800317TEST_F(UserCollectorTest, GetStateFromStatus) {
318 std::string state;
319 EXPECT_FALSE(collector_.GetStateFromStatus(SplitLines("nothing here"),
320 &state));
321 EXPECT_EQ("", state);
322
323 EXPECT_TRUE(collector_.GetStateFromStatus(SplitLines("State:\tR (running)"),
324 &state));
325 EXPECT_EQ("R (running)", state);
326
327 EXPECT_TRUE(collector_.GetStateFromStatus(
328 SplitLines("Name:\tls\nState:\tZ (zombie)\n"), &state));
329 EXPECT_EQ("Z (zombie)", state);
330}
331
Ken Mixter777484c2010-07-23 16:22:44 -0700332TEST_F(UserCollectorTest, GetUserInfoFromName) {
333 gid_t gid = 100;
334 uid_t uid = 100;
335 EXPECT_TRUE(collector_.GetUserInfoFromName("root", &uid, &gid));
336 EXPECT_EQ(0, uid);
337 EXPECT_EQ(0, gid);
338}
339
Ken Mixter777484c2010-07-23 16:22:44 -0700340TEST_F(UserCollectorTest, CopyOffProcFilesBadPath) {
341 // Try a path that is not writable.
342 ASSERT_FALSE(collector_.CopyOffProcFiles(pid_, FilePath("/bad/path")));
Ken Mixtera3249322011-03-03 08:47:38 -0800343 EXPECT_TRUE(FindLog("Could not create /bad/path"));
Ken Mixter777484c2010-07-23 16:22:44 -0700344}
345
346TEST_F(UserCollectorTest, CopyOffProcFilesBadPid) {
Steve Fung78fcf662016-01-20 01:45:11 -0800347 FilePath container_path(test_dir_.path().Append("test/container"));
Ken Mixter777484c2010-07-23 16:22:44 -0700348 ASSERT_FALSE(collector_.CopyOffProcFiles(0, container_path));
Ken Mixtera3249322011-03-03 08:47:38 -0800349 EXPECT_TRUE(FindLog("Path /proc/0 does not exist"));
Ken Mixter777484c2010-07-23 16:22:44 -0700350}
351
352TEST_F(UserCollectorTest, CopyOffProcFilesOK) {
Steve Fung78fcf662016-01-20 01:45:11 -0800353 FilePath container_path(test_dir_.path().Append("test/container"));
Ken Mixter777484c2010-07-23 16:22:44 -0700354 ASSERT_TRUE(collector_.CopyOffProcFiles(pid_, container_path));
Ken Mixtera3249322011-03-03 08:47:38 -0800355 EXPECT_FALSE(FindLog("Could not copy"));
Ken Mixter777484c2010-07-23 16:22:44 -0700356 static struct {
357 const char *name;
358 bool exists;
359 } expectations[] = {
360 { "auxv", true },
361 { "cmdline", true },
362 { "environ", true },
363 { "maps", true },
364 { "mem", false },
365 { "mounts", false },
366 { "sched", false },
367 { "status", true }
368 };
369 for (unsigned i = 0; i < sizeof(expectations)/sizeof(expectations[0]); ++i) {
370 EXPECT_EQ(expectations[i].exists,
Mike Frysingera557c112014-02-05 22:55:39 -0500371 base::PathExists(
Ken Mixter777484c2010-07-23 16:22:44 -0700372 container_path.Append(expectations[i].name)));
373 }
374}
375
Ben Chanf13bb582012-01-06 08:22:07 -0800376TEST_F(UserCollectorTest, ValidateProcFiles) {
Steve Fung78fcf662016-01-20 01:45:11 -0800377 FilePath container_dir = test_dir_.path();
Ben Chanf13bb582012-01-06 08:22:07 -0800378
379 // maps file not exists (i.e. GetFileSize fails)
380 EXPECT_FALSE(collector_.ValidateProcFiles(container_dir));
381
382 // maps file is empty
383 FilePath maps_file = container_dir.Append("maps");
Ben Chan262d7982014-09-18 08:05:20 -0700384 ASSERT_EQ(0, base::WriteFile(maps_file, nullptr, 0));
Mike Frysingera557c112014-02-05 22:55:39 -0500385 ASSERT_TRUE(base::PathExists(maps_file));
Ben Chanf13bb582012-01-06 08:22:07 -0800386 EXPECT_FALSE(collector_.ValidateProcFiles(container_dir));
387
388 // maps file is not empty
389 const char data[] = "test data";
Ben Chanf30c6412014-05-22 23:09:01 -0700390 ASSERT_EQ(sizeof(data), base::WriteFile(maps_file, data, sizeof(data)));
Mike Frysingera557c112014-02-05 22:55:39 -0500391 ASSERT_TRUE(base::PathExists(maps_file));
Ben Chanf13bb582012-01-06 08:22:07 -0800392 EXPECT_TRUE(collector_.ValidateProcFiles(container_dir));
393}
394
Ben Chan6e709a12012-02-29 12:10:44 -0800395TEST_F(UserCollectorTest, ValidateCoreFile) {
Steve Fung78fcf662016-01-20 01:45:11 -0800396 FilePath container_dir = test_dir_.path();
Ben Chan6e709a12012-02-29 12:10:44 -0800397 FilePath core_file = container_dir.Append("core");
398
399 // Core file does not exist
400 EXPECT_EQ(UserCollector::kErrorInvalidCoreFile,
401 collector_.ValidateCoreFile(core_file));
402 char e_ident[EI_NIDENT];
403 e_ident[EI_MAG0] = ELFMAG0;
404 e_ident[EI_MAG1] = ELFMAG1;
405 e_ident[EI_MAG2] = ELFMAG2;
406 e_ident[EI_MAG3] = ELFMAG3;
407#if __WORDSIZE == 32
408 e_ident[EI_CLASS] = ELFCLASS32;
409#elif __WORDSIZE == 64
410 e_ident[EI_CLASS] = ELFCLASS64;
411#else
412#error Unknown/unsupported value of __WORDSIZE.
413#endif
414
415 // Core file has the expected header
Ben Chanf30c6412014-05-22 23:09:01 -0700416 ASSERT_TRUE(base::WriteFile(core_file, e_ident, sizeof(e_ident)));
Ben Chan6e709a12012-02-29 12:10:44 -0800417 EXPECT_EQ(UserCollector::kErrorNone,
418 collector_.ValidateCoreFile(core_file));
419
420#if __WORDSIZE == 64
421 // 32-bit core file on 64-bit platform
422 e_ident[EI_CLASS] = ELFCLASS32;
Ben Chanf30c6412014-05-22 23:09:01 -0700423 ASSERT_TRUE(base::WriteFile(core_file, e_ident, sizeof(e_ident)));
Ben Chan6e709a12012-02-29 12:10:44 -0800424 EXPECT_EQ(UserCollector::kErrorUnsupported32BitCoreFile,
425 collector_.ValidateCoreFile(core_file));
426 e_ident[EI_CLASS] = ELFCLASS64;
427#endif
428
429 // Invalid core files
Ben Chanf30c6412014-05-22 23:09:01 -0700430 ASSERT_TRUE(base::WriteFile(core_file, e_ident, sizeof(e_ident) - 1));
Ben Chan6e709a12012-02-29 12:10:44 -0800431 EXPECT_EQ(UserCollector::kErrorInvalidCoreFile,
432 collector_.ValidateCoreFile(core_file));
433
434 e_ident[EI_MAG0] = 0;
Ben Chanf30c6412014-05-22 23:09:01 -0700435 ASSERT_TRUE(base::WriteFile(core_file, e_ident, sizeof(e_ident)));
Ben Chan6e709a12012-02-29 12:10:44 -0800436 EXPECT_EQ(UserCollector::kErrorInvalidCoreFile,
437 collector_.ValidateCoreFile(core_file));
438}