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

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

Issue 4790070691233792: Case-insensitive string class (Closed)
Patch Set: Created March 30, 2014, 7:49 p.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" 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 ) ;
14 const wstring_ci exact_exe_string_ci( exact_exe_name ) ;
15
13 const wchar_t mixedcase_exe_name[] = L"Installer-CA-Tests.exe" ; 16 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 " } ; 17 const wstring_ci mixedcase_exe_string_ci( mixedcase_exe_name ) ;
18
19 const wchar_t unknown_name[] = L"non-matching-name" ;
20 const wchar_t * multiple_exe_names[] = { mixedcase_exe_name, unknown_name } ;
15 21
16 /** 22 /**
17 * Compare to our own process name, case-sensitive, no length limit 23 * Compare to our own process name, case-sensitive, no length limit
18 */ 24 */
19 struct our_process_by_name 25 struct our_process_by_name
20 : std::unary_function< PROCESSENTRY32W, bool > 26 : std::unary_function< PROCESSENTRY32W, bool >
21 { 27 {
22 bool operator()( const PROCESSENTRY32W & process ) 28 bool operator()( const PROCESSENTRY32W & process )
23 { 29 {
24 return 0 == wcscmp( process.szExeFile, exact_exe_name ) ; 30 return std::wstring( process.szExeFile ) == exact_exe_string ;
25 } ; 31 } ;
26 }; 32 };
27 33
28 /** 34 /**
29 * Compare to our own process name, case-insensitive, no length limit 35 * Compare to our own process name, case-insensitive, no length limit
30 */ 36 */
31 struct our_process_by_name_CI 37 struct our_process_by_name_CI
32 : std::unary_function< PROCESSENTRY32W, bool > 38 : std::unary_function< PROCESSENTRY32W, bool >
33 { 39 {
34 bool operator()( const PROCESSENTRY32W & process ) 40 bool operator()( const PROCESSENTRY32W & process )
35 { 41 {
36 return 0 == wcscmpi( process.szExeFile, mixedcase_exe_name ) ; 42 return wstring_ci( process.szExeFile ) == mixedcase_exe_string_ci ;
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 } ; 43 } ;
50 } ; 44 } ;
51 45
52 46
47 struct our_process_by_name_subclassed
48 : public process_by_any_exe_name_CI
49 {
50 our_process_by_name_subclassed( file_name_set( multiple_exe_names ) ) ;
51 } ;
52
53 /**
54 * Filter by process name. Comparison is case-insensitive.
55 */
56 class process_by_name_CI
57 : public std::unary_function< PROCESSENTRY32W, bool >
58 {
59 const wstring_ci _name ;
60 public:
61 bool operator()( const PROCESSENTRY32W & process )
62 {
63 return _name == wstring_ci( process.szExeFile ) ;
64 }
65
66 process_by_name_CI( const wchar_t * name )
67 : _name( name )
68 {}
69 } ;
70
53 //------------------------------------------------------- 71 //-------------------------------------------------------
54 // TESTS, no snapshots 72 // TESTS, no snapshots
55 //------------------------------------------------------- 73 //-------------------------------------------------------
56 PROCESSENTRY32 process_with_name( const wchar_t * s ) 74 PROCESSENTRY32 process_with_name( const wchar_t * s )
57 { 75 {
58 PROCESSENTRY32W p ; 76 PROCESSENTRY32W p ;
59 wcsncpy( p.szExeFile, s, MAX_PATH ) ; 77 wcsncpy( p.szExeFile, s, MAX_PATH ) ;
60 return p ; 78 return p ;
61 } 79 }
62 80
63 PROCESSENTRY32 process_empty = process_with_name( L"" ) ; 81 PROCESSENTRY32 process_empty = process_with_name( L"" ) ;
64 PROCESSENTRY32 process_exact = process_with_name( exact_exe_name ) ; 82 PROCESSENTRY32 process_exact = process_with_name( exact_exe_name ) ;
65 PROCESSENTRY32 process_mixedcase = process_with_name( mixedcase_exe_name ) ; 83 PROCESSENTRY32 process_mixedcase = process_with_name( mixedcase_exe_name ) ;
66 PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ; 84 PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ;
67 PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ; 85 PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ;
68 86
69 exe_name_set multiple_name_set( multiple_exe_names, 2 ) ; 87 file_name_set multiple_name_set( multiple_exe_names ) ;
70 process_by_any_exe_name_CI find_in_set( multiple_name_set ) ; 88 process_by_any_exe_name_CI find_in_set( multiple_name_set ) ;
71 89
72 TEST( exe_name_set, validate_setup ) 90 TEST( exe_name_set, validate_setup )
73 { 91 {
74 ASSERT_EQ( 2u, multiple_name_set.size() ) ; 92 ASSERT_EQ( 2u, multiple_name_set.size() ) ;
75 ASSERT_TRUE( multiple_name_set.find( mixedcase_exe_name ) != multiple_name_set .end() ) ; 93 ASSERT_TRUE( multiple_name_set.find( exact_exe_string_ci ) != multiple_name_se t.end() ) ;
76 ASSERT_TRUE( multiple_name_set.find( exact_exe_name ) != multiple_name_set.end () ) ; 94 ASSERT_TRUE( multiple_name_set.find( mixedcase_exe_string_ci ) != multiple_nam e_set.end() ) ;
77 ASSERT_TRUE( multiple_name_set.find( L"" ) == multiple_name_set.end() ) ; 95 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 () ) ; 96 ASSERT_TRUE( multiple_name_set.find( L"not-in-list" ) == multiple_name_set.end () ) ;
79 } 97 }
80 98
81 TEST( process_by_any_exe_name_CI, empty ) 99 TEST( process_by_any_exe_name_CI, empty )
82 { 100 {
83 const wchar_t * elements[ 1 ] = { 0 } ; // cheating a bit 101 file_name_set s ;
84 exe_name_set s( elements, 0 ) ;
85 process_by_any_exe_name_CI x( s ) ; 102 process_by_any_exe_name_CI x( s ) ;
86 103
87 ASSERT_FALSE( x( process_empty ) ) ; 104 ASSERT_FALSE( x( process_empty ) ) ;
88 ASSERT_FALSE( x( process_exact ) ) ; 105 ASSERT_FALSE( x( process_exact ) ) ;
89 ASSERT_FALSE( x( process_mixedcase ) ) ; 106 ASSERT_FALSE( x( process_mixedcase ) ) ;
90 ASSERT_FALSE( x( process_explorer ) ) ; 107 ASSERT_FALSE( x( process_explorer ) ) ;
91 ASSERT_FALSE( x( process_absent ) ) ; 108 ASSERT_FALSE( x( process_absent ) ) ;
92 } 109 }
93 110
94 TEST( process_by_any_exe_name_CI, single_element ) 111 TEST( process_by_any_exe_name_CI, single_element_known )
95 { 112 {
96 const wchar_t * elements[ 1 ] = { exact_exe_name } ; 113 const wchar_t * elements[ 1 ] = { exact_exe_name } ;
97 exe_name_set s( elements, 1 ) ; 114 file_name_set s( elements ) ;
98 process_by_any_exe_name_CI x( s ) ; 115 process_by_any_exe_name_CI x( s ) ;
99 116
100 ASSERT_FALSE( x( process_empty ) ) ; 117 ASSERT_FALSE( x( process_empty ) ) ;
101 ASSERT_TRUE( x( process_exact ) ) ; 118 ASSERT_TRUE( x( process_exact ) ) ;
102 ASSERT_TRUE( x( process_mixedcase ) ) ; 119 ASSERT_TRUE( x( process_mixedcase ) ) ;
103 ASSERT_FALSE( x( process_explorer ) ) ; 120 ASSERT_FALSE( x( process_explorer ) ) ;
104 ASSERT_FALSE( x( process_absent ) ) ; 121 ASSERT_FALSE( x( process_absent ) ) ;
105 } 122 }
106 123
124 TEST( process_by_any_exe_name_CI, single_element_unknown )
125 {
126 const wchar_t * elements[ 1 ] = { unknown_name } ;
127 file_name_set s( elements ) ;
128 process_by_any_exe_name_CI x( s ) ;
129
130 ASSERT_FALSE( x( process_empty ) ) ;
131 ASSERT_FALSE( x( process_exact ) ) ;
132 ASSERT_FALSE( x( process_mixedcase ) ) ;
133 ASSERT_FALSE( x( process_explorer ) ) ;
134 ASSERT_FALSE( x( process_absent ) ) ;
135 }
136
107 TEST( process_by_any_exe_name_CI, two_elements ) 137 TEST( process_by_any_exe_name_CI, two_elements )
108 { 138 {
109 exe_name_set s( multiple_exe_names, 2 ) ; 139 file_name_set s( multiple_exe_names ) ;
110 process_by_any_exe_name_CI x( s ) ; 140 process_by_any_exe_name_CI x( s ) ;
111 141
112 ASSERT_FALSE( find_in_set( process_empty ) ) ; 142 ASSERT_FALSE( find_in_set( process_empty ) ) ;
113 ASSERT_TRUE( find_in_set( process_exact ) ) ; 143 ASSERT_TRUE( find_in_set( process_exact ) ) ;
114 ASSERT_TRUE( find_in_set( process_mixedcase ) ) ; 144 ASSERT_TRUE( find_in_set( process_mixedcase ) ) ;
115 ASSERT_FALSE( find_in_set( process_explorer ) ) ; 145 ASSERT_FALSE( find_in_set( process_explorer ) ) ;
116 ASSERT_FALSE( find_in_set( process_absent ) ) ; 146 ASSERT_FALSE( find_in_set( process_absent ) ) ;
117 } 147 }
118 148
119 //------------------------------------------------------- 149 //-------------------------------------------------------
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 { 187 {
158 std::vector< PROCESSENTRY32W > v ; 188 std::vector< PROCESSENTRY32W > v ;
159 initialize_process_list( v, our_process_by_name(), copy_all() ) ; 189 initialize_process_list( v, our_process_by_name(), copy_all() ) ;
160 unsigned int size( v.size() ); 190 unsigned int size( v.size() );
161 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul taneously 191 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul taneously
162 ASSERT_GE( 1u, size ); 192 ASSERT_GE( 1u, size );
163 } 193 }
164 194
165 /** 195 /**
166 * The only process we are really guaranteed to have is this test process itself . 196 * 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. 197 * This test uses the generic filter function.
168 */ 198 */
169 TEST( Process_List_Test, find_our_process_CI_N_special ) 199 TEST( Process_List_Test, find_our_process_CI_generic )
170 { 200 {
171 std::vector< PROCESSENTRY32W > v ; 201 std::vector< PROCESSENTRY32W > v ;
172 initialize_process_list( v, our_process_by_name_CI_N(), copy_all() ) ; 202 initialize_process_list( v, process_by_name_CI( mixedcase_exe_name ), copy_all () ) ;
173 unsigned int size( v.size() ); 203 unsigned int size( v.size() );
174 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul taneously 204 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul taneously
175 ASSERT_GE( 1u, size ); 205 ASSERT_GE( 1u, size );
176 } 206 }
177 207
178 /** 208 /**
179 * The only process we are really guaranteed to have is this test process itself . 209 * The only process we are really guaranteed to have is this test process itself .
180 * This test uses the generic filter function. 210 * This test uses same one used in Process_Closer
181 */ 211 */
182 TEST( Process_List_Test, find_our_process_CI_N_generic ) 212 TEST( Process_List_Test, find_our_process_CI_as_used )
183 { 213 {
184 std::vector< PROCESSENTRY32W > v ; 214 std::vector< PROCESSENTRY32W > v ;
185 initialize_process_list( v, process_by_name_CI( mixedcase_exe_name ), copy_all () ) ; 215 initialize_process_list( v, process_by_any_exe_name_CI( file_name_set( multipl e_exe_names ) ), copy_all() ) ;
186 unsigned int size( v.size() ); 216 unsigned int size( v.size() );
187 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul taneously 217 EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simul taneously
188 ASSERT_GE( 1u, size ); 218 ASSERT_GE( 1u, size );
189 } 219 }
190 220
191 /** 221 /**
192 * Locate the PID of our process. 222 * Locate the PID of our process.
193 */ 223 */
194 TEST( Process_List_Test, find_our_PID ) 224 TEST( Process_List_Test, find_our_PID )
195 { 225 {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 } 261 }
232 262
233 TEST( pid_set, find_our_process_in_set ) 263 TEST( pid_set, find_our_process_in_set )
234 { 264 {
235 std::set< DWORD > s ; 265 std::set< DWORD > s ;
236 initialize_process_set( s, find_in_set, copy_PID() ) ; 266 initialize_process_set( s, find_in_set, copy_PID() ) ;
237 size_t size( s.size() ) ; 267 size_t size( s.size() ) ;
238 EXPECT_EQ( size, 1u ); 268 EXPECT_EQ( size, 1u );
239 ASSERT_GE( size, 1u ); 269 ASSERT_GE( size, 1u );
240 } 270 }
OLDNEW

Powered by Google App Engine
This is Rietveld