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_file_name[] = L"installer-ca-tests.exe" ; |
13 const wchar_t mixedcase_exe_name[] = L"Installer-CA-Tests.exe" ; | 13 const wchar_t mixedcase_file_name[] = L"Installer-CA-Tests.exe" ; |
14 const wchar_t * multiple_exe_names[] = { mixedcase_exe_name, L"non-matching-name
" } ; | 14 const wchar_t * multiple_file_names[] = { mixedcase_file_name, L"non-matching-na
me" } ; |
| 15 const wchar_t * multiple_module_names[] = { L"kernel32.dll", L"non-matching-name
" } ; |
| 16 const wchar_t * non_existent_module_names[] = { L"non-matching-name" } ; |
15 | 17 |
16 /** | 18 /** |
17 * Compare to our own process name, case-sensitive, no length limit | 19 * Compare to our own process name, case-sensitive, no length limit |
18 */ | 20 */ |
19 struct our_process_by_name | 21 struct our_process_by_name |
20 : std::unary_function< PROCESSENTRY32W, bool > | 22 : std::unary_function< PROCESSENTRY32W, bool > |
21 { | 23 { |
22 bool operator()( const PROCESSENTRY32W & process ) | 24 bool operator()( const PROCESSENTRY32W & process ) |
23 { | 25 { |
24 return 0 == wcscmp( process.szExeFile, exact_exe_name ) ; | 26 return 0 == wcscmp( process.szExeFile, exact_file_name ) ; |
25 } ; | 27 } ; |
26 }; | 28 }; |
27 | 29 |
28 /** | 30 /** |
29 * Compare to our own process name, case-insensitive, no length limit | 31 * Compare to our own process name, case-insensitive, no length limit |
30 */ | 32 */ |
31 struct our_process_by_name_CI | 33 struct our_process_by_name_CI |
32 : std::unary_function< PROCESSENTRY32W, bool > | 34 : std::unary_function< PROCESSENTRY32W, bool > |
33 { | 35 { |
34 bool operator()( const PROCESSENTRY32W & process ) | 36 bool operator()( const PROCESSENTRY32W & process ) |
35 { | 37 { |
36 return 0 == wcscmpi( process.szExeFile, mixedcase_exe_name ) ; | 38 return 0 == wcscmpi( process.szExeFile, mixedcase_file_name ) ; |
37 } ; | 39 } ; |
38 } ; | 40 } ; |
39 | 41 |
40 /** | 42 /** |
41 * Compare to our own process name, case-insensitive, length-limited | 43 * Compare to our own process name, case-insensitive, length-limited |
42 */ | 44 */ |
43 struct our_process_by_name_CI_N | 45 struct our_process_by_name_CI_N |
44 : std::unary_function< PROCESSENTRY32W, bool > | 46 : std::unary_function< PROCESSENTRY32W, bool > |
45 { | 47 { |
46 bool operator()( const PROCESSENTRY32W & process ) | 48 bool operator()( const PROCESSENTRY32W & process ) |
47 { | 49 { |
48 return 0 == wcsncmpi( process.szExeFile, mixedcase_exe_name, sizeof( mixedca
se_exe_name ) / sizeof( wchar_t ) ) ; | 50 return 0 == wcsncmpi( process.szExeFile, mixedcase_file_name, sizeof( mixedc
ase_file_name ) / sizeof( wchar_t ) ) ; |
49 } ; | 51 } ; |
50 } ; | 52 } ; |
51 | 53 |
52 | 54 |
53 //------------------------------------------------------- | 55 //------------------------------------------------------- |
| 56 //------------------------------------------------------- |
| 57 /** |
| 58 * Filter by process name. Comparison is case-insensitive. |
| 59 */ |
| 60 class process_by_any_file_name_CI |
| 61 : public std::unary_function< PROCESSENTRY32W, bool > |
| 62 { |
| 63 const file_name_set & names ; |
| 64 public: |
| 65 bool operator()( const PROCESSENTRY32W & process) |
| 66 { |
| 67 return names.find( process.szExeFile ) != names.end() ; |
| 68 } |
| 69 process_by_any_file_name_CI( const file_name_set & names ) |
| 70 : names( names ) |
| 71 {} |
| 72 } ; |
| 73 |
| 74 |
| 75 //------------------------------------------------------- |
54 // TESTS, no snapshots | 76 // TESTS, no snapshots |
55 //------------------------------------------------------- | 77 //------------------------------------------------------- |
56 PROCESSENTRY32 process_with_name( const wchar_t * s ) | 78 PROCESSENTRY32 process_with_name( const wchar_t * s ) |
57 { | 79 { |
58 PROCESSENTRY32W p ; | 80 PROCESSENTRY32W p ; |
59 wcsncpy( p.szExeFile, s, MAX_PATH ) ; | 81 wcsncpy( p.szExeFile, s, MAX_PATH ) ; |
60 return p ; | 82 return p ; |
61 } | 83 } |
62 | 84 |
63 PROCESSENTRY32 process_empty = process_with_name( L"" ) ; | 85 PROCESSENTRY32 process_empty = process_with_name( L"" ) ; |
64 PROCESSENTRY32 process_exact = process_with_name( exact_exe_name ) ; | 86 PROCESSENTRY32 process_exact = process_with_name( exact_file_name ) ; |
65 PROCESSENTRY32 process_mixedcase = process_with_name( mixedcase_exe_name ) ; | 87 PROCESSENTRY32 process_mixedcase = process_with_name( mixedcase_file_name ) ; |
66 PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ; | 88 PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ; |
67 PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ; | 89 PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ; |
68 | 90 |
69 exe_name_set multiple_name_set( multiple_exe_names, 2 ) ; | 91 file_name_set multiple_name_set( multiple_file_names, 2 ) ; |
70 process_by_any_exe_name_CI find_in_set( multiple_name_set ) ; | 92 file_name_set multiple_name_set_modules( multiple_module_names, 2 ) ; |
| 93 file_name_set non_existent_name_set_modules( non_existent_module_names, 1 ) ; |
| 94 process_by_any_file_name_CI find_in_set( multiple_name_set ) ; |
| 95 process_by_any_exe_with_any_module find_in_set_w_kernel32( multiple_name_set, mu
ltiple_name_set_modules ) ; |
| 96 process_by_any_exe_with_any_module find_in_set_w_non_existent( multiple_name_set
, non_existent_name_set_modules ) ; |
71 | 97 |
72 TEST( exe_name_set, validate_setup ) | 98 TEST( file_name_set, validate_setup ) |
73 { | 99 { |
74 ASSERT_EQ( 2u, multiple_name_set.size() ) ; | 100 ASSERT_EQ( 2u, multiple_name_set.size() ) ; |
75 ASSERT_TRUE( multiple_name_set.find( mixedcase_exe_name ) != multiple_name_set
.end() ) ; | 101 ASSERT_TRUE( multiple_name_set.find( mixedcase_file_name ) != multiple_name_se
t.end() ) ; |
76 ASSERT_TRUE( multiple_name_set.find( exact_exe_name ) != multiple_name_set.end
() ) ; | 102 ASSERT_TRUE( multiple_name_set.find( exact_file_name ) != multiple_name_set.en
d() ) ; |
77 ASSERT_TRUE( multiple_name_set.find( L"" ) == multiple_name_set.end() ) ; | 103 ASSERT_TRUE( multiple_name_set.find( L"" ) == multiple_name_set.end() ) ; |
78 ASSERT_TRUE( multiple_name_set.find( L"not-in-list" ) == multiple_name_set.end
() ) ; | 104 ASSERT_TRUE( multiple_name_set.find( L"not-in-list" ) == multiple_name_set.end
() ) ; |
79 } | 105 } |
80 | 106 |
81 TEST( process_by_any_exe_name_CI, empty ) | 107 TEST( process_by_any_file_name_CI, empty ) |
82 { | 108 { |
83 const wchar_t * elements[ 1 ] = { 0 } ; // cheating a bit | 109 const wchar_t * elements[ 1 ] = { 0 } ; // cheating a bit |
84 exe_name_set s( elements, 0 ) ; | 110 file_name_set s( elements, 0 ) ; |
85 process_by_any_exe_name_CI x( s ) ; | 111 process_by_any_file_name_CI x( s ) ; |
86 | 112 |
87 ASSERT_FALSE( x( process_empty ) ) ; | 113 ASSERT_FALSE( x( process_empty ) ) ; |
88 ASSERT_FALSE( x( process_exact ) ) ; | 114 ASSERT_FALSE( x( process_exact ) ) ; |
89 ASSERT_FALSE( x( process_mixedcase ) ) ; | 115 ASSERT_FALSE( x( process_mixedcase ) ) ; |
90 ASSERT_FALSE( x( process_explorer ) ) ; | 116 ASSERT_FALSE( x( process_explorer ) ) ; |
91 ASSERT_FALSE( x( process_absent ) ) ; | 117 ASSERT_FALSE( x( process_absent ) ) ; |
92 } | 118 } |
93 | 119 |
94 TEST( process_by_any_exe_name_CI, single_element ) | 120 TEST( process_by_any_file_name_CI, single_element ) |
95 { | 121 { |
96 const wchar_t * elements[ 1 ] = { exact_exe_name } ; | 122 const wchar_t * elements[ 1 ] = { exact_file_name } ; |
97 exe_name_set s( elements, 1 ) ; | 123 file_name_set s( elements, 1 ) ; |
98 process_by_any_exe_name_CI x( s ) ; | 124 process_by_any_file_name_CI x( s ) ; |
99 | 125 |
100 ASSERT_FALSE( x( process_empty ) ) ; | 126 ASSERT_FALSE( x( process_empty ) ) ; |
101 ASSERT_TRUE( x( process_exact ) ) ; | 127 ASSERT_TRUE( x( process_exact ) ) ; |
102 ASSERT_TRUE( x( process_mixedcase ) ) ; | 128 ASSERT_TRUE( x( process_mixedcase ) ) ; |
103 ASSERT_FALSE( x( process_explorer ) ) ; | 129 ASSERT_FALSE( x( process_explorer ) ) ; |
104 ASSERT_FALSE( x( process_absent ) ) ; | 130 ASSERT_FALSE( x( process_absent ) ) ; |
105 } | 131 } |
106 | 132 |
107 TEST( process_by_any_exe_name_CI, two_elements ) | 133 TEST( process_by_any_file_name_CI, two_elements ) |
108 { | 134 { |
109 exe_name_set s( multiple_exe_names, 2 ) ; | 135 file_name_set s( multiple_file_names, 2 ) ; |
110 process_by_any_exe_name_CI x( s ) ; | 136 process_by_any_file_name_CI x( s ) ; |
111 | 137 |
112 ASSERT_FALSE( find_in_set( process_empty ) ) ; | 138 ASSERT_FALSE( find_in_set( process_empty ) ) ; |
113 ASSERT_TRUE( find_in_set( process_exact ) ) ; | 139 ASSERT_TRUE( find_in_set( process_exact ) ) ; |
114 ASSERT_TRUE( find_in_set( process_mixedcase ) ) ; | 140 ASSERT_TRUE( find_in_set( process_mixedcase ) ) ; |
115 ASSERT_FALSE( find_in_set( process_explorer ) ) ; | 141 ASSERT_FALSE( find_in_set( process_explorer ) ) ; |
116 ASSERT_FALSE( find_in_set( process_absent ) ) ; | 142 ASSERT_FALSE( find_in_set( process_absent ) ) ; |
117 } | 143 } |
118 | 144 |
119 //------------------------------------------------------- | 145 //------------------------------------------------------- |
120 // Single-snapshot version of initializers | 146 // Single-snapshot version of initializers |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 ASSERT_GE( 1u, size ); | 201 ASSERT_GE( 1u, size ); |
176 } | 202 } |
177 | 203 |
178 /** | 204 /** |
179 * The only process we are really guaranteed to have is this test process itself
. | 205 * The only process we are really guaranteed to have is this test process itself
. |
180 * This test uses the generic filter function. | 206 * This test uses the generic filter function. |
181 */ | 207 */ |
182 TEST( Process_List_Test, find_our_process_CI_N_generic ) | 208 TEST( Process_List_Test, find_our_process_CI_N_generic ) |
183 { | 209 { |
184 std::vector< PROCESSENTRY32W > v ; | 210 std::vector< PROCESSENTRY32W > v ; |
185 initialize_process_list( v, process_by_name_CI( mixedcase_exe_name ), copy_all
() ) ; | 211 initialize_process_list( v, process_by_name_CI( mixedcase_file_name ), copy_al
l() ) ; |
186 unsigned int size( v.size() ); | 212 unsigned int size( v.size() ); |
187 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul
taneously | 213 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul
taneously |
188 ASSERT_GE( 1u, size ); | 214 ASSERT_GE( 1u, size ); |
189 } | 215 } |
190 | 216 |
191 /** | 217 /** |
192 * Locate the PID of our process. | 218 * Locate the PID of our process. |
193 */ | 219 */ |
194 TEST( Process_List_Test, find_our_PID ) | 220 TEST( Process_List_Test, find_our_PID ) |
195 { | 221 { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 } | 257 } |
232 | 258 |
233 TEST( pid_set, find_our_process_in_set ) | 259 TEST( pid_set, find_our_process_in_set ) |
234 { | 260 { |
235 std::set< DWORD > s ; | 261 std::set< DWORD > s ; |
236 initialize_process_set( s, find_in_set, copy_PID() ) ; | 262 initialize_process_set( s, find_in_set, copy_PID() ) ; |
237 size_t size( s.size() ) ; | 263 size_t size( s.size() ) ; |
238 EXPECT_EQ( size, 1u ); | 264 EXPECT_EQ( size, 1u ); |
239 ASSERT_GE( size, 1u ); | 265 ASSERT_GE( size, 1u ); |
240 } | 266 } |
| 267 |
| 268 TEST( pid_set, find_our_process_in_set_w_kernel32 ) |
| 269 { |
| 270 std::set< DWORD > s ; |
| 271 initialize_process_set( s, find_in_set_w_kernel32, copy_PID() ) ; |
| 272 size_t size( s.size() ) ; |
| 273 EXPECT_EQ( size, 1u ); |
| 274 ASSERT_GE( size, 1u ); |
| 275 } |
| 276 TEST( pid_set, find_our_process_in_set_w_non_existant ) |
| 277 { |
| 278 std::set< DWORD > s ; |
| 279 initialize_process_set( s, find_in_set_w_non_existent, copy_PID() ) ; |
| 280 size_t size( s.size() ) ; |
| 281 EXPECT_EQ( size, 0u ); |
| 282 ASSERT_GE( size, 0u ); |
| 283 } |
OLD | NEW |