1--TEST-- 2Test sha1_file() function with ASCII output and raw binary output. Based on ext/standard/tests/strings/md5_file.phpt 3--FILE-- 4<?php 5 6 7echo "*** Testing sha1_file() : basic functionality ***\n"; 8 9/* Creating an empty file */ 10if (($handle = fopen( "EmptyFileSHA1.txt", "w+")) == FALSE) 11return false; 12 13/* Creating a data file */ 14if (($handle2 = fopen( "DataFileSHA1.txt", "w+")) == FALSE) 15return false; 16 17/* Writing into file */ 18$filename = "DataFileSHA1.txt"; 19$content = "Add this to the file\n"; 20if (is_writable($filename)) { 21 if (fwrite($handle2, $content) === FALSE) { 22 echo "Cannot write to file ($filename)"; 23 exit; 24 } 25} 26 27// close the files 28fclose($handle); 29fclose($handle2); 30 31/* Testing error conditions */ 32echo "\n*** Testing for error conditions ***\n"; 33 34echo "\n-- No filename --\n"; 35try { 36 var_dump( sha1_file("") ); 37} catch (\ValueError $e) { 38 echo $e->getMessage() . \PHP_EOL; 39} 40 41echo "\n-- invalid filename --\n"; 42var_dump( sha1_file("rewncwYcn89q") ); 43 44echo "\n-- Scalar value as filename --\n"; 45var_dump( sha1_file(12) ); 46 47echo "\n-- NULL as filename --\n"; 48try { 49 var_dump( sha1_file(NULL) ); 50} catch (\ValueError $e) { 51 echo $e->getMessage() . \PHP_EOL; 52} 53 54echo "\n-- Hexadecimal Output for Empty file as Argument --\n"; 55var_dump( sha1_file("EmptyFileSHA1.txt") ); 56 57echo "\n-- Raw Binary Output for Empty file as Argument --\n"; 58var_dump( bin2hex(sha1_file("EmptyFileSHA1.txt", true))); 59 60echo "\n-- Hexadecimal Output for a valid file with some contents --\n"; 61var_dump( sha1_file("DataFileSHA1.txt") ); 62 63echo "\n-- Raw Binary Output for a valid file with some contents --\n"; 64var_dump ( bin2hex(sha1_file("DataFileSHA1.txt", true))); 65 66// remove temp files 67unlink("DataFileSHA1.txt"); 68unlink("EmptyFileSHA1.txt"); 69 70?> 71--EXPECTF-- 72*** Testing sha1_file() : basic functionality *** 73 74*** Testing for error conditions *** 75 76-- No filename -- 77Path must not be empty 78 79-- invalid filename -- 80 81Warning: sha1_file(rewncwYcn89q): Failed to open stream: No such file or directory in %s on line %d 82bool(false) 83 84-- Scalar value as filename -- 85 86Warning: sha1_file(12): Failed to open stream: No such file or directory in %s on line %d 87bool(false) 88 89-- NULL as filename -- 90 91Deprecated: sha1_file(): Passing null to parameter #1 ($filename) of type string is deprecated in %s on line %d 92Path must not be empty 93 94-- Hexadecimal Output for Empty file as Argument -- 95string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709" 96 97-- Raw Binary Output for Empty file as Argument -- 98string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709" 99 100-- Hexadecimal Output for a valid file with some contents -- 101string(40) "d16a568ab98233deff7ec8b1668eb4b3d9e53fee" 102 103-- Raw Binary Output for a valid file with some contents -- 104string(40) "d16a568ab98233deff7ec8b1668eb4b3d9e53fee" 105