Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: installer/src/installer-lib/test/process_test.cpp

Issue 6003395731128320: Only take into account processes that have our plugin loaded (Closed)
Left Patch Set: Created March 27, 2014, 3:29 p.m.
Right Patch Set: Simplify Process_Closer constructor Created March 31, 2014, 8:31 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « installer/src/installer-lib/process.cpp ('k') | installer/src/installer-lib/test/test-installer-lib.wxs » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 " } ; 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" } ; 16 const wchar_t * non_existent_module_names[] = { L"non-matching-name" } ;
17 17
18 /** 18 /**
19 * Compare to our own process name, case-sensitive, no length limit 19 * Compare to our own process name, case-sensitive, no length limit
20 */ 20 */
21 struct our_process_by_name 21 struct our_process_by_name
22 : std::unary_function< PROCESSENTRY32W, bool > 22 : std::unary_function< PROCESSENTRY32W, bool >
23 { 23 {
24 bool operator()( const PROCESSENTRY32W & process ) 24 bool operator()( const PROCESSENTRY32W & process )
25 { 25 {
26 return 0 == wcscmp( process.szExeFile, exact_exe_name ) ; 26 return 0 == wcscmp( process.szExeFile, exact_file_name ) ;
27 } ; 27 } ;
28 }; 28 };
29 29
30 /** 30 /**
31 * Compare to our own process name, case-insensitive, no length limit 31 * Compare to our own process name, case-insensitive, no length limit
32 */ 32 */
33 struct our_process_by_name_CI 33 struct our_process_by_name_CI
34 : std::unary_function< PROCESSENTRY32W, bool > 34 : std::unary_function< PROCESSENTRY32W, bool >
35 { 35 {
36 bool operator()( const PROCESSENTRY32W & process ) 36 bool operator()( const PROCESSENTRY32W & process )
37 { 37 {
38 return 0 == wcscmpi( process.szExeFile, mixedcase_exe_name ) ; 38 return 0 == wcscmpi( process.szExeFile, mixedcase_file_name ) ;
39 } ; 39 } ;
40 } ; 40 } ;
41 41
42 /** 42 /**
43 * Compare to our own process name, case-insensitive, length-limited 43 * Compare to our own process name, case-insensitive, length-limited
44 */ 44 */
45 struct our_process_by_name_CI_N 45 struct our_process_by_name_CI_N
46 : std::unary_function< PROCESSENTRY32W, bool > 46 : std::unary_function< PROCESSENTRY32W, bool >
47 { 47 {
48 bool operator()( const PROCESSENTRY32W & process ) 48 bool operator()( const PROCESSENTRY32W & process )
49 { 49 {
50 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 ) ) ;
51 } ; 51 } ;
52 } ;
53
54
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 {}
52 } ; 72 } ;
53 73
54 74
55 //------------------------------------------------------- 75 //-------------------------------------------------------
56 // TESTS, no snapshots 76 // TESTS, no snapshots
57 //------------------------------------------------------- 77 //-------------------------------------------------------
58 PROCESSENTRY32 process_with_name( const wchar_t * s ) 78 PROCESSENTRY32 process_with_name( const wchar_t * s )
59 { 79 {
60 PROCESSENTRY32W p ; 80 PROCESSENTRY32W p ;
61 wcsncpy( p.szExeFile, s, MAX_PATH ) ; 81 wcsncpy( p.szExeFile, s, MAX_PATH ) ;
62 return p ; 82 return p ;
63 } 83 }
64 84
65 PROCESSENTRY32 process_empty = process_with_name( L"" ) ; 85 PROCESSENTRY32 process_empty = process_with_name( L"" ) ;
66 PROCESSENTRY32 process_exact = process_with_name( exact_exe_name ) ; 86 PROCESSENTRY32 process_exact = process_with_name( exact_file_name ) ;
67 PROCESSENTRY32 process_mixedcase = process_with_name( mixedcase_exe_name ) ; 87 PROCESSENTRY32 process_mixedcase = process_with_name( mixedcase_file_name ) ;
68 PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ; 88 PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ;
69 PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ; 89 PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ;
70 90
71 exe_name_set multiple_name_set( multiple_exe_names, 2 ) ; 91 file_name_set multiple_name_set( multiple_file_names, 2 ) ;
72 exe_name_set multiple_name_set_modules( multiple_module_names, 2 ) ; 92 file_name_set multiple_name_set_modules( multiple_module_names, 2 ) ;
73 exe_name_set non_existent_name_set_modules( non_existent_module_names, 1 ) ; 93 file_name_set non_existent_name_set_modules( non_existent_module_names, 1 ) ;
74 process_by_any_exe_name_CI find_in_set( multiple_name_set ) ; 94 process_by_any_file_name_CI find_in_set( multiple_name_set ) ;
75 process_by_any_exe_name_CI_w_ABP find_in_set_w_kernel32( multiple_name_set, mult iple_name_set_modules ) ; 95 process_by_any_exe_with_any_module find_in_set_w_kernel32( multiple_name_set, mu ltiple_name_set_modules ) ;
76 process_by_any_exe_name_CI_w_ABP find_in_set_w_non_existent( multiple_name_set, non_existent_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 ) ;
77 97
78 TEST( exe_name_set, validate_setup ) 98 TEST( file_name_set, validate_setup )
79 { 99 {
80 ASSERT_EQ( 2u, multiple_name_set.size() ) ; 100 ASSERT_EQ( 2u, multiple_name_set.size() ) ;
81 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() ) ;
82 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() ) ;
83 ASSERT_TRUE( multiple_name_set.find( L"" ) == multiple_name_set.end() ) ; 103 ASSERT_TRUE( multiple_name_set.find( L"" ) == multiple_name_set.end() ) ;
84 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 () ) ;
85 } 105 }
86 106
87 TEST( process_by_any_exe_name_CI, empty ) 107 TEST( process_by_any_file_name_CI, empty )
88 { 108 {
89 const wchar_t * elements[ 1 ] = { 0 } ; // cheating a bit 109 const wchar_t * elements[ 1 ] = { 0 } ; // cheating a bit
90 exe_name_set s( elements, 0 ) ; 110 file_name_set s( elements, 0 ) ;
91 process_by_any_exe_name_CI x( s ) ; 111 process_by_any_file_name_CI x( s ) ;
92 112
93 ASSERT_FALSE( x( process_empty ) ) ; 113 ASSERT_FALSE( x( process_empty ) ) ;
94 ASSERT_FALSE( x( process_exact ) ) ; 114 ASSERT_FALSE( x( process_exact ) ) ;
95 ASSERT_FALSE( x( process_mixedcase ) ) ; 115 ASSERT_FALSE( x( process_mixedcase ) ) ;
96 ASSERT_FALSE( x( process_explorer ) ) ; 116 ASSERT_FALSE( x( process_explorer ) ) ;
97 ASSERT_FALSE( x( process_absent ) ) ; 117 ASSERT_FALSE( x( process_absent ) ) ;
98 } 118 }
99 119
100 TEST( process_by_any_exe_name_CI, single_element ) 120 TEST( process_by_any_file_name_CI, single_element )
101 { 121 {
102 const wchar_t * elements[ 1 ] = { exact_exe_name } ; 122 const wchar_t * elements[ 1 ] = { exact_file_name } ;
103 exe_name_set s( elements, 1 ) ; 123 file_name_set s( elements, 1 ) ;
104 process_by_any_exe_name_CI x( s ) ; 124 process_by_any_file_name_CI x( s ) ;
105 125
106 ASSERT_FALSE( x( process_empty ) ) ; 126 ASSERT_FALSE( x( process_empty ) ) ;
107 ASSERT_TRUE( x( process_exact ) ) ; 127 ASSERT_TRUE( x( process_exact ) ) ;
108 ASSERT_TRUE( x( process_mixedcase ) ) ; 128 ASSERT_TRUE( x( process_mixedcase ) ) ;
109 ASSERT_FALSE( x( process_explorer ) ) ; 129 ASSERT_FALSE( x( process_explorer ) ) ;
110 ASSERT_FALSE( x( process_absent ) ) ; 130 ASSERT_FALSE( x( process_absent ) ) ;
111 } 131 }
112 132
113 TEST( process_by_any_exe_name_CI, two_elements ) 133 TEST( process_by_any_file_name_CI, two_elements )
114 { 134 {
115 exe_name_set s( multiple_exe_names, 2 ) ; 135 file_name_set s( multiple_file_names, 2 ) ;
116 process_by_any_exe_name_CI x( s ) ; 136 process_by_any_file_name_CI x( s ) ;
117 137
118 ASSERT_FALSE( find_in_set( process_empty ) ) ; 138 ASSERT_FALSE( find_in_set( process_empty ) ) ;
119 ASSERT_TRUE( find_in_set( process_exact ) ) ; 139 ASSERT_TRUE( find_in_set( process_exact ) ) ;
120 ASSERT_TRUE( find_in_set( process_mixedcase ) ) ; 140 ASSERT_TRUE( find_in_set( process_mixedcase ) ) ;
121 ASSERT_FALSE( find_in_set( process_explorer ) ) ; 141 ASSERT_FALSE( find_in_set( process_explorer ) ) ;
122 ASSERT_FALSE( find_in_set( process_absent ) ) ; 142 ASSERT_FALSE( find_in_set( process_absent ) ) ;
123 } 143 }
124 144
125 //------------------------------------------------------- 145 //-------------------------------------------------------
126 // Single-snapshot version of initializers 146 // Single-snapshot version of initializers
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 ASSERT_GE( 1u, size ); 201 ASSERT_GE( 1u, size );
182 } 202 }
183 203
184 /** 204 /**
185 * 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 .
186 * This test uses the generic filter function. 206 * This test uses the generic filter function.
187 */ 207 */
188 TEST( Process_List_Test, find_our_process_CI_N_generic ) 208 TEST( Process_List_Test, find_our_process_CI_N_generic )
189 { 209 {
190 std::vector< PROCESSENTRY32W > v ; 210 std::vector< PROCESSENTRY32W > v ;
191 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() ) ;
192 unsigned int size( v.size() ); 212 unsigned int size( v.size() );
193 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
194 ASSERT_GE( 1u, size ); 214 ASSERT_GE( 1u, size );
195 } 215 }
196 216
197 /** 217 /**
198 * Locate the PID of our process. 218 * Locate the PID of our process.
199 */ 219 */
200 TEST( Process_List_Test, find_our_PID ) 220 TEST( Process_List_Test, find_our_PID )
201 { 221 {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 ASSERT_GE( size, 1u ); 274 ASSERT_GE( size, 1u );
255 } 275 }
256 TEST( pid_set, find_our_process_in_set_w_non_existant ) 276 TEST( pid_set, find_our_process_in_set_w_non_existant )
257 { 277 {
258 std::set< DWORD > s ; 278 std::set< DWORD > s ;
259 initialize_process_set( s, find_in_set_w_non_existent, copy_PID() ) ; 279 initialize_process_set( s, find_in_set_w_non_existent, copy_PID() ) ;
260 size_t size( s.size() ) ; 280 size_t size( s.size() ) ;
261 EXPECT_EQ( size, 0u ); 281 EXPECT_EQ( size, 0u );
262 ASSERT_GE( size, 0u ); 282 ASSERT_GE( size, 0u );
263 } 283 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld