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

Side by Side Diff: installer/src/installer-lib/test/process_test.cpp

Issue 5675960980471808: Updated installer with custom action (Closed)
Patch Set: Created March 8, 2014, 5:06 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include <gtest/gtest.h> 1 #include <gtest/gtest.h>
2 #include "../process.h"
3 #include <functional>
2 4
3 #include "../process.h" 5 // Turn off warnings for string copies
6 #pragma warning( disable : 4996 )
7
8 //-------------------------------------------------------
9 // Comparison objects
10 //-------------------------------------------------------
11
12 const wchar_t exact_exe_name[] = L"installer-ca-tests.exe" ;
13 const wchar_t mixedcase_exe_name[] = L"Installer-CA-Tests.exe" ;
14 const wchar_t * multiple_exe_names[] = { mixedcase_exe_name, L"non-matching-name " } ;
15
16 /**
17 * Compare to our own process name, case-sensitive, no length limit
18 */
19 struct our_process_by_name
20 : std::unary_function< PROCESSENTRY32W, bool >
21 {
22 bool operator()( const PROCESSENTRY32W & process )
23 {
24 return 0 == wcscmp( process.szExeFile, exact_exe_name ) ;
25 } ;
26 };
27
28 /**
29 * Compare to our own process name, case-insensitive, no length limit
30 */
31 struct our_process_by_name_CI
32 : std::unary_function< PROCESSENTRY32W, bool >
33 {
34 bool operator()( const PROCESSENTRY32W & process )
35 {
36 return 0 == wcscmpi( process.szExeFile, mixedcase_exe_name ) ;
37 } ;
38 } ;
39
40 /**
41 * Compare to our own process name, case-insensitive, length-limited
42 */
43 struct our_process_by_name_CI_N
44 : std::unary_function< PROCESSENTRY32W, bool >
45 {
46 bool operator()( const PROCESSENTRY32W & process )
47 {
48 return 0 == wcsncmpi( process.szExeFile, mixedcase_exe_name, sizeof( mixedca se_exe_name ) / sizeof( wchar_t ) ) ;
49 } ;
50 } ;
4 51
5 52
6 /** 53 //-------------------------------------------------------
7 * A promiscuous filter admits everything. 54 // TESTS, no snapshots
8 */ 55 //-------------------------------------------------------
9 bool trivial_true( PROCESSENTRY32W & ) 56 PROCESSENTRY32 process_with_name( const wchar_t * s )
10 { 57 {
11 return true; 58 PROCESSENTRY32W p ;
59 wcsncpy( p.szExeFile, s, MAX_PATH ) ;
60 return p ;
12 } 61 }
13 62
14 /* 63 PROCESSENTRY32 process_empty = process_with_name( L"" ) ;
15 * Converter that copies everything, by reference. 64 PROCESSENTRY32 process_exact = process_with_name( exact_exe_name ) ;
65 PROCESSENTRY32 process_mixedcase = process_with_name( mixedcase_exe_name ) ;
66 PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ;
67 PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ;
68
69 exe_name_set multiple_name_set( multiple_exe_names, 2 ) ;
70 process_by_any_exe_name_CI find_in_set( multiple_name_set ) ;
71
72 TEST( exe_name_set, validate_setup )
73 {
74 ASSERT_EQ( 2u, multiple_name_set.size() ) ;
75 ASSERT_TRUE( multiple_name_set.find( mixedcase_exe_name ) != multiple_name_set .end() ) ;
76 ASSERT_TRUE( multiple_name_set.find( exact_exe_name ) != multiple_name_set.end () ) ;
77 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 () ) ;
79 }
80
81 TEST( process_by_any_exe_name_CI, empty )
82 {
83 const wchar_t * elements[ 1 ] = { 0 } ; // cheating a bit
84 exe_name_set s( elements, 0 ) ;
85 process_by_any_exe_name_CI x( s ) ;
86
87 ASSERT_FALSE( x( process_empty ) ) ;
88 ASSERT_FALSE( x( process_exact ) ) ;
89 ASSERT_FALSE( x( process_mixedcase ) ) ;
90 ASSERT_FALSE( x( process_explorer ) ) ;
91 ASSERT_FALSE( x( process_absent ) ) ;
92 }
93
94 TEST( process_by_any_exe_name_CI, single_element )
95 {
96 const wchar_t * elements[ 1 ] = { exact_exe_name } ;
97 exe_name_set s( elements, 1 ) ;
98 process_by_any_exe_name_CI x( s ) ;
99
100 ASSERT_FALSE( x( process_empty ) ) ;
101 ASSERT_TRUE( x( process_exact ) ) ;
102 ASSERT_TRUE( x( process_mixedcase ) ) ;
103 ASSERT_FALSE( x( process_explorer ) ) ;
104 ASSERT_FALSE( x( process_absent ) ) ;
105 }
106
107 TEST( process_by_any_exe_name_CI, two_elements )
108 {
109 exe_name_set s( multiple_exe_names, 2 ) ;
110 process_by_any_exe_name_CI x( s ) ;
111
112 ASSERT_FALSE( find_in_set( process_empty ) ) ;
113 ASSERT_TRUE( find_in_set( process_exact ) ) ;
114 ASSERT_TRUE( find_in_set( process_mixedcase ) ) ;
115 ASSERT_FALSE( find_in_set( process_explorer ) ) ;
116 ASSERT_FALSE( find_in_set( process_absent ) ) ;
117 }
118
119 //-------------------------------------------------------
120 // Single-snapshot version of initializers
121 //-------------------------------------------------------
122 /**
123 * Single-snapshot version of initialize_process_list, for testing.
16 */ 124 */
17 PROCESSENTRY32W & copy_all( PROCESSENTRY32W & process ) 125 template< class T, class Admittance, class Extractor >
126 void initialize_process_list( std::vector< T > & v, Admittance admit = Admittanc e(), Extractor extract = Extractor() )
18 { 127 {
19 return process ; 128 initialize_process_list( v, Snapshot(), admit, extract ) ;
20 } 129 }
21 130
22 /** 131 /**
132 * Single-snapshot version of initialize_process_set, for testing.
133 */
134 template< class T, class Admittance, class Extractor >
135 void initialize_process_set( std::set< T > & s, Admittance admit = Admittance(), Extractor extract = Extractor() )
136 {
137 initialize_process_set( s, Snapshot(), admit, extract ) ;
138 }
139
140 //-------------------------------------------------------
141 // TESTS with snapshots
142 //-------------------------------------------------------
143 /**
23 * Construction test ensures that we don't throw and that at least one process s hows up. 144 * Construction test ensures that we don't throw and that at least one process s hows up.
24 */ 145 */
25 TEST( Process_List_Test, construct ) 146 TEST( Process_List_Test, construct_vector )
26 { 147 {
27 Process_List< PROCESSENTRY32W > pl( trivial_true, copy_all ); 148 std::vector< PROCESSENTRY32W > v ;
28 ASSERT_GE( pl.v.size(), 1u ); 149 initialize_process_list( v, every_process(), copy_all() ) ;
150 ASSERT_GE( v.size(), 1u );
29 } 151 }
30 152
31 /** 153 /**
32 * Filter by a fixed name.
33 */
34 bool our_process_by_name( PROCESSENTRY32W & process )
35 {
36 return 0 == wcscmp( process.szExeFile, L"installer-ca-tests.exe" );
37 }
38
39 /**
40 * The only process we are really guaranteed to have is this test process itself . 154 * The only process we are really guaranteed to have is this test process itself .
41 */ 155 */
42 TEST( Process_List_Test, find_our_process ) 156 TEST( Process_List_Test, find_our_process )
43 { 157 {
44 Process_List< PROCESSENTRY32W > pl( our_process_by_name, copy_all ); 158 std::vector< PROCESSENTRY32W > v ;
45 unsigned int size( pl.v.size() ); 159 initialize_process_list( v, our_process_by_name(), copy_all() ) ;
46 EXPECT_EQ( size, 1u ); // Please, don't run multiple test executables simul taneously 160 unsigned int size( v.size() );
47 ASSERT_GE( size, 1u ); 161 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul taneously
48 } 162 ASSERT_GE( 1u, size );
49
50 /*
51 * Converter that copies only the PID, by value.
52 */
53 DWORD copy_PID( PROCESSENTRY32W & process )
54 {
55 return process.th32ProcessID ;
56 } 163 }
57 164
58 /** 165 /**
166 * The only process we are really guaranteed to have is this test process itself .
167 * This test uses a filter function class with a special, fixed name comparison.
168 */
169 TEST( Process_List_Test, find_our_process_CI_N_special )
170 {
171 std::vector< PROCESSENTRY32W > v ;
172 initialize_process_list( v, our_process_by_name_CI_N(), copy_all() ) ;
173 unsigned int size( v.size() );
174 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul taneously
175 ASSERT_GE( 1u, size );
176 }
177
178 /**
179 * The only process we are really guaranteed to have is this test process itself .
180 * This test uses the generic filter function.
181 */
182 TEST( Process_List_Test, find_our_process_CI_N_generic )
183 {
184 std::vector< PROCESSENTRY32W > v ;
185 initialize_process_list( v, process_by_name_CI( mixedcase_exe_name ), copy_all () ) ;
186 unsigned int size( v.size() );
187 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul taneously
188 ASSERT_GE( 1u, size );
189 }
190
191 /**
59 * Locate the PID of our process. 192 * Locate the PID of our process.
60 */ 193 */
61 TEST( Process_List_Test, find_our_PID ) 194 TEST( Process_List_Test, find_our_PID )
62 { 195 {
63 Process_List< DWORD > pl( our_process_by_name, copy_PID ); 196 std::vector< DWORD > v ;
64 unsigned int size( pl.v.size() ); 197 initialize_process_list( v, our_process_by_name(), copy_PID() ) ;
198 unsigned int size( v.size() );
65 EXPECT_EQ( size, 1u ); // Please, don't run multiple test executables simul taneously 199 EXPECT_EQ( size, 1u ); // Please, don't run multiple test executables simul taneously
66 ASSERT_GE( size, 1u ); 200 ASSERT_GE( size, 1u );
67 } 201 }
68 202
203 /**
204 * Locate the PID of our process using the
205 */
206 TEST( Process_List_Test, find_our_process_in_set )
207 {
208 std::vector< DWORD > v ;
209 initialize_process_list( v, find_in_set, copy_PID() ) ;
210 unsigned int size( v.size() );
211 EXPECT_EQ( size, 1u ); // Please, don't run multiple test executables simul taneously
212 ASSERT_GE( size, 1u );
213 }
214
215 //-------------------------------------------------------
216 // TESTS for process ID sets
217 //-------------------------------------------------------
218 /*
219 * Can't use copy_all without a definition for "less< PROCESSENTRY32W >".
220 * Thus all tests only use copy_PID
221 */
222
223 /**
224 * Construction test ensures that we don't throw and that at least one process s hows up.
225 */
226 TEST( pid_set, construct_set )
227 {
228 std::set< DWORD > s ;
229 initialize_process_set( s, every_process(), copy_PID() ) ;
230 ASSERT_GE( s.size(), 1u );
231 }
232
233 TEST( pid_set, find_our_process_in_set )
234 {
235 std::set< DWORD > s ;
236 initialize_process_set( s, find_in_set, copy_PID() ) ;
237 size_t size( s.size() ) ;
238 EXPECT_EQ( size, 1u );
239 ASSERT_GE( size, 1u );
240 }
OLDNEW

Powered by Google App Engine
This is Rietveld