You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

115 regels
2.5 KiB

  1. <?php
  2. /**
  3. * PhpUnderControl_PhalApiCryptMcrypt_Test
  4. *
  5. * 针对 ./../../PhalApi/Crypt/Mcrypt.php PhalApi_Crypt_Mcrypt 类的PHPUnit单元测试
  6. *
  7. * @author: dogstar 20141210
  8. */
  9. require_once dirname(__FILE__) . '/../test_env.php';
  10. if (!class_exists('PhalApi_Crypt_Mcrypt')) {
  11. require dirname(__FILE__) . '/../../PhalApi/Crypt/Mcrypt.php';
  12. }
  13. class PhpUnderControl_PhalApiCryptMcrypt_Test extends PHPUnit_Framework_TestCase
  14. {
  15. public $coreCryptMcrypt;
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. if (!function_exists('mcrypt_module_open')) {
  20. throw new Exception('function mcrypt_module_open() not exists');
  21. }
  22. $this->coreCryptMcrypt = new PhalApi_Crypt_Mcrypt();
  23. }
  24. protected function tearDown()
  25. {
  26. }
  27. /**
  28. * @group testEncrypt
  29. */
  30. public function testEncrypt()
  31. {
  32. $data = 'dogstar test哈哈 !!~~ ';
  33. $key = '2014';
  34. $rs = $this->coreCryptMcrypt->encrypt($data, $key);
  35. return array($data, $key, $rs);
  36. }
  37. /**
  38. * @depends testEncrypt
  39. * @group testDecrypt
  40. */
  41. public function testDecrypt($rsData)
  42. {
  43. list($data, $key, $encryptData) = $rsData;
  44. $rs = $this->coreCryptMcrypt->decrypt($encryptData, $key);
  45. $this->assertEquals($data, $rs);
  46. }
  47. /**
  48. * @dataProvider provideIv
  49. */
  50. public function testWithIV($iv)
  51. {
  52. $mcrypt = new PhalApi_Crypt_Mcrypt($iv);
  53. $data = 'dogstar';
  54. $key = 'phalapi';
  55. $this->assertEquals($mcrypt->decrypt($mcrypt->encrypt($data, $key), $key), $data);
  56. }
  57. public function provideIv()
  58. {
  59. return array(
  60. array(12),
  61. array('12'),
  62. array('12345678'),
  63. array('1234567890'),
  64. array('&632(jnD'),
  65. );
  66. }
  67. /**
  68. * @dataProvider provideComplicateData
  69. */
  70. public function testWorkWithMoreComplicateData($data)
  71. {
  72. $mcrypt = new PhalApi_Crypt_Mcrypt('12345678');
  73. $key = 'phalapi';
  74. $encryptData = $mcrypt->encrypt($data, $key);
  75. $decryptData = $mcrypt->decrypt($encryptData, $key);
  76. $this->assertEquals($data, $decryptData);
  77. }
  78. public function provideComplicateData()
  79. {
  80. return array(
  81. array(''),
  82. array(' '),
  83. array('0'),
  84. array(0),
  85. array(1),
  86. array('12#d_'),
  87. array(12345678),
  88. array('来点中文行不行?'),
  89. array('843435Jhe*&混合'),
  90. );
  91. }
  92. }