OLD | NEW |
| (Empty) |
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/string_number_conversions.h" | |
9 #include "base/stringprintf.h" | |
10 #include "base/test/test_reg_util_win.h" | |
11 #include "base/time.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "base/win/registry.h" | |
14 #include "chrome/common/guid.h" | |
15 #include "chrome/installer/gcapi/gcapi.h" | |
16 #include "chrome/installer/gcapi/gcapi_reactivation.h" | |
17 #include "chrome/installer/util/google_update_constants.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using base::Time; | |
21 using base::TimeDelta; | |
22 using base::win::RegKey; | |
23 | |
24 | |
25 class 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(guid::GenerateGUID())); | |
31 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, hkcu_override); | |
32 } | |
33 | |
34 bool SetChromeInstallMarker(HKEY hive) { | |
35 // Create the client state keys in the right places. | |
36 std::wstring reg_path(google_update::kRegPathClients); | |
37 reg_path += L"\\"; | |
38 reg_path += google_update::kChromeUpgradeCode; | |
39 RegKey client_state(hive, | |
40 reg_path.c_str(), | |
41 KEY_CREATE_SUB_KEY | KEY_SET_VALUE); | |
42 return (client_state.Valid() && | |
43 client_state.WriteValue( | |
44 google_update::kRegVersionField, L"1.2.3.4") == ERROR_SUCCESS); | |
45 } | |
46 | |
47 bool SetLastRunTime(HKEY hive, int64 last_run_time) { | |
48 return SetLastRunTimeString(hive, base::Int64ToString16(last_run_time)); | |
49 } | |
50 | |
51 bool SetLastRunTimeString(HKEY hive, const string16& last_run_time_string) { | |
52 const wchar_t* base_path = | |
53 (hive == HKEY_LOCAL_MACHINE) ? | |
54 google_update::kRegPathClientStateMedium : | |
55 google_update::kRegPathClientState; | |
56 std::wstring path(base_path); | |
57 path += L"\\"; | |
58 path += google_update::kChromeUpgradeCode; | |
59 | |
60 RegKey client_state(hive, path.c_str(), KEY_SET_VALUE); | |
61 return (client_state.Valid() && | |
62 client_state.WriteValue( | |
63 google_update::kRegLastRunTimeField, | |
64 last_run_time_string.c_str()) == ERROR_SUCCESS); | |
65 } | |
66 | |
67 std::wstring GetReactivationString(HKEY hive) { | |
68 const wchar_t* base_path = | |
69 (hive == HKEY_LOCAL_MACHINE) ? | |
70 google_update::kRegPathClientStateMedium : | |
71 google_update::kRegPathClientState; | |
72 std::wstring path(base_path); | |
73 path += L"\\"; | |
74 path += google_update::kChromeUpgradeCode; | |
75 | |
76 RegKey client_state(hive, path.c_str(), KEY_QUERY_VALUE); | |
77 if (client_state.Valid()) { | |
78 std::wstring actual_brand; | |
79 if (client_state.ReadValue(google_update::kRegRLZReactivationBrandField, | |
80 &actual_brand) == ERROR_SUCCESS) { | |
81 return actual_brand; | |
82 } | |
83 } | |
84 | |
85 return L"ERROR"; | |
86 } | |
87 | |
88 private: | |
89 registry_util::RegistryOverrideManager override_manager_; | |
90 }; | |
91 | |
92 TEST_F(GCAPIReactivationTest, CheckSetReactivationBrandCode) { | |
93 EXPECT_TRUE(SetReactivationBrandCode(L"GAGA")); | |
94 EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER)); | |
95 | |
96 std::vector<std::wstring> check_codes; | |
97 check_codes.push_back(L"GAGA"); | |
98 EXPECT_TRUE(HasBeenReactivatedByBrandCodes(check_codes)); | |
99 | |
100 check_codes.push_back(L"GOOGOO"); | |
101 EXPECT_TRUE(HasBeenReactivatedByBrandCodes(check_codes)); | |
102 | |
103 check_codes.erase(check_codes.begin()); | |
104 EXPECT_FALSE(HasBeenReactivatedByBrandCodes(check_codes)); | |
105 } | |
106 | |
107 TEST_F(GCAPIReactivationTest, CanOfferReactivation_Basic) { | |
108 const wchar_t* previous_brands[] = {L"GOOGOO", L"MAMA", L"DADA"}; | |
109 DWORD error; | |
110 | |
111 // We're not installed yet. Make sure CanOfferReactivation fails. | |
112 EXPECT_FALSE(CanOfferReactivation(L"GAGA", arraysize(previous_brands), | |
113 previous_brands, &error)); | |
114 EXPECT_EQ(REACTIVATE_ERROR_NOTINSTALLED, error); | |
115 | |
116 // Now pretend to be installed. CanOfferReactivation should pass. | |
117 EXPECT_TRUE(SetChromeInstallMarker(HKEY_CURRENT_USER)); | |
118 EXPECT_TRUE(CanOfferReactivation(L"GAGA", arraysize(previous_brands), | |
119 previous_brands, &error)); | |
120 | |
121 // Now set a recent last_run value. CanOfferReactivation should fail again. | |
122 Time hkcu_last_run = Time::NowFromSystemTime() - TimeDelta::FromDays(20); | |
123 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, | |
124 hkcu_last_run.ToInternalValue())); | |
125 EXPECT_FALSE(CanOfferReactivation(L"GAGA", arraysize(previous_brands), | |
126 previous_brands, &error)); | |
127 EXPECT_EQ(REACTIVATE_ERROR_NOTDORMANT, error); | |
128 | |
129 // Now set a last_run value that exceeds the threshold. | |
130 hkcu_last_run = Time::NowFromSystemTime() - | |
131 TimeDelta::FromDays(kReactivationMinDaysDormant); | |
132 EXPECT_TRUE(SetLastRunTime(HKEY_CURRENT_USER, | |
133 hkcu_last_run.ToInternalValue())); | |
134 EXPECT_TRUE(CanOfferReactivation(L"GAGA", arraysize(previous_brands), | |
135 previous_brands, &error)); | |
136 | |
137 // Test some invalid inputs | |
138 EXPECT_FALSE(CanOfferReactivation(NULL, arraysize(previous_brands), | |
139 previous_brands, &error)); | |
140 EXPECT_EQ(REACTIVATE_ERROR_INVALID_INPUT, error); | |
141 EXPECT_FALSE(CanOfferReactivation(L"GAGA", arraysize(previous_brands), | |
142 NULL, &error)); | |
143 EXPECT_EQ(REACTIVATE_ERROR_INVALID_INPUT, error); | |
144 | |
145 // One more valid one | |
146 EXPECT_TRUE(CanOfferReactivation(L"GAGA", 0, NULL, &error)); | |
147 | |
148 // Check that the previous brands check works: | |
149 EXPECT_TRUE(SetReactivationBrandCode(L"GOOGOO")); | |
150 EXPECT_FALSE(CanOfferReactivation(L"GAGA", arraysize(previous_brands), | |
151 previous_brands, &error)); | |
152 EXPECT_EQ(REACTIVATE_ERROR_ALREADY_REACTIVATED, error); | |
153 } | |
154 | |
155 TEST_F(GCAPIReactivationTest, Reactivation_Flow) { | |
156 const wchar_t* previous_brands[] = {L"GOOGOO", L"MAMA", L"DADA"}; | |
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", arraysize(previous_brands), | |
168 previous_brands, &error)); | |
169 EXPECT_EQ(L"GAGA", GetReactivationString(HKEY_CURRENT_USER)); | |
170 | |
171 // Make sure we can't reactivate again: | |
172 EXPECT_FALSE(ReactivateChrome(L"GAGA", arraysize(previous_brands), | |
173 previous_brands, &error)); | |
174 EXPECT_EQ(REACTIVATE_ERROR_ALREADY_REACTIVATED, error); | |
175 | |
176 // Should still be able to reactivate under other brands: | |
177 EXPECT_TRUE(ReactivateChrome(L"MAMA", arraysize(previous_brands), | |
178 previous_brands, &error)); | |
179 EXPECT_EQ(L"MAMA", GetReactivationString(HKEY_CURRENT_USER)); | |
180 | |
181 // Validate that previous_brands are rejected: | |
182 EXPECT_FALSE(ReactivateChrome(L"PFFT", arraysize(previous_brands), | |
183 previous_brands, &error)); | |
184 EXPECT_EQ(REACTIVATE_ERROR_ALREADY_REACTIVATED, error); | |
185 EXPECT_EQ(L"MAMA", GetReactivationString(HKEY_CURRENT_USER)); | |
186 } | |
OLD | NEW |