blob: d8d96b371050b98620127a854823f49f79d0b26c [file] [log] [blame]
sorin97bd0292016-11-14 19:46:531// 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
Nico Weber356b3042019-08-23 15:30:415#include "components/update_client/updater_state.h"
6
sorin97bd0292016-11-14 19:46:537#include "base/macros.h"
8#include "base/time/time.h"
9#include "base/version.h"
Nico Weber356b3042019-08-23 15:30:4110#include "build/branding_buildflags.h"
sorin96d71e0c2017-01-25 17:39:1111#include "build/build_config.h"
sorin97bd0292016-11-14 19:46:5312#include "testing/gtest/include/gtest/gtest.h"
13
14namespace update_client {
15
16class UpdaterStateTest : public testing::Test {
17 public:
Sorin Jianu30881152020-03-16 14:31:1918 UpdaterStateTest() = default;
19 ~UpdaterStateTest() override = default;
sorin97bd0292016-11-14 19:46:5320
21 private:
22 DISALLOW_COPY_AND_ASSIGN(UpdaterStateTest);
23};
24
25TEST_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();
rogerta57aed382017-02-23 14:50:4532 updater_state.is_enterprise_managed_ = false;
sorin97bd0292016-11-14 19:46:5333 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
Nico Weber356b3042019-08-23 15:30:4147#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
Sorin Jianudf2b9cc2018-02-05 19:11:0348#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());
Olivier Robin694c68aa2017-07-28 08:40:2355#elif defined(OS_MACOSX) && !defined(OS_IOS)
Sorin Jianudf2b9cc2018-02-05 19:11:0356 // MacOS does not serialize "ismachine".
Xiaoling Bao8fe2c8ab2018-02-23 19:38:3057 EXPECT_EQ(0UL, UpdaterState::GetState(false)->count("ismachine"));
58 EXPECT_EQ(0UL, UpdaterState::GetState(true)->count("ismachine"));
Olivier Robin694c68aa2017-07-28 08:40:2359 EXPECT_STREQ("Keystone", UpdaterState::GetState(false)->at("name").c_str());
Sorin Jianudf2b9cc2018-02-05 19:11:0360#endif // OS_WIN
Nico Weber356b3042019-08-23 15:30:4161#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
sorin97bd0292016-11-14 19:46:5362
hansc6430822016-11-16 22:13:2963 // Tests some of the remaining values.
64 updater_state = UpdaterState(false);
65
sorin97bd0292016-11-14 19:46:5366 // 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();
Joshua Pawlickicf62dcba2020-02-26 16:56:5778 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());
sorin97bd0292016-11-14 19:46:5384
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();
Joshua Pawlickicf62dcba2020-02-26 16:56:5798 EXPECT_STREQ("336", attributes.at("lastchecked").c_str());
sorin97bd0292016-11-14 19:46:5399
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
rogerta57aed382017-02-23 14:50:45110 updater_state.is_enterprise_managed_ = true;
sorin97bd0292016-11-14 19:46:53111 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