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