184af0c9bSNicolas Bonnefon #include "gmock/gmock.h" 284af0c9bSNicolas Bonnefon 387e05652SNicolas Bonnefon #include "config.h" 487e05652SNicolas Bonnefon 5b827fa8eSNicolas Bonnefon #include <cstdio> 684af0c9bSNicolas Bonnefon #include <fcntl.h> 784af0c9bSNicolas Bonnefon 884af0c9bSNicolas Bonnefon #include <memory> 984af0c9bSNicolas Bonnefon #include <condition_variable> 1084af0c9bSNicolas Bonnefon #include <thread> 1184af0c9bSNicolas Bonnefon #include <mutex> 1284af0c9bSNicolas Bonnefon #include <chrono> 1384af0c9bSNicolas Bonnefon 1484b2179eSNicolas Bonnefon #include "log.h" 1584b2179eSNicolas Bonnefon 16f09fa651SNicolas Bonnefon #include "watchtower.h" 17f09fa651SNicolas Bonnefon 1887e05652SNicolas Bonnefon #ifdef _WIN32 19f09fa651SNicolas Bonnefon # include "winwatchtowerdriver.h" 20f09fa651SNicolas Bonnefon using PlatformWatchTower = WatchTower<WinWatchTowerDriver>; 2187e05652SNicolas Bonnefon #else 22b278d183SNicolas Bonnefon # include "inotifywatchtowerdriver.h" 23f09fa651SNicolas Bonnefon using PlatformWatchTower = WatchTower<INotifyWatchTowerDriver>; 2487e05652SNicolas Bonnefon #endif 2584af0c9bSNicolas Bonnefon 2684af0c9bSNicolas Bonnefon using namespace std; 2784af0c9bSNicolas Bonnefon using namespace testing; 2884af0c9bSNicolas Bonnefon 2984af0c9bSNicolas Bonnefon class WatchTowerBehaviour: public testing::Test { 3084af0c9bSNicolas Bonnefon public: 3196bde7d5SNicolas Bonnefon shared_ptr<PlatformWatchTower> watch_tower = make_shared<PlatformWatchTower>(); 3284af0c9bSNicolas Bonnefon 33a0b17fd1SNicolas Bonnefon const char* createTempName() 34a0b17fd1SNicolas Bonnefon { 35a0b17fd1SNicolas Bonnefon const char* name; 36a0b17fd1SNicolas Bonnefon #if _WIN32 37a0b17fd1SNicolas Bonnefon name = _tempnam( "c:\\temp", "glogg_test" ); 38a0b17fd1SNicolas Bonnefon #else 39a0b17fd1SNicolas Bonnefon name = tmpnam( nullptr ); 40a0b17fd1SNicolas Bonnefon #endif 41a0b17fd1SNicolas Bonnefon return name; 42a0b17fd1SNicolas Bonnefon } 43a0b17fd1SNicolas Bonnefon 44b827fa8eSNicolas Bonnefon string createTempEmptyFile( string file_name = "" ) { 45b827fa8eSNicolas Bonnefon const char* name; 46b827fa8eSNicolas Bonnefon 47b827fa8eSNicolas Bonnefon if ( ! file_name.empty() ) { 48b827fa8eSNicolas Bonnefon name = file_name.c_str(); 49b827fa8eSNicolas Bonnefon } 50b827fa8eSNicolas Bonnefon else { 5184af0c9bSNicolas Bonnefon // I know tmpnam is bad but I need control over the file 5284af0c9bSNicolas Bonnefon // and it is the only one which exits on Windows. 53a0b17fd1SNicolas Bonnefon name = createTempName(); 54b827fa8eSNicolas Bonnefon } 55b827fa8eSNicolas Bonnefon int fd = creat( name, S_IRUSR | S_IWUSR ); 5684af0c9bSNicolas Bonnefon close( fd ); 5784af0c9bSNicolas Bonnefon 58b827fa8eSNicolas Bonnefon return string( name ); 5984af0c9bSNicolas Bonnefon } 6084af0c9bSNicolas Bonnefon 6184af0c9bSNicolas Bonnefon string getNonExistingFileName() { 6284b2179eSNicolas Bonnefon #if _WIN32 6384b2179eSNicolas Bonnefon return string( _tempnam( "c:\\temp", "inexistant" ) ); 6484b2179eSNicolas Bonnefon #else 6584af0c9bSNicolas Bonnefon return string( tmpnam( nullptr ) ); 6684b2179eSNicolas Bonnefon #endif 6784b2179eSNicolas Bonnefon } 6884b2179eSNicolas Bonnefon 6984b2179eSNicolas Bonnefon WatchTowerBehaviour() { 7084b2179eSNicolas Bonnefon // Default to quiet, but increase to debug 715d489994SNicolas Bonnefon FILELog::setReportingLevel( logERROR ); 7284af0c9bSNicolas Bonnefon } 7384af0c9bSNicolas Bonnefon }; 7484af0c9bSNicolas Bonnefon 7584af0c9bSNicolas Bonnefon TEST_F( WatchTowerBehaviour, AcceptsAnExistingFileToWatch ) { 7684af0c9bSNicolas Bonnefon auto file_name = createTempEmptyFile(); 7787e05652SNicolas Bonnefon auto registration = watch_tower->addFile( file_name, [] (void) { } ); 7884af0c9bSNicolas Bonnefon } 7984af0c9bSNicolas Bonnefon 8084af0c9bSNicolas Bonnefon TEST_F( WatchTowerBehaviour, AcceptsANonExistingFileToWatch ) { 8187e05652SNicolas Bonnefon auto registration = watch_tower->addFile( getNonExistingFileName(), [] (void) { } ); 8284af0c9bSNicolas Bonnefon } 8384af0c9bSNicolas Bonnefon 84b827fa8eSNicolas Bonnefon /*****/ 85b827fa8eSNicolas Bonnefon 8684af0c9bSNicolas Bonnefon class WatchTowerSingleFile: public WatchTowerBehaviour { 8784af0c9bSNicolas Bonnefon public: 88b827fa8eSNicolas Bonnefon static const int TIMEOUT; 8984af0c9bSNicolas Bonnefon 90*3104b268SNicolas Bonnefon mutex mutex_; 91*3104b268SNicolas Bonnefon condition_variable cv_; 92*3104b268SNicolas Bonnefon 9384af0c9bSNicolas Bonnefon string file_name; 9496bde7d5SNicolas Bonnefon Registration registration; 9584af0c9bSNicolas Bonnefon 968f031b5aSNicolas Bonnefon int notification_received = 0; 9784af0c9bSNicolas Bonnefon 9896bde7d5SNicolas Bonnefon Registration registerFile( const string& filename ) { 99b827fa8eSNicolas Bonnefon weak_ptr<void> weakHeartbeat( heartbeat_ ); 100b827fa8eSNicolas Bonnefon 10184b2179eSNicolas Bonnefon auto reg = watch_tower->addFile( filename, [this, weakHeartbeat] (void) { 102b827fa8eSNicolas Bonnefon // Ensure the fixture object is still alive using the heartbeat 103b827fa8eSNicolas Bonnefon if ( auto keep = weakHeartbeat.lock() ) { 10484af0c9bSNicolas Bonnefon unique_lock<mutex> lock(mutex_); 1058f031b5aSNicolas Bonnefon ++notification_received; 106b827fa8eSNicolas Bonnefon cv_.notify_one(); 107b827fa8eSNicolas Bonnefon } } ); 108b827fa8eSNicolas Bonnefon 109b827fa8eSNicolas Bonnefon return reg; 110b827fa8eSNicolas Bonnefon } 111b827fa8eSNicolas Bonnefon 11284b2179eSNicolas Bonnefon WatchTowerSingleFile() 11384b2179eSNicolas Bonnefon : heartbeat_( shared_ptr<void>( (void*) 0xDEADC0DE, [] (void*) {} ) ) 11484b2179eSNicolas Bonnefon { 115b827fa8eSNicolas Bonnefon file_name = createTempEmptyFile(); 116b827fa8eSNicolas Bonnefon registration = registerFile( file_name ); 11784af0c9bSNicolas Bonnefon } 11884af0c9bSNicolas Bonnefon 11984b2179eSNicolas Bonnefon ~WatchTowerSingleFile() { 12084b2179eSNicolas Bonnefon remove( file_name.c_str() ); 12184b2179eSNicolas Bonnefon } 12284b2179eSNicolas Bonnefon 1238f031b5aSNicolas Bonnefon bool waitNotificationReceived( int number = 1 ) { 12484af0c9bSNicolas Bonnefon unique_lock<mutex> lock(mutex_); 1258f031b5aSNicolas Bonnefon bool result = ( cv_.wait_for( lock, std::chrono::milliseconds(TIMEOUT), 1268f031b5aSNicolas Bonnefon [this, number] { return notification_received >= number; } ) ); 127b827fa8eSNicolas Bonnefon 128b827fa8eSNicolas Bonnefon // Reinit the notification 1298f031b5aSNicolas Bonnefon notification_received = 0; 130b827fa8eSNicolas Bonnefon 1318f031b5aSNicolas Bonnefon return result; 13284af0c9bSNicolas Bonnefon } 13384af0c9bSNicolas Bonnefon 13484af0c9bSNicolas Bonnefon void appendDataToFile( const string& file_name ) { 13584af0c9bSNicolas Bonnefon static const char* string = "Test line\n"; 13684af0c9bSNicolas Bonnefon int fd = open( file_name.c_str(), O_WRONLY | O_APPEND ); 13784af0c9bSNicolas Bonnefon write( fd, (void*) string, strlen( string ) ); 13884af0c9bSNicolas Bonnefon close( fd ); 13984af0c9bSNicolas Bonnefon } 140b827fa8eSNicolas Bonnefon 141b827fa8eSNicolas Bonnefon private: 142b827fa8eSNicolas Bonnefon // Heartbeat ensures the object is still alive 143b827fa8eSNicolas Bonnefon shared_ptr<void> heartbeat_; 14484af0c9bSNicolas Bonnefon }; 14584af0c9bSNicolas Bonnefon 146a0936e1eSNicolas Bonnefon #ifdef _WIN32 147*3104b268SNicolas Bonnefon const int WatchTowerSingleFile::TIMEOUT = 2000; 148a0936e1eSNicolas Bonnefon #else 149b827fa8eSNicolas Bonnefon const int WatchTowerSingleFile::TIMEOUT = 20; 150a0936e1eSNicolas Bonnefon #endif 151a0936e1eSNicolas Bonnefon 15284b2179eSNicolas Bonnefon TEST_F( WatchTowerSingleFile, SignalsWhenAWatchedFileIsAppended ) { 15384af0c9bSNicolas Bonnefon appendDataToFile( file_name ); 15484af0c9bSNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 15584af0c9bSNicolas Bonnefon } 15684af0c9bSNicolas Bonnefon 157b827fa8eSNicolas Bonnefon TEST_F( WatchTowerSingleFile, SignalsWhenAWatchedFileIsRemoved) { 15884af0c9bSNicolas Bonnefon remove( file_name.c_str() ); 15984af0c9bSNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 16084af0c9bSNicolas Bonnefon } 161b827fa8eSNicolas Bonnefon 162b827fa8eSNicolas Bonnefon TEST_F( WatchTowerSingleFile, SignalsWhenADeletedFileReappears ) { 163b827fa8eSNicolas Bonnefon remove( file_name.c_str() ); 164b827fa8eSNicolas Bonnefon waitNotificationReceived(); 165b827fa8eSNicolas Bonnefon createTempEmptyFile( file_name ); 166b827fa8eSNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 167b827fa8eSNicolas Bonnefon } 168b827fa8eSNicolas Bonnefon 169b827fa8eSNicolas Bonnefon TEST_F( WatchTowerSingleFile, StopSignalingWhenWatchDeleted ) { 170b827fa8eSNicolas Bonnefon auto second_file_name = createTempEmptyFile(); 171*3104b268SNicolas Bonnefon std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) ); 172*3104b268SNicolas Bonnefon // Ensure file creation has been 'digested' 173b827fa8eSNicolas Bonnefon { 174b827fa8eSNicolas Bonnefon auto second_registration = registerFile( second_file_name ); 175b827fa8eSNicolas Bonnefon appendDataToFile( second_file_name ); 176b827fa8eSNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 177b827fa8eSNicolas Bonnefon } 178b827fa8eSNicolas Bonnefon 179b827fa8eSNicolas Bonnefon // The registration will be removed here. 180b827fa8eSNicolas Bonnefon appendDataToFile( second_file_name ); 181b827fa8eSNicolas Bonnefon ASSERT_FALSE( waitNotificationReceived() ); 182b827fa8eSNicolas Bonnefon 183b827fa8eSNicolas Bonnefon remove( second_file_name.c_str() ); 184b827fa8eSNicolas Bonnefon } 185b827fa8eSNicolas Bonnefon 186f09fa651SNicolas Bonnefon TEST_F( WatchTowerSingleFile, SignalsWhenSameFileIsFollowedMultipleTimes ) { 187f09fa651SNicolas Bonnefon auto second_file_name = createTempEmptyFile(); 188f09fa651SNicolas Bonnefon 189*3104b268SNicolas Bonnefon for ( int i = 0; i < 100; i++ ) 190f09fa651SNicolas Bonnefon { 191f09fa651SNicolas Bonnefon auto second_registration = registerFile( second_file_name ); 192f09fa651SNicolas Bonnefon appendDataToFile( second_file_name ); 193f09fa651SNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 194f09fa651SNicolas Bonnefon } 195f09fa651SNicolas Bonnefon 196f09fa651SNicolas Bonnefon // The registration will be removed here. 197f09fa651SNicolas Bonnefon appendDataToFile( second_file_name ); 198f09fa651SNicolas Bonnefon ASSERT_FALSE( waitNotificationReceived() ); 199f09fa651SNicolas Bonnefon 200f09fa651SNicolas Bonnefon remove( second_file_name.c_str() ); 201f09fa651SNicolas Bonnefon } 202f09fa651SNicolas Bonnefon 203a0b17fd1SNicolas Bonnefon TEST_F( WatchTowerSingleFile, TwoWatchesOnSameFileYieldsTwoNotifications ) { 2048f031b5aSNicolas Bonnefon auto second_registration = registerFile( file_name ); 2058f031b5aSNicolas Bonnefon appendDataToFile( file_name ); 2068f031b5aSNicolas Bonnefon 2078f031b5aSNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived( 2 ) ); 2088f031b5aSNicolas Bonnefon } 2098f031b5aSNicolas Bonnefon 210a0b17fd1SNicolas Bonnefon TEST_F( WatchTowerSingleFile, RemovingOneWatchOfTwoStillYieldsOneNotification ) { 2118f031b5aSNicolas Bonnefon { 2128f031b5aSNicolas Bonnefon auto second_registration = registerFile( file_name ); 2138f031b5aSNicolas Bonnefon } 2148f031b5aSNicolas Bonnefon 2158f031b5aSNicolas Bonnefon appendDataToFile( file_name ); 2168f031b5aSNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived( 1 ) ); 2178f031b5aSNicolas Bonnefon } 2188f031b5aSNicolas Bonnefon 219a0b17fd1SNicolas Bonnefon TEST_F( WatchTowerSingleFile, RenamingTheFileYieldsANotification ) { 220a0b17fd1SNicolas Bonnefon auto new_file_name = createTempName(); 2218f031b5aSNicolas Bonnefon 222a0b17fd1SNicolas Bonnefon rename( file_name.c_str(), new_file_name ); 2238f031b5aSNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 2248f031b5aSNicolas Bonnefon 225a0b17fd1SNicolas Bonnefon rename( new_file_name, file_name.c_str() ); 2268f031b5aSNicolas Bonnefon } 2278f031b5aSNicolas Bonnefon 228a0b17fd1SNicolas Bonnefon TEST_F( WatchTowerSingleFile, RenamingAFileToTheWatchedNameYieldsANotification ) { 2298f031b5aSNicolas Bonnefon remove( file_name.c_str() ); 2308f031b5aSNicolas Bonnefon waitNotificationReceived(); 2318f031b5aSNicolas Bonnefon 232a0b17fd1SNicolas Bonnefon std::string new_file_name = createTempEmptyFile(); 2338f031b5aSNicolas Bonnefon appendDataToFile( new_file_name ); 2348f031b5aSNicolas Bonnefon 2358f031b5aSNicolas Bonnefon rename( new_file_name.c_str(), file_name.c_str() ); 2368f031b5aSNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 2378f031b5aSNicolas Bonnefon } 2388f031b5aSNicolas Bonnefon 2398f031b5aSNicolas Bonnefon /*****/ 2408f031b5aSNicolas Bonnefon 24187e05652SNicolas Bonnefon #ifdef HAVE_SYMLINK 2423af68c64SNicolas Bonnefon class WatchTowerSymlink: public WatchTowerSingleFile { 2433af68c64SNicolas Bonnefon public: 2443af68c64SNicolas Bonnefon string symlink_name; 2453af68c64SNicolas Bonnefon 2463af68c64SNicolas Bonnefon void SetUp() override { 2473af68c64SNicolas Bonnefon file_name = createTempEmptyFile(); 2483af68c64SNicolas Bonnefon symlink_name = createTempEmptyFile(); 2493af68c64SNicolas Bonnefon remove( symlink_name.c_str() ); 2503af68c64SNicolas Bonnefon symlink( file_name.c_str(), symlink_name.c_str() ); 2513af68c64SNicolas Bonnefon 2523af68c64SNicolas Bonnefon registration = registerFile( symlink_name ); 2533af68c64SNicolas Bonnefon } 2543af68c64SNicolas Bonnefon 2553af68c64SNicolas Bonnefon void TearDown() override { 2563af68c64SNicolas Bonnefon remove( symlink_name.c_str() ); 2573af68c64SNicolas Bonnefon remove( file_name.c_str() ); 2583af68c64SNicolas Bonnefon } 2593af68c64SNicolas Bonnefon }; 2603af68c64SNicolas Bonnefon 2613af68c64SNicolas Bonnefon TEST_F( WatchTowerSymlink, AppendingToTheSymlinkYieldsANotification ) { 2623af68c64SNicolas Bonnefon appendDataToFile( symlink_name ); 2633af68c64SNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 2643af68c64SNicolas Bonnefon } 2653af68c64SNicolas Bonnefon 2663af68c64SNicolas Bonnefon TEST_F( WatchTowerSymlink, AppendingToTheTargetYieldsANotification ) { 2673af68c64SNicolas Bonnefon appendDataToFile( file_name ); 2683af68c64SNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 2693af68c64SNicolas Bonnefon } 2703af68c64SNicolas Bonnefon 2713af68c64SNicolas Bonnefon TEST_F( WatchTowerSymlink, RemovingTheSymlinkYieldsANotification ) { 2723af68c64SNicolas Bonnefon remove( symlink_name.c_str() ); 2733af68c64SNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 2743af68c64SNicolas Bonnefon } 2753af68c64SNicolas Bonnefon 2763af68c64SNicolas Bonnefon TEST_F( WatchTowerSymlink, RemovingTheTargetYieldsANotification ) { 2773af68c64SNicolas Bonnefon remove( file_name.c_str() ); 2783af68c64SNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 2793af68c64SNicolas Bonnefon } 2803af68c64SNicolas Bonnefon 2813af68c64SNicolas Bonnefon TEST_F( WatchTowerSymlink, ReappearingSymlinkYieldsANotification ) { 2823af68c64SNicolas Bonnefon auto new_target = createTempEmptyFile(); 2833af68c64SNicolas Bonnefon remove( symlink_name.c_str() ); 2843af68c64SNicolas Bonnefon waitNotificationReceived(); 2853af68c64SNicolas Bonnefon 2863af68c64SNicolas Bonnefon symlink( new_target.c_str(), symlink_name.c_str() ); 2873af68c64SNicolas Bonnefon ASSERT_TRUE( waitNotificationReceived() ); 2883af68c64SNicolas Bonnefon 2893af68c64SNicolas Bonnefon remove( new_target.c_str() ); 2903af68c64SNicolas Bonnefon } 29187e05652SNicolas Bonnefon #endif //HAVE_SYMLINK 2923af68c64SNicolas Bonnefon 2933af68c64SNicolas Bonnefon /*****/ 2943af68c64SNicolas Bonnefon 295b827fa8eSNicolas Bonnefon TEST( WatchTowerLifetime, RegistrationCanBeDeletedWhenWeAreDead ) { 29696bde7d5SNicolas Bonnefon auto mortal_watch_tower = new PlatformWatchTower(); 297b827fa8eSNicolas Bonnefon auto reg = mortal_watch_tower->addFile( "/tmp/test_file", [] (void) { } ); 298b827fa8eSNicolas Bonnefon 299b827fa8eSNicolas Bonnefon delete mortal_watch_tower; 3008f031b5aSNicolas Bonnefon // reg will be destroyed after the watch_tower 301b827fa8eSNicolas Bonnefon } 302af7adfddSNicolas Bonnefon 303af7adfddSNicolas Bonnefon /*****/ 304af7adfddSNicolas Bonnefon 305*3104b268SNicolas Bonnefon class WatchTowerDirectories: public WatchTowerSingleFile { 306*3104b268SNicolas Bonnefon public: 307*3104b268SNicolas Bonnefon string second_dir_name; 308*3104b268SNicolas Bonnefon string second_file_name; 309*3104b268SNicolas Bonnefon string third_file_name; 310*3104b268SNicolas Bonnefon 311*3104b268SNicolas Bonnefon Registration registration_two; 312*3104b268SNicolas Bonnefon Registration registration_three; 313*3104b268SNicolas Bonnefon 314*3104b268SNicolas Bonnefon WatchTowerDirectories() { 315*3104b268SNicolas Bonnefon second_dir_name = createTempDir(); 316*3104b268SNicolas Bonnefon second_file_name = createTempEmptyFileInDir( second_dir_name ); 317*3104b268SNicolas Bonnefon third_file_name = createTempEmptyFileInDir( second_dir_name ); 318*3104b268SNicolas Bonnefon } 319*3104b268SNicolas Bonnefon 320*3104b268SNicolas Bonnefon ~WatchTowerDirectories() { 321*3104b268SNicolas Bonnefon remove( third_file_name.c_str() ); 322*3104b268SNicolas Bonnefon remove( second_file_name.c_str() ); 323*3104b268SNicolas Bonnefon 324*3104b268SNicolas Bonnefon removeDir( second_dir_name ); 325*3104b268SNicolas Bonnefon } 326*3104b268SNicolas Bonnefon 327*3104b268SNicolas Bonnefon string createTempDir() { 328*3104b268SNicolas Bonnefon #ifdef _WIN32 329*3104b268SNicolas Bonnefon static int counter = 1; 330*3104b268SNicolas Bonnefon char temp_dir[255]; 331*3104b268SNicolas Bonnefon 332*3104b268SNicolas Bonnefon GetTempPath( sizeof temp_dir, temp_dir ); 333*3104b268SNicolas Bonnefon 334*3104b268SNicolas Bonnefon string dir_name = string { temp_dir } + string { "\\test" } + to_string( counter++ ); 335*3104b268SNicolas Bonnefon mkdir( dir_name.c_str() ); 336*3104b268SNicolas Bonnefon return dir_name; 337*3104b268SNicolas Bonnefon #else 338*3104b268SNicolas Bonnefon char dir_template[] = "/tmp/XXXXXX"; 339*3104b268SNicolas Bonnefon return { mkdtemp( dir_template ) }; 340*3104b268SNicolas Bonnefon #endif 341*3104b268SNicolas Bonnefon } 342*3104b268SNicolas Bonnefon 343*3104b268SNicolas Bonnefon string createTempEmptyFileInDir( const string& dir ) { 344*3104b268SNicolas Bonnefon static int counter = 1; 345*3104b268SNicolas Bonnefon return createTempEmptyFile( dir + std::string { "/temp" } + to_string( counter++ ) ); 346*3104b268SNicolas Bonnefon } 347*3104b268SNicolas Bonnefon 348*3104b268SNicolas Bonnefon void removeDir( const string& name ) { 349*3104b268SNicolas Bonnefon rmdir( name.c_str() ); 350*3104b268SNicolas Bonnefon } 351*3104b268SNicolas Bonnefon }; 352*3104b268SNicolas Bonnefon 353*3104b268SNicolas Bonnefon TEST_F( WatchTowerDirectories, FollowThreeFilesInTwoDirs ) { 354*3104b268SNicolas Bonnefon registration_two = registerFile( second_file_name ); 355*3104b268SNicolas Bonnefon registration_three = registerFile( third_file_name ); 356*3104b268SNicolas Bonnefon 357*3104b268SNicolas Bonnefon ASSERT_THAT( watch_tower->numberWatchedDirectories(), Eq( 2 ) ); 358*3104b268SNicolas Bonnefon } 359*3104b268SNicolas Bonnefon 360*3104b268SNicolas Bonnefon TEST_F( WatchTowerDirectories, FollowTwoFilesInTwoDirs ) { 361*3104b268SNicolas Bonnefon registration_two = registerFile( second_file_name ); 362*3104b268SNicolas Bonnefon { 363*3104b268SNicolas Bonnefon auto temp_registration_three = registerFile( third_file_name ); 364*3104b268SNicolas Bonnefon } 365*3104b268SNicolas Bonnefon 366*3104b268SNicolas Bonnefon ASSERT_THAT( watch_tower->numberWatchedDirectories(), Eq( 2 ) ); 367*3104b268SNicolas Bonnefon } 368*3104b268SNicolas Bonnefon 369*3104b268SNicolas Bonnefon TEST_F( WatchTowerDirectories, FollowOneFileInOneDir ) { 370*3104b268SNicolas Bonnefon { 371*3104b268SNicolas Bonnefon auto temp_registration_two = registerFile( second_file_name ); 372*3104b268SNicolas Bonnefon auto temp_registration_three = registerFile( third_file_name ); 373*3104b268SNicolas Bonnefon 374*3104b268SNicolas Bonnefon ASSERT_THAT( watch_tower->numberWatchedDirectories(), Eq( 2 ) ); 375*3104b268SNicolas Bonnefon } 376*3104b268SNicolas Bonnefon 377*3104b268SNicolas Bonnefon ASSERT_THAT( watch_tower->numberWatchedDirectories(), Eq( 1 ) ); 378*3104b268SNicolas Bonnefon } 379*3104b268SNicolas Bonnefon 380*3104b268SNicolas Bonnefon /*****/ 381*3104b268SNicolas Bonnefon 382a0b17fd1SNicolas Bonnefon #ifdef _WIN32 383af7adfddSNicolas Bonnefon class WinNotificationInfoListTest : public testing::Test { 384af7adfddSNicolas Bonnefon public: 385af7adfddSNicolas Bonnefon using Action = WinNotificationInfo::Action; 386af7adfddSNicolas Bonnefon 387af7adfddSNicolas Bonnefon struct Buffer { 388af7adfddSNicolas Bonnefon uint32_t next_addr; 389af7adfddSNicolas Bonnefon uint32_t action; 390af7adfddSNicolas Bonnefon uint32_t filename_length; 391af7adfddSNicolas Bonnefon wchar_t filename[13]; 392af7adfddSNicolas Bonnefon }; 393af7adfddSNicolas Bonnefon static struct Buffer buffer[2]; 394af7adfddSNicolas Bonnefon 395af7adfddSNicolas Bonnefon WinNotificationInfoList list { reinterpret_cast<char*>( buffer ), sizeof( buffer ) }; 396af7adfddSNicolas Bonnefon WinNotificationInfoList::iterator iterator { std::begin( list ) }; 397af7adfddSNicolas Bonnefon }; 398af7adfddSNicolas Bonnefon 399af7adfddSNicolas Bonnefon struct WinNotificationInfoListTest::Buffer WinNotificationInfoListTest::buffer[] = 400af7adfddSNicolas Bonnefon { { 40, 1, 26, L"Filename.txt" }, 401af7adfddSNicolas Bonnefon { 0, 3, 18, L"file2.txt" } }; 402af7adfddSNicolas Bonnefon 403af7adfddSNicolas Bonnefon TEST_F( WinNotificationInfoListTest, FirstNotificationCanBeObtained ) { 404af7adfddSNicolas Bonnefon auto notification = *iterator; 405af7adfddSNicolas Bonnefon ASSERT_THAT( ¬ification, NotNull() ); 406af7adfddSNicolas Bonnefon } 407af7adfddSNicolas Bonnefon 408af7adfddSNicolas Bonnefon TEST_F( WinNotificationInfoListTest, FirstNotificationHasRightAction ) { 409af7adfddSNicolas Bonnefon ASSERT_THAT( (*iterator).action(), Eq( Action::ADDED ) ); 410af7adfddSNicolas Bonnefon } 411af7adfddSNicolas Bonnefon 412af7adfddSNicolas Bonnefon TEST_F( WinNotificationInfoListTest, FirstNotificationHasRightFileName ) { 413af7adfddSNicolas Bonnefon ASSERT_THAT( (*iterator).fileName(), Eq( std::wstring( L"Filename.txt\0", 13 ) ) ); 414af7adfddSNicolas Bonnefon } 415af7adfddSNicolas Bonnefon 416af7adfddSNicolas Bonnefon TEST_F( WinNotificationInfoListTest, SecondNotificationCanBeObtained ) { 417af7adfddSNicolas Bonnefon ++iterator; 418af7adfddSNicolas Bonnefon auto notification = *iterator; 419af7adfddSNicolas Bonnefon ASSERT_THAT( ¬ification, NotNull() ); 420af7adfddSNicolas Bonnefon } 421af7adfddSNicolas Bonnefon 422af7adfddSNicolas Bonnefon TEST_F( WinNotificationInfoListTest, SecondNotificationIsCorrect ) { 423af7adfddSNicolas Bonnefon iterator++; 424af7adfddSNicolas Bonnefon ASSERT_THAT( iterator->action(), Eq( Action::MODIFIED ) ); 425af7adfddSNicolas Bonnefon ASSERT_THAT( iterator->fileName(), Eq( L"file2.txt" ) ); 426af7adfddSNicolas Bonnefon } 427af7adfddSNicolas Bonnefon 428af7adfddSNicolas Bonnefon TEST_F( WinNotificationInfoListTest, CanBeIteratedByFor ) { 429af7adfddSNicolas Bonnefon for ( auto notification : list ) { 430af7adfddSNicolas Bonnefon notification.action(); 431af7adfddSNicolas Bonnefon } 432af7adfddSNicolas Bonnefon } 433a0b17fd1SNicolas Bonnefon #endif 434