mcrypt_ecb_variation3.phpt 4.6 KB

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