OLD | NEW |
| (Empty) |
1 // Copyright 2007-2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Common include file for unit testing. | |
17 | |
18 #ifndef OMAHA_TESTING_UNIT_TEST_H_ | |
19 #define OMAHA_TESTING_UNIT_TEST_H_ | |
20 | |
21 #include <windows.h> | |
22 #include <atlstr.h> | |
23 #include "base/scoped_ptr.h" | |
24 #include "omaha/testing/unittest_debug_helper.h" | |
25 #pragma warning(push) | |
26 // C4628: digraphs not supported with -Ze. | |
27 #pragma warning(disable : 4628) | |
28 // C4826: Conversion from 'TYPE *' to 'testing::internal::UInt64' is | |
29 // sign-extended. This may cause unexpected runtime behavior. | |
30 // Caused by a hack in DefaultPrintTo. | |
31 #pragma warning(disable : 4826) | |
32 #include "omaha/third_party/gmock/include/gmock/gmock.h" | |
33 #pragma warning(pop) | |
34 #include "omaha/third_party/gtest/include/gtest/gtest.h" | |
35 | |
36 namespace omaha { | |
37 | |
38 const TCHAR* const kUnittestName = _T("omaha_unittest.exe"); | |
39 | |
40 // Predicates needed by ASSERT_PRED1 for function returning an HRESULT. | |
41 inline testing::AssertionResult Succeeded(const char* s, HRESULT hr) { | |
42 if (SUCCEEDED(hr)) { | |
43 return testing::AssertionSuccess(); | |
44 } else { | |
45 CStringA text; | |
46 text.AppendFormat("%s failed with error 0x%08x", s, hr); | |
47 testing::Message msg; | |
48 msg << text; | |
49 return testing::AssertionFailure(msg); | |
50 } | |
51 } | |
52 | |
53 inline testing::AssertionResult Failed(const char* s, HRESULT hr) { | |
54 if (FAILED(hr)) { | |
55 return testing::AssertionSuccess(); | |
56 } else { | |
57 CStringA text; | |
58 text.AppendFormat("%s failed with error 0x%08x", s, hr); | |
59 testing::Message msg; | |
60 msg << text; | |
61 return testing::AssertionFailure(msg); | |
62 } | |
63 } | |
64 | |
65 // Returns true if the variable exists in the environment, even if it is "0". | |
66 bool IsEnvironmentVariableSet(const TCHAR* name); | |
67 | |
68 // Returns true if current unit test process owner is LOCALSYSTEM. | |
69 bool IsTestRunByLocalSystem(); | |
70 | |
71 // Returns the path to the base local app data directory for the user on the | |
72 // current OS. | |
73 CString GetLocalAppDataPath(); | |
74 | |
75 // Returns the path to the base Google directory for the user on the current OS. | |
76 CString GetGoogleUserPath(); | |
77 | |
78 // Returns the path to the base Google Update directory for the user on the | |
79 // current OS. | |
80 CString GetGoogleUpdateUserPath(); | |
81 | |
82 // Returns the path to the base Google Update directory for the per-machine | |
83 // install on the current OS. | |
84 CString GetGoogleUpdateMachinePath(); | |
85 | |
86 // Returns a DWORD registry value from the registry. Assumes the value exists. | |
87 // Useful for inline comparisons in EXPECT_EQ. | |
88 DWORD GetDwordValue(const CString& full_key_name, const CString& value_name); | |
89 | |
90 // Returns a SZ registry value from the registry. Assumes the value exists. | |
91 // Useful for inline comparisons in EXPECT_STREQ. | |
92 CString GetSzValue(const CString& full_key_name, const CString& value_name); | |
93 | |
94 // Converts string to GUID. Assumes the string is a valid GUID. | |
95 GUID StringToGuid(const CString& str); | |
96 | |
97 const TCHAR* const kRegistryHiveOverrideRoot = | |
98 _T("HKCU\\Software\\") _T(SHORT_COMPANY_NAME_ANSI) | |
99 _T("\\") _T(PRODUCT_NAME_ANSI) | |
100 _T("\\UnitTest\\"); | |
101 const TCHAR* const kCsidlSystemIdsRegKey = | |
102 _T("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion"); | |
103 const TCHAR* const kCsidlProgramFilesRegValue = | |
104 _T("ProgramFilesDir"); | |
105 | |
106 // TODO(omaha): consider renaming hive_override_key_name to new_key. | |
107 // TODO(omaha): consider making these utility functions, maybe extend the | |
108 // RegKey class. | |
109 // | |
110 // Overrides the HKLM and HKCU registry hives so that accesses go to the | |
111 // specified registry key instead. The function creates the | |
112 // hive_override_key_name. In other words, overriding HKCU with | |
113 // "HKCU\\Software\\Google\\Update\\UnitTest\\" and accessing HKCU\\Foo results | |
114 // in an access at "HKCU\\Software\\Google\\Update\\UnitTest\\Foo". | |
115 // This method is most often used in SetUp(). | |
116 void OverrideRegistryHives(const CString& hive_override_key_name); | |
117 | |
118 // Overrides only the specified hives. | |
119 // This is useful when modifying registry settings in one hive while using | |
120 // code (e.g. WinHttp) that relies on valid registry entries that are difficult | |
121 // to reproduce. | |
122 // | |
123 // TODO(omaha): Consider renaming to: | |
124 // void OverrideRegistryHive(HKEY hive, const CString& new_key); | |
125 void OverrideSpecifiedRegistryHives(const CString& hive_override_key_name, | |
126 bool override_hklm, | |
127 bool override_hkcu); | |
128 | |
129 // Overrides the HKLM and HKCU registry hives so that accesses go to the | |
130 // specified registry key instead. Provides permissions to execute local files. | |
131 void OverrideRegistryHivesWithExecutionPermissions( | |
132 const CString& hive_override_key_name); | |
133 | |
134 // Restores HKLM and HKCU registry accesses to the real hives. | |
135 // This method is most often used in TearDown(). It does not cleanup the | |
136 // registry key that is created by OverrideRegistryHives. | |
137 void RestoreRegistryHives(); | |
138 | |
139 // Specifies the location of psexec.exe. Only call during initialization. | |
140 void SetPsexecDir(const CString& dir); | |
141 | |
142 // Returns the location of psexec.exe. | |
143 CString GetPsexecDir(); | |
144 | |
145 // Accepts the psexec.exe EULA. Only use for automated testing when you have | |
146 // already read and agreed to the EULA terms. | |
147 // Returns true if the process was successfully started. | |
148 bool AcceptPsexecEula(); | |
149 | |
150 // Specifies that the tests are running on or on behalf of the build system. | |
151 void SetIsBuildSystem(); | |
152 | |
153 // Returns whether tests are running on or on behalf of the build system. | |
154 bool IsBuildSystem(); | |
155 | |
156 // Sets TestSource=buildsystem. | |
157 void SetBuildSystemTestSource(); | |
158 | |
159 // Returns whether large tests should be run. Large tests are always run on the | |
160 // build system and if the "OMAHA_TEST_RUN_LARGE" or "OMAHA_TEST_RUN_ALL" | |
161 // environment variable is set. | |
162 bool ShouldRunLargeTest(); | |
163 | |
164 // Returns whether enourmous tests should be run. Enormous tests are always run | |
165 // on the build system and if the "OMAHA_TEST_RUN_ALL" environment variable is | |
166 // set. This method should be used sparingly and only by tests that take a | |
167 // really long time to complete. | |
168 bool ShouldRunEnormousTest(); | |
169 | |
170 // Terminates all processes named GoogleUpdate.exe or GoogleCrashHandler.exe. | |
171 void TerminateAllGoogleUpdateProcesses(); | |
172 | |
173 // Launches a process and returns its handle. | |
174 void LaunchProcess(const CString& exe_path, | |
175 const CString& args, | |
176 bool as_system, | |
177 HANDLE* process); | |
178 | |
179 // Launches a process as system and returns its handle. The function uses | |
180 // psexec to run the process. | |
181 void LaunchProcessAsSystem(const CString& launch_cmd, HANDLE* process); | |
182 | |
183 // Copies Omaha installation files under omaha_path. | |
184 void CopyGoopdateFiles(const CString& omaha_path, const CString& version); | |
185 | |
186 // A generic test fixture that overrides the HKLM and HKCU hives. | |
187 class RegistryProtectedTest : public testing::Test { | |
188 protected: | |
189 RegistryProtectedTest() | |
190 : hive_override_key_name_(kRegistryHiveOverrideRoot) { | |
191 } | |
192 | |
193 virtual void SetUp(); | |
194 virtual void TearDown(); | |
195 | |
196 const CString hive_override_key_name_; | |
197 }; | |
198 | |
199 // Returns the full path of a unique directory under the user temp directory. | |
200 CString GetUniqueTempDirectoryName(); | |
201 | |
202 // Runs the command as an administrator. | |
203 void RunAsAdmin(const CString& exe_path, const CString& cmd_line); | |
204 | |
205 void RegisterOrUnregisterGoopdateLocalServer(bool reg); | |
206 | |
207 void RegisterOrUnregisterGoopdateService(bool reg); | |
208 | |
209 } // namespace omaha | |
210 | |
211 // TODO(omaha): Replace custom predicates with EXPECT_HRESULT_SUCCEEDED/FAILED. | |
212 #define ASSERT_SUCCEEDED(x) ASSERT_PRED_FORMAT1(omaha::Succeeded, x) | |
213 #define EXPECT_SUCCEEDED(x) EXPECT_PRED_FORMAT1(omaha::Succeeded, x) | |
214 #define ASSERT_FAILED(x) ASSERT_PRED_FORMAT1(omaha::Failed, x) | |
215 #define EXPECT_FAILED(x) EXPECT_PRED_FORMAT1(omaha::Failed, x) | |
216 | |
217 // As of Google Test 1.4.0, expressions get converted to 'bool', resulting in | |
218 // "warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance | |
219 // warning)" in some uses. | |
220 // These must be kept in sync with gtest.h. | |
221 // TODO(omaha): Try to get this fixed in Google Test. | |
222 #undef EXPECT_TRUE | |
223 #define EXPECT_TRUE(condition) \ | |
224 GTEST_TEST_BOOLEAN_(!!(condition), #condition, false, true, \ | |
225 GTEST_NONFATAL_FAILURE_) | |
226 #undef ASSERT_TRUE | |
227 #define ASSERT_TRUE(condition) \ | |
228 GTEST_TEST_BOOLEAN_(!!(condition), #condition, false, true, \ | |
229 GTEST_FATAL_FAILURE_) | |
230 | |
231 // GMock's ACTION* macros have 10 parameters, most of which go unused. | |
232 // This macro can be used inside ACTION* definitions to suppress warning | |
233 // C4100: unreferenced formal parameter. | |
234 #define UNREFERENCED_ACTION_PARAMETERS \ | |
235 UNREFERENCED_PARAMETER(args); \ | |
236 UNREFERENCED_PARAMETER(arg0); \ | |
237 UNREFERENCED_PARAMETER(arg1); \ | |
238 UNREFERENCED_PARAMETER(arg2); \ | |
239 UNREFERENCED_PARAMETER(arg3); \ | |
240 UNREFERENCED_PARAMETER(arg4); \ | |
241 UNREFERENCED_PARAMETER(arg5); \ | |
242 UNREFERENCED_PARAMETER(arg6); \ | |
243 UNREFERENCED_PARAMETER(arg7); \ | |
244 UNREFERENCED_PARAMETER(arg8); \ | |
245 UNREFERENCED_PARAMETER(arg9) | |
246 | |
247 #endif // OMAHA_TESTING_UNIT_TEST_H_ | |
OLD | NEW |