mcrypt_encrypt_variation5.phpt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. --TEST--
  2. Test mcrypt_encrypt() 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. /* 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. echo "*** Testing mcrypt_encrypt() : usage variation ***\n";
  17. // Define error handler
  18. function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
  19. if (error_reporting() != 0) {
  20. // report non-silenced errors
  21. echo "Error: $err_no - $err_msg, $filename($linenum)\n";
  22. }
  23. }
  24. set_error_handler('test_error_handler');
  25. // Initialise function arguments not being substituted (if any)
  26. $cipher = MCRYPT_TRIPLEDES;
  27. $key = b"string_val\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  28. $data = b'string_val';
  29. //in php, it incorrectly reports problems with iv in ECB mode.
  30. $mode = MCRYPT_MODE_CBC;
  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 iv
  93. foreach($inputs as $valueType =>$value) {
  94. echo "\n--$valueType--\n";
  95. var_dump( bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $value)));
  96. };
  97. fclose($fp);
  98. ?>
  99. ===DONE===
  100. --EXPECTF--
  101. *** Testing mcrypt_encrypt() : usage variation ***
  102. --int 0--
  103. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  104. string(0) ""
  105. --int 1--
  106. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  107. string(0) ""
  108. --int 12345--
  109. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  110. string(0) ""
  111. --int -12345--
  112. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  113. string(0) ""
  114. --float 10.5--
  115. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  116. string(0) ""
  117. --float -10.5--
  118. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  119. string(0) ""
  120. --float 12.3456789000e10--
  121. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  122. string(0) ""
  123. --float -12.3456789000e10--
  124. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  125. string(0) ""
  126. --float .5--
  127. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  128. string(0) ""
  129. --empty array--
  130. Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d)
  131. string(0) ""
  132. --int indexed array--
  133. Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d)
  134. string(0) ""
  135. --associative array--
  136. Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d)
  137. string(0) ""
  138. --nested arrays--
  139. Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d)
  140. string(0) ""
  141. --uppercase NULL--
  142. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  143. string(0) ""
  144. --lowercase null--
  145. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  146. string(0) ""
  147. --lowercase true--
  148. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  149. string(0) ""
  150. --lowercase false--
  151. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  152. string(0) ""
  153. --uppercase TRUE--
  154. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  155. string(0) ""
  156. --uppercase FALSE--
  157. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  158. string(0) ""
  159. --empty string DQ--
  160. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  161. string(0) ""
  162. --empty string SQ--
  163. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  164. string(0) ""
  165. --instance of classWithToString--
  166. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  167. string(0) ""
  168. --instance of classWithoutToString--
  169. Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, object given, %s(%d)
  170. string(0) ""
  171. --undefined var--
  172. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  173. string(0) ""
  174. --unset var--
  175. Error: 2 - mcrypt_encrypt(): Received initialization vector of size %d, but size 8 is required for this encryption mode, %s(%d)
  176. string(0) ""
  177. --resource--
  178. Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, resource given, %s(%d)
  179. string(0) ""
  180. ===DONE===