OLD | NEW |
---|---|
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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/watcher_metrics_provider_win.h" | 5 #include "components/browser_watcher/watcher_metrics_provider_win.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <cstdlib> | 10 #include <cstdlib> |
11 | 11 |
12 #include "base/process/process_handle.h" | 12 #include "base/process/process_handle.h" |
13 #include "base/run_loop.h" | |
13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 #include "base/test/histogram_tester.h" | 16 #include "base/test/histogram_tester.h" |
16 #include "base/test/test_reg_util_win.h" | 17 #include "base/test/test_reg_util_win.h" |
17 #include "base/test/test_simple_task_runner.h" | |
18 #include "base/win/registry.h" | 18 #include "base/win/registry.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace browser_watcher { | 21 namespace browser_watcher { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 using GetExecutableDetailsCallback = | 25 using GetExecutableDetailsCallback = |
26 WatcherMetricsProviderWin::GetExecutableDetailsCallback; | 26 WatcherMetricsProviderWin::GetExecutableDetailsCallback; |
27 | 27 |
28 const wchar_t kRegistryPath[] = L"Software\\WatcherMetricsProviderWinTest"; | 28 const wchar_t kRegistryPath[] = L"Software\\WatcherMetricsProviderWinTest"; |
29 | 29 |
30 class WatcherMetricsProviderWinTest : public testing::Test { | 30 class WatcherMetricsProviderWinTest : public testing::Test { |
31 public: | 31 public: |
32 typedef testing::Test Super; | 32 typedef testing::Test Super; |
33 | 33 |
34 void SetUp() override { | 34 void SetUp() override { |
35 Super::SetUp(); | 35 Super::SetUp(); |
36 | 36 |
37 ASSERT_NO_FATAL_FAILURE( | 37 ASSERT_NO_FATAL_FAILURE( |
38 override_manager_.OverrideRegistry(HKEY_CURRENT_USER)); | 38 override_manager_.OverrideRegistry(HKEY_CURRENT_USER)); |
39 test_task_runner_ = new base::TestSimpleTaskRunner(); | |
40 } | 39 } |
41 | 40 |
42 void AddProcessExitCode(bool use_own_pid, int exit_code) { | 41 void AddProcessExitCode(bool use_own_pid, int exit_code) { |
43 int pid = 0; | 42 int pid = 0; |
44 if (use_own_pid) { | 43 if (use_own_pid) { |
45 pid = base::GetCurrentProcId(); | 44 pid = base::GetCurrentProcId(); |
46 } else { | 45 } else { |
47 // Make sure not to accidentally collide with own pid. | 46 // Make sure not to accidentally collide with own pid. |
48 do { | 47 do { |
49 pid = rand(); | 48 pid = rand(); |
(...skipping 11 matching lines...) Expand all Loading... | |
61 } | 60 } |
62 | 61 |
63 size_t ExitCodeRegistryPathValueCount() { | 62 size_t ExitCodeRegistryPathValueCount() { |
64 base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_READ); | 63 base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_READ); |
65 return key.GetValueCount(); | 64 return key.GetValueCount(); |
66 } | 65 } |
67 | 66 |
68 protected: | 67 protected: |
69 registry_util::RegistryOverrideManager override_manager_; | 68 registry_util::RegistryOverrideManager override_manager_; |
70 base::HistogramTester histogram_tester_; | 69 base::HistogramTester histogram_tester_; |
71 scoped_refptr<base::TestSimpleTaskRunner> test_task_runner_; | |
72 }; | 70 }; |
73 | 71 |
74 } // namespace | 72 } // namespace |
75 | 73 |
76 TEST_F(WatcherMetricsProviderWinTest, RecordsStabilityHistogram) { | 74 TEST_F(WatcherMetricsProviderWinTest, RecordsStabilityHistogram) { |
77 // Record multiple success exits. | 75 // Record multiple success exits. |
78 for (size_t i = 0; i < 11; ++i) | 76 for (size_t i = 0; i < 11; ++i) |
79 AddProcessExitCode(false, 0); | 77 AddProcessExitCode(false, 0); |
80 | 78 |
81 // Record a single failure. | 79 // Record a single failure. |
82 AddProcessExitCode(false, 100); | 80 AddProcessExitCode(false, 100); |
83 | 81 |
84 WatcherMetricsProviderWin provider( | 82 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(), |
85 kRegistryPath, base::FilePath(), base::FilePath(), | 83 base::FilePath(), |
86 GetExecutableDetailsCallback(), test_task_runner_.get()); | 84 GetExecutableDetailsCallback()); |
87 | 85 |
88 provider.ProvideStabilityMetrics(NULL); | 86 provider.ProvideStabilityMetrics(NULL); |
89 histogram_tester_.ExpectBucketCount( | 87 histogram_tester_.ExpectBucketCount( |
90 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); | 88 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); |
91 histogram_tester_.ExpectBucketCount( | 89 histogram_tester_.ExpectBucketCount( |
92 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 100, 1); | 90 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 100, 1); |
93 histogram_tester_.ExpectTotalCount( | 91 histogram_tester_.ExpectTotalCount( |
94 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 12); | 92 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 12); |
95 | 93 |
96 // Verify that the reported values are gone. | 94 // Verify that the reported values are gone. |
97 EXPECT_EQ(0u, ExitCodeRegistryPathValueCount()); | 95 EXPECT_EQ(0u, ExitCodeRegistryPathValueCount()); |
98 } | 96 } |
99 | 97 |
100 TEST_F(WatcherMetricsProviderWinTest, DoesNotReportOwnProcessId) { | 98 TEST_F(WatcherMetricsProviderWinTest, DoesNotReportOwnProcessId) { |
101 // Record multiple success exits. | 99 // Record multiple success exits. |
102 for (size_t i = 0; i < 11; ++i) | 100 for (size_t i = 0; i < 11; ++i) |
103 AddProcessExitCode(i, 0); | 101 AddProcessExitCode(i, 0); |
104 | 102 |
105 // Record own process as STILL_ACTIVE. | 103 // Record own process as STILL_ACTIVE. |
106 AddProcessExitCode(true, STILL_ACTIVE); | 104 AddProcessExitCode(true, STILL_ACTIVE); |
107 | 105 |
108 WatcherMetricsProviderWin provider( | 106 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(), |
109 kRegistryPath, base::FilePath(), base::FilePath(), | 107 base::FilePath(), |
110 GetExecutableDetailsCallback(), test_task_runner_.get()); | 108 GetExecutableDetailsCallback()); |
111 | 109 |
112 provider.ProvideStabilityMetrics(NULL); | 110 provider.ProvideStabilityMetrics(NULL); |
113 histogram_tester_.ExpectUniqueSample( | 111 histogram_tester_.ExpectUniqueSample( |
114 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); | 112 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); |
115 | 113 |
116 // Verify that the reported values are gone. | 114 // Verify that the reported values are gone. |
117 EXPECT_EQ(1u, ExitCodeRegistryPathValueCount()); | 115 EXPECT_EQ(1u, ExitCodeRegistryPathValueCount()); |
118 } | 116 } |
119 | 117 |
120 TEST_F(WatcherMetricsProviderWinTest, DeletesExitcodeKeyWhenNotReporting) { | 118 TEST_F(WatcherMetricsProviderWinTest, DeletesExitcodeKeyWhenNotReporting) { |
121 // Test that the registry at kRegistryPath is deleted when reporting is | 119 // Test that the registry at kRegistryPath is deleted when reporting is |
122 // disabled. | 120 // disabled. |
123 | 121 |
124 // Record multiple success exits. | 122 // Record multiple success exits. |
125 for (size_t i = 0; i < 11; ++i) | 123 for (size_t i = 0; i < 11; ++i) |
126 AddProcessExitCode(false, 0); | 124 AddProcessExitCode(false, 0); |
127 // Record a single failure. | 125 // Record a single failure. |
128 AddProcessExitCode(false, 100); | 126 AddProcessExitCode(false, 100); |
129 | 127 |
130 // Make like the user is opted out of reporting. | 128 // Make like the user is opted out of reporting. |
131 WatcherMetricsProviderWin provider( | 129 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(), |
132 kRegistryPath, base::FilePath(), base::FilePath(), | 130 base::FilePath(), |
133 GetExecutableDetailsCallback(), test_task_runner_.get()); | 131 GetExecutableDetailsCallback()); |
134 provider.OnRecordingDisabled(); | 132 provider.OnRecordingDisabled(); |
135 | 133 |
136 base::win::RegKey key; | 134 base::win::RegKey key; |
137 { | 135 { |
138 // The deletion should be scheduled to the test_task_runner, and not happen | 136 // The deletion should be scheduled to the task scheduler, and not happen |
139 // immediately. | 137 // immediately. |
140 ASSERT_EQ(ERROR_SUCCESS, | 138 ASSERT_EQ(ERROR_SUCCESS, |
141 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ)); | 139 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ)); |
Ilya Sherman
2017/06/30 18:29:41
It's not clear to me whether this is appropriate t
manzagop (departed)
2017/06/30 20:11:43
Could this simply go to l.127?
Ilya Sherman
2017/07/03 02:35:07
It could, but it would be changing the meaning of
manzagop (departed)
2017/07/04 12:26:25
Ah, got it. So deletion is the intent and the post
Ilya Sherman
2017/07/05 18:09:42
Done.
| |
142 } | 140 } |
143 | 141 |
144 // Flush the task(s). | 142 // Flush the task(s). |
145 test_task_runner_->RunPendingTasks(); | 143 base::RunLoop().RunUntilIdle(); |
146 | 144 |
147 // Make sure the subkey for the pseudo process has been deleted on reporting. | 145 // Make sure the subkey for the pseudo process has been deleted on reporting. |
148 ASSERT_EQ(ERROR_FILE_NOT_FOUND, | 146 ASSERT_EQ(ERROR_FILE_NOT_FOUND, |
149 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ)); | 147 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ)); |
150 } | 148 } |
151 | 149 |
152 } // namespace browser_watcher | 150 } // namespace browser_watcher |
OLD | NEW |