OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 <limits> |
| 6 #include <string> |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/string_number_conversions.h" |
| 10 #include "base/stringprintf.h" |
| 11 #include "base/test/test_reg_util_win.h" |
| 12 #include "base/time.h" |
| 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/win/registry.h" |
| 15 #include "chrome/common/guid.h" |
| 16 #include "chrome/installer/gcapi/gcapi.h" |
| 17 #include "chrome/installer/util/google_update_constants.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using base::Time; |
| 21 using base::TimeDelta; |
| 22 using base::win::RegKey; |
| 23 |
| 24 namespace { |
| 25 const wchar_t kChromeRegClientStateKey[] = |
| 26 L"Software\\Google\\Update\\ClientState\\" |
| 27 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
| 28 const wchar_t kChromeRegClientStateMediumKey[] = |
| 29 L"Software\\Google\\Update\\ClientStateMedium\\" |
| 30 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
| 31 } |
| 32 |
| 33 class GCAPILastRunTest : public ::testing::Test { |
| 34 protected: |
| 35 void SetUp() { |
| 36 std::wstring hklm_override = base::StringPrintf( |
| 37 L"hklm_override\\%ls", ASCIIToWide(guid::GenerateGUID())); |
| 38 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, hklm_override); |
| 39 |
| 40 std::wstring hkcu_override = base::StringPrintf( |
| 41 L"hkcu_override\\%ls", ASCIIToWide(guid::GenerateGUID())); |
| 42 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, hkcu_override); |
| 43 |
| 44 // Create the client state keys in the right places. |
| 45 struct { |
| 46 HKEY hive; |
| 47 const wchar_t* path; |
| 48 } reg_data[] = { |
| 49 { HKEY_LOCAL_MACHINE, kChromeRegClientStateMediumKey }, |
| 50 { HKEY_CURRENT_USER, kChromeRegClientStateKey } |
| 51 }; |
| 52 for (int i = 0; i < arraysize(reg_data); ++i) { |
| 53 RegKey client_state(reg_data[i].hive, |
| 54 reg_data[i].path, |
| 55 KEY_CREATE_SUB_KEY); |
| 56 ASSERT_TRUE(client_state.Valid()); |
| 57 } |
| 58 } |
| 59 |
| 60 void TearDown() { |
| 61 override_manager_.RemoveAllOverrides(); |
| 62 } |
| 63 |
| 64 bool SetLastRunTime(HKEY hive, int64 last_run_time) { |
| 65 return SetLastRunTimeString(hive, base::Int64ToString16(last_run_time)); |
| 66 } |
| 67 |
| 68 bool SetLastRunTimeString(HKEY hive, const string16& last_run_time_string) { |
| 69 const wchar_t* path = |
| 70 (hive == HKEY_LOCAL_MACHINE) ? kChromeRegClientStateMediumKey : |
| 71 kChromeRegClientStateKey; |
| 72 |
| 73 RegKey client_state(hive, path, KEY_SET_VALUE); |
| 74 return (client_state.Valid() && |
| 75 client_state.WriteValue( |
| 76 google_update::kRegLastRunTimeField, |
| 77 last_run_time_string.c_str()) == ERROR_SUCCESS); |
| 78 } |
| 79 |
| 80 private: |
| 81 registry_util::RegistryOverrideManager override_manager_; |
| 82 }; |
| 83 |
| 84 TEST_F(GCAPILastRunTest, Basic_HKCU) { |
| 85 Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(10); |
| 86 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, last_run.ToInternalValue())); |
| 87 |
| 88 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 89 EXPECT_EQ(10, days_since_last_run); |
| 90 } |
| 91 |
| 92 TEST_F(GCAPILastRunTest, Basic_HKLM) { |
| 93 Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(10); |
| 94 EXPECT_TRUE(SetLastRunTime(HKEY_LOCAL_MACHINE, last_run.ToInternalValue())); |
| 95 |
| 96 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 97 EXPECT_EQ(10, days_since_last_run); |
| 98 } |
| 99 |
| 100 TEST_F(GCAPILastRunTest, HKLM_and_HKCU) { |
| 101 Time hklm_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(30); |
| 102 EXPECT_TRUE(SetLastRunTime(HKEY_LOCAL_MACHINE, |
| 103 hklm_last_run.ToInternalValue())); |
| 104 |
| 105 Time hkcu_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(20); |
| 106 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, |
| 107 hkcu_last_run.ToInternalValue())); |
| 108 |
| 109 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 110 EXPECT_EQ(20, days_since_last_run); |
| 111 } |
| 112 |
| 113 TEST_F(GCAPILastRunTest, NoLastRun) { |
| 114 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 115 EXPECT_EQ(-1, days_since_last_run); |
| 116 } |
| 117 |
| 118 TEST_F(GCAPILastRunTest, InvalidLastRun) { |
| 119 EXPECT_TRUE(SetLastRunTimeString(HKEY_CURRENT_USER, L"Hi Mum!")); |
| 120 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 121 EXPECT_EQ(-1, days_since_last_run); |
| 122 } |
| 123 |
| 124 TEST_F(GCAPILastRunTest, OutOfRangeLastRun) { |
| 125 Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(-42); |
| 126 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, last_run.ToInternalValue())); |
| 127 |
| 128 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 129 EXPECT_EQ(-1, days_since_last_run); |
| 130 } |
OLD | NEW |