1--TEST-- 2Test mcrypt_encrypt() 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_encrypt(string cipher, string key, string data, string mode, string iv) 12 * Description: OFB 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_encrypt() : 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$key = b'string_val'; 31$data = b'string_val'; 32//in php, it incorrectly reports problems with iv in ECB mode. 33$mode = MCRYPT_MODE_CBC; 34 35//get an unset variable 36$unset_var = 10; 37unset ($unset_var); 38 39// define some classes 40class classWithToString 41{ 42 public function __toString() { 43 return b"Class A object"; 44 } 45} 46 47class classWithoutToString 48{ 49} 50 51// heredoc string 52$heredoc = b<<<EOT 53hello world 54EOT; 55 56// get a resource variable 57$fp = fopen(__FILE__, "r"); 58 59// add arrays 60$index_array = array (1, 2, 3); 61$assoc_array = array ('one' => 1, 'two' => 2); 62 63//array of values to iterate over 64$inputs = array( 65 66 // int data 67 'int 0' => 0, 68 'int 1' => 1, 69 'int 12345' => 12345, 70 'int -12345' => -2345, 71 72 // float data 73 'float 10.5' => 10.5, 74 'float -10.5' => -10.5, 75 'float 12.3456789000e10' => 12.3456789000e10, 76 'float -12.3456789000e10' => -12.3456789000e10, 77 'float .5' => .5, 78 79 // array data 80 'empty array' => array(), 81 'int indexed array' => $index_array, 82 'associative array' => $assoc_array, 83 'nested arrays' => array('foo', $index_array, $assoc_array), 84 85 // null data 86 'uppercase NULL' => NULL, 87 'lowercase null' => null, 88 89 // boolean data 90 'lowercase true' => true, 91 'lowercase false' =>false, 92 'uppercase TRUE' =>TRUE, 93 'uppercase FALSE' =>FALSE, 94 95 // empty data 96 'empty string DQ' => "", 97 'empty string SQ' => '', 98 99 // object data 100 'instance of classWithToString' => new classWithToString(), 101 'instance of classWithoutToString' => new classWithoutToString(), 102 103 // undefined data 104 'undefined var' => @$undefined_var, 105 106 // unset data 107 'unset var' => @$unset_var, 108 109 // resource variable 110 'resource' => $fp 111); 112 113// loop through each element of the array for iv 114 115foreach($inputs as $valueType =>$value) { 116 echo "\n--$valueType--\n"; 117 var_dump( bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $value))); 118}; 119 120fclose($fp); 121 122?> 123===DONE=== 124--EXPECTF-- 125*** Testing mcrypt_encrypt() : usage variation *** 126 127--int 0-- 128Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 129string(32) "6438db90653c4d3080c3ceab43618c05" 130 131--int 1-- 132Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 133string(32) "6438db90653c4d3080c3ceab43618c05" 134 135--int 12345-- 136Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 137string(32) "6438db90653c4d3080c3ceab43618c05" 138 139--int -12345-- 140Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 141string(32) "6438db90653c4d3080c3ceab43618c05" 142 143--float 10.5-- 144Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 145string(32) "6438db90653c4d3080c3ceab43618c05" 146 147--float -10.5-- 148Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 149string(32) "6438db90653c4d3080c3ceab43618c05" 150 151--float 12.3456789000e10-- 152Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 153string(32) "6438db90653c4d3080c3ceab43618c05" 154 155--float -12.3456789000e10-- 156Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 157string(32) "6438db90653c4d3080c3ceab43618c05" 158 159--float .5-- 160Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 161string(32) "6438db90653c4d3080c3ceab43618c05" 162 163--empty array-- 164Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d) 165string(0) "" 166 167--int indexed array-- 168Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d) 169string(0) "" 170 171--associative array-- 172Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d) 173string(0) "" 174 175--nested arrays-- 176Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d) 177string(0) "" 178 179--uppercase NULL-- 180Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 181string(32) "6438db90653c4d3080c3ceab43618c05" 182 183--lowercase null-- 184Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 185string(32) "6438db90653c4d3080c3ceab43618c05" 186 187--lowercase true-- 188Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 189string(32) "6438db90653c4d3080c3ceab43618c05" 190 191--lowercase false-- 192Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 193string(32) "6438db90653c4d3080c3ceab43618c05" 194 195--uppercase TRUE-- 196Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 197string(32) "6438db90653c4d3080c3ceab43618c05" 198 199--uppercase FALSE-- 200Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 201string(32) "6438db90653c4d3080c3ceab43618c05" 202 203--empty string DQ-- 204Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 205string(32) "6438db90653c4d3080c3ceab43618c05" 206 207--empty string SQ-- 208Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 209string(32) "6438db90653c4d3080c3ceab43618c05" 210 211--instance of classWithToString-- 212Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 213string(32) "6438db90653c4d3080c3ceab43618c05" 214 215--instance of classWithoutToString-- 216Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, object given, %s(%d) 217string(0) "" 218 219--undefined var-- 220Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 221string(32) "6438db90653c4d3080c3ceab43618c05" 222 223--unset var-- 224Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) 225string(32) "6438db90653c4d3080c3ceab43618c05" 226 227--resource-- 228Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, resource given, %s(%d) 229string(0) "" 230===DONE=== 231 232