Home | History | Annotate | Download | only in gcapi
      1 // Copyright (c) 2012 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/guid.h"
     10 #include "base/strings/string_number_conversions.h"
     11 #include "base/strings/stringprintf.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "base/test/test_reg_util_win.h"
     14 #include "base/time/time.h"
     15 #include "base/win/registry.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 hkcu_override = base::StringPrintf(
     30         L"hkcu_override\\%ls", base::ASCIIToWide(base::GenerateGUID()));
     31     override_manager_.OverrideRegistry(HKEY_CURRENT_USER, hkcu_override);
     32 
     33     // Create the client state key in the right places.
     34     std::wstring reg_path(google_update::kRegPathClientState);
     35     reg_path += L"\\";
     36     reg_path += google_update::kChromeUpgradeCode;
     37     RegKey client_state(HKEY_CURRENT_USER,
     38                         reg_path.c_str(),
     39                         KEY_CREATE_SUB_KEY | KEY_WOW64_32KEY);
     40     ASSERT_TRUE(client_state.Valid());
     41 
     42     // Place a bogus "pv" value in the right places to make the last run
     43     // checker believe Chrome is installed.
     44     std::wstring clients_path(google_update::kRegPathClients);
     45     clients_path += L"\\";
     46     clients_path += google_update::kChromeUpgradeCode;
     47     RegKey client_key(HKEY_CURRENT_USER,
     48                       clients_path.c_str(),
     49                       KEY_CREATE_SUB_KEY | KEY_SET_VALUE | KEY_WOW64_32KEY);
     50     ASSERT_TRUE(client_key.Valid());
     51     client_key.WriteValue(L"pv", L"1.2.3.4");
     52   }
     53 
     54   bool SetLastRunTime(int64 last_run_time) {
     55     return SetLastRunTimeString(base::Int64ToString16(last_run_time));
     56   }
     57 
     58   bool SetLastRunTimeString(const base::string16& last_run_time_string) {
     59     const wchar_t* base_path = google_update::kRegPathClientState;
     60     std::wstring path(base_path);
     61     path += L"\\";
     62     path += google_update::kChromeUpgradeCode;
     63 
     64     RegKey client_state(
     65         HKEY_CURRENT_USER, path.c_str(), KEY_SET_VALUE | KEY_WOW64_32KEY);
     66     return (client_state.Valid() &&
     67             client_state.WriteValue(
     68                 google_update::kRegLastRunTimeField,
     69                 last_run_time_string.c_str()) == ERROR_SUCCESS);
     70   }
     71 
     72  private:
     73   registry_util::RegistryOverrideManager override_manager_;
     74 };
     75 
     76 TEST_F(GCAPILastRunTest, Basic) {
     77   Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(10);
     78   EXPECT_TRUE(SetLastRunTime(last_run.ToInternalValue()));
     79 
     80   int days_since_last_run = GoogleChromeDaysSinceLastRun();
     81   EXPECT_EQ(10, days_since_last_run);
     82 }
     83 
     84 TEST_F(GCAPILastRunTest, NoLastRun) {
     85   int days_since_last_run = GoogleChromeDaysSinceLastRun();
     86   EXPECT_EQ(-1, days_since_last_run);
     87 }
     88 
     89 TEST_F(GCAPILastRunTest, InvalidLastRun) {
     90   EXPECT_TRUE(SetLastRunTimeString(L"Hi Mum!"));
     91   int days_since_last_run = GoogleChromeDaysSinceLastRun();
     92   EXPECT_EQ(-1, days_since_last_run);
     93 }
     94 
     95 TEST_F(GCAPILastRunTest, OutOfRangeLastRun) {
     96   Time last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(-42);
     97   EXPECT_TRUE(SetLastRunTime(last_run.ToInternalValue()));
     98 
     99   int days_since_last_run = GoogleChromeDaysSinceLastRun();
    100   EXPECT_EQ(-1, days_since_last_run);
    101 }
    102