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