1--TEST--
2Test mcrypt_cbc() function : usage variation
3--SKIPIF--
4<?php
5if (!extension_loaded("mcrypt")) {
6	print "skip - mcrypt extension not loaded";
7}
8?>
9--FILE--
10<?php
11/* Prototype  : string mcrypt_cbc(string cipher, string key, string data, int mode, string iv)
12 * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv
13 * Source code: ext/mcrypt/mcrypt.c
14 * Alias to functions:
15 */
16
17echo "*** Testing mcrypt_cbc() : usage variation ***\n";
18
19// Define error handler
20function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
21	if (error_reporting() != 0) {
22		// report non-silenced errors
23		echo "Error: $err_no - $err_msg, $filename($linenum)\n";
24	}
25}
26set_error_handler('test_error_handler');
27
28// Initialise function arguments not being substituted (if any)
29$cipher = MCRYPT_TRIPLEDES;
30$data = b'string_val';
31$mode = MCRYPT_ENCRYPT;
32$iv = b'01234567';
33
34//get an unset variable
35$unset_var = 10;
36unset ($unset_var);
37
38// define some classes
39class classWithToString
40{
41	public function __toString() {
42		return b"Class A object";
43	}
44}
45
46class classWithoutToString
47{
48}
49
50// heredoc string
51$heredoc = b<<<EOT
52hello world
53EOT;
54
55// get a resource variable
56$fp = fopen(__FILE__, "r");
57
58// add arrays
59$index_array = array (1, 2, 3);
60$assoc_array = array ('one' => 1, 'two' => 2);
61
62//array of values to iterate over
63$inputs = array(
64
65      // int data
66      'int 0' => 0,
67      'int 1' => 1,
68      'int 12345' => 12345,
69      'int -12345' => -2345,
70
71      // float data
72      'float 10.5' => 10.5,
73      'float -10.5' => -10.5,
74      'float 12.3456789000e10' => 12.3456789000e10,
75      'float -12.3456789000e10' => -12.3456789000e10,
76      'float .5' => .5,
77
78      // array data
79      'empty array' => array(),
80      'int indexed array' => $index_array,
81      'associative array' => $assoc_array,
82      'nested arrays' => array('foo', $index_array, $assoc_array),
83
84      // null data
85      'uppercase NULL' => NULL,
86      'lowercase null' => null,
87
88      // boolean data
89      'lowercase true' => true,
90      'lowercase false' =>false,
91      'uppercase TRUE' =>TRUE,
92      'uppercase FALSE' =>FALSE,
93
94      // empty data
95      'empty string DQ' => "",
96      'empty string SQ' => '',
97
98      // object data
99      'instance of classWithToString' => new classWithToString(),
100      'instance of classWithoutToString' => new classWithoutToString(),
101
102      // undefined data
103      'undefined var' => @$undefined_var,
104
105      // unset data
106      'unset var' => @$unset_var,
107
108      // resource variable
109      'resource' => $fp
110);
111
112// loop through each element of the array for key
113
114foreach($inputs as $valueType =>$value) {
115      echo "\n--$valueType--\n";
116      var_dump(bin2hex(mcrypt_cbc($cipher, $value, $data, $mode, $iv)));
117};
118
119fclose($fp);
120
121?>
122===DONE===
123--EXPECTF--
124*** Testing mcrypt_cbc() : usage variation ***
125
126--int 0--
127string(32) "bc27b3a4e33b531d5983fc7df693cd09"
128
129--int 1--
130string(32) "bc27b3a4e33b531d5983fc7df693cd09"
131
132--int 12345--
133string(32) "d109b7973383127002474ae731c4b3a8"
134
135--int -12345--
136string(32) "3e82a931cedb03a38b91a637ff8c9f9e"
137
138--float 10.5--
139string(32) "de71833586c1d7132a289960ebeeca7a"
140
141--float -10.5--
142string(32) "7d0489dd2e99ae910ecc015573f3dd16"
143
144--float 12.3456789000e10--
145string(32) "978055b42c0506a8947e3c3c8d994baf"
146
147--float -12.3456789000e10--
148string(32) "4aa84ba400c2b8ef467d4d98372b4f4e"
149
150--float .5--
151string(32) "e731dc5059b84e0c8774ac490f77d6e6"
152
153--empty array--
154Error: 2 - mcrypt_cbc() expects parameter 2 to be string, array given, %s(%d)
155string(0) ""
156
157--int indexed array--
158Error: 2 - mcrypt_cbc() expects parameter 2 to be string, array given, %s(%d)
159string(0) ""
160
161--associative array--
162Error: 2 - mcrypt_cbc() expects parameter 2 to be string, array given, %s(%d)
163string(0) ""
164
165--nested arrays--
166Error: 2 - mcrypt_cbc() expects parameter 2 to be string, array given, %s(%d)
167string(0) ""
168
169--uppercase NULL--
170string(32) "be722a5ffc361d721fbcab1eacc6acf5"
171
172--lowercase null--
173string(32) "be722a5ffc361d721fbcab1eacc6acf5"
174
175--lowercase true--
176string(32) "bc27b3a4e33b531d5983fc7df693cd09"
177
178--lowercase false--
179string(32) "be722a5ffc361d721fbcab1eacc6acf5"
180
181--uppercase TRUE--
182string(32) "bc27b3a4e33b531d5983fc7df693cd09"
183
184--uppercase FALSE--
185string(32) "be722a5ffc361d721fbcab1eacc6acf5"
186
187--empty string DQ--
188string(32) "be722a5ffc361d721fbcab1eacc6acf5"
189
190--empty string SQ--
191string(32) "be722a5ffc361d721fbcab1eacc6acf5"
192
193--instance of classWithToString--
194string(32) "19420fa26f561ee82ed84abbcd2d284b"
195
196--instance of classWithoutToString--
197Error: 2 - mcrypt_cbc() expects parameter 2 to be string, object given, %s(%d)
198string(0) ""
199
200--undefined var--
201string(32) "be722a5ffc361d721fbcab1eacc6acf5"
202
203--unset var--
204string(32) "be722a5ffc361d721fbcab1eacc6acf5"
205
206--resource--
207Error: 2 - mcrypt_cbc() expects parameter 2 to be string, resource given, %s(%d)
208string(0) ""
209===DONE===
210
211