OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_debugging.h" | 5 #include "components/browser_watcher/stability_debugging.h" |
6 | 6 |
7 #include "base/files/file_enumerator.h" | 7 #include <windows.h> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/debug/activity_tracker.h" |
8 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
11 #include "base/process/process.h" | 14 #include "base/process/process.h" |
12 #include "base/test/multiprocess_test.h" | 15 #include "base/test/multiprocess_test.h" |
13 #include "components/browser_watcher/stability_paths.h" | 16 #include "base/test/test_timeouts.h" |
| 17 #include "components/browser_watcher/stability_report.pb.h" |
| 18 #include "components/browser_watcher/stability_report_extractor.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "testing/multiprocess_func_list.h" | 20 #include "testing/multiprocess_func_list.h" |
16 | 21 |
17 namespace browser_watcher { | 22 namespace browser_watcher { |
18 | 23 |
19 class StabilityDebuggingWinMultiProcTest : public base::MultiProcessTest {}; | 24 using base::debug::GlobalActivityTracker; |
20 | 25 |
21 MULTIPROCESS_TEST_MAIN(DummyProcess) { | 26 const int kMemorySize = 1 << 20; // 1MiB |
22 return 0; | 27 const uint32_t exception_code = 42U; |
23 } | 28 const uint32_t exception_flag_continuable = 0U; |
24 | 29 |
25 TEST_F(StabilityDebuggingWinMultiProcTest, GetStabilityFileForProcessTest) { | 30 class StabilityDebuggingTest : public testing::Test { |
26 const base::FilePath empty_path; | 31 public: |
27 | 32 StabilityDebuggingTest() {} |
28 // Get the path for the current process. | 33 ~StabilityDebuggingTest() override { |
29 base::FilePath stability_path; | 34 GlobalActivityTracker* global_tracker = GlobalActivityTracker::Get(); |
30 ASSERT_TRUE(GetStabilityFileForProcess(base::Process::Current(), empty_path, | 35 if (global_tracker) { |
31 &stability_path)); | 36 global_tracker->ReleaseTrackerForCurrentThreadForTesting(); |
32 | 37 delete global_tracker; |
33 // Ensure requesting a second time produces the same. | 38 } |
34 base::FilePath stability_path_two; | |
35 ASSERT_TRUE(GetStabilityFileForProcess(base::Process::Current(), empty_path, | |
36 &stability_path_two)); | |
37 EXPECT_EQ(stability_path, stability_path_two); | |
38 | |
39 // Ensure a different process has a different stability path. | |
40 base::SpawnChildResult spawn_result = SpawnChild("DummyProcess"); | |
41 base::FilePath stability_path_other; | |
42 ASSERT_TRUE(GetStabilityFileForProcess(spawn_result.process, empty_path, | |
43 &stability_path_other)); | |
44 EXPECT_NE(stability_path, stability_path_other); | |
45 } | |
46 | |
47 TEST(StabilityDebuggingWinTest, | |
48 GetStabilityFilePatternMatchesGetStabilityFileForProcessResult) { | |
49 // GetStabilityFileForProcess file names must match GetStabilityFilePattern | |
50 // according to | |
51 // FileEnumerator's algorithm. We test this by writing out some files and | |
52 // validating what is matched. | |
53 | |
54 base::ScopedTempDir temp_dir; | |
55 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
56 base::FilePath user_data_dir = temp_dir.GetPath(); | |
57 | |
58 // Create the stability directory. | |
59 base::FilePath stability_dir = GetStabilityDir(user_data_dir); | |
60 ASSERT_TRUE(base::CreateDirectory(stability_dir)); | |
61 | |
62 // Write a stability file. | |
63 base::FilePath stability_file; | |
64 ASSERT_TRUE(GetStabilityFileForProcess(base::Process::Current(), | |
65 user_data_dir, &stability_file)); | |
66 { | |
67 base::ScopedFILE file(base::OpenFile(stability_file, "w")); | |
68 ASSERT_TRUE(file.get()); | |
69 } | 39 } |
70 | 40 |
71 // Write a file that shouldn't match. | 41 void SetUp() override { |
72 base::FilePath non_matching_file = | 42 testing::Test::SetUp(); |
73 stability_dir.AppendASCII("non_matching.foo"); | 43 |
74 { | 44 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
75 base::ScopedFILE file(base::OpenFile(non_matching_file, "w")); | 45 debug_path_ = temp_dir_.GetPath().AppendASCII("debug.pma"); |
76 ASSERT_TRUE(file.get()); | 46 |
| 47 GlobalActivityTracker::CreateWithFile(debug_path_, kMemorySize, 0ULL, "", |
| 48 3); |
77 } | 49 } |
78 | 50 |
79 // Validate only the stability file matches. | 51 const base::FilePath& debug_path() { return debug_path_; } |
80 base::FileEnumerator enumerator(stability_dir, false /* recursive */, | 52 |
81 base::FileEnumerator::FILES, | 53 private: |
82 GetStabilityFilePattern()); | 54 base::ScopedTempDir temp_dir_; |
83 ASSERT_EQ(stability_file, enumerator.Next()); | 55 base::FilePath debug_path_; |
84 ASSERT_TRUE(enumerator.Next().empty()); | 56 }; |
| 57 |
| 58 TEST_F(StabilityDebuggingTest, CrashingTest) { |
| 59 RegisterStabilityVEH(); |
| 60 |
| 61 // Raise an exception, then continue. |
| 62 __try { |
| 63 ::RaiseException(exception_code, exception_flag_continuable, 0U, nullptr); |
| 64 } __except (EXCEPTION_CONTINUE_EXECUTION) { |
| 65 } |
| 66 |
| 67 // Collect the report. |
| 68 StabilityReport report; |
| 69 ASSERT_EQ(SUCCESS, Extract(debug_path(), &report)); |
| 70 |
| 71 // Validate expectations. |
| 72 ASSERT_EQ(1, report.process_states_size()); |
| 73 const ProcessState& process_state = report.process_states(0); |
| 74 ASSERT_EQ(1, process_state.threads_size()); |
| 75 |
| 76 bool thread_found = false; |
| 77 for (const ThreadState& thread : process_state.threads()) { |
| 78 if (thread.thread_id() == ::GetCurrentThreadId()) { |
| 79 thread_found = true; |
| 80 ASSERT_TRUE(thread.has_exception()); |
| 81 const Exception& exception = thread.exception(); |
| 82 EXPECT_EQ(exception_code, exception.code()); |
| 83 EXPECT_NE(0ULL, exception.program_counter()); |
| 84 EXPECT_NE(0ULL, exception.exception_address()); |
| 85 EXPECT_NE(0LL, exception.time()); |
| 86 } |
| 87 } |
| 88 ASSERT_TRUE(thread_found); |
85 } | 89 } |
86 | 90 |
87 } // namespace browser_watcher | 91 } // namespace browser_watcher |
OLD | NEW |