Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
|
gunsch
2015/06/09 18:46:19
nit: remove blank line
slan
2015/06/10 01:49:13
Done.
| |
| 18 #include "chromecast/crash/minidump_manager.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace chromecast { | |
| 22 namespace { | |
| 23 | |
| 24 const char kLockfileName[] = "lockfile"; | |
| 25 const char kMinidumpSubdir[] = "minidumps"; | |
| 26 | |
| 27 // A trivial implementation of MinidumpManager, which does no work to the | |
| 28 // minidump and exposes its protected members for testing. | |
| 29 class MinidumpManagerSimple : public MinidumpManager { | |
| 30 public: | |
| 31 MinidumpManagerSimple() : MinidumpManager(), work_done_(false) {} | |
| 32 ~MinidumpManagerSimple() override {} | |
| 33 | |
| 34 // MinidumpManager implementation: | |
| 35 int DoWork() override { | |
| 36 work_done_ = true; | |
| 37 return 0; | |
| 38 } | |
| 39 | |
| 40 // Accessors for testing. | |
| 41 const std::string& dump_path() { return dump_path_; } | |
| 42 const std::string& lockfile_path() { return lockfile_path_; } | |
| 43 bool work_done() { return work_done_; } | |
| 44 | |
| 45 private: | |
| 46 bool work_done_; | |
| 47 }; | |
| 48 | |
| 49 class MinidumpManagerTest : public testing::Test { | |
| 50 public: | |
| 51 MinidumpManagerTest() {} | |
| 52 ~MinidumpManagerTest() override {} | |
| 53 | |
| 54 void SetUp() override { | |
| 55 // Set up a temporary directory which will be used as our fake home dir. | |
| 56 ASSERT_TRUE(base::CreateNewTempDirectory("", &fake_home_dir_)); | |
| 57 path_override_.reset( | |
| 58 new base::ScopedPathOverride(base::DIR_HOME, fake_home_dir_)); | |
| 59 minidump_dir_ = fake_home_dir_.Append(kMinidumpSubdir); | |
| 60 } | |
| 61 | |
| 62 void TearDown() override { | |
| 63 // Remove the temp directory. | |
| 64 path_override_.reset(); | |
| 65 ASSERT_TRUE(base::DeleteFile(fake_home_dir_, true)); | |
| 66 } | |
| 67 | |
| 68 protected: | |
| 69 base::FilePath | |
| 70 fake_home_dir_; // Path to the home directory used for the test. | |
| 71 base::FilePath minidump_dir_; // Path the the minidump directory. | |
| 72 | |
| 73 private: | |
| 74 scoped_ptr<base::ScopedPathOverride> path_override_; | |
| 75 }; | |
| 76 | |
| 77 } // namespace | |
| 78 | |
| 79 TEST_F(MinidumpManagerTest, FilePathsAreCorrect) { | |
| 80 MinidumpManagerSimple manager; | |
| 81 | |
| 82 // Verify file paths for directory and lock file. | |
| 83 ASSERT_EQ(minidump_dir_.value(), manager.dump_path()); | |
| 84 ASSERT_EQ(minidump_dir_.Append(kLockfileName).value(), | |
| 85 manager.lockfile_path()); | |
| 86 } | |
| 87 | |
| 88 TEST_F(MinidumpManagerTest, AcquireLockOnNonExistentDirectory) { | |
| 89 // Create the manager and do the minidump work. | |
| 90 MinidumpManagerSimple manager; | |
| 91 ASSERT_EQ(0, manager.DoWorkLocked()); | |
| 92 ASSERT_TRUE(manager.work_done()); | |
| 93 | |
| 94 // Verify the directory and the lockfile both exist. | |
| 95 ASSERT_TRUE(base::DirectoryExists(minidump_dir_)); | |
| 96 ASSERT_TRUE(base::PathExists(minidump_dir_.Append(kLockfileName))); | |
| 97 } | |
| 98 | |
| 99 TEST_F(MinidumpManagerTest, AcquireLockOnExistingEmptyDirectory) { | |
| 100 // Create the empty minidump directory. | |
| 101 ASSERT_TRUE(base::CreateDirectory(minidump_dir_)); | |
| 102 ASSERT_TRUE(base::IsDirectoryEmpty(minidump_dir_)); | |
| 103 | |
| 104 MinidumpManagerSimple manager; | |
| 105 ASSERT_EQ(0, manager.DoWorkLocked()); | |
| 106 ASSERT_TRUE(manager.work_done()); | |
| 107 | |
| 108 // Verify the directory and the lockfile both exist. | |
| 109 ASSERT_TRUE(base::DirectoryExists(minidump_dir_)); | |
| 110 ASSERT_TRUE(base::PathExists(minidump_dir_.Append(kLockfileName))); | |
| 111 } | |
| 112 | |
| 113 TEST_F(MinidumpManagerTest, AcquireLockOnExistingDirectoryWithLockfile) { | |
| 114 // Create a minidump directory. | |
| 115 ASSERT_TRUE(base::CreateDirectory(minidump_dir_)); | |
| 116 ASSERT_TRUE(base::IsDirectoryEmpty(minidump_dir_)); | |
| 117 | |
| 118 // Create a lockfile in that directory. | |
| 119 base::File lockfile(minidump_dir_.Append(kLockfileName), | |
| 120 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | |
| 121 ASSERT_TRUE(lockfile.IsValid()); | |
| 122 | |
| 123 MinidumpManagerSimple manager; | |
| 124 ASSERT_EQ(0, manager.DoWorkLocked()); | |
| 125 ASSERT_TRUE(manager.work_done()); | |
| 126 | |
| 127 // Verify the directory and the lockfile both exist. | |
| 128 ASSERT_TRUE(base::DirectoryExists(minidump_dir_)); | |
| 129 ASSERT_TRUE(base::PathExists(minidump_dir_.Append(kLockfileName))); | |
| 130 } | |
| 131 | |
| 132 TEST_F(MinidumpManagerTest, DoNotBlockOnAcquiringLock) { | |
|
gunsch
2015/06/09 18:46:19
You can rename the test as DISABLED_DoNotBlockOnAc
slan
2015/06/10 01:49:13
Done.
| |
| 133 // TODO(slan): Determine how to lock the lockfile before attempting to | |
| 134 // aquire lock on it from this process, and test that this thread does not | |
| 135 // block. | |
| 136 | |
| 137 //// Create a minidump directory. | |
| 138 // ASSERT_TRUE(base::CreateDirectory(minidump_dir_)); | |
| 139 // ASSERT_TRUE(base::IsDirectoryEmpty(minidump_dir_)); | |
| 140 | |
| 141 //// Create a lockfile in that directory. | |
| 142 // base::File lockfile(minidump_dir_.Append(kLockfileName), | |
| 143 // base::File::FLAG_CREATE_ALWAYS|base::File::FLAG_WRITE); | |
| 144 // ASSERT_TRUE(lockfile.IsValid()); | |
| 145 | |
| 146 //// Fork the process. | |
| 147 // pid_t pid = base::ForkWithFlags(0u, nullptr, nullptr); | |
| 148 // if (pid != 0) { | |
| 149 // // The child process should hold a lock on the file for 20ms then exit. | |
| 150 // lockfile.Lock(); | |
| 151 // base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1000)); | |
| 152 // lockfile.Unlock(); | |
| 153 // return; | |
| 154 //} else { | |
| 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 | |
| 170 } // namespace chromecast | |
| OLD | NEW |