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