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
3 // found in the LICENSE file.
4
5 #include "components/update_client/updater_state.h"
6
7 #include "base/macros.h"
8 #include "base/time/time.h"
9 #include "base/version.h"
10 #include "build/branding_buildflags.h"
11 #include "build/build_config.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace update_client {
15
16 class UpdaterStateTest : public testing::Test {
17 public:
18 UpdaterStateTest() = default;
19 ~UpdaterStateTest() override = default;
20
21 private:
22 DISALLOW_COPY_AND_ASSIGN(UpdaterStateTest);
23 };
24
TEST_F(UpdaterStateTest,Serialize)25 TEST_F(UpdaterStateTest, Serialize) {
26 UpdaterState updater_state(false);
27
28 updater_state.updater_name_ = "the updater";
29 updater_state.updater_version_ = base::Version("1.0");
30 updater_state.last_autoupdate_started_ = base::Time::NowFromSystemTime();
31 updater_state.last_checked_ = base::Time::NowFromSystemTime();
32 updater_state.is_enterprise_managed_ = false;
33 updater_state.is_autoupdate_check_enabled_ = true;
34 updater_state.update_policy_ = 1;
35
36 auto attributes = updater_state.BuildAttributes();
37
38 // Sanity check all members.
39 EXPECT_STREQ("the updater", attributes.at("name").c_str());
40 EXPECT_STREQ("1.0", attributes.at("version").c_str());
41 EXPECT_STREQ("0", attributes.at("laststarted").c_str());
42 EXPECT_STREQ("0", attributes.at("lastchecked").c_str());
43 EXPECT_STREQ("0", attributes.at("domainjoined").c_str());
44 EXPECT_STREQ("1", attributes.at("autoupdatecheckenabled").c_str());
45 EXPECT_STREQ("1", attributes.at("updatepolicy").c_str());
46
47 #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
48 #if defined(OS_WIN)
49 // The value of "ismachine".
50 EXPECT_STREQ("0", UpdaterState::GetState(false)->at("ismachine").c_str());
51 EXPECT_STREQ("1", UpdaterState::GetState(true)->at("ismachine").c_str());
52
53 // The name of the Windows updater for Chrome.
54 EXPECT_STREQ("Omaha", UpdaterState::GetState(false)->at("name").c_str());
55 #elif defined(OS_MACOSX) && !defined(OS_IOS)
56 // MacOS does not serialize "ismachine".
57 EXPECT_EQ(0UL, UpdaterState::GetState(false)->count("ismachine"));
58 EXPECT_EQ(0UL, UpdaterState::GetState(true)->count("ismachine"));
59 EXPECT_STREQ("Keystone", UpdaterState::GetState(false)->at("name").c_str());
60 #endif // OS_WIN
61 #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
62
63 // Tests some of the remaining values.
64 updater_state = UpdaterState(false);
65
66 // Don't serialize an invalid version if it could not be read.
67 updater_state.updater_version_ = base::Version();
68 attributes = updater_state.BuildAttributes();
69 EXPECT_EQ(0u, attributes.count("version"));
70
71 updater_state.updater_version_ = base::Version("0.0.0.0");
72 attributes = updater_state.BuildAttributes();
73 EXPECT_STREQ("0.0.0.0", attributes.at("version").c_str());
74
75 updater_state.last_autoupdate_started_ =
76 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(15);
77 attributes = updater_state.BuildAttributes();
78 EXPECT_STREQ("336", attributes.at("laststarted").c_str());
79
80 updater_state.last_autoupdate_started_ =
81 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(58);
82 attributes = updater_state.BuildAttributes();
83 EXPECT_STREQ("1344", attributes.at("laststarted").c_str());
84
85 updater_state.last_autoupdate_started_ =
86 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(90);
87 attributes = updater_state.BuildAttributes();
88 EXPECT_STREQ("1344", attributes.at("laststarted").c_str());
89
90 // Don't serialize the time if it could not be read.
91 updater_state.last_autoupdate_started_ = base::Time();
92 attributes = updater_state.BuildAttributes();
93 EXPECT_EQ(0u, attributes.count("laststarted"));
94
95 updater_state.last_checked_ =
96 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(15);
97 attributes = updater_state.BuildAttributes();
98 EXPECT_STREQ("336", attributes.at("lastchecked").c_str());
99
100 updater_state.last_checked_ =
101 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(90);
102 attributes = updater_state.BuildAttributes();
103 EXPECT_STREQ("1344", attributes.at("lastchecked").c_str());
104
105 // Don't serialize the time if it could not be read (the value is invalid).
106 updater_state.last_checked_ = base::Time();
107 attributes = updater_state.BuildAttributes();
108 EXPECT_EQ(0u, attributes.count("lastchecked"));
109
110 updater_state.is_enterprise_managed_ = true;
111 attributes = updater_state.BuildAttributes();
112 EXPECT_STREQ("1", attributes.at("domainjoined").c_str());
113
114 updater_state.is_autoupdate_check_enabled_ = false;
115 attributes = updater_state.BuildAttributes();
116 EXPECT_STREQ("0", attributes.at("autoupdatecheckenabled").c_str());
117
118 updater_state.update_policy_ = 0;
119 attributes = updater_state.BuildAttributes();
120 EXPECT_STREQ("0", attributes.at("updatepolicy").c_str());
121
122 updater_state.update_policy_ = -1;
123 attributes = updater_state.BuildAttributes();
124 EXPECT_STREQ("-1", attributes.at("updatepolicy").c_str());
125 }
126
127 } // namespace update_client
128