blob: 20fa0f711ec4437f3793b06745728c1efef513fa [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
5#include "base/macros.h"
6#include "base/time/time.h"
7#include "base/version.h"
sorin96d71e0c2017-01-25 17:39:118#include "build/build_config.h"
sorin97bd0292016-11-14 19:46:539#include "components/update_client/updater_state.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace update_client {
13
14class UpdaterStateTest : public testing::Test {
15 public:
16 UpdaterStateTest() {}
17 ~UpdaterStateTest() override {}
18
19 private:
20 DISALLOW_COPY_AND_ASSIGN(UpdaterStateTest);
21};
22
23TEST_F(UpdaterStateTest, Serialize) {
24 UpdaterState updater_state(false);
25
26 updater_state.updater_name_ = "the updater";
27 updater_state.updater_version_ = base::Version("1.0");
28 updater_state.last_autoupdate_started_ = base::Time::NowFromSystemTime();
29 updater_state.last_checked_ = base::Time::NowFromSystemTime();
rogerta57aed382017-02-23 14:50:4530 updater_state.is_enterprise_managed_ = false;
sorin97bd0292016-11-14 19:46:5331 updater_state.is_autoupdate_check_enabled_ = true;
32 updater_state.update_policy_ = 1;
33
34 auto attributes = updater_state.BuildAttributes();
35
36 // Sanity check all members.
37 EXPECT_STREQ("the updater", attributes.at("name").c_str());
38 EXPECT_STREQ("1.0", attributes.at("version").c_str());
39 EXPECT_STREQ("0", attributes.at("laststarted").c_str());
40 EXPECT_STREQ("0", attributes.at("lastchecked").c_str());
41 EXPECT_STREQ("0", attributes.at("domainjoined").c_str());
42 EXPECT_STREQ("1", attributes.at("autoupdatecheckenabled").c_str());
43 EXPECT_STREQ("1", attributes.at("updatepolicy").c_str());
44
borisva97d5302017-05-10 17:11:5545#if defined(GOOGLE_CHROME_BUILD)
Sorin Jianudf2b9cc2018-02-05 19:11:0346#if defined(OS_WIN)
47 // The value of "ismachine".
48 EXPECT_STREQ("0", UpdaterState::GetState(false)->at("ismachine").c_str());
49 EXPECT_STREQ("1", UpdaterState::GetState(true)->at("ismachine").c_str());
50
51 // The name of the Windows updater for Chrome.
52 EXPECT_STREQ("Omaha", UpdaterState::GetState(false)->at("name").c_str());
Olivier Robin694c68aa2017-07-28 08:40:2353#elif defined(OS_MACOSX) && !defined(OS_IOS)
Sorin Jianudf2b9cc2018-02-05 19:11:0354 // MacOS does not serialize "ismachine".
Xiaoling Bao8fe2c8ab2018-02-23 19:38:3055 EXPECT_EQ(0UL, UpdaterState::GetState(false)->count("ismachine"));
56 EXPECT_EQ(0UL, UpdaterState::GetState(true)->count("ismachine"));
Olivier Robin694c68aa2017-07-28 08:40:2357 EXPECT_STREQ("Keystone", UpdaterState::GetState(false)->at("name").c_str());
Sorin Jianudf2b9cc2018-02-05 19:11:0358#endif // OS_WIN
sorin97bd0292016-11-14 19:46:5359#endif // GOOGLE_CHROME_BUILD
60
hansc6430822016-11-16 22:13:2961 // Tests some of the remaining values.
62 updater_state = UpdaterState(false);
63
sorin97bd0292016-11-14 19:46:5364 // Don't serialize an invalid version if it could not be read.
65 updater_state.updater_version_ = base::Version();
66 attributes = updater_state.BuildAttributes();
67 EXPECT_EQ(0u, attributes.count("version"));
68
69 updater_state.updater_version_ = base::Version("0.0.0.0");
70 attributes = updater_state.BuildAttributes();
71 EXPECT_STREQ("0.0.0.0", attributes.at("version").c_str());
72
73 updater_state.last_autoupdate_started_ =
74 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(15);
75 attributes = updater_state.BuildAttributes();
76 EXPECT_STREQ("408", attributes.at("laststarted").c_str());
77
78 updater_state.last_autoupdate_started_ =
79 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(90);
80 attributes = updater_state.BuildAttributes();
81 EXPECT_STREQ("1344", attributes.at("laststarted").c_str());
82
83 // Don't serialize the time if it could not be read.
84 updater_state.last_autoupdate_started_ = base::Time();
85 attributes = updater_state.BuildAttributes();
86 EXPECT_EQ(0u, attributes.count("laststarted"));
87
88 updater_state.last_checked_ =
89 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(15);
90 attributes = updater_state.BuildAttributes();
91 EXPECT_STREQ("408", attributes.at("lastchecked").c_str());
92
93 updater_state.last_checked_ =
94 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(90);
95 attributes = updater_state.BuildAttributes();
96 EXPECT_STREQ("1344", attributes.at("lastchecked").c_str());
97
98 // Don't serialize the time if it could not be read (the value is invalid).
99 updater_state.last_checked_ = base::Time();
100 attributes = updater_state.BuildAttributes();
101 EXPECT_EQ(0u, attributes.count("lastchecked"));
102
rogerta57aed382017-02-23 14:50:45103 updater_state.is_enterprise_managed_ = true;
sorin97bd0292016-11-14 19:46:53104 attributes = updater_state.BuildAttributes();
105 EXPECT_STREQ("1", attributes.at("domainjoined").c_str());
106
107 updater_state.is_autoupdate_check_enabled_ = false;
108 attributes = updater_state.BuildAttributes();
109 EXPECT_STREQ("0", attributes.at("autoupdatecheckenabled").c_str());
110
111 updater_state.update_policy_ = 0;
112 attributes = updater_state.BuildAttributes();
113 EXPECT_STREQ("0", attributes.at("updatepolicy").c_str());
114
115 updater_state.update_policy_ = -1;
116 attributes = updater_state.BuildAttributes();
117 EXPECT_STREQ("-1", attributes.at("updatepolicy").c_str());
118}
119
120} // namespace update_client