• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
6 
7 #include "base/basictypes.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/time/time.h"
10 #include "base/win/registry.h"
11 #include "chrome/installer/gcapi/gcapi.h"
12 #include "chrome/installer/gcapi/gcapi_omaha_experiment.h"
13 #include "chrome/installer/gcapi/gcapi_reactivation.h"
14 #include "chrome/installer/gcapi/gcapi_test_registry_overrider.h"
15 #include "chrome/installer/util/google_update_constants.h"
16 #include "chrome/installer/util/google_update_experiment_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 using base::Time;
20 using base::TimeDelta;
21 using base::win::RegKey;
22 
23 class GCAPIReactivationTest : public ::testing::Test {
24  protected:
GCAPIReactivationTest()25   GCAPIReactivationTest() {}
26 
SetChromeInstallMarker(HKEY hive)27   bool SetChromeInstallMarker(HKEY hive) {
28     // Create the client state keys in the right places.
29     std::wstring reg_path(google_update::kRegPathClients);
30     reg_path += L"\\";
31     reg_path += google_update::kChromeUpgradeCode;
32     RegKey client_state(hive,
33                         reg_path.c_str(),
34                         KEY_CREATE_SUB_KEY | KEY_SET_VALUE);
35     return (client_state.Valid() &&
36             client_state.WriteValue(
37                 google_update::kRegVersionField, L"1.2.3.4") == ERROR_SUCCESS);
38   }
39 
SetLastRunTime(HKEY hive,int64 last_run_time)40   bool SetLastRunTime(HKEY hive, int64 last_run_time) {
41     return SetLastRunTimeString(hive, base::Int64ToString16(last_run_time));
42   }
43 
SetLastRunTimeString(HKEY hive,const string16 & last_run_time_string)44   bool SetLastRunTimeString(HKEY hive, const string16& last_run_time_string) {
45     const wchar_t* base_path =
46         (hive == HKEY_LOCAL_MACHINE) ?
47             google_update::kRegPathClientStateMedium :
48             google_update::kRegPathClientState;
49     std::wstring path(base_path);
50     path += L"\\";
51     path += google_update::kChromeUpgradeCode;
52 
53     RegKey client_state(hive, path.c_str(), KEY_SET_VALUE);
54     return (client_state.Valid() &&
55             client_state.WriteValue(
56                 google_update::kRegLastRunTimeField,
57                 last_run_time_string.c_str()) == ERROR_SUCCESS);
58   }
59 
HasExperimentLabels(HKEY hive)60   bool HasExperimentLabels(HKEY hive) {
61     string16 client_state_path(google_update::kRegPathClientState);
62     client_state_path.push_back(L'\\');
63     client_state_path.append(google_update::kChromeUpgradeCode);
64 
65     RegKey client_state_key(hive,
66                             client_state_path.c_str(),
67                             KEY_QUERY_VALUE);
68     return client_state_key.Valid() &&
69         client_state_key.HasValue(google_update::kExperimentLabels);
70   }
71 
GetReactivationString(HKEY hive)72   std::wstring GetReactivationString(HKEY hive) {
73     const wchar_t* base_path =
74         (hive == HKEY_LOCAL_MACHINE) ?
75             google_update::kRegPathClientStateMedium :
76             google_update::kRegPathClientState;
77     std::wstring path(base_path);
78     path += L"\\";
79     path += google_update::kChromeUpgradeCode;
80 
81     RegKey client_state(hive, path.c_str(), KEY_QUERY_VALUE);
82     if (client_state.Valid()) {
83       std::wstring actual_brand;
84       if (client_state.ReadValue(google_update::kRegRLZReactivationBrandField,
85                                  &actual_brand) == ERROR_SUCCESS) {
86         return actual_brand;
87       }
88     }
89 
90     return L"ERROR";
91   }
92 
93   const GCAPITestRegistryOverrider gcapi_test_registry_overrider_;
94 };
95 
TEST_F(GCAPIReactivationTest,CheckSetReactivationBrandCode)96 TEST_F(GCAPIReactivationTest, CheckSetReactivationBrandCode) {
97   EXPECT_TRUE(SetReactivationBrandCode(L"GAGA", GCAPI_INVOKED_STANDARD_SHELL));
98   EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER));
99 
100   EXPECT_TRUE(HasBeenReactivated());
101 }
102 
TEST_F(GCAPIReactivationTest,CanOfferReactivation_Basic)103 TEST_F(GCAPIReactivationTest, CanOfferReactivation_Basic) {
104   DWORD error;
105 
106   // We're not installed yet. Make sure CanOfferReactivation fails.
107   EXPECT_FALSE(CanOfferReactivation(L"GAGA",
108                                     GCAPI_INVOKED_STANDARD_SHELL,
109                                     &error));
110   EXPECT_EQ(REACTIVATE_ERROR_NOTINSTALLED, error);
111 
112   // Now pretend to be installed. CanOfferReactivation should pass.
113   EXPECT_TRUE(SetChromeInstallMarker(HKEY_CURRENT_USER));
114   EXPECT_TRUE(CanOfferReactivation(L"GAGA",
115                                    GCAPI_INVOKED_STANDARD_SHELL,
116                                    &error));
117 
118   // Now set a recent last_run value. CanOfferReactivation should fail again.
119   Time hkcu_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(20);
120   EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER,
121                              hkcu_last_run.ToInternalValue()));
122   EXPECT_FALSE(CanOfferReactivation(L"GAGA",
123                                     GCAPI_INVOKED_STANDARD_SHELL,
124                                     &error));
125   EXPECT_EQ(REACTIVATE_ERROR_NOTDORMANT, error);
126 
127   // Now set a last_run value that exceeds the threshold.
128   hkcu_last_run = Time::NowFromSystemTime() -
129       TimeDelta::FromDays(kReactivationMinDaysDormant);
130   EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER,
131                              hkcu_last_run.ToInternalValue()));
132   EXPECT_TRUE(CanOfferReactivation(L"GAGA",
133                                    GCAPI_INVOKED_STANDARD_SHELL,
134                                    &error));
135 
136   // Test some invalid inputs
137   EXPECT_FALSE(CanOfferReactivation(NULL,
138                                     GCAPI_INVOKED_STANDARD_SHELL,
139                                     &error));
140   EXPECT_EQ(REACTIVATE_ERROR_INVALID_INPUT, error);
141 
142   // One more valid one
143   EXPECT_TRUE(CanOfferReactivation(L"GAGA",
144                                    GCAPI_INVOKED_STANDARD_SHELL,
145                                    &error));
146 
147   // Check that the previous brands check works:
148   EXPECT_TRUE(SetReactivationBrandCode(L"GOOGOO",
149                                        GCAPI_INVOKED_STANDARD_SHELL));
150   EXPECT_FALSE(CanOfferReactivation(L"GAGA",
151                                     GCAPI_INVOKED_STANDARD_SHELL,
152                                     &error));
153   EXPECT_EQ(REACTIVATE_ERROR_ALREADY_REACTIVATED, error);
154 }
155 
TEST_F(GCAPIReactivationTest,Reactivation_Flow)156 TEST_F(GCAPIReactivationTest, Reactivation_Flow) {
157   DWORD error;
158 
159   // Set us up as a candidate for reactivation.
160   EXPECT_TRUE(SetChromeInstallMarker(HKEY_CURRENT_USER));
161 
162   Time hkcu_last_run = Time::NowFromSystemTime() -
163       TimeDelta::FromDays(kReactivationMinDaysDormant);
164   EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER,
165                              hkcu_last_run.ToInternalValue()));
166 
167   EXPECT_TRUE(ReactivateChrome(L"GAGA",
168                                GCAPI_INVOKED_STANDARD_SHELL,
169                                &error));
170   EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER));
171 
172   // Make sure we can't reactivate again:
173   EXPECT_FALSE(ReactivateChrome(L"GAGA",
174                                 GCAPI_INVOKED_STANDARD_SHELL,
175                                 &error));
176   EXPECT_EQ(REACTIVATE_ERROR_ALREADY_REACTIVATED, error);
177 
178   // Should not be able to reactivate under other brands:
179   EXPECT_FALSE(ReactivateChrome(L"MAMA",
180                                 GCAPI_INVOKED_STANDARD_SHELL,
181                                 &error));
182   EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER));
183 
184   // Validate that previous_brands are rejected:
185   EXPECT_FALSE(ReactivateChrome(L"PFFT",
186                                 GCAPI_INVOKED_STANDARD_SHELL,
187                                 &error));
188   EXPECT_EQ(REACTIVATE_ERROR_ALREADY_REACTIVATED, error);
189   EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER));
190 }
191 
TEST_F(GCAPIReactivationTest,ExperimentLabelCheck)192 TEST_F(GCAPIReactivationTest, ExperimentLabelCheck) {
193   DWORD error;
194 
195   // Set us up as a candidate for reactivation.
196   EXPECT_TRUE(SetChromeInstallMarker(HKEY_CURRENT_USER));
197 
198   Time hkcu_last_run = Time::NowFromSystemTime() -
199       TimeDelta::FromDays(kReactivationMinDaysDormant);
200   EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER,
201                              hkcu_last_run.ToInternalValue()));
202 
203   EXPECT_TRUE(ReactivateChrome(L"GAGA",
204                                GCAPI_INVOKED_STANDARD_SHELL,
205                                &error));
206   EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER));
207 
208   EXPECT_TRUE(HasExperimentLabels(HKEY_CURRENT_USER));
209 }
210