| Index: installer/src/installer-lib/test/process_test.cpp |
| =================================================================== |
| --- a/installer/src/installer-lib/test/process_test.cpp |
| +++ b/installer/src/installer-lib/test/process_test.cpp |
| @@ -1,39 +1,153 @@ |
| #include <gtest/gtest.h> |
| +#include "../process.h" |
| +#include <functional> |
| -#include "../process.h" |
| +// Turn off warnings for string copies |
| +#pragma warning( disable : 4996 ) |
| +//------------------------------------------------------- |
| +// Comparison objects |
| +//------------------------------------------------------- |
| + |
| +const wchar_t exact_exe_name[] = L"installer-ca-tests.exe" ; |
| +const wchar_t mixedcase_exe_name[] = L"Installer-CA-Tests.exe" ; |
| +const wchar_t * multiple_exe_names[] = { mixedcase_exe_name, L"non-matching-name" } ; |
| /** |
| - * A promiscuous filter admits everything. |
| + * Compare to our own process name, case-sensitive, no length limit |
| */ |
| -bool trivial_true( PROCESSENTRY32W & ) |
| +struct our_process_by_name |
| + : std::unary_function< PROCESSENTRY32W, bool > |
| { |
| - return true; |
| + bool operator()( const PROCESSENTRY32W & process ) |
| + { |
| + return 0 == wcscmp( process.szExeFile, exact_exe_name ) ; |
| + } ; |
| +}; |
| + |
| +/** |
| + * Compare to our own process name, case-insensitive, no length limit |
| + */ |
| +struct our_process_by_name_CI |
| + : std::unary_function< PROCESSENTRY32W, bool > |
| +{ |
| + bool operator()( const PROCESSENTRY32W & process ) |
| + { |
| + return 0 == wcscmpi( process.szExeFile, mixedcase_exe_name ) ; |
| + } ; |
| +} ; |
| + |
| +/** |
| + * Compare to our own process name, case-insensitive, length-limited |
| + */ |
| +struct our_process_by_name_CI_N |
| + : std::unary_function< PROCESSENTRY32W, bool > |
| +{ |
| + bool operator()( const PROCESSENTRY32W & process ) |
| + { |
| + return 0 == wcsncmpi( process.szExeFile, mixedcase_exe_name, sizeof( mixedcase_exe_name ) / sizeof( wchar_t ) ) ; |
| + } ; |
| +} ; |
| + |
| + |
| +//------------------------------------------------------- |
| +// TESTS, no snapshots |
| +//------------------------------------------------------- |
| +PROCESSENTRY32 process_with_name( const wchar_t * s ) |
| +{ |
| + PROCESSENTRY32W p ; |
| + wcsncpy( p.szExeFile, s, MAX_PATH ) ; |
| + return p ; |
| } |
| -/* |
| - * Converter that copies everything, by reference. |
| +PROCESSENTRY32 process_empty = process_with_name( L"" ) ; |
| +PROCESSENTRY32 process_exact = process_with_name( exact_exe_name ) ; |
| +PROCESSENTRY32 process_mixedcase = process_with_name( mixedcase_exe_name ) ; |
| +PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ; |
| +PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ; |
| + |
| +exe_name_set multiple_name_set( multiple_exe_names, 2 ) ; |
| +process_by_any_exe_name_CI find_in_set( multiple_name_set ) ; |
| + |
| +TEST( exe_name_set, validate_setup ) |
| +{ |
| + ASSERT_EQ( 2u, multiple_name_set.size() ) ; |
| + ASSERT_TRUE( multiple_name_set.find( mixedcase_exe_name ) != multiple_name_set.end() ) ; |
| + ASSERT_TRUE( multiple_name_set.find( exact_exe_name ) != multiple_name_set.end() ) ; |
| + ASSERT_TRUE( multiple_name_set.find( L"" ) == multiple_name_set.end() ) ; |
| + ASSERT_TRUE( multiple_name_set.find( L"not-in-list" ) == multiple_name_set.end() ) ; |
| +} |
| + |
| +TEST( process_by_any_exe_name_CI, empty ) |
| +{ |
| + const wchar_t * elements[ 1 ] = { 0 } ; // cheating a bit |
| + exe_name_set s( elements, 0 ) ; |
| + process_by_any_exe_name_CI x( s ) ; |
| + |
| + ASSERT_FALSE( x( process_empty ) ) ; |
| + ASSERT_FALSE( x( process_exact ) ) ; |
| + ASSERT_FALSE( x( process_mixedcase ) ) ; |
| + ASSERT_FALSE( x( process_explorer ) ) ; |
| + ASSERT_FALSE( x( process_absent ) ) ; |
| +} |
| + |
| +TEST( process_by_any_exe_name_CI, single_element ) |
| +{ |
| + const wchar_t * elements[ 1 ] = { exact_exe_name } ; |
| + exe_name_set s( elements, 1 ) ; |
| + process_by_any_exe_name_CI x( s ) ; |
| + |
| + ASSERT_FALSE( x( process_empty ) ) ; |
| + ASSERT_TRUE( x( process_exact ) ) ; |
| + ASSERT_TRUE( x( process_mixedcase ) ) ; |
| + ASSERT_FALSE( x( process_explorer ) ) ; |
| + ASSERT_FALSE( x( process_absent ) ) ; |
| +} |
| + |
| +TEST( process_by_any_exe_name_CI, two_elements ) |
| +{ |
| + exe_name_set s( multiple_exe_names, 2 ) ; |
| + process_by_any_exe_name_CI x( s ) ; |
| + |
| + ASSERT_FALSE( find_in_set( process_empty ) ) ; |
| + ASSERT_TRUE( find_in_set( process_exact ) ) ; |
| + ASSERT_TRUE( find_in_set( process_mixedcase ) ) ; |
| + ASSERT_FALSE( find_in_set( process_explorer ) ) ; |
| + ASSERT_FALSE( find_in_set( process_absent ) ) ; |
| +} |
| + |
| +//------------------------------------------------------- |
| +// Single-snapshot version of initializers |
| +//------------------------------------------------------- |
| +/** |
| + * Single-snapshot version of initialize_process_list, for testing. |
| */ |
| -PROCESSENTRY32W & copy_all( PROCESSENTRY32W & process ) |
| +template< class T, class Admittance, class Extractor > |
| +void initialize_process_list( std::vector< T > & v, Admittance admit = Admittance(), Extractor extract = Extractor() ) |
| { |
| - return process ; |
| + initialize_process_list( v, Snapshot(), admit, extract ) ; |
| } |
| /** |
| + * Single-snapshot version of initialize_process_set, for testing. |
| + */ |
| +template< class T, class Admittance, class Extractor > |
| +void initialize_process_set( std::set< T > & s, Admittance admit = Admittance(), Extractor extract = Extractor() ) |
| +{ |
| + initialize_process_set( s, Snapshot(), admit, extract ) ; |
| +} |
| + |
| +//------------------------------------------------------- |
| +// TESTS with snapshots |
| +//------------------------------------------------------- |
| +/** |
| * Construction test ensures that we don't throw and that at least one process shows up. |
| */ |
| -TEST( Process_List_Test, construct ) |
| +TEST( Process_List_Test, construct_vector ) |
| { |
| - Process_List< PROCESSENTRY32W > pl( trivial_true, copy_all ); |
| - ASSERT_GE( pl.v.size(), 1u ); |
| -} |
| - |
| -/** |
| - * Filter by a fixed name. |
| - */ |
| -bool our_process_by_name( PROCESSENTRY32W & process ) |
| -{ |
| - return 0 == wcscmp( process.szExeFile, L"installer-ca-tests.exe" ); |
| + std::vector< PROCESSENTRY32W > v ; |
| + initialize_process_list( v, every_process(), copy_all() ) ; |
| + ASSERT_GE( v.size(), 1u ); |
| } |
| /** |
| @@ -41,18 +155,37 @@ |
| */ |
| TEST( Process_List_Test, find_our_process ) |
| { |
| - Process_List< PROCESSENTRY32W > pl( our_process_by_name, copy_all ); |
| - unsigned int size( pl.v.size() ); |
| - EXPECT_EQ( size, 1u ); // Please, don't run multiple test executables simultaneously |
| - ASSERT_GE( size, 1u ); |
| + std::vector< PROCESSENTRY32W > v ; |
| + initialize_process_list( v, our_process_by_name(), copy_all() ) ; |
| + unsigned int size( v.size() ); |
| + EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simultaneously |
| + ASSERT_GE( 1u, size ); |
| } |
| -/* |
| - * Converter that copies only the PID, by value. |
| +/** |
| + * The only process we are really guaranteed to have is this test process itself. |
| + * This test uses a filter function class with a special, fixed name comparison. |
| */ |
| -DWORD copy_PID( PROCESSENTRY32W & process ) |
| +TEST( Process_List_Test, find_our_process_CI_N_special ) |
| { |
| - return process.th32ProcessID ; |
| + std::vector< PROCESSENTRY32W > v ; |
| + initialize_process_list( v, our_process_by_name_CI_N(), copy_all() ) ; |
| + unsigned int size( v.size() ); |
| + EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simultaneously |
| + ASSERT_GE( 1u, size ); |
| +} |
| + |
| +/** |
| + * The only process we are really guaranteed to have is this test process itself. |
| + * This test uses the generic filter function. |
| + */ |
| +TEST( Process_List_Test, find_our_process_CI_N_generic ) |
| +{ |
| + std::vector< PROCESSENTRY32W > v ; |
| + initialize_process_list( v, process_by_name_CI( mixedcase_exe_name ), copy_all() ) ; |
| + unsigned int size( v.size() ); |
| + EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simultaneously |
| + ASSERT_GE( 1u, size ); |
| } |
| /** |
| @@ -60,9 +193,48 @@ |
| */ |
| TEST( Process_List_Test, find_our_PID ) |
| { |
| - Process_List< DWORD > pl( our_process_by_name, copy_PID ); |
| - unsigned int size( pl.v.size() ); |
| + std::vector< DWORD > v ; |
| + initialize_process_list( v, our_process_by_name(), copy_PID() ) ; |
| + unsigned int size( v.size() ); |
| EXPECT_EQ( size, 1u ); // Please, don't run multiple test executables simultaneously |
| ASSERT_GE( size, 1u ); |
| } |
| +/** |
| + * Locate the PID of our process using the |
| + */ |
| +TEST( Process_List_Test, find_our_process_in_set ) |
| +{ |
| + std::vector< DWORD > v ; |
| + initialize_process_list( v, find_in_set, copy_PID() ) ; |
| + unsigned int size( v.size() ); |
| + EXPECT_EQ( size, 1u ); // Please, don't run multiple test executables simultaneously |
| + ASSERT_GE( size, 1u ); |
| +} |
| + |
| +//------------------------------------------------------- |
| +// TESTS for process ID sets |
| +//------------------------------------------------------- |
| +/* |
| + * Can't use copy_all without a definition for "less< PROCESSENTRY32W >". |
| + * Thus all tests only use copy_PID |
| + */ |
| + |
| +/** |
| + * Construction test ensures that we don't throw and that at least one process shows up. |
| + */ |
| +TEST( pid_set, construct_set ) |
| +{ |
| + std::set< DWORD > s ; |
| + initialize_process_set( s, every_process(), copy_PID() ) ; |
| + ASSERT_GE( s.size(), 1u ); |
| +} |
| + |
| +TEST( pid_set, find_our_process_in_set ) |
| +{ |
| + std::set< DWORD > s ; |
| + initialize_process_set( s, find_in_set, copy_PID() ) ; |
| + size_t size( s.size() ) ; |
| + EXPECT_EQ( size, 1u ); |
| + ASSERT_GE( size, 1u ); |
| +} |