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