OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <fcntl.h> | 5 #include <fcntl.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include <sys/file.h> | 7 #include <sys/file.h> |
8 #include <sys/stat.h> // mkdir | 8 #include <sys/stat.h> // mkdir |
9 #include <sys/types.h> // | 9 #include <sys/types.h> // |
10 #include <stdio.h> // perror | 10 #include <stdio.h> // perror |
11 #include <time.h> | 11 #include <time.h> |
12 | 12 |
13 #include <fstream> | 13 #include <fstream> |
14 | 14 |
15 #include "base/base_paths.h" | 15 #include "base/base_paths.h" |
16 #include "base/bind.h" | 16 #include "base/bind.h" |
17 #include "base/files/file.h" | 17 #include "base/files/file.h" |
18 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
| 19 #include "base/files/scoped_temp_dir.h" |
19 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
20 #include "base/memory/scoped_vector.h" | 21 #include "base/memory/scoped_vector.h" |
21 #include "base/process/launch.h" | 22 #include "base/process/launch.h" |
22 #include "base/test/scoped_path_override.h" | 23 #include "base/test/scoped_path_override.h" |
23 #include "base/threading/platform_thread.h" | 24 #include "base/threading/platform_thread.h" |
24 #include "base/threading/thread.h" | 25 #include "base/threading/thread.h" |
| 26 #include "chromecast/base/scoped_temp_file.h" |
25 #include "chromecast/crash/linux/crash_testing_utils.h" | 27 #include "chromecast/crash/linux/crash_testing_utils.h" |
26 #include "chromecast/crash/linux/dump_info.h" | 28 #include "chromecast/crash/linux/dump_info.h" |
27 #include "chromecast/crash/linux/synchronized_minidump_manager.h" | 29 #include "chromecast/crash/linux/synchronized_minidump_manager.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
29 | 31 |
30 namespace chromecast { | 32 namespace chromecast { |
31 namespace { | 33 namespace { |
32 | 34 |
33 const char kLockfileName[] = "lockfile"; | 35 const char kLockfileName[] = "lockfile"; |
34 const char kMetadataName[] = "metadata"; | 36 const char kMetadataName[] = "metadata"; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 const int sleep_duration_ms_; | 132 const int sleep_duration_ms_; |
131 }; | 133 }; |
132 | 134 |
133 class SynchronizedMinidumpManagerTest : public testing::Test { | 135 class SynchronizedMinidumpManagerTest : public testing::Test { |
134 public: | 136 public: |
135 SynchronizedMinidumpManagerTest() {} | 137 SynchronizedMinidumpManagerTest() {} |
136 ~SynchronizedMinidumpManagerTest() override {} | 138 ~SynchronizedMinidumpManagerTest() override {} |
137 | 139 |
138 void SetUp() override { | 140 void SetUp() override { |
139 // Set up a temporary directory which will be used as our fake home dir. | 141 // Set up a temporary directory which will be used as our fake home dir. |
140 ASSERT_TRUE(base::CreateNewTempDirectory("", &fake_home_dir_)); | 142 ASSERT_TRUE(fake_home_dir_.CreateUniqueTempDir()); |
141 path_override_.reset( | 143 path_override_.reset( |
142 new base::ScopedPathOverride(base::DIR_HOME, fake_home_dir_)); | 144 new base::ScopedPathOverride(base::DIR_HOME, fake_home_dir_.path())); |
143 minidump_dir_ = fake_home_dir_.Append(kMinidumpSubdir); | 145 |
| 146 minidump_dir_ = fake_home_dir_.path().Append(kMinidumpSubdir); |
144 lockfile_ = minidump_dir_.Append(kLockfileName); | 147 lockfile_ = minidump_dir_.Append(kLockfileName); |
145 metadata_ = minidump_dir_.Append(kMetadataName); | 148 metadata_ = minidump_dir_.Append(kMetadataName); |
146 | 149 |
147 // Create a minidump directory. | 150 // Create a minidump directory. |
148 ASSERT_TRUE(base::CreateDirectory(minidump_dir_)); | 151 ASSERT_TRUE(base::CreateDirectory(minidump_dir_)); |
149 ASSERT_TRUE(base::IsDirectoryEmpty(minidump_dir_)); | 152 ASSERT_TRUE(base::IsDirectoryEmpty(minidump_dir_)); |
150 | 153 |
151 // Create a lockfile in that directory. | 154 // Create a lockfile in that directory. |
152 base::File lockfile( | 155 base::File lockfile( |
153 lockfile_, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | 156 lockfile_, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
154 ASSERT_TRUE(lockfile.IsValid()); | 157 ASSERT_TRUE(lockfile.IsValid()); |
155 } | 158 } |
156 | 159 |
157 void TearDown() override { | |
158 // Remove the temp directory. | |
159 path_override_.reset(); | |
160 ASSERT_TRUE(base::DeleteFile(fake_home_dir_, true)); | |
161 } | |
162 | |
163 protected: | 160 protected: |
164 base::FilePath fake_home_dir_; // Path to the test home directory. | |
165 base::FilePath minidump_dir_; // Path the the minidump directory. | 161 base::FilePath minidump_dir_; // Path the the minidump directory. |
166 base::FilePath lockfile_; // Path to the lockfile in |minidump_dir_|. | 162 base::FilePath lockfile_; // Path to the lockfile in |minidump_dir_|. |
167 base::FilePath metadata_; // Path to the metadata in |minidump_dir_|. | 163 base::FilePath metadata_; // Path to the metadata in |minidump_dir_|. |
168 | 164 |
169 private: | 165 private: |
| 166 base::ScopedTempDir fake_home_dir_; |
170 scoped_ptr<base::ScopedPathOverride> path_override_; | 167 scoped_ptr<base::ScopedPathOverride> path_override_; |
171 }; | 168 }; |
172 | 169 |
173 // Have |producer| generate |num_dumps| while checking there are no errors. | 170 // Have |producer| generate |num_dumps| while checking there are no errors. |
174 void produce_dumps(SynchronizedMinidumpManagerSimple& producer, int num_dumps) { | 171 void produce_dumps(SynchronizedMinidumpManagerSimple& producer, int num_dumps) { |
175 for (int i = 0; i < num_dumps; ++i) { | 172 for (int i = 0; i < num_dumps; ++i) { |
176 ASSERT_EQ(0, producer.DoWorkLocked()); | 173 ASSERT_EQ(0, producer.DoWorkLocked()); |
177 ASSERT_EQ(0, producer.add_entry_return_code()); | 174 ASSERT_EQ(0, producer.add_entry_return_code()); |
178 } | 175 } |
179 } | 176 } |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 const base::FilePath path = | 543 const base::FilePath path = |
547 base::FilePath(manager.dump_path()).Append("hello123"); | 544 base::FilePath(manager.dump_path()).Append("hello123"); |
548 const char kFileContents[] = "foobar"; | 545 const char kFileContents[] = "foobar"; |
549 ASSERT_EQ(static_cast<int>(sizeof(kFileContents)), | 546 ASSERT_EQ(static_cast<int>(sizeof(kFileContents)), |
550 WriteFile(path, kFileContents, sizeof(kFileContents))); | 547 WriteFile(path, kFileContents, sizeof(kFileContents))); |
551 | 548 |
552 ASSERT_TRUE(manager.HasDumps()); | 549 ASSERT_TRUE(manager.HasDumps()); |
553 } | 550 } |
554 | 551 |
555 } // namespace chromecast | 552 } // namespace chromecast |
OLD | NEW |