OLD | NEW |
1 #include <gtest/gtest.h> | 1 #include <gtest/gtest.h> |
2 #include "../process.h" | 2 #include "../process.h" |
3 #include <functional> | 3 #include <functional> |
4 | 4 |
5 // Turn off warnings for string copies | 5 // Turn off warnings for string copies |
6 #pragma warning( disable : 4996 ) | 6 #pragma warning( disable : 4996 ) |
7 | 7 |
8 //------------------------------------------------------- | 8 //------------------------------------------------------- |
9 // Comparison objects | 9 // Comparison objects |
10 //------------------------------------------------------- | 10 //------------------------------------------------------- |
11 | 11 |
12 const wchar_t exact_exe_name[] = L"installer-ca-tests.exe" ; | 12 const wchar_t exact_exe_name[] = L"installer-ca-tests.exe"; |
13 const std::wstring exact_exe_string( exact_exe_name ) ; | 13 const std::wstring exact_exe_string(exact_exe_name); |
14 const WstringCaseInsensitive exact_exe_string_ci( exact_exe_name ) ; | 14 const WstringCaseInsensitive exact_exe_string_ci(exact_exe_name); |
15 | 15 |
16 const wchar_t mixedcase_exe_name[] = L"Installer-CA-Tests.exe" ; | 16 const wchar_t mixedcase_exe_name[] = L"Installer-CA-Tests.exe"; |
17 const WstringCaseInsensitive mixedcase_exe_string_ci( mixedcase_exe_name ) ; | 17 const WstringCaseInsensitive mixedcase_exe_string_ci(mixedcase_exe_name); |
18 | 18 |
19 const wchar_t unknown_name[] = L"non-matching-name" ; | 19 const wchar_t unknown_name[] = L"non-matching-name"; |
20 const wchar_t * multiple_exe_names[] = { mixedcase_exe_name, unknown_name } ; | 20 const wchar_t* multiple_exe_names[] = { mixedcase_exe_name, unknown_name }; |
21 | 21 |
22 /** | 22 /** |
23 * Compare to our own process name, case-sensitive, no length limit | 23 * Compare to our own process name, case-sensitive, no length limit |
24 */ | 24 */ |
25 struct our_process_by_name | 25 struct our_process_by_name |
26 : std::unary_function< PROCESSENTRY32W, bool > | 26 : std::unary_function<PROCESSENTRY32W, bool> |
27 { | 27 { |
28 bool operator()( const PROCESSENTRY32W & process ) | 28 bool operator()(const PROCESSENTRY32W& process) |
29 { | 29 { |
30 return std::wstring( process.szExeFile ) == exact_exe_string ; | 30 return std::wstring(process.szExeFile) == exact_exe_string; |
31 } ; | 31 }; |
32 }; | 32 }; |
33 | 33 |
34 /** | 34 /** |
35 * Compare to our own process name, case-insensitive, no length limit | 35 * Compare to our own process name, case-insensitive, no length limit |
36 */ | 36 */ |
37 struct our_process_by_name_CI | 37 struct our_process_by_name_CI |
38 : std::unary_function< PROCESSENTRY32W, bool > | 38 : std::unary_function<PROCESSENTRY32W, bool> |
39 { | 39 { |
40 bool operator()( const PROCESSENTRY32W & process ) | 40 bool operator()(const PROCESSENTRY32W& process) |
41 { | 41 { |
42 return WstringCaseInsensitive( process.szExeFile ) == mixedcase_exe_string_c
i ; | 42 return WstringCaseInsensitive(process.szExeFile) == mixedcase_exe_string_ci; |
43 } ; | 43 }; |
44 } ; | 44 }; |
45 | 45 |
46 //------------------------------------------------------- | 46 //------------------------------------------------------- |
47 //------------------------------------------------------- | 47 //------------------------------------------------------- |
48 /** | 48 /** |
49 * Filter by process name. Comparison is case-insensitive. | 49 * Filter by process name. Comparison is case-insensitive. |
50 */ | 50 */ |
51 class process_by_any_file_name_CI | 51 class process_by_any_file_name_CI |
52 : public std::unary_function< PROCESSENTRY32W, bool > | 52 : public std::unary_function<PROCESSENTRY32W, bool> |
53 { | 53 { |
54 const FileNameSet & names ; | 54 const FileNameSet& names; |
55 public: | 55 public: |
56 bool operator()( const PROCESSENTRY32W & process) | 56 bool operator()(const PROCESSENTRY32W& process) |
57 { | 57 { |
58 return names.find( process.szExeFile ) != names.end() ; | 58 return names.find(process.szExeFile) != names.end(); |
59 } | 59 } |
60 process_by_any_file_name_CI( const FileNameSet & names ) | 60 process_by_any_file_name_CI(const FileNameSet& names) |
61 : names( names ) | 61 : names(names) |
62 {} | 62 {} |
63 } ; | 63 }; |
64 | 64 |
65 /** | 65 /** |
66 * Filter by process name. Comparison is case-insensitive. | 66 * Filter by process name. Comparison is case-insensitive. |
67 */ | 67 */ |
68 class process_by_name_CI | 68 class process_by_name_CI |
69 : public std::unary_function< PROCESSENTRY32W, bool > | 69 : public std::unary_function<PROCESSENTRY32W, bool> |
70 { | 70 { |
71 const WstringCaseInsensitive _name ; | 71 const WstringCaseInsensitive _name; |
72 public: | 72 public: |
73 bool operator()( const PROCESSENTRY32W & process ) | 73 bool operator()(const PROCESSENTRY32W& process) |
74 { | 74 { |
75 return _name == WstringCaseInsensitive( process.szExeFile ) ; | 75 return _name == WstringCaseInsensitive(process.szExeFile); |
76 } | 76 } |
77 | 77 |
78 process_by_name_CI( const wchar_t * name ) | 78 process_by_name_CI(const wchar_t* name) |
79 : _name( name ) | 79 : _name(name) |
80 {} | 80 {} |
81 } ; | 81 }; |
82 | 82 |
83 //------------------------------------------------------- | 83 //------------------------------------------------------- |
84 // TESTS, no snapshots | 84 // TESTS, no snapshots |
85 //------------------------------------------------------- | 85 //------------------------------------------------------- |
86 PROCESSENTRY32 process_with_name( const wchar_t * s ) | 86 PROCESSENTRY32 process_with_name(const wchar_t* s) |
87 { | 87 { |
88 PROCESSENTRY32W p ; | 88 PROCESSENTRY32W p; |
89 wcsncpy( p.szExeFile, s, MAX_PATH ) ; | 89 wcsncpy(p.szExeFile, s, MAX_PATH); |
90 return p ; | 90 return p; |
91 } | 91 } |
92 | 92 |
93 PROCESSENTRY32 process_empty = process_with_name( L"" ) ; | 93 PROCESSENTRY32 process_empty = process_with_name(L""); |
94 PROCESSENTRY32 process_exact = process_with_name( exact_exe_name ) ; | 94 PROCESSENTRY32 process_exact = process_with_name(exact_exe_name); |
95 PROCESSENTRY32 process_mixedcase = process_with_name( mixedcase_exe_name ) ; | 95 PROCESSENTRY32 process_mixedcase = process_with_name(mixedcase_exe_name); |
96 PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ; | 96 PROCESSENTRY32 process_explorer = process_with_name(L"explorer.exe"); |
97 PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ; | 97 PROCESSENTRY32 process_absent = process_with_name(L"no_such_name"); |
98 | 98 |
99 FileNameSet multiple_name_set( multiple_exe_names ) ; | 99 FileNameSet multiple_name_set(multiple_exe_names); |
100 process_by_any_file_name_CI find_in_set( multiple_name_set ) ; | 100 process_by_any_file_name_CI find_in_set(multiple_name_set); |
101 ProcessByAnyExeNotImmersive find_in_set_not_immersive( multiple_name_set ) ; | 101 ProcessByAnyExeNotImmersive find_in_set_not_immersive(multiple_name_set); |
102 | 102 |
103 | 103 |
104 TEST( file_name_set, validate_setup ) | 104 TEST(file_name_set, validate_setup) |
105 { | 105 { |
106 ASSERT_EQ( 2u, multiple_name_set.size() ) ; | 106 ASSERT_EQ(2u, multiple_name_set.size()); |
107 ASSERT_TRUE( multiple_name_set.find( exact_exe_string_ci ) != multiple_name_se
t.end() ) ; | 107 ASSERT_TRUE(multiple_name_set.find(exact_exe_string_ci) != multiple_name_set.e
nd()); |
108 ASSERT_TRUE( multiple_name_set.find( mixedcase_exe_string_ci ) != multiple_nam
e_set.end() ) ; | 108 ASSERT_TRUE(multiple_name_set.find(mixedcase_exe_string_ci) != multiple_name_s
et.end()); |
109 ASSERT_TRUE( multiple_name_set.find( L"" ) == multiple_name_set.end() ) ; | 109 ASSERT_TRUE(multiple_name_set.find(L"") == multiple_name_set.end()); |
110 ASSERT_TRUE( multiple_name_set.find( L"not-in-list" ) == multiple_name_set.end
() ) ; | 110 ASSERT_TRUE(multiple_name_set.find(L"not-in-list") == multiple_name_set.end())
; |
111 } | 111 } |
112 | 112 |
113 TEST( process_by_any_file_name_CI, empty ) | 113 TEST(process_by_any_file_name_CI, empty) |
114 { | 114 { |
115 FileNameSet s ; | 115 FileNameSet s; |
116 process_by_any_file_name_CI x( s ) ; | 116 process_by_any_file_name_CI x(s); |
117 | 117 |
118 ASSERT_FALSE( x( process_empty ) ) ; | 118 ASSERT_FALSE(x(process_empty)); |
119 ASSERT_FALSE( x( process_exact ) ) ; | 119 ASSERT_FALSE(x(process_exact)); |
120 ASSERT_FALSE( x( process_mixedcase ) ) ; | 120 ASSERT_FALSE(x(process_mixedcase)); |
121 ASSERT_FALSE( x( process_explorer ) ) ; | 121 ASSERT_FALSE(x(process_explorer)); |
122 ASSERT_FALSE( x( process_absent ) ) ; | 122 ASSERT_FALSE(x(process_absent)); |
123 } | 123 } |
124 | 124 |
125 TEST( process_by_any_file_name_CI, single_element_known ) | 125 TEST(process_by_any_file_name_CI, single_element_known) |
126 { | 126 { |
127 const wchar_t * elements[ 1 ] = { exact_exe_name } ; | 127 const wchar_t* elements[1] = { exact_exe_name }; |
128 FileNameSet s( elements ) ; | 128 FileNameSet s(elements); |
129 process_by_any_file_name_CI x( s ) ; | 129 process_by_any_file_name_CI x(s); |
130 | 130 |
131 ASSERT_FALSE( x( process_empty ) ) ; | 131 ASSERT_FALSE(x(process_empty)); |
132 ASSERT_TRUE( x( process_exact ) ) ; | 132 ASSERT_TRUE(x(process_exact)); |
133 ASSERT_TRUE( x( process_mixedcase ) ) ; | 133 ASSERT_TRUE(x(process_mixedcase)); |
134 ASSERT_FALSE( x( process_explorer ) ) ; | 134 ASSERT_FALSE(x(process_explorer)); |
135 ASSERT_FALSE( x( process_absent ) ) ; | 135 ASSERT_FALSE(x(process_absent)); |
136 } | 136 } |
137 | 137 |
138 TEST( process_by_any_file_name_CI, single_element_unknown ) | 138 TEST(process_by_any_file_name_CI, single_element_unknown) |
139 { | 139 { |
140 const wchar_t * elements[ 1 ] = { unknown_name } ; | 140 const wchar_t* elements[1] = { unknown_name }; |
141 FileNameSet s( elements ) ; | 141 FileNameSet s(elements); |
142 process_by_any_file_name_CI x( s ) ; | 142 process_by_any_file_name_CI x(s); |
143 | 143 |
144 ASSERT_FALSE( x( process_empty ) ) ; | 144 ASSERT_FALSE(x(process_empty)); |
145 ASSERT_FALSE( x( process_exact ) ) ; | 145 ASSERT_FALSE(x(process_exact)); |
146 ASSERT_FALSE( x( process_mixedcase ) ) ; | 146 ASSERT_FALSE(x(process_mixedcase)); |
147 ASSERT_FALSE( x( process_explorer ) ) ; | 147 ASSERT_FALSE(x(process_explorer)); |
148 ASSERT_FALSE( x( process_absent ) ) ; | 148 ASSERT_FALSE(x(process_absent)); |
149 } | 149 } |
150 | 150 |
151 TEST( process_by_any_file_name_CI, two_elements ) | 151 TEST(process_by_any_file_name_CI, two_elements) |
152 { | 152 { |
153 FileNameSet s( multiple_exe_names ) ; | 153 FileNameSet s(multiple_exe_names); |
154 process_by_any_file_name_CI x( s ) ; | 154 process_by_any_file_name_CI x(s); |
155 | 155 |
156 ASSERT_FALSE( find_in_set( process_empty ) ) ; | 156 ASSERT_FALSE(find_in_set(process_empty)); |
157 ASSERT_TRUE( find_in_set( process_exact ) ) ; | 157 ASSERT_TRUE(find_in_set(process_exact)); |
158 ASSERT_TRUE( find_in_set( process_mixedcase ) ) ; | 158 ASSERT_TRUE(find_in_set(process_mixedcase)); |
159 ASSERT_FALSE( find_in_set( process_explorer ) ) ; | 159 ASSERT_FALSE(find_in_set(process_explorer)); |
160 ASSERT_FALSE( find_in_set( process_absent ) ) ; | 160 ASSERT_FALSE(find_in_set(process_absent)); |
161 } | 161 } |
162 | 162 |
163 //------------------------------------------------------- | 163 //------------------------------------------------------- |
164 // Single-snapshot version of initializers | 164 // Single-snapshot version of initializers |
165 //------------------------------------------------------- | 165 //------------------------------------------------------- |
166 /** | 166 /** |
167 * Single-snapshot version of initialize_process_list, for testing. | 167 * Single-snapshot version of initialize_process_list, for testing. |
168 */ | 168 */ |
169 template< class T, class Admittance, class Extractor > | 169 template<class T, class Admittance, class Extractor> |
170 void initialize_process_list( std::vector< T > & v, Admittance admit = Admittanc
e(), Extractor extract = Extractor() ) | 170 void initialize_process_list(std::vector<T>& v, Admittance admit = Admittance(),
Extractor extract = Extractor()) |
171 { | 171 { |
172 InitializeProcessList( v, ProcessSnapshot(), admit, extract ) ; | 172 InitializeProcessList(v, ProcessSnapshot(), admit, extract); |
173 } | 173 } |
174 | 174 |
175 /** | 175 /** |
176 * Single-snapshot version of initialize_process_set, for testing. | 176 * Single-snapshot version of initialize_process_set, for testing. |
177 */ | 177 */ |
178 template< class T, class Admittance, class Extractor > | 178 template<class T, class Admittance, class Extractor> |
179 void initialize_process_set( std::set< T > & s, Admittance admit = Admittance(),
Extractor extract = Extractor() ) | 179 void initialize_process_set(std::set<T>& s, Admittance admit = Admittance(), Ext
ractor extract = Extractor()) |
180 { | 180 { |
181 InitializeProcessSet( s, ProcessSnapshot(), admit, extract ) ; | 181 InitializeProcessSet(s, ProcessSnapshot(), admit, extract); |
182 } | 182 } |
183 | 183 |
184 //------------------------------------------------------- | 184 //------------------------------------------------------- |
185 // TESTS with snapshots | 185 // TESTS with snapshots |
186 //------------------------------------------------------- | 186 //------------------------------------------------------- |
187 /** | 187 /** |
188 * Construction test ensures that we don't throw and that at least one process s
hows up. | 188 * Construction test ensures that we don't throw and that at least one process s
hows up. |
189 */ | 189 */ |
190 TEST( Process_List_Test, construct_vector ) | 190 TEST(Process_List_Test, construct_vector) |
191 { | 191 { |
192 std::vector< PROCESSENTRY32W > v ; | 192 std::vector<PROCESSENTRY32W> v; |
193 initialize_process_list( v, EveryProcess(), CopyAll() ) ; | 193 initialize_process_list(v, EveryProcess(), CopyAll()); |
194 ASSERT_GE( v.size(), 1u ); | 194 ASSERT_GE(v.size(), 1u); |
195 } | 195 } |
196 | 196 |
197 /** | 197 /** |
198 * The only process we are really guaranteed to have is this test process itself
. | 198 * The only process we are really guaranteed to have is this test process itself
. |
199 */ | 199 */ |
200 TEST( Process_List_Test, find_our_process ) | 200 TEST(Process_List_Test, find_our_process) |
201 { | 201 { |
202 std::vector< PROCESSENTRY32W > v ; | 202 std::vector<PROCESSENTRY32W> v; |
203 initialize_process_list( v, our_process_by_name(), CopyAll() ) ; | 203 initialize_process_list(v, our_process_by_name(), CopyAll()); |
204 size_t size( v.size() ); | 204 size_t size(v.size()); |
205 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul
taneously | 205 EXPECT_EQ(1u, size); // Please, don't run multiple test executables simul
taneously |
206 ASSERT_GE( 1u, size ); | 206 ASSERT_GE(1u, size); |
207 } | 207 } |
208 | 208 |
209 /** | 209 /** |
210 * The only process we are really guaranteed to have is this test process itself
. | 210 * The only process we are really guaranteed to have is this test process itself
. |
211 * This test uses same one used in Process_Closer | 211 * This test uses same one used in Process_Closer |
212 */ | 212 */ |
213 TEST( Process_List_Test, find_our_process_CI_generic ) | 213 TEST(Process_List_Test, find_our_process_CI_generic) |
214 { | 214 { |
215 std::vector< PROCESSENTRY32W > v ; | 215 std::vector<PROCESSENTRY32W> v; |
216 initialize_process_list( v, process_by_name_CI( mixedcase_exe_name ), CopyAll(
) ) ; | 216 initialize_process_list(v, process_by_name_CI(mixedcase_exe_name), CopyAll()); |
217 size_t size( v.size() ); | 217 size_t size(v.size()); |
218 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul
taneously | 218 EXPECT_EQ(1u, size); // Please, don't run multiple test executables simul
taneously |
219 ASSERT_GE( 1u, size ); | 219 ASSERT_GE(1u, size); |
220 } | 220 } |
221 | 221 |
222 /** | 222 /** |
223 * The only process we are really guaranteed to have is this test process itself
. | 223 * The only process we are really guaranteed to have is this test process itself
. |
224 * This test uses the generic filter function. | 224 * This test uses the generic filter function. |
225 */ | 225 */ |
226 TEST( Process_List_Test, find_our_process_CI_as_used ) | 226 TEST(Process_List_Test, find_our_process_CI_as_used) |
227 { | 227 { |
228 std::vector< PROCESSENTRY32W > v ; | 228 std::vector<PROCESSENTRY32W> v; |
229 initialize_process_list( v, process_by_any_file_name_CI( FileNameSet( multiple
_exe_names ) ), CopyAll() ) ; | 229 initialize_process_list(v, process_by_any_file_name_CI(FileNameSet(multiple_ex
e_names)), CopyAll()); |
230 size_t size( v.size() ); | 230 size_t size(v.size()); |
231 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul
taneously | 231 EXPECT_EQ(1u, size); // Please, don't run multiple test executables simul
taneously |
232 ASSERT_GE( 1u, size ); | 232 ASSERT_GE(1u, size); |
233 } | 233 } |
234 | 234 |
235 /** | 235 /** |
236 * Locate the PID of our process. | 236 * Locate the PID of our process. |
237 */ | 237 */ |
238 TEST( Process_List_Test, find_our_PID ) | 238 TEST(Process_List_Test, find_our_PID) |
239 { | 239 { |
240 std::vector< DWORD > v ; | 240 std::vector<DWORD> v; |
241 initialize_process_list( v, our_process_by_name(), CopyPID() ) ; | 241 initialize_process_list(v, our_process_by_name(), CopyPID()); |
242 size_t size( v.size() ); | 242 size_t size(v.size()); |
243 EXPECT_EQ( size, 1u ); // Please, don't run multiple test executables simul
taneously | 243 EXPECT_EQ(size, 1u); // Please, don't run multiple test executables simul
taneously |
244 ASSERT_GE( size, 1u ); | 244 ASSERT_GE(size, 1u); |
245 } | 245 } |
246 | 246 |
247 /** | 247 /** |
248 * Locate the PID of our process using the | 248 * Locate the PID of our process using the |
249 */ | 249 */ |
250 TEST( Process_List_Test, find_our_process_in_set ) | 250 TEST(Process_List_Test, find_our_process_in_set) |
251 { | 251 { |
252 std::vector< DWORD > v ; | 252 std::vector<DWORD> v; |
253 initialize_process_list( v, find_in_set, CopyPID() ) ; | 253 initialize_process_list(v, find_in_set, CopyPID()); |
254 size_t size( v.size() ); | 254 size_t size(v.size()); |
255 EXPECT_EQ( size, 1u ); // Please, don't run multiple test executables simul
taneously | 255 EXPECT_EQ(size, 1u); // Please, don't run multiple test executables simul
taneously |
256 ASSERT_GE( size, 1u ); | 256 ASSERT_GE(size, 1u); |
257 } | 257 } |
258 | 258 |
259 //------------------------------------------------------- | 259 //------------------------------------------------------- |
260 // TESTS for process ID sets | 260 // TESTS for process ID sets |
261 //------------------------------------------------------- | 261 //------------------------------------------------------- |
262 /* | 262 /* |
263 * Can't use copy_all without a definition for "less< PROCESSENTRY32W >". | 263 * Can't use copy_all without a definition for "less<PROCESSENTRY32W>". |
264 * Thus all tests only use copy_PID | 264 * Thus all tests only use copy_PID |
265 */ | 265 */ |
266 | 266 |
267 /** | 267 /** |
268 * Construction test ensures that we don't throw and that at least one process s
hows up. | 268 * Construction test ensures that we don't throw and that at least one process s
hows up. |
269 */ | 269 */ |
270 TEST( pid_set, construct_set ) | 270 TEST(pid_set, construct_set) |
271 { | 271 { |
272 std::set< DWORD > s ; | 272 std::set<DWORD> s; |
273 initialize_process_set( s, EveryProcess(), CopyPID() ) ; | 273 initialize_process_set(s, EveryProcess(), CopyPID()); |
274 ASSERT_GE( s.size(), 1u ); | 274 ASSERT_GE(s.size(), 1u); |
275 } | 275 } |
276 | 276 |
277 TEST( pid_set, find_our_process_in_set ) | 277 TEST(pid_set, find_our_process_in_set) |
278 { | 278 { |
279 std::set< DWORD > s ; | 279 std::set<DWORD> s; |
280 initialize_process_set( s, find_in_set, CopyPID() ) ; | 280 initialize_process_set(s, find_in_set, CopyPID()); |
281 size_t size( s.size() ) ; | 281 size_t size(s.size()); |
282 EXPECT_EQ( size, 1u ); | 282 EXPECT_EQ(size, 1u); |
283 ASSERT_GE( size, 1u ); | 283 ASSERT_GE(size, 1u); |
284 } | 284 } |
285 | 285 |
286 TEST( pid_set, find_our_process_in_set_not_immersive ) | 286 TEST(pid_set, find_our_process_in_set_not_immersive) |
287 { | 287 { |
288 std::set< DWORD > s ; | 288 std::set<DWORD> s; |
289 initialize_process_set( s, find_in_set_not_immersive, CopyPID() ) ; | 289 initialize_process_set(s, find_in_set_not_immersive, CopyPID()); |
290 size_t size( s.size() ) ; | 290 size_t size(s.size()); |
291 EXPECT_EQ( size, 1u ); | 291 EXPECT_EQ(size, 1u); |
292 ASSERT_GE( size, 1u ); | 292 ASSERT_GE(size, 1u); |
293 } | 293 } |
OLD | NEW |