summaryrefslogtreecommitdiffstats
path: root/chrome/installer/gcapi/gcapi_reactivation_test.cc
blob: 7f4a14251be8d590cda70c83367907bdf1a625de (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// 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/guid.h"
#include "base/string_number_conversions.h"
#include "base/stringprintf.h"
#include "base/test/test_reg_util_win.h"
#include "base/time.h"
#include "base/utf_string_conversions.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/util/google_update_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::Time;
using base::TimeDelta;
using base::win::RegKey;

namespace {

const wchar_t kExperimentLabels[] = L"experiment_labels";

const wchar_t* kExperimentAppGuids[] = {
    L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}",
    L"{8A69D345-D564-463C-AFF1-A69D9E530F96}",
};

}

class GCAPIReactivationTest : public ::testing::Test {
 protected:
  void SetUp() {
    // Override keys - this is undone during destruction.
    std::wstring hkcu_override = base::StringPrintf(
        L"hkcu_override\\%ls", ASCIIToWide(base::GenerateGUID()));
    override_manager_.OverrideRegistry(HKEY_CURRENT_USER, hkcu_override);
    std::wstring hklm_override = base::StringPrintf(
        L"hklm_override\\%ls", ASCIIToWide(base::GenerateGUID()));
    override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, hklm_override);
  }

  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);
    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 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);
    return (client_state.Valid() &&
            client_state.WriteValue(
                google_update::kRegLastRunTimeField,
                last_run_time_string.c_str()) == ERROR_SUCCESS);
  }

  bool HasExperimentLabels(HKEY hive) {
    int label_count = 0;
    for (int i = 0; i < arraysize(kExperimentAppGuids); ++i) {
      string16 client_state_path(google_update::kRegPathClientState);
      client_state_path += L"\\";
      client_state_path += kExperimentAppGuids[i];

      RegKey client_state_key(hive,
                              client_state_path.c_str(),
                              KEY_QUERY_VALUE);
      if (client_state_key.Valid() &&
          client_state_key.HasValue(kExperimentLabels)) {
        label_count++;
      }
    }
    return label_count == arraysize(kExperimentAppGuids);
  }

  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);
    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";
  }

 private:
  registry_util::RegistryOverrideManager override_manager_;
};

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));
}