php-src/ext/standard/tests/strings/md5_file.phpt
Rasmus Lerdorf ffb1c4d8ef Fix typo and don't use 'a' as an invalid filename since it is the default
gcc binary filename and is thus not unlikely to be left lying around in the
build dir which cause these tests to fail
2011-11-12 18:46:04 +00:00

110 lines
2.8 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--TEST--
Test md5_file() function with ASCII output and raw binary output
--FILE--
<?php
/* Prototype: string md5_file( string filename[, bool raw_output] )
* Description: Calculate the MD5 hash of a given file
*/
/* Creating an empty file */
if (($handle = fopen( "EmptyFile.txt", "w+")) == FALSE)
return false;
/* Creating a data file */
if (($handle2 = fopen( "DataFile.txt", "w+")) == FALSE)
return false;
/* Writing into file */
$filename = "DataFile.txt";
$content = "Add this to the file\n";
if (is_writable($filename)) {
if (fwrite($handle2, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
}
// close the files
fclose($handle);
fclose($handle2);
/* Testing error conditions */
echo "\n*** Testing for error conditions ***\n";
/* No filename */
var_dump( md5_file("") );
/* invalid filename */
var_dump( md5_file("aZrq16u") );
/* Scalar value as filename */
var_dump( md5_file(12) );
/* NULL as filename */
var_dump( md5_file(NULL) );
/* Zero arguments */
var_dump ( md5_file() );
/* More than valid number of arguments ( valid is 2) */
var_dump ( md5_file("EmptyFile.txt", true, NULL) );
/* Hexadecimal Output for Empty file as input */
echo "\n*** Hexadecimal Output for Empty file as Argument ***\n";
var_dump( md5_file("EmptyFile.txt") );
/* Raw Binary Output for Empty file as input */
echo "\n*** Raw Binary Output for Empty file as Argument ***\n";
var_dump( md5_file("EmptyFile.txt", true) );
/* Normal operation with hexadecimal output */
echo "\n*** Hexadecimal Output for a valid file with some contents ***\n";
var_dump( md5_file("DataFile.txt") );
/* Normal operation with raw binary output */
echo "\n*** Raw Binary Output for a valid file with some contents ***\n";
var_dump ( md5_file("DataFile.txt", true) );
// remove temp files
unlink("DataFile.txt");
unlink("EmptyFile.txt");
echo "\nDone";
?>
--EXPECTF--
*** Testing for error conditions ***
Warning: md5_file(): Filename cannot be empty in %s on line %d
bool(false)
Warning: md5_file(aZrq16u): failed to open stream: No such file or directory in %s on line %d
bool(false)
Warning: md5_file(12): failed to open stream: No such file or directory in %s on line %d
bool(false)
Warning: md5_file(): Filename cannot be empty in %s on line %d
bool(false)
Warning: md5_file() expects at least 1 parameter, 0 given in %s on line %d
NULL
Warning: md5_file() expects at most 2 parameters, 3 given in %s on line %d
NULL
*** Hexadecimal Output for Empty file as Argument ***
string(32) "d41d8cd98f00b204e9800998ecf8427e"
*** Raw Binary Output for Empty file as Argument ***
string(16) "<22><1D>ُ<00><04><> <09><><EFBFBD>B~"
*** Hexadecimal Output for a valid file with some contents ***
string(32) "7f28ec647825e2a70bf67778472cd4a2"
*** Raw Binary Output for a valid file with some contents ***
string(16) "(<28>dx%<25><> <0B>wxG,Ԣ"
Done