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