Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / installer / gcapi / gcapi_reactivation_test.cc
blob76069f782849e1b11a30e856505b9da4659b7e97
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.
5 #include <string>
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 "testing/gtest/include/gtest/gtest.h"
18 using base::Time;
19 using base::TimeDelta;
20 using base::win::RegKey;
22 class GCAPIReactivationTest : public ::testing::Test {
23 protected:
24 GCAPIReactivationTest() {}
26 bool SetChromeInstallMarker(HKEY hive) {
27 // Create the client state keys in the right places.
28 std::wstring reg_path(google_update::kRegPathClients);
29 reg_path += L"\\";
30 reg_path += google_update::kChromeUpgradeCode;
31 RegKey client_state(hive,
32 reg_path.c_str(),
33 KEY_CREATE_SUB_KEY | KEY_SET_VALUE | KEY_WOW64_32KEY);
34 return (client_state.Valid() &&
35 client_state.WriteValue(
36 google_update::kRegVersionField, L"1.2.3.4") == ERROR_SUCCESS);
39 bool SetLastRunTime(HKEY hive, int64 last_run_time) {
40 return SetLastRunTimeString(hive, base::Int64ToString16(last_run_time));
43 bool SetLastRunTimeString(HKEY hive,
44 const base::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;
53 RegKey client_state(hive, path.c_str(), KEY_SET_VALUE | KEY_WOW64_32KEY);
54 return (client_state.Valid() &&
55 client_state.WriteValue(
56 google_update::kRegLastRunTimeField,
57 last_run_time_string.c_str()) == ERROR_SUCCESS);
60 bool HasExperimentLabels(HKEY hive) {
61 base::string16 client_state_path(google_update::kRegPathClientState);
62 client_state_path.push_back(L'\\');
63 client_state_path.append(google_update::kChromeUpgradeCode);
65 RegKey client_state_key(hive,
66 client_state_path.c_str(),
67 KEY_QUERY_VALUE | KEY_WOW64_32KEY);
68 return client_state_key.Valid() &&
69 client_state_key.HasValue(google_update::kExperimentLabels);
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;
81 RegKey client_state(hive, path.c_str(), KEY_QUERY_VALUE | KEY_WOW64_32KEY);
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;
90 return L"ERROR";
93 const GCAPITestRegistryOverrider gcapi_test_registry_overrider_;
96 TEST_F(GCAPIReactivationTest, CheckSetReactivationBrandCode) {
97 EXPECT_TRUE(SetReactivationBrandCode(L"GAGA", GCAPI_INVOKED_STANDARD_SHELL));
98 EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER));
100 EXPECT_TRUE(HasBeenReactivated());
103 TEST_F(GCAPIReactivationTest, CanOfferReactivation_Basic) {
104 DWORD error;
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);
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));
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);
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));
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);
142 // One more valid one
143 EXPECT_TRUE(CanOfferReactivation(L"GAGA",
144 GCAPI_INVOKED_STANDARD_SHELL,
145 &error));
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);
156 TEST_F(GCAPIReactivationTest, Reactivation_Flow) {
157 DWORD error;
159 // Set us up as a candidate for reactivation.
160 EXPECT_TRUE(SetChromeInstallMarker(HKEY_CURRENT_USER));
162 Time hkcu_last_run = Time::NowFromSystemTime() -
163 TimeDelta::FromDays(kReactivationMinDaysDormant);
164 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER,
165 hkcu_last_run.ToInternalValue()));
167 EXPECT_TRUE(ReactivateChrome(L"GAGA",
168 GCAPI_INVOKED_STANDARD_SHELL,
169 &error));
170 EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER));
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);
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));
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));
192 TEST_F(GCAPIReactivationTest, ExperimentLabelCheck) {
193 DWORD error;
195 // Set us up as a candidate for reactivation.
196 EXPECT_TRUE(SetChromeInstallMarker(HKEY_CURRENT_USER));
198 Time hkcu_last_run = Time::NowFromSystemTime() -
199 TimeDelta::FromDays(kReactivationMinDaysDormant);
200 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER,
201 hkcu_last_run.ToInternalValue()));
203 EXPECT_TRUE(ReactivateChrome(L"GAGA",
204 GCAPI_INVOKED_STANDARD_SHELL,
205 &error));
206 EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER));
208 EXPECT_TRUE(HasExperimentLabels(HKEY_CURRENT_USER));