OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "components/browser_watcher/stability_debugging_win.h" |
| 6 |
| 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/process/process.h" |
| 12 #include "base/test/multiprocess_test.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/multiprocess_func_list.h" |
| 15 |
| 16 namespace browser_watcher { |
| 17 |
| 18 class StabilityDebuggingWinMultiProcTest : public base::MultiProcessTest {}; |
| 19 |
| 20 MULTIPROCESS_TEST_MAIN(DummyProcess) { |
| 21 return 0; |
| 22 } |
| 23 |
| 24 TEST_F(StabilityDebuggingWinMultiProcTest, GetStabilityFileForProcessTest) { |
| 25 const base::FilePath empty_path; |
| 26 |
| 27 // Get the path for the current process. |
| 28 base::FilePath stability_path; |
| 29 ASSERT_TRUE(GetStabilityFileForProcess(base::Process::Current(), empty_path, |
| 30 &stability_path)); |
| 31 |
| 32 // Ensure requesting a second time produces the same. |
| 33 base::FilePath stability_path_two; |
| 34 ASSERT_TRUE(GetStabilityFileForProcess(base::Process::Current(), empty_path, |
| 35 &stability_path_two)); |
| 36 EXPECT_EQ(stability_path, stability_path_two); |
| 37 |
| 38 // Ensure a different process has a different stability path. |
| 39 base::FilePath stability_path_other; |
| 40 ASSERT_TRUE(GetStabilityFileForProcess(SpawnChild("DummyProcess"), empty_path, |
| 41 &stability_path_other)); |
| 42 EXPECT_NE(stability_path, stability_path_other); |
| 43 } |
| 44 |
| 45 TEST(StabilityDebuggingWinTest, |
| 46 GetStabilityFilePatternMatchesGetStabilityFileForProcessResult) { |
| 47 // GetStabilityFileForProcess file names must match GetStabilityFilePattern |
| 48 // according to |
| 49 // FileEnumerator's algorithm. We test this by writing out some files and |
| 50 // validating what is matched. |
| 51 |
| 52 base::ScopedTempDir temp_dir; |
| 53 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 54 base::FilePath user_data_dir = temp_dir.path(); |
| 55 |
| 56 // Create the stability directory. |
| 57 base::FilePath stability_dir = GetStabilityDir(user_data_dir); |
| 58 ASSERT_TRUE(base::CreateDirectory(stability_dir)); |
| 59 |
| 60 // Write a stability file. |
| 61 base::FilePath stability_file; |
| 62 ASSERT_TRUE(GetStabilityFileForProcess(base::Process::Current(), |
| 63 user_data_dir, &stability_file)); |
| 64 { |
| 65 base::ScopedFILE file(base::OpenFile(stability_file, "w")); |
| 66 ASSERT_TRUE(file.get()); |
| 67 } |
| 68 |
| 69 // Write a file that shouldn't match. |
| 70 base::FilePath non_matching_file = |
| 71 stability_dir.AppendASCII("non_matching.foo"); |
| 72 { |
| 73 base::ScopedFILE file(base::OpenFile(non_matching_file, "w")); |
| 74 ASSERT_TRUE(file.get()); |
| 75 } |
| 76 |
| 77 // Validate only the stability file matches. |
| 78 base::FileEnumerator enumerator(stability_dir, false /* recursive */, |
| 79 base::FileEnumerator::FILES, |
| 80 GetStabilityFilePattern()); |
| 81 ASSERT_EQ(stability_file, enumerator.Next()); |
| 82 ASSERT_TRUE(enumerator.Next().empty()); |
| 83 } |
| 84 |
| 85 } // namespace browser_watcher |
OLD | NEW |