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 |
| 25 class GCAPILastRunTest : public ::testing::Test { |
| 26 protected: |
| 27 void SetUp() { |
| 28 // Override keys - this is undone during destruction. |
| 29 std::wstring hklm_override = base::StringPrintf( |
| 30 L"hklm_override\\%ls", ASCIIToWide(guid::GenerateGUID())); |
| 31 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, hklm_override); |
| 32 std::wstring hkcu_override = base::StringPrintf( |
| 33 L"hkcu_override\\%ls", ASCIIToWide(guid::GenerateGUID())); |
| 34 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, hkcu_override); |
| 35 |
| 36 // Create the client state keys in the right places. |
| 37 struct { |
| 38 HKEY hive; |
| 39 const wchar_t* path; |
| 40 } reg_data[] = { |
| 41 { HKEY_LOCAL_MACHINE, google_update::kRegPathClientStateMedium }, |
| 42 { HKEY_CURRENT_USER, google_update::kRegPathClientState } |
| 43 }; |
| 44 for (int i = 0; i < arraysize(reg_data); ++i) { |
| 45 std::wstring reg_path(reg_data[i].path); |
| 46 reg_path += L"\\"; |
| 47 reg_path += google_update::kChromeUpgradeCode; |
| 48 RegKey client_state(reg_data[i].hive, |
| 49 reg_path.c_str(), |
| 50 KEY_CREATE_SUB_KEY); |
| 51 ASSERT_TRUE(client_state.Valid()); |
| 52 |
| 53 // Place a bogus "pv" value in the right places to make the last run |
| 54 // checker believe Chrome is installed. |
| 55 std::wstring clients_path(google_update::kRegPathClients); |
| 56 clients_path += L"\\"; |
| 57 clients_path += google_update::kChromeUpgradeCode; |
| 58 RegKey client_key(reg_data[i].hive, |
| 59 clients_path.c_str(), |
| 60 KEY_CREATE_SUB_KEY | KEY_SET_VALUE); |
| 61 ASSERT_TRUE(client_key.Valid()); |
| 62 client_key.WriteValue(L"pv", L"1.2.3.4"); |
| 63 } |
| 64 } |
| 65 |
| 66 bool SetLastRunTime(HKEY hive, int64 last_run_time) { |
| 67 return SetLastRunTimeString(hive, base::Int64ToString16(last_run_time)); |
| 68 } |
| 69 |
| 70 bool SetLastRunTimeString(HKEY hive, const string16& last_run_time_string) { |
| 71 const wchar_t* base_path = |
| 72 (hive == HKEY_LOCAL_MACHINE) ? |
| 73 google_update::kRegPathClientStateMedium : |
| 74 google_update::kRegPathClientState; |
| 75 std::wstring path(base_path); |
| 76 path += L"\\"; |
| 77 path += google_update::kChromeUpgradeCode; |
| 78 |
| 79 RegKey client_state(hive, path.c_str(), KEY_SET_VALUE); |
| 80 return (client_state.Valid() && |
| 81 client_state.WriteValue( |
| 82 google_update::kRegLastRunTimeField, |
| 83 last_run_time_string.c_str()) == ERROR_SUCCESS); |
| 84 } |
| 85 |
| 86 private: |
| 87 registry_util::RegistryOverrideManager override_manager_; |
| 88 }; |
| 89 |
| 90 TEST_F(GCAPILastRunTest, Basic_HKCU) { |
| 91 Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(10); |
| 92 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, last_run.ToInternalValue())); |
| 93 |
| 94 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 95 EXPECT_EQ(10, days_since_last_run); |
| 96 } |
| 97 |
| 98 TEST_F(GCAPILastRunTest, Basic_HKLM) { |
| 99 Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(10); |
| 100 EXPECT_TRUE(SetLastRunTime(HKEY_LOCAL_MACHINE, last_run.ToInternalValue())); |
| 101 |
| 102 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 103 EXPECT_EQ(10, days_since_last_run); |
| 104 } |
| 105 |
| 106 TEST_F(GCAPILastRunTest, HKLM_and_HKCU) { |
| 107 Time hklm_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(30); |
| 108 EXPECT_TRUE(SetLastRunTime(HKEY_LOCAL_MACHINE, |
| 109 hklm_last_run.ToInternalValue())); |
| 110 |
| 111 Time hkcu_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(20); |
| 112 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, |
| 113 hkcu_last_run.ToInternalValue())); |
| 114 |
| 115 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 116 EXPECT_EQ(20, days_since_last_run); |
| 117 } |
| 118 |
| 119 TEST_F(GCAPILastRunTest, NoLastRun) { |
| 120 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 121 EXPECT_EQ(-1, days_since_last_run); |
| 122 } |
| 123 |
| 124 TEST_F(GCAPILastRunTest, InvalidLastRun) { |
| 125 EXPECT_TRUE(SetLastRunTimeString(HKEY_CURRENT_USER, L"Hi Mum!")); |
| 126 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 127 EXPECT_EQ(-1, days_since_last_run); |
| 128 } |
| 129 |
| 130 TEST_F(GCAPILastRunTest, OutOfRangeLastRun) { |
| 131 Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(-42); |
| 132 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, last_run.ToInternalValue())); |
| 133 |
| 134 int days_since_last_run = GoogleChromeDaysSinceLastRun(); |
| 135 EXPECT_EQ(-1, days_since_last_run); |
| 136 } |
OLD | NEW |