OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "components/browser_watcher/stability_paths.h" | 5 #include "components/browser_watcher/stability_paths.h" |
6 | 6 |
| 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/files/file_path.h" |
7 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
8 #include "base/files/scoped_file.h" | 10 #include "base/files/scoped_file.h" |
9 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/process/process.h" |
| 13 #include "base/test/multiprocess_test.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/multiprocess_func_list.h" |
12 | 17 |
13 namespace browser_watcher { | 18 namespace browser_watcher { |
14 | 19 |
| 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. |
| 41 base::SpawnChildResult spawn_result = SpawnChild("DummyProcess"); |
| 42 base::FilePath stability_path_other; |
| 43 ASSERT_TRUE(GetStabilityFileForProcess(spawn_result.process, empty_path, |
| 44 &stability_path_other)); |
| 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 |
15 TEST(StabilityPathsTest, GetStabilityFiles) { | 88 TEST(StabilityPathsTest, GetStabilityFiles) { |
16 base::ScopedTempDir temp_dir; | 89 base::ScopedTempDir temp_dir; |
17 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 90 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
18 | 91 |
19 // Create files. | 92 // Create files. |
20 std::vector<base::FilePath> expected_paths; | 93 std::vector<base::FilePath> expected_paths; |
21 std::set<base::FilePath> excluded_paths; | 94 std::set<base::FilePath> excluded_paths; |
22 { | 95 { |
23 // Matches the pattern. | 96 // Matches the pattern. |
24 base::FilePath path = temp_dir.GetPath().AppendASCII("foo1.pma"); | 97 base::FilePath path = temp_dir.GetPath().AppendASCII("foo1.pma"); |
(...skipping 18 matching lines...) Expand all Loading... |
43 file.reset(base::OpenFile(path, "w")); | 116 file.reset(base::OpenFile(path, "w")); |
44 ASSERT_NE(file.get(), nullptr); | 117 ASSERT_NE(file.get(), nullptr); |
45 } | 118 } |
46 | 119 |
47 EXPECT_THAT(GetStabilityFiles(temp_dir.GetPath(), | 120 EXPECT_THAT(GetStabilityFiles(temp_dir.GetPath(), |
48 FILE_PATH_LITERAL("foo*.pma"), excluded_paths), | 121 FILE_PATH_LITERAL("foo*.pma"), excluded_paths), |
49 testing::UnorderedElementsAreArray(expected_paths)); | 122 testing::UnorderedElementsAreArray(expected_paths)); |
50 } | 123 } |
51 | 124 |
52 } // namespace browser_watcher | 125 } // namespace browser_watcher |
OLD | NEW |