xref: /aosp_15_r20/external/cronet/components/metrics/environment_recorder_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 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.
4 
5 #include "components/metrics/environment_recorder.h"
6 
7 #include "components/metrics/metrics_pref_names.h"
8 #include "components/prefs/testing_pref_service.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/metrics_proto/system_profile.pb.h"
11 
12 namespace metrics {
13 
14 class EnvironmentRecorderTest : public testing::Test {
15  public:
EnvironmentRecorderTest()16   EnvironmentRecorderTest() {
17     EnvironmentRecorder::RegisterPrefs(prefs_.registry());
18   }
19 
20   EnvironmentRecorderTest(const EnvironmentRecorderTest&) = delete;
21   EnvironmentRecorderTest& operator=(const EnvironmentRecorderTest&) = delete;
22 
~EnvironmentRecorderTest()23   ~EnvironmentRecorderTest() override {}
24 
25  protected:
26   TestingPrefServiceSimple prefs_;
27 };
28 
TEST_F(EnvironmentRecorderTest,LoadEnvironmentFromPrefs)29 TEST_F(EnvironmentRecorderTest, LoadEnvironmentFromPrefs) {
30   const char* kSystemProfilePref = prefs::kStabilitySavedSystemProfile;
31   const char* kSystemProfileHashPref = prefs::kStabilitySavedSystemProfileHash;
32 
33   // The pref value is empty, so loading it from prefs should fail.
34   {
35     EnvironmentRecorder recorder(&prefs_);
36     SystemProfileProto system_profile;
37     EXPECT_FALSE(recorder.LoadEnvironmentFromPrefs(&system_profile));
38     EXPECT_FALSE(system_profile.has_app_version());
39   }
40 
41   // Do a RecordEnvironment() call and check whether the pref is recorded.
42   {
43     EnvironmentRecorder recorder(&prefs_);
44     SystemProfileProto system_profile;
45     system_profile.set_app_version("bogus version");
46     std::string serialized_profile =
47         recorder.SerializeAndRecordEnvironmentToPrefs(system_profile);
48     EXPECT_FALSE(serialized_profile.empty());
49     EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
50     EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
51   }
52 
53   // Load it and check that it has the right value.
54   {
55     EnvironmentRecorder recorder(&prefs_);
56     SystemProfileProto system_profile;
57     EXPECT_TRUE(recorder.LoadEnvironmentFromPrefs(&system_profile));
58     EXPECT_EQ("bogus version", system_profile.app_version());
59     // Ensure that the call did not clear the prefs.
60     EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
61     EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
62   }
63 
64   // Ensure that a non-matching hash results in the pref being invalid.
65   {
66     // Set the hash to a bad value.
67     prefs_.SetString(kSystemProfileHashPref, "deadbeef");
68     EnvironmentRecorder recorder(&prefs_);
69     SystemProfileProto system_profile;
70     EXPECT_FALSE(recorder.LoadEnvironmentFromPrefs(&system_profile));
71     EXPECT_FALSE(system_profile.has_app_version());
72   }
73 }
74 
75 }  // namespace metrics
76