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

Side by Side Diff: chromecast/crash/linux/minidump_manager_unittest.cc

Issue 1154383006: Adding crash utilities to chromecast/crash. (Closed) Base URL: https://eureka-internal.googlesource.com/chromium/src@master
Patch Set: Linux-specific utils moved to linux/ Created 5 years, 6 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
OLDNEW
(Empty)
1 // Copyright 2015 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 <stdlib.h>
6 #include <sys/stat.h> // mkdir
7 #include <stdio.h> // perror
8 #include <time.h>
9
10 #include "base/base_paths.h"
11 #include "base/files/file.h"
12 #include "base/files/file_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/process/launch.h"
15 #include "base/test/scoped_path_override.h"
16 #include "base/threading/platform_thread.h"
17 #include "chromecast/crash/linux/minidump_manager.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace chromecast {
21 namespace {
22
23 const char kLockfileName[] = "lockfile";
24 const char kMinidumpSubdir[] = "minidumps";
25
26 // A trivial implementation of MinidumpManager, which does no work to the
27 // minidump and exposes its protected members for testing.
28 class MinidumpManagerSimple : public MinidumpManager {
29 public:
30 MinidumpManagerSimple() : MinidumpManager(), work_done_(false) {}
31 ~MinidumpManagerSimple() override {}
32
33 // MinidumpManager implementation:
34 int DoWork() override {
35 work_done_ = true;
36 return 0;
37 }
38
39 // Accessors for testing.
40 const std::string& dump_path() { return dump_path_; }
41 const std::string& lockfile_path() { return lockfile_path_; }
42 bool work_done() { return work_done_; }
43
44 private:
45 bool work_done_;
46 };
47
48 class MinidumpManagerTest : public testing::Test {
49 public:
50 MinidumpManagerTest() {}
51 ~MinidumpManagerTest() override {}
52
53 void SetUp() override {
54 // Set up a temporary directory which will be used as our fake home dir.
55 ASSERT_TRUE(base::CreateNewTempDirectory("", &fake_home_dir_));
56 path_override_.reset(
57 new base::ScopedPathOverride(base::DIR_HOME, fake_home_dir_));
58 minidump_dir_ = fake_home_dir_.Append(kMinidumpSubdir);
59 }
60
61 void TearDown() override {
62 // Remove the temp directory.
63 path_override_.reset();
64 ASSERT_TRUE(base::DeleteFile(fake_home_dir_, true));
65 }
66
67 protected:
68 base::FilePath
69 fake_home_dir_; // Path to the home directory used for the test.
70 base::FilePath minidump_dir_; // Path the the minidump directory.
71
72 private:
73 scoped_ptr<base::ScopedPathOverride> path_override_;
74 };
75
76 } // namespace
77
78 TEST_F(MinidumpManagerTest, FilePathsAreCorrect) {
79 MinidumpManagerSimple manager;
80
81 // Verify file paths for directory and lock file.
82 ASSERT_EQ(minidump_dir_.value(), manager.dump_path());
83 ASSERT_EQ(minidump_dir_.Append(kLockfileName).value(),
84 manager.lockfile_path());
85 }
86
87 TEST_F(MinidumpManagerTest, AcquireLockOnNonExistentDirectory) {
88 // Create the manager and do the minidump work.
89 MinidumpManagerSimple manager;
90 ASSERT_EQ(0, manager.DoWorkLocked());
91 ASSERT_TRUE(manager.work_done());
92
93 // Verify the directory and the lockfile both exist.
94 ASSERT_TRUE(base::DirectoryExists(minidump_dir_));
95 ASSERT_TRUE(base::PathExists(minidump_dir_.Append(kLockfileName)));
96 }
97
98 TEST_F(MinidumpManagerTest, AcquireLockOnExistingEmptyDirectory) {
99 // Create the empty minidump directory.
100 ASSERT_TRUE(base::CreateDirectory(minidump_dir_));
101 ASSERT_TRUE(base::IsDirectoryEmpty(minidump_dir_));
102
103 MinidumpManagerSimple manager;
104 ASSERT_EQ(0, manager.DoWorkLocked());
105 ASSERT_TRUE(manager.work_done());
106
107 // Verify the directory and the lockfile both exist.
108 ASSERT_TRUE(base::DirectoryExists(minidump_dir_));
109 ASSERT_TRUE(base::PathExists(minidump_dir_.Append(kLockfileName)));
110 }
111
112 TEST_F(MinidumpManagerTest, AcquireLockOnExistingDirectoryWithLockfile) {
113 // Create a minidump directory.
114 ASSERT_TRUE(base::CreateDirectory(minidump_dir_));
115 ASSERT_TRUE(base::IsDirectoryEmpty(minidump_dir_));
116
117 // Create a lockfile in that directory.
118 base::File lockfile(minidump_dir_.Append(kLockfileName),
119 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
120 ASSERT_TRUE(lockfile.IsValid());
121
122 MinidumpManagerSimple manager;
123 ASSERT_EQ(0, manager.DoWorkLocked());
124 ASSERT_TRUE(manager.work_done());
125
126 // Verify the directory and the lockfile both exist.
127 ASSERT_TRUE(base::DirectoryExists(minidump_dir_));
128 ASSERT_TRUE(base::PathExists(minidump_dir_.Append(kLockfileName)));
129 }
130
131 TEST_F(MinidumpManagerTest, DISABLED_DoNotBlockOnAcquiringLock) {
132 // TODO(slan): Determine how to lock the lockfile before attempting to
133 // aquire lock on it from this process, and test that this thread does not
134 // block.
135
136 // Create a minidump directory.
137 ASSERT_TRUE(base::CreateDirectory(minidump_dir_));
138 ASSERT_TRUE(base::IsDirectoryEmpty(minidump_dir_));
139
140 // Create a lockfile in that directory.
141 base::File lockfile(minidump_dir_.Append(kLockfileName),
142 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
143 ASSERT_TRUE(lockfile.IsValid());
144
145 // Fork the process.
146 pid_t pid = base::ForkWithFlags(0u, nullptr, nullptr);
147 if (pid != 0) {
148 // The child process should hold a lock on the file for 20ms then exit.
149 lockfile.Lock();
150 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1000));
151 lockfile.Unlock();
152 return;
153 }
154
155 // The parent process should attempt to grab the lock after 10ms, fail
156 // immediately, and exit imediately.
157 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500));
158
159 MinidumpManagerSimple manager;
160 manager.SetNonblocking(true);
161 ASSERT_EQ(-1, manager.DoWorkLocked());
162 ASSERT_FALSE(manager.work_done());
163
164 // Verify the directory and the lockfile both exist.
165 ASSERT_TRUE(base::DirectoryExists(minidump_dir_));
166 ASSERT_TRUE(base::PathExists(minidump_dir_.Append(kLockfileName)));
167 }
168
169 } // namespace chromecast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698