1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/clean_exit_beacon.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_refptr.h"
14 #include "base/metrics/field_trial.h"
15 #include "base/test/gtest_util.h"
16 #include "base/test/metrics/histogram_tester.h"
17 #include "base/test/mock_entropy_provider.h"
18 #include "base/test/task_environment.h"
19 #include "base/time/time.h"
20 #include "build/build_config.h"
21 #include "components/metrics/metrics_pref_names.h"
22 #include "components/prefs/pref_registry_simple.h"
23 #include "components/prefs/pref_service_factory.h"
24 #include "components/prefs/testing_pref_service.h"
25 #include "components/prefs/testing_pref_store.h"
26 #include "components/variations/pref_names.h"
27 #include "components/variations/variations_test_utils.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "third_party/abseil-cpp/absl/types/optional.h"
34 const wchar_t kDummyWindowsRegistryKey[] = L"";
38 class TestCleanExitBeacon : public CleanExitBeacon {
40 explicit TestCleanExitBeacon(
41 PrefService* local_state,
42 const base::FilePath& user_data_dir = base::FilePath())
43 : CleanExitBeacon(kDummyWindowsRegistryKey, user_data_dir, local_state) {
47 ~TestCleanExitBeacon() override = default;
50 class CleanExitBeaconTest : public ::testing::Test {
52 void SetUp() override {
53 metrics::CleanExitBeacon::RegisterPrefs(prefs_.registry());
54 ASSERT_TRUE(user_data_dir_.CreateUniqueTempDir());
58 base::HistogramTester histogram_tester_;
59 TestingPrefServiceSimple prefs_;
60 base::ScopedTempDir user_data_dir_;
63 base::test::TaskEnvironment task_environment_;
66 struct BadBeaconTestParams {
67 const std::string test_name;
68 bool beacon_file_exists;
69 const std::string beacon_file_contents;
70 BeaconFileState beacon_file_state;
73 // Used for testing beacon files that are not well-formed, do not exist, etc.
74 class BadBeaconFileTest
75 : public testing::WithParamInterface<BadBeaconTestParams>,
76 public CleanExitBeaconTest {};
78 struct BeaconConsistencyTestParams {
80 const std::string test_name;
81 absl::optional<bool> beacon_file_beacon_value;
82 absl::optional<bool> platform_specific_beacon_value;
83 absl::optional<bool> local_state_beacon_value;
85 CleanExitBeaconConsistency expected_consistency;
89 // Used for testing the logic that emits to the UMA.CleanExitBeaconConsistency3
91 class BeaconFileAndPlatformBeaconConsistencyTest
92 : public testing::WithParamInterface<BeaconConsistencyTestParams>,
93 public CleanExitBeaconTest {};
94 #endif // BUILDFLAG(IS_IOS)
96 // Verify that the crash streak metric is 0 when default pref values are used.
97 TEST_F(CleanExitBeaconTest, CrashStreakMetricWithDefaultPrefs) {
98 CleanExitBeacon::ResetStabilityExitedCleanlyForTesting(&prefs_);
99 TestCleanExitBeacon clean_exit_beacon(&prefs_);
100 histogram_tester_.ExpectUniqueSample("Variations.SafeMode.Streak.Crashes", 0,
104 // Verify that the crash streak metric is 0 when prefs are explicitly set to
106 TEST_F(CleanExitBeaconTest, CrashStreakMetricWithNoCrashes) {
107 // The default value for kStabilityExitedCleanly is true, but defaults can
108 // change, so we explicitly set it to true here. Similarly, we explicitly set
109 // kVariationsCrashStreak to 0.
110 CleanExitBeacon::SetStabilityExitedCleanlyForTesting(&prefs_, true);
111 prefs_.SetInteger(variations::prefs::kVariationsCrashStreak, 0);
112 TestCleanExitBeacon clean_exit_beacon(&prefs_);
113 histogram_tester_.ExpectUniqueSample("Variations.SafeMode.Streak.Crashes", 0,
117 // Verify that the crash streak metric is correctly recorded when there is a
118 // non-zero crash streak.
119 TEST_F(CleanExitBeaconTest, CrashStreakMetricWithSomeCrashes) {
120 // The default value for kStabilityExitedCleanly is true, but defaults can
121 // change, so we explicitly set it to true here.
122 CleanExitBeacon::SetStabilityExitedCleanlyForTesting(&prefs_, true);
123 prefs_.SetInteger(variations::prefs::kVariationsCrashStreak, 1);
124 TestCleanExitBeacon clean_exit_beacon(&prefs_);
125 histogram_tester_.ExpectUniqueSample("Variations.SafeMode.Streak.Crashes", 1,
129 // Verify that the crash streak is correctly incremented and recorded when the
130 // last Chrome session did not exit cleanly.
131 TEST_F(CleanExitBeaconTest, CrashIncrementsCrashStreak) {
132 CleanExitBeacon::SetStabilityExitedCleanlyForTesting(&prefs_, false);
133 prefs_.SetInteger(variations::prefs::kVariationsCrashStreak, 1);
134 TestCleanExitBeacon clean_exit_beacon(&prefs_);
135 EXPECT_EQ(prefs_.GetInteger(variations::prefs::kVariationsCrashStreak), 2);
136 histogram_tester_.ExpectUniqueSample("Variations.SafeMode.Streak.Crashes", 2,
140 // Verify that the crash streak is correctly incremented and recorded when the
141 // last Chrome session did not exit cleanly and the default crash streak value
143 TEST_F(CleanExitBeaconTest,
144 CrashIncrementsCrashStreakWithDefaultCrashStreakPref) {
145 CleanExitBeacon::SetStabilityExitedCleanlyForTesting(&prefs_, false);
146 TestCleanExitBeacon clean_exit_beacon(&prefs_);
147 EXPECT_EQ(prefs_.GetInteger(variations::prefs::kVariationsCrashStreak), 1);
148 histogram_tester_.ExpectUniqueSample("Variations.SafeMode.Streak.Crashes", 1,
152 // Verify that no attempt is made to read the beacon file when no user
153 // data dir is provided.
154 TEST_F(CleanExitBeaconTest, InitWithoutUserDataDir) {
155 TestCleanExitBeacon beacon(&prefs_, base::FilePath());
156 EXPECT_TRUE(beacon.GetUserDataDirForTesting().empty());
157 EXPECT_TRUE(beacon.GetBeaconFilePathForTesting().empty());
158 histogram_tester_.ExpectTotalCount(
159 "Variations.ExtendedSafeMode.BeaconFileStateAtStartup", 0);
162 INSTANTIATE_TEST_SUITE_P(
167 .test_name = "NoVariationsFile",
168 .beacon_file_exists = false,
169 .beacon_file_contents = "",
170 .beacon_file_state = BeaconFileState::kNotDeserializable},
172 .test_name = "EmptyVariationsFile",
173 .beacon_file_exists = true,
174 .beacon_file_contents = "",
175 .beacon_file_state = BeaconFileState::kNotDeserializable},
177 .test_name = "NotDictionary",
178 .beacon_file_exists = true,
179 .beacon_file_contents = "{abc123",
180 .beacon_file_state = BeaconFileState::kNotDeserializable},
182 .test_name = "EmptyDictionary",
183 .beacon_file_exists = true,
184 .beacon_file_contents = "{}",
185 .beacon_file_state = BeaconFileState::kMissingDictionary},
187 .test_name = "MissingCrashStreak",
188 .beacon_file_exists = true,
189 .beacon_file_contents =
190 "{\"user_experience_metrics.stability.exited_cleanly\":true}",
191 .beacon_file_state = BeaconFileState::kMissingCrashStreak},
193 .test_name = "MissingBeacon",
194 .beacon_file_exists = true,
195 .beacon_file_contents = "{\"variations_crash_streak\":1}",
196 .beacon_file_state = BeaconFileState::kMissingBeacon}),
197 [](const ::testing::TestParamInfo<BadBeaconTestParams>& params) {
198 return params.param.test_name;
201 // Verify that the inability to get the beacon file's contents for a plethora of
202 // reasons (a) doesn't crash and (b) correctly records the BeaconFileState
204 TEST_P(BadBeaconFileTest, InitWithUnusableBeaconFile) {
205 BadBeaconTestParams params = GetParam();
207 const base::FilePath user_data_dir_path = user_data_dir_.GetPath();
208 if (params.beacon_file_exists) {
209 const base::FilePath temp_beacon_file_path =
210 user_data_dir_path.Append(kCleanExitBeaconFilename);
212 base::WriteFile(temp_beacon_file_path, params.beacon_file_contents));
215 TestCleanExitBeacon beacon(&prefs_, user_data_dir_path);
216 histogram_tester_.ExpectUniqueSample(
217 "Variations.ExtendedSafeMode.BeaconFileStateAtStartup",
218 params.beacon_file_state, 1);
221 // Verify that successfully reading the beacon file's contents results in
222 // correctly (a) setting the |did_previous_session_exit_cleanly_| field and (b)
223 // recording metrics when the last session exited cleanly.
224 TEST_F(CleanExitBeaconTest, InitWithBeaconFile) {
225 const base::FilePath user_data_dir_path = user_data_dir_.GetPath();
226 const base::FilePath temp_beacon_file_path =
227 user_data_dir_path.Append(kCleanExitBeaconFilename);
228 const int num_crashes = 2;
229 ASSERT_TRUE(base::WriteFile(
230 temp_beacon_file_path,
231 CleanExitBeacon::CreateBeaconFileContentsForTesting(
232 /*exited_cleanly=*/true, /*crash_streak=*/num_crashes)));
234 TestCleanExitBeacon clean_exit_beacon(&prefs_, user_data_dir_path);
235 histogram_tester_.ExpectUniqueSample(
236 "Variations.ExtendedSafeMode.BeaconFileStateAtStartup",
237 BeaconFileState::kReadable, 1);
238 EXPECT_TRUE(clean_exit_beacon.exited_cleanly());
239 histogram_tester_.ExpectUniqueSample("Variations.SafeMode.Streak.Crashes",
243 // Verify that successfully reading the beacon file's contents results in
244 // correctly (a) setting the |did_previous_session_exit_cleanly_| field and (b)
245 // recording metrics when the last session did not exit cleanly.
246 TEST_F(CleanExitBeaconTest, InitWithCrashAndBeaconFile) {
247 const base::FilePath user_data_dir_path = user_data_dir_.GetPath();
248 const base::FilePath temp_beacon_file_path =
249 user_data_dir_path.Append(kCleanExitBeaconFilename);
250 const int last_session_num_crashes = 2;
252 base::WriteFile(temp_beacon_file_path,
253 CleanExitBeacon::CreateBeaconFileContentsForTesting(
254 /*exited_cleanly=*/false,
255 /*crash_streak=*/last_session_num_crashes)));
257 const int updated_num_crashes = last_session_num_crashes + 1;
258 TestCleanExitBeacon clean_exit_beacon(&prefs_, user_data_dir_path);
259 histogram_tester_.ExpectUniqueSample(
260 "Variations.ExtendedSafeMode.BeaconFileStateAtStartup",
261 BeaconFileState::kReadable, 1);
262 EXPECT_FALSE(clean_exit_beacon.exited_cleanly());
263 histogram_tester_.ExpectUniqueSample("Variations.SafeMode.Streak.Crashes",
264 updated_num_crashes, 1);
267 TEST_F(CleanExitBeaconTest, WriteBeaconValueWhenNotExitingCleanly) {
268 const base::FilePath user_data_dir_path = user_data_dir_.GetPath();
269 const base::FilePath beacon_file_path =
270 user_data_dir_path.Append(kCleanExitBeaconFilename);
271 ASSERT_FALSE(base::PathExists(beacon_file_path));
273 TestCleanExitBeacon clean_exit_beacon(&prefs_, user_data_dir_path);
274 clean_exit_beacon.WriteBeaconValue(/*exited_cleanly=*/false,
275 /*is_extended_safe_mode=*/true);
277 // Verify that the beacon file exists and has well-formed contents after
278 // updating the beacon value.
279 EXPECT_TRUE(base::PathExists(beacon_file_path));
280 std::string beacon_file_contents1;
281 ASSERT_TRUE(base::ReadFileToString(beacon_file_path, &beacon_file_contents1));
282 EXPECT_EQ(beacon_file_contents1,
283 "{\"user_experience_metrics.stability.exited_cleanly\":false,"
284 "\"variations_crash_streak\":0}");
285 // Verify that the BeaconFileWrite metric was emitted.
286 histogram_tester_.ExpectUniqueSample(
287 "Variations.ExtendedSafeMode.BeaconFileWrite", 1, 1);
289 // Write the beacon value again. This is done because it is possible for
290 // WriteBeaconValue() to be called twice during startup or shutdown with the
291 // same value for |exited_cleanly|.
292 clean_exit_beacon.WriteBeaconValue(/*exited_cleanly*/ false,
293 /*is_extended_safe_mode=*/false);
295 // Verify that the beacon file exists and has well-formed contents after
296 // updating the beacon value.
297 EXPECT_TRUE(base::PathExists(beacon_file_path));
298 std::string beacon_file_contents2;
299 ASSERT_TRUE(base::ReadFileToString(beacon_file_path, &beacon_file_contents2));
300 EXPECT_EQ(beacon_file_contents2,
301 "{\"user_experience_metrics.stability.exited_cleanly\":false,"
302 "\"variations_crash_streak\":0}");
303 // Verify that the BeaconFileWrite metric was not emitted a second time. The
304 // beacon file should not have been written again since the beacon value did
306 histogram_tester_.ExpectUniqueSample(
307 "Variations.ExtendedSafeMode.BeaconFileWrite", 1, 1);
310 TEST_F(CleanExitBeaconTest, WriteBeaconValueWhenExitingCleanly) {
311 const base::FilePath user_data_dir_path = user_data_dir_.GetPath();
312 const base::FilePath beacon_file_path =
313 user_data_dir_path.Append(kCleanExitBeaconFilename);
314 ASSERT_FALSE(base::PathExists(beacon_file_path));
316 TestCleanExitBeacon clean_exit_beacon(&prefs_, user_data_dir_path);
317 clean_exit_beacon.WriteBeaconValue(/*exited_cleanly=*/true,
318 /*is_extended_safe_mode=*/false);
320 // Verify that the beacon file exists and has well-formed contents after
321 // updating the beacon value.
322 EXPECT_TRUE(base::PathExists(beacon_file_path));
323 std::string beacon_file_contents1;
324 ASSERT_TRUE(base::ReadFileToString(beacon_file_path, &beacon_file_contents1));
325 EXPECT_EQ(beacon_file_contents1,
326 "{\"user_experience_metrics.stability.exited_cleanly\":true,"
327 "\"variations_crash_streak\":0}");
328 // Verify that the BeaconFileWrite metric was emitted.
329 histogram_tester_.ExpectUniqueSample(
330 "Variations.ExtendedSafeMode.BeaconFileWrite", 1, 1);
332 // Write the beacon value again. This is done because it is possible for
333 // WriteBeaconValue() to be called twice during startup or shutdown with the
334 // same value for |exited_cleanly|.
335 clean_exit_beacon.WriteBeaconValue(/*exited_cleanly*/ true,
336 /*is_extended_safe_mode=*/false);
338 // Verify that the beacon file exists and has well-formed contents after
339 // updating the beacon value.
340 EXPECT_TRUE(base::PathExists(beacon_file_path));
341 std::string beacon_file_contents2;
342 ASSERT_TRUE(base::ReadFileToString(beacon_file_path, &beacon_file_contents2));
343 EXPECT_EQ(beacon_file_contents2,
344 "{\"user_experience_metrics.stability.exited_cleanly\":true,"
345 "\"variations_crash_streak\":0}");
346 // Verify that the BeaconFileWrite metric was not emitted a second time. The
347 // beacon file should not have been written again since the beacon value did
349 histogram_tester_.ExpectUniqueSample(
350 "Variations.ExtendedSafeMode.BeaconFileWrite", 1, 1);
353 // Verify that there's a DCHECK when attempting to write a clean beacon with
354 // |is_extended_safe_mode| set to true. When |is_extended_safe_mode| is true,
355 // the only valid value for |exited_cleanly| is false.
356 TEST_F(CleanExitBeaconTest, InvalidWriteBeaconValueArgsTriggerDcheck) {
357 TestCleanExitBeacon clean_exit_beacon(&prefs_, user_data_dir_.GetPath());
359 clean_exit_beacon.WriteBeaconValue(/*exited_cleanly=*/true,
360 /*is_extended_safe_mode=*/true));
363 #if BUILDFLAG(IS_IOS)
364 // Verify the logic for recording UMA.CleanExitBeaconConsistency3.
365 INSTANTIATE_TEST_SUITE_P(
367 BeaconFileAndPlatformBeaconConsistencyTest,
369 BeaconConsistencyTestParams{
370 .test_name = "MissingMissing",
371 .expected_consistency =
372 CleanExitBeaconConsistency::kMissingMissing},
373 BeaconConsistencyTestParams{
374 .test_name = "MissingClean",
375 .platform_specific_beacon_value = true,
376 .expected_consistency = CleanExitBeaconConsistency::kMissingClean},
377 BeaconConsistencyTestParams{
378 .test_name = "MissingDirty",
379 .platform_specific_beacon_value = false,
380 .expected_consistency = CleanExitBeaconConsistency::kMissingDirty},
381 BeaconConsistencyTestParams{
382 .test_name = "CleanMissing",
383 .beacon_file_beacon_value = true,
384 .expected_consistency = CleanExitBeaconConsistency::kCleanMissing},
385 BeaconConsistencyTestParams{
386 .test_name = "DirtyMissing",
387 .beacon_file_beacon_value = false,
388 .expected_consistency = CleanExitBeaconConsistency::kDirtyMissing},
389 BeaconConsistencyTestParams{
390 .test_name = "CleanClean",
391 .beacon_file_beacon_value = true,
392 .platform_specific_beacon_value = true,
393 .expected_consistency = CleanExitBeaconConsistency::kCleanClean},
394 BeaconConsistencyTestParams{
395 .test_name = "CleanDirty",
396 .beacon_file_beacon_value = true,
397 .platform_specific_beacon_value = false,
398 .expected_consistency = CleanExitBeaconConsistency::kCleanDirty},
399 BeaconConsistencyTestParams{
400 .test_name = "DirtyClean",
401 .beacon_file_beacon_value = false,
402 .platform_specific_beacon_value = true,
403 .expected_consistency = CleanExitBeaconConsistency::kDirtyClean},
404 BeaconConsistencyTestParams{
405 .test_name = "DirtyDirty",
406 .beacon_file_beacon_value = false,
407 .platform_specific_beacon_value = false,
408 .expected_consistency = CleanExitBeaconConsistency::kDirtyDirty}),
409 [](const ::testing::TestParamInfo<BeaconConsistencyTestParams>& params) {
410 return params.param.test_name;
413 TEST_P(BeaconFileAndPlatformBeaconConsistencyTest, BeaconConsistency) {
414 // Verify that the beacon file is not present. Unless set below, this beacon
415 // is considered missing.
416 const base::FilePath user_data_dir_path = user_data_dir_.GetPath();
417 const base::FilePath temp_beacon_file_path =
418 user_data_dir_path.Append(kCleanExitBeaconFilename);
419 ASSERT_FALSE(base::PathExists(temp_beacon_file_path));
420 // Clear the platform-specific beacon. Unless set below, this beacon is also
421 // considered missing.
422 CleanExitBeacon::ResetStabilityExitedCleanlyForTesting(&prefs_);
424 BeaconConsistencyTestParams params = GetParam();
425 if (params.beacon_file_beacon_value) {
426 ASSERT_TRUE(base::WriteFile(
427 temp_beacon_file_path,
428 CleanExitBeacon::CreateBeaconFileContentsForTesting(
429 /*exited_cleanly=*/params.beacon_file_beacon_value.value(),
430 /*crash_streak=*/0)));
432 if (params.platform_specific_beacon_value) {
433 CleanExitBeacon::SetUserDefaultsBeacon(
434 /*exited_cleanly=*/params.platform_specific_beacon_value.value());
437 TestCleanExitBeacon clean_exit_beacon(&prefs_, user_data_dir_path);
438 histogram_tester_.ExpectUniqueSample("UMA.CleanExitBeaconConsistency3",
439 params.expected_consistency, 1);
441 #endif // BUILDFLAG(IS_IOS)
443 } // namespace metrics