manzagop | 194acde | 2017-06-05 21:12:24 | [diff] [blame] | 1 | // Copyright 2017 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_paths.h" |
| 6 | |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 7 | #include "base/files/file_enumerator.h" |
| 8 | #include "base/files/file_path.h" |
manzagop | 194acde | 2017-06-05 21:12:24 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
| 10 | #include "base/files/scoped_file.h" |
| 11 | #include "base/files/scoped_temp_dir.h" |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 12 | #include "base/process/process.h" |
| 13 | #include "base/test/multiprocess_test.h" |
manzagop | 194acde | 2017-06-05 21:12:24 | [diff] [blame] | 14 | #include "testing/gmock/include/gmock/gmock.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 16 | #include "testing/multiprocess_func_list.h" |
manzagop | 194acde | 2017-06-05 21:12:24 | [diff] [blame] | 17 | |
| 18 | namespace browser_watcher { |
| 19 | |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 20 | class StabilityPathsMultiProcTest : public base::MultiProcessTest {}; |
| 21 | |
| 22 | MULTIPROCESS_TEST_MAIN(DummyProcess) { |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | TEST_F(StabilityPathsMultiProcTest, GetStabilityFileForProcessTest) { |
| 27 | const base::FilePath empty_path; |
| 28 | |
| 29 | // Get the path for the current process. |
| 30 | base::FilePath stability_path; |
| 31 | ASSERT_TRUE(GetStabilityFileForProcess(base::Process::Current(), empty_path, |
| 32 | &stability_path)); |
| 33 | |
| 34 | // Ensure requesting a second time produces the same. |
| 35 | base::FilePath stability_path_two; |
| 36 | ASSERT_TRUE(GetStabilityFileForProcess(base::Process::Current(), empty_path, |
| 37 | &stability_path_two)); |
| 38 | EXPECT_EQ(stability_path, stability_path_two); |
| 39 | |
| 40 | // Ensure a different process has a different stability path. |
Jay Civelli | 4a44260b | 2017-08-21 19:26:29 | [diff] [blame] | 41 | base::Process process = SpawnChild("DummyProcess"); |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 42 | base::FilePath stability_path_other; |
Jay Civelli | 4a44260b | 2017-08-21 19:26:29 | [diff] [blame] | 43 | ASSERT_TRUE( |
| 44 | GetStabilityFileForProcess(process, empty_path, &stability_path_other)); |
manzagop | e17e32d | 2017-06-14 20:51:27 | [diff] [blame] | 45 | EXPECT_NE(stability_path, stability_path_other); |
| 46 | } |
| 47 | |
| 48 | TEST(StabilityPathsTest, |
| 49 | GetStabilityFilePatternMatchesGetStabilityFileForProcessResult) { |
| 50 | // GetStabilityFileForProcess file names must match GetStabilityFilePattern |
| 51 | // according to |
| 52 | // FileEnumerator's algorithm. We test this by writing out some files and |
| 53 | // validating what is matched. |
| 54 | |
| 55 | base::ScopedTempDir temp_dir; |
| 56 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 57 | base::FilePath user_data_dir = temp_dir.GetPath(); |
| 58 | |
| 59 | // Create the stability directory. |
| 60 | base::FilePath stability_dir = GetStabilityDir(user_data_dir); |
| 61 | ASSERT_TRUE(base::CreateDirectory(stability_dir)); |
| 62 | |
| 63 | // Write a stability file. |
| 64 | base::FilePath stability_file; |
| 65 | ASSERT_TRUE(GetStabilityFileForProcess(base::Process::Current(), |
| 66 | user_data_dir, &stability_file)); |
| 67 | { |
| 68 | base::ScopedFILE file(base::OpenFile(stability_file, "w")); |
| 69 | ASSERT_TRUE(file.get()); |
| 70 | } |
| 71 | |
| 72 | // Write a file that shouldn't match. |
| 73 | base::FilePath non_matching_file = |
| 74 | stability_dir.AppendASCII("non_matching.foo"); |
| 75 | { |
| 76 | base::ScopedFILE file(base::OpenFile(non_matching_file, "w")); |
| 77 | ASSERT_TRUE(file.get()); |
| 78 | } |
| 79 | |
| 80 | // Validate only the stability file matches. |
| 81 | base::FileEnumerator enumerator(stability_dir, false /* recursive */, |
| 82 | base::FileEnumerator::FILES, |
| 83 | GetStabilityFilePattern()); |
| 84 | ASSERT_EQ(stability_file, enumerator.Next()); |
| 85 | ASSERT_TRUE(enumerator.Next().empty()); |
| 86 | } |
| 87 | |
manzagop | 194acde | 2017-06-05 21:12:24 | [diff] [blame] | 88 | TEST(StabilityPathsTest, GetStabilityFiles) { |
| 89 | base::ScopedTempDir temp_dir; |
| 90 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 91 | |
| 92 | // Create files. |
| 93 | std::vector<base::FilePath> expected_paths; |
| 94 | std::set<base::FilePath> excluded_paths; |
| 95 | { |
| 96 | // Matches the pattern. |
| 97 | base::FilePath path = temp_dir.GetPath().AppendASCII("foo1.pma"); |
| 98 | base::ScopedFILE file(base::OpenFile(path, "w")); |
| 99 | ASSERT_NE(file.get(), nullptr); |
| 100 | expected_paths.push_back(path); |
| 101 | |
| 102 | // Matches the pattern, but is excluded. |
| 103 | path = temp_dir.GetPath().AppendASCII("foo2.pma"); |
| 104 | file.reset(base::OpenFile(path, "w")); |
| 105 | ASSERT_NE(file.get(), nullptr); |
| 106 | ASSERT_TRUE(excluded_paths.insert(path).second); |
| 107 | |
| 108 | // Matches the pattern. |
| 109 | path = temp_dir.GetPath().AppendASCII("foo3.pma"); |
| 110 | file.reset(base::OpenFile(path, "w")); |
| 111 | ASSERT_NE(file.get(), nullptr); |
| 112 | expected_paths.push_back(path); |
| 113 | |
| 114 | // Does not match the pattern. |
| 115 | path = temp_dir.GetPath().AppendASCII("bar.baz"); |
| 116 | file.reset(base::OpenFile(path, "w")); |
| 117 | ASSERT_NE(file.get(), nullptr); |
| 118 | } |
| 119 | |
| 120 | EXPECT_THAT(GetStabilityFiles(temp_dir.GetPath(), |
| 121 | FILE_PATH_LITERAL("foo*.pma"), excluded_paths), |
| 122 | testing::UnorderedElementsAreArray(expected_paths)); |
| 123 | } |
| 124 | |
| 125 | } // namespace browser_watcher |