blob: 9fb5f5bda92333021cc8c5fd7bf3596e090e7cec [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();
30 updater_state.is_joined_to_domain_ = false;
31 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
krasin325a4952016-11-29 02:51:1945#if defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
sorin97bd0292016-11-14 19:46:5346 // The name of the Windows updater for Chrome.
hansc6430822016-11-16 22:13:2947 EXPECT_STREQ("Omaha", UpdaterState::GetState(false)->at("name").c_str());
sorin97bd0292016-11-14 19:46:5348#endif // GOOGLE_CHROME_BUILD
49
hansc6430822016-11-16 22:13:2950 // Tests some of the remaining values.
51 updater_state = UpdaterState(false);
52
sorin97bd0292016-11-14 19:46:5353 // Don't serialize an invalid version if it could not be read.
54 updater_state.updater_version_ = base::Version();
55 attributes = updater_state.BuildAttributes();
56 EXPECT_EQ(0u, attributes.count("version"));
57
58 updater_state.updater_version_ = base::Version("0.0.0.0");
59 attributes = updater_state.BuildAttributes();
60 EXPECT_STREQ("0.0.0.0", attributes.at("version").c_str());
61
62 updater_state.last_autoupdate_started_ =
63 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(15);
64 attributes = updater_state.BuildAttributes();
65 EXPECT_STREQ("408", attributes.at("laststarted").c_str());
66
67 updater_state.last_autoupdate_started_ =
68 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(90);
69 attributes = updater_state.BuildAttributes();
70 EXPECT_STREQ("1344", attributes.at("laststarted").c_str());
71
72 // Don't serialize the time if it could not be read.
73 updater_state.last_autoupdate_started_ = base::Time();
74 attributes = updater_state.BuildAttributes();
75 EXPECT_EQ(0u, attributes.count("laststarted"));
76
77 updater_state.last_checked_ =
78 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(15);
79 attributes = updater_state.BuildAttributes();
80 EXPECT_STREQ("408", attributes.at("lastchecked").c_str());
81
82 updater_state.last_checked_ =
83 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(90);
84 attributes = updater_state.BuildAttributes();
85 EXPECT_STREQ("1344", attributes.at("lastchecked").c_str());
86
87 // Don't serialize the time if it could not be read (the value is invalid).
88 updater_state.last_checked_ = base::Time();
89 attributes = updater_state.BuildAttributes();
90 EXPECT_EQ(0u, attributes.count("lastchecked"));
91
92 updater_state.is_joined_to_domain_ = true;
93 attributes = updater_state.BuildAttributes();
94 EXPECT_STREQ("1", attributes.at("domainjoined").c_str());
95
96 updater_state.is_autoupdate_check_enabled_ = false;
97 attributes = updater_state.BuildAttributes();
98 EXPECT_STREQ("0", attributes.at("autoupdatecheckenabled").c_str());
99
100 updater_state.update_policy_ = 0;
101 attributes = updater_state.BuildAttributes();
102 EXPECT_STREQ("0", attributes.at("updatepolicy").c_str());
103
104 updater_state.update_policy_ = -1;
105 attributes = updater_state.BuildAttributes();
106 EXPECT_STREQ("-1", attributes.at("updatepolicy").c_str());
107}
108
109} // namespace update_client