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