mcrypt_encrypt_variation3.phpt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. $mode = MCRYPT_MODE_ECB;
  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 b"Class A object";
  38. }
  39. }
  40. class classWithoutToString
  41. {
  42. }
  43. // heredoc string
  44. $heredoc = b<<<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 data
  92. foreach($inputs as $valueType =>$value) {
  93. echo "\n--$valueType--\n";
  94. var_dump( bin2hex(mcrypt_encrypt($cipher, $key, $value, $mode, $iv) ));
  95. };
  96. fclose($fp);
  97. ?>
  98. ===DONE===
  99. --EXPECTF--
  100. *** Testing mcrypt_encrypt() : usage variation ***
  101. --int 0--
  102. string(16) "51dc9cd9179b718b"
  103. --int 1--
  104. string(16) "619c335f8c4f9cbf"
  105. --int 12345--
  106. string(16) "b1258d67ab73de00"
  107. --int -12345--
  108. string(16) "8eecf134443bd6b9"
  109. --float 10.5--
  110. string(16) "34b5750a793baff5"
  111. --float -10.5--
  112. string(16) "7a605f2aacc8a11d"
  113. --float 12.3456789000e10--
  114. string(32) "74a0d7026ae586f476d4b17808851e86"
  115. --float -12.3456789000e10--
  116. string(32) "bfb155997017986c01090afebd62c7ca"
  117. --float .5--
  118. string(16) "cc60ac201164b6c7"
  119. --empty array--
  120. Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, array given, %s(%d)
  121. string(0) ""
  122. --int indexed array--
  123. Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, array given, %s(%d)
  124. string(0) ""
  125. --associative array--
  126. Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, array given, %s(%d)
  127. string(0) ""
  128. --nested arrays--
  129. Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, array given, %s(%d)
  130. string(0) ""
  131. --uppercase NULL--
  132. string(16) "6ece228c41457539"
  133. --lowercase null--
  134. string(16) "6ece228c41457539"
  135. --lowercase true--
  136. string(16) "619c335f8c4f9cbf"
  137. --lowercase false--
  138. string(16) "6ece228c41457539"
  139. --uppercase TRUE--
  140. string(16) "619c335f8c4f9cbf"
  141. --uppercase FALSE--
  142. string(16) "6ece228c41457539"
  143. --empty string DQ--
  144. string(16) "6ece228c41457539"
  145. --empty string SQ--
  146. string(16) "6ece228c41457539"
  147. --instance of classWithToString--
  148. string(32) "749c3b4d16731d98370128754b7c930f"
  149. --instance of classWithoutToString--
  150. Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, object given, %s(%d)
  151. string(0) ""
  152. --undefined var--
  153. string(16) "6ece228c41457539"
  154. --unset var--
  155. string(16) "6ece228c41457539"
  156. --resource--
  157. Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, resource given, %s(%d)
  158. string(0) ""
  159. ===DONE===