1; 2; various basic tests on FILE_TEST() 3; written by NATCHKEBIA Ilia, May 2015 4; under GNU GPL v2 or any later 5; 6; ----------------------------------------------- 7; 8; Modifications history : 9; 10; - 2018 G. Jung Use FILE_LINK, FILE_MKDIR, and FILE_DELETE instead of SPAWN. 11; - 2018-09-24 : AC. 12; * Splitting code in various topics. 13; * Adding tests on Dangling Symlinks ... 14; * Adding new tests on Unix files (get_mode=, special type ...) 15; - 2020-06-03 : small change for *BSD 16; 17; ----------------------------------------------- 18; 19pro TEST_FILE_TEST_DIR_DANGLING, cumul_errors, test=test 20; 21if (!version.os_family NE 'unix') then begin 22 MESSAGE, /continue, 'This code works only on Unix-like OS' 23 return 24endif 25; 26nb_errors=0 27; 28; Testing dangling symlink 29; 30tdirsym1='test_dangling_Symlink_Dir_for_FILE_TEST_1' 31tdirsym2='test_dangling_Symlink_Dir_for_FILE_TEST_2' 32; 33; cleaning to avoid unwanted message 34FILE_DELETE, tdirsym1, tdirsym2, /allow_nonexistent 35; 36FILE_MKDIR, tdirsym1 37FILE_LINK, tdirsym1, tdirsym2 38FILE_DELETE, tdirsym1 39; 40; if "tdirsym1" fully removed ? dir, symlink & dangling_symlink ? 41if FILE_TEST(tdirsym1) then ERRORS_ADD, nb_errors, 'dir 1 not removed !!' 42; 43if ~FILE_TEST(tdirsym2) then ERRORS_ADD, nb_errors, 'dir. 2 (a symlink) was removed' 44if FILE_TEST(tdirsym2, /regular) then ERRORS_ADD, nb_errors, 'dir. 2 is now regular' 45if ~FILE_TEST(tdirsym2, /symlink) then ERRORS_ADD, nb_errors, 'dir. 2 is not a symlink' 46if ~FILE_TEST(tdirsym2, /dangling) then ERRORS_ADD, nb_errors, 'dir. 2 NOT seen as dangling' 47; 48; removing dangling file 2 49FILE_DELETE, tdirsym2 50; 51; we redo all the test because something wrong may happen 52; on dangling symlink ! (not being well done as expected !) 53if FILE_TEST(tdirsym2) then ERRORS_ADD, nb_errors, 'dir. 2 not removed (after)' 54; 55; ----- final ---- 56; 57BANNER_FOR_TESTSUITE, 'TEST_FILE_TEST_DIR_DANGLING', nb_errors, /status 58ERRORS_CUMUL, cumul_errors, nb_errors 59if KEYWORD_SET(test) then STOP 60; 61end 62; 63; ----------------------------------------------- 64; 65pro TEST_FILE_TEST_DIR_BASIC, cumul_errors, test=test 66; 67nb_errors=0 68; 69; Create test directory 70tdir='tdir_for_file_test' 71FILE_MKDIR, tdir 72; 73; Test if exists 74if ~FILE_TEST(tdir) then ERRORS_ADD, nb_errors, 'Dir. not detected' 75; Test if it is directory 76if ~FILE_TEST(tdir, /dir) then ERRORS_ADD, nb_errors, 'Dir. not considered as Dir' 77; Test if it is symlink 78if FILE_TEST(tdir, /sym) then ERRORS_ADD, nb_errors, 'Dir. is considered as symlink (but should not !)' 79; Test if it is regular 80if FILE_TEST(tdir, /regular) then ERRORS_ADD, nb_errors, 'Dir. is considered as a regular file (but should not !)' 81; 82; cleaning 83FILE_DELETE, tdir 84; 85; ----- final ---- 86; 87BANNER_FOR_TESTSUITE, 'TEST_FILE_TEST_DIR_BASIC', nb_errors, /status 88ERRORS_CUMUL, cumul_errors, nb_errors 89if KEYWORD_SET(test) then STOP 90; 91end 92; 93; ----------------------------------------------- 94; 95pro TEST_FILE_TEST_DIR_SYMLINK, cumul_errors, test=test 96; 97if (!version.os_family NE 'unix') then begin 98 MESSAGE, /continue, 'This code works only on Unix-like OS' 99 return 100endif 101; 102nb_errors=0 103; 104; Create test directory 105tdir='tdir_for_file_test' 106FILE_MKDIR, tdir 107; 108;Create test folder symlink 109tdirsym='testSymlinkDirectory_for_FILE_TEST' 110FILE_LINK, tdir, tdirsym 111; 112;Test if it exists 113if ~FILE_TEST(tdirsym) then ERRORS_ADD, nb_errors, 'symlink of Dir. not detected' 114;Test if it is symlink of directory 115if ~FILE_TEST(tdirsym, /dir) then ERRORS_ADD, nb_errors, 'symlink of Dir. not considered as Dir.' 116;Test if it is symlink 117if ~FILE_TEST(tdirsym, /sym) then ERRORS_ADD, nb_errors, 'symlink is not considered as symlink' 118; 119;Remove test directory and symlink 120FILE_DELETE, /recur, tdir 121FILE_DELETE, tdirsym 122; 123; ----- final ---- 124; 125BANNER_FOR_TESTSUITE, 'TEST_FILE_TEST_DIR_SYMLINK', nb_errors, /status 126ERRORS_CUMUL, cumul_errors, nb_errors 127if KEYWORD_SET(test) then STOP 128; 129end 130; 131; ----------------------------------------------- 132 133pro TEST_FILE_TEST_FILE_BASIC, cumul_errors, test=test 134; 135nb_errors=0 136; 137; Create 2 test files : one without nothing, the other one with content 138tfile1='tfile_for_FILE_TEST_void' 139tfile2='tfile_for_FILE_TEST_non_void' 140; 141; cleaning to avoid unwanted message 142FILE_DELETE, tfile1, tfile2, /allow_nonexistent 143; 144; creating a void file 145OPENW, flun, tfile1, /get_lun 146CLOSE, flun 147FREE_LUN, flun 148; 149OPENW, flun, tfile2, /get_lun 150PRINTF, flun, 'this is a non void file !' 151CLOSE, flun 152FREE_LUN, flun 153; 154; Test if both files exist 155if ~FILE_TEST(tfile1) then ERRORS_ADD, nb_errors, 'file 1 not detected' 156if ~FILE_TEST(tfile2) then ERRORS_ADD, nb_errors, 'file 2 not detected' 157; 158; Test if it is directory 159if FILE_TEST(tfile1, /dir) then ERRORS_ADD, nb_errors, 'file 1 is considered as Dir.' 160if FILE_TEST(tfile2, /dir) then ERRORS_ADD, nb_errors, 'file 2 is considered as Dir.' 161; 162; Test if it is symlink 163if (!version.os_family EQ 'unix') then begin 164 if FILE_TEST(tfile1, /sym) then ERRORS_ADD, nb_errors, 'file 1 is considered as symlink' 165 if FILE_TEST(tfile2, /sym) eq 1 then ERRORS_ADD, nb_errors, 'file 2 is considered as symlink' 166endif 167; 168; Test if it is symlink 169if ~FILE_TEST(tfile1, /zero_length) then ERRORS_ADD, nb_errors, 'file 1 is NOT considered as zero_length' 170if FILE_TEST(tfile2, /zero_length) then ERRORS_ADD, nb_errors, 'file 2 is considered as zero_length' 171; 172; Test if regular 173if ~FILE_TEST(tfile1, /regular) then ERRORS_ADD, nb_errors, 'file 1 is NOT considered as zero_length' 174if ~FILE_TEST(tfile2, /regular) then ERRORS_ADD, nb_errors, 'file 2 is considered as zero_length' 175; 176; final cleaning 177FILE_DELETE, tfile1, tfile2, /allow_nonexistent 178 179; ----- final ---- 180; 181BANNER_FOR_TESTSUITE, 'TEST_FILE_TEST_FILE_BASIC', nb_errors, /status 182ERRORS_CUMUL, cumul_errors, nb_errors 183if KEYWORD_SET(test) then STOP 184; 185end 186; 187; ----------------------------------------------- 188; 189pro TEST_FILE_TEST_FILE_SYMLINK, cumul_errors, test=test 190; 191if (!version.os_family NE 'unix') then begin 192 MESSAGE, /continue, 'This code works only on Unix-like OS' 193 return 194endif 195; 196nb_errors=0 197; 198tfile='tfile_for_FILE_TEST' 199; cleaning to avoid unwanted message 200FILE_DELETE, tfile, /allow_nonexistent 201; creating 202OPENW, flun, tfile, /get_lun 203CLOSE, flun 204FREE_LUN, flun 205; 206; Create test file symlink 207tfilesym='testSymlinkFile_for_FILE_TEST' 208FILE_LINK, tfile, tfilesym 209; 210; Test if it exists 211if ~FILE_TEST(tfilesym) then ERRORS_ADD, nb_errors, 'symlink of file not detected' 212; Test if it is symlink of directory 213if FILE_TEST(tfilesym, /dir) then ERRORS_ADD, nb_errors, 'symlink of file is considered as Dir.' 214; Test if it is symlink 215if ~FILE_TEST(tfilesym, /sym) then ERRORS_ADD, nb_errors, 'symlink of file is not considered as symlink' 216; 217; Remove test file and symlink 218FILE_DELETE, tfile, tfilesym, /allow_nonexistent 219; 220; ----- final ---- 221; 222BANNER_FOR_TESTSUITE, 'TEST_FILE_TEST_FILE_SYMLINK', nb_errors, /status 223ERRORS_CUMUL, cumul_errors, nb_errors 224if KEYWORD_SET(test) then STOP 225; 226end 227; 228; ----------------------------------------------- 229; testing special files 230; https://askubuntu.com/questions/397493/what-does-the-first-character-of-unix-mode-string-indicate 231; 232pro TEST_FILE_TEST_UNIX, cumul_errors, test=test 233; 234if (!version.os_family NE 'unix') then begin 235 MESSAGE, /continue, 'This code works only on Unix-like OS' 236 return 237endif 238; 239nb_errors=0 240; 241; testing /char file 242; 243special_file='/dev/urandom' 244; see discussion in #764 245if (STRPOS(STRlowCASE(!version.os), 'bsd') GE 0) then special_file='/dev/random' 246; 247mess='This special file '+special_file+' ' 248; 249; Test if it exists 250if ~FILE_TEST(special_file) then ERRORS_ADD, nb_errors, mess+'should exist !' 251; Test if it is symlink of directory 252if ~FILE_TEST(special_file, /char) then ERRORS_ADD, nb_errors, mess+'is a Char (crw-rw-rw-) !' 253; Test if it is symlink 254if FILE_TEST(special_file, /sym) then ERRORS_ADD, nb_errors, mess+'is not a symlink !' 255;; Test if it is regular 256if FILE_TEST(special_file, /regular) then ERRORS_ADD, nb_errors, mess+'is NOT a regular !' 257; 258; testing /block file 259; 260special_file='' 261if (!version.os EQ 'linux') then special_file='/dev/sda' 262if (!version.os EQ 'darwin') then special_file='/dev/disk0' 263if (STRLEN(special_file) EQ 0) then begin 264 MESSAGE, /continue, 'We don''t know a special Block file on your OS' 265 MESSAGE, /continue, 'Please contribute !! FixMe' 266endif else begin 267 mess='This special file '+special_file+' ' 268 if ~FILE_TEST(special_file) then ERRORS_ADD, nb_errors, mess+'should exist !' 269 ;; Test if it is symlink of directory 270 if FILE_TEST(special_file, /char) then ERRORS_ADD, nb_errors, mess+'is NOT a Char (crw-rw-rw-) !' 271 ;; Test if it is symlink 272 if FILE_TEST(special_file, /sym) then ERRORS_ADD, nb_errors, mess+'is NOT a symlink !' 273 ;; Test if it is block 274 if ~FILE_TEST(special_file, /block) then ERRORS_ADD, nb_errors, mess+'must be a block device !' 275 ;; Test if it is regular 276 if FILE_TEST(special_file, /regular) then ERRORS_ADD, nb_errors, mess+'is NOT a regular !' 277endelse 278 279; ----- final ---- 280; 281BANNER_FOR_TESTSUITE, 'TEST_FILE_TEST_UNIX', nb_errors, /status 282ERRORS_CUMUL, cumul_errors, nb_errors 283if KEYWORD_SET(test) then STOP 284; 285end 286; 287; ----------------------------------------------- 288; testing returned mode for some files 289; 290pro TEST_FILE_TEST_GET_MODE, cumul_errors, test=test 291; 292Notunix = !version.os_family NE 'unix' 293if (Notunix) then begin 294 MESSAGE, /continue, 'This code applies only to Unix-like OS' 295 return 296endif 297; 298nb_errors=0 299; 300file=FILE_WHICH('Saturn.jpg', /INCLUDE_CURRENT_DIR) 301mess='This file '+file+' ' 302expected_modes=[511L,436L,420L] 303exist=FILE_TEST(file, get_mode=mode) 304if ~exist then begin 305 ERRORS_ADD, nb_errors, mess+'should exist !' 306endif else begin 307 w=where(mode eq expected_modes, count) 308 if (count eq 0) then ERRORS_ADD, nb_errors, mess+'has a bad mode !' 309endelse 310; 311; testing /char file 312; 313file_char='/dev/urandom' 314mess='This file '+file_char+' ' 315expected_mode=438L 316exist=FILE_TEST(file_char, get_mode=mode) 317if ~exist then begin 318 ERRORS_ADD, nb_errors, mess+'should exist !' 319endif else begin 320 if (mode ne expected_mode) then $ 321 ERRORS_ADD, nb_errors, mess+'has a bad mode !' 322endelse 323; 324; testing /block file 325; 326file_block='' 327if (!version.os EQ 'linux') then file_block='/dev/sda' 328if (!version.os EQ 'darwin') then file_block='/dev/disk0' 329if (STRLEN(file_block) EQ 0) then begin 330 MESSAGE, /continue, 'We don''t know a special Block file on your OS' 331 MESSAGE, /continue, 'Please contribute !! FixMe' 332endif else begin 333 mess='This file '+file_block+' ' 334 expected_mode=432L 335 exist=FILE_TEST(file_block, get_mode=mode) 336 if ~exist then begin 337 ERRORS_ADD, nb_errors, mess+'should exist !' 338 endif else begin 339 if (mode ne expected_mode) then $ 340 ERRORS_ADD, nb_errors, mess+'has a bad mode !' 341 endelse 342endelse 343; 344; ----- final ---- 345; 346BANNER_FOR_TESTSUITE, 'TEST_FILE_TEST_GET_MODE', nb_errors, /status 347ERRORS_CUMUL, cumul_errors, nb_errors 348if KEYWORD_SET(test) then STOP 349; 350end 351; 352; ----------------------------------------------- 353; 354pro TEST_FILE_TEST, test=test, no_exit=no_exit, help=help 355; 356if KEYWORD_SET(help) then begin 357 print, 'pro TEST_FILE_TEST, test=test, no_exit=no_exit, help=help' 358 return 359endif 360; 361cumul_errors=0 362; 363TEST_FILE_TEST_UNIX, cumul_errors, test=test 364TEST_FILE_TEST_GET_MODE, cumul_errors, test=test 365; 366TEST_FILE_TEST_FILE_BASIC, cumul_errors, test=test 367TEST_FILE_TEST_FILE_SYMLINK, cumul_errors, test=test 368; 369TEST_FILE_TEST_DIR_BASIC, cumul_errors, test=test 370TEST_FILE_TEST_DIR_SYMLINK, cumul_errors, test=test 371TEST_FILE_TEST_DIR_DANGLING, cumul_errors, test=test 372; 373; ----------------- final message ---------- 374; 375BANNER_FOR_TESTSUITE, 'TEST_FILE_TEST', cumul_errors 376; 377if (cumul_errors GT 0) AND ~KEYWORD_SET(no_exit) then EXIT, status=1 378; 379if KEYWORD_SET(test) then STOP 380; 381end 382