1--TEST--
2Test md5_file() function with ASCII output and raw binary output
3--FILE--
4<?php
5
6/* Prototype: string md5_file( string filename[, bool raw_output] )
7 * Description: Calculate the MD5 hash of a given file
8 */
9
10/* Creating an empty file */
11if (($handle = fopen( "EmptyFile.txt", "w+")) == FALSE)
12return false;
13
14/* Creating a data file */
15if (($handle2 = fopen( "DataFile.txt", "w+")) == FALSE)
16return false;
17
18/* Writing into file */
19$filename = "DataFile.txt";
20$content = "Add this to the file\n";
21if (is_writable($filename)) {
22  if (fwrite($handle2, $content) === FALSE) {
23    echo "Cannot write to file ($filename)";
24    exit;
25  }
26}
27
28// close the files
29fclose($handle);
30fclose($handle2);
31
32/* Testing error conditions */
33echo "\n*** Testing for error conditions ***\n";
34
35/* No filename */
36var_dump( md5_file("") );
37
38/* invalid filename */
39var_dump( md5_file("aZrq16u") );
40
41/* Scalar value as filename  */
42var_dump( md5_file(12) );
43
44/* NULL as filename */
45var_dump( md5_file(NULL) );
46
47/* Zero arguments */
48 var_dump ( md5_file() );
49
50/* More than valid number of arguments ( valid is 2)  */
51var_dump ( md5_file("EmptyFile.txt", true, NULL) );
52
53/* Hexadecimal Output for Empty file as input */
54echo "\n*** Hexadecimal Output for Empty file as Argument ***\n";
55var_dump( md5_file("EmptyFile.txt") );
56
57/* Raw Binary Output for Empty file as input */
58echo "\n*** Raw Binary Output for Empty file as Argument ***\n";
59var_dump( md5_file("EmptyFile.txt", true) );
60
61/* Normal operation with hexadecimal output */
62echo "\n*** Hexadecimal Output for a valid file with some contents ***\n";
63var_dump( md5_file("DataFile.txt") );
64
65/* Normal operation with raw binary output */
66echo "\n*** Raw Binary Output for a valid file with some contents ***\n";
67var_dump ( md5_file("DataFile.txt", true) );
68
69// remove temp files
70unlink("DataFile.txt");
71unlink("EmptyFile.txt");
72
73echo "\nDone";
74?>
75--EXPECTF--
76*** Testing for error conditions ***
77
78Warning: md5_file(): Filename cannot be empty in %s on line %d
79bool(false)
80
81Warning: md5_file(aZrq16u): failed to open stream: No such file or directory in %s on line %d
82bool(false)
83
84Warning: md5_file(12): failed to open stream: No such file or directory in %s on line %d
85bool(false)
86
87Warning: md5_file(): Filename cannot be empty in %s on line %d
88bool(false)
89
90Warning: md5_file() expects at least 1 parameter, 0 given in %s on line %d
91NULL
92
93Warning: md5_file() expects at most 2 parameters, 3 given in %s on line %d
94NULL
95
96*** Hexadecimal Output for Empty file as Argument ***
97string(32) "d41d8cd98f00b204e9800998ecf8427e"
98
99*** Raw Binary Output for Empty file as Argument ***
100string(16) "��ُ���	���B~"
101
102*** Hexadecimal Output for a valid file with some contents ***
103string(32) "7f28ec647825e2a70bf67778472cd4a2"
104
105*** Raw Binary Output for a valid file with some contents ***
106string(16) "(�dx%��wxG,Ԣ"
107
108Done
109