sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 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 | |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame] | 5 | #include "components/update_client/updater_state.h" |
| 6 | |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 7 | #include "base/macros.h" |
| 8 | #include "base/time/time.h" |
| 9 | #include "base/version.h" |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame] | 10 | #include "build/branding_buildflags.h" |
sorin | 96d71e0c | 2017-01-25 17:39:11 | [diff] [blame] | 11 | #include "build/build_config.h" |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace update_client { |
| 15 | |
| 16 | class UpdaterStateTest : public testing::Test { |
| 17 | public: |
Sorin Jianu | 3088115 | 2020-03-16 14:31:19 | [diff] [blame] | 18 | UpdaterStateTest() = default; |
| 19 | ~UpdaterStateTest() override = default; |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 20 | |
| 21 | private: |
| 22 | DISALLOW_COPY_AND_ASSIGN(UpdaterStateTest); |
| 23 | }; |
| 24 | |
| 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(); |
rogerta | 57aed38 | 2017-02-23 14:50:45 | [diff] [blame] | 32 | updater_state.is_enterprise_managed_ = false; |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 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 | |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame] | 47 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
Sorin Jianu | df2b9cc | 2018-02-05 19:11:03 | [diff] [blame] | 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()); |
Olivier Robin | 694c68aa | 2017-07-28 08:40:23 | [diff] [blame] | 55 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
Sorin Jianu | df2b9cc | 2018-02-05 19:11:03 | [diff] [blame] | 56 | // MacOS does not serialize "ismachine". |
Xiaoling Bao | 8fe2c8ab | 2018-02-23 19:38:30 | [diff] [blame] | 57 | EXPECT_EQ(0UL, UpdaterState::GetState(false)->count("ismachine")); |
| 58 | EXPECT_EQ(0UL, UpdaterState::GetState(true)->count("ismachine")); |
Olivier Robin | 694c68aa | 2017-07-28 08:40:23 | [diff] [blame] | 59 | EXPECT_STREQ("Keystone", UpdaterState::GetState(false)->at("name").c_str()); |
Sorin Jianu | df2b9cc | 2018-02-05 19:11:03 | [diff] [blame] | 60 | #endif // OS_WIN |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame] | 61 | #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 62 | |
hans | c643082 | 2016-11-16 22:13:29 | [diff] [blame] | 63 | // Tests some of the remaining values. |
| 64 | updater_state = UpdaterState(false); |
| 65 | |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 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(); |
Joshua Pawlicki | cf62dcba | 2020-02-26 16:56:57 | [diff] [blame] | 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()); |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 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(); |
Joshua Pawlicki | cf62dcba | 2020-02-26 16:56:57 | [diff] [blame] | 98 | EXPECT_STREQ("336", attributes.at("lastchecked").c_str()); |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 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 | |
rogerta | 57aed38 | 2017-02-23 14:50:45 | [diff] [blame] | 110 | updater_state.is_enterprise_managed_ = true; |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 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 |