mcrypt_encrypt_variation4.phpt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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';
  28. $data = b'string_val';
  29. $iv = b'01234567';
  30. //get an unset variable
  31. $unset_var = 10;
  32. unset ($unset_var);
  33. // define some classes
  34. class classWithToString
  35. {
  36. public function __toString() {
  37. return "Class A object";
  38. }
  39. }
  40. class classWithoutToString
  41. {
  42. }
  43. // heredoc string
  44. $heredoc = <<<EOT
  45. hello world
  46. EOT;
  47. // get a resource variable
  48. $fp = fopen(__FILE__, "r");
  49. // add arrays
  50. $index_array = array (1, 2, 3);
  51. $assoc_array = array ('one' => 1, 'two' => 2);
  52. //array of values to iterate over
  53. $inputs = array(
  54. // int data
  55. 'int 0' => 0,
  56. 'int 1' => 1,
  57. 'int 12345' => 12345,
  58. 'int -12345' => -2345,
  59. // float data
  60. 'float 10.5' => 10.5,
  61. 'float -10.5' => -10.5,
  62. 'float 12.3456789000e10' => 12.3456789000e10,
  63. 'float -12.3456789000e10' => -12.3456789000e10,
  64. 'float .5' => .5,
  65. // array data
  66. 'empty array' => array(),
  67. 'int indexed array' => $index_array,
  68. 'associative array' => $assoc_array,
  69. 'nested arrays' => array('foo', $index_array, $assoc_array),
  70. // null data
  71. 'uppercase NULL' => NULL,
  72. 'lowercase null' => null,
  73. // boolean data
  74. 'lowercase true' => true,
  75. 'lowercase false' =>false,
  76. 'uppercase TRUE' =>TRUE,
  77. 'uppercase FALSE' =>FALSE,
  78. // empty data
  79. 'empty string DQ' => "",
  80. 'empty string SQ' => '',
  81. // object data
  82. 'instance of classWithToString' => new classWithToString(),
  83. 'instance of classWithoutToString' => new classWithoutToString(),
  84. // undefined data
  85. 'undefined var' => @$undefined_var,
  86. // unset data
  87. 'unset var' => @$unset_var,
  88. // resource variable
  89. 'resource' => $fp
  90. );
  91. // loop through each element of the array for mode
  92. foreach($inputs as $valueType =>$value) {
  93. echo "\n--$valueType--\n";
  94. var_dump( mcrypt_encrypt($cipher, $key, $data, $value, $iv) );
  95. };
  96. fclose($fp);
  97. ?>
  98. ===DONE===
  99. --EXPECTF--
  100. *** Testing mcrypt_encrypt() : usage variation ***
  101. --int 0--
  102. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  103. bool(false)
  104. --int 1--
  105. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  106. bool(false)
  107. --int 12345--
  108. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  109. bool(false)
  110. --int -12345--
  111. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  112. bool(false)
  113. --float 10.5--
  114. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  115. bool(false)
  116. --float -10.5--
  117. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  118. bool(false)
  119. --float 12.3456789000e10--
  120. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  121. bool(false)
  122. --float -12.3456789000e10--
  123. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  124. bool(false)
  125. --float .5--
  126. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  127. bool(false)
  128. --empty array--
  129. Error: 2 - mcrypt_encrypt() expects parameter 4 to be string, array given, %s(%d)
  130. NULL
  131. --int indexed array--
  132. Error: 2 - mcrypt_encrypt() expects parameter 4 to be string, array given, %s(%d)
  133. NULL
  134. --associative array--
  135. Error: 2 - mcrypt_encrypt() expects parameter 4 to be string, array given, %s(%d)
  136. NULL
  137. --nested arrays--
  138. Error: 2 - mcrypt_encrypt() expects parameter 4 to be string, array given, %s(%d)
  139. NULL
  140. --uppercase NULL--
  141. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  142. bool(false)
  143. --lowercase null--
  144. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  145. bool(false)
  146. --lowercase true--
  147. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  148. bool(false)
  149. --lowercase false--
  150. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  151. bool(false)
  152. --uppercase TRUE--
  153. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  154. bool(false)
  155. --uppercase FALSE--
  156. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  157. bool(false)
  158. --empty string DQ--
  159. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  160. bool(false)
  161. --empty string SQ--
  162. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  163. bool(false)
  164. --instance of classWithToString--
  165. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  166. bool(false)
  167. --instance of classWithoutToString--
  168. Error: 2 - mcrypt_encrypt() expects parameter 4 to be string, object given, %s(%d)
  169. NULL
  170. --undefined var--
  171. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  172. bool(false)
  173. --unset var--
  174. Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d)
  175. bool(false)
  176. --resource--
  177. Error: 2 - mcrypt_encrypt() expects parameter 4 to be string, resource given, %s(%d)
  178. NULL
  179. ===DONE===