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.
 
 
 
 
 
 

111 lines
2.5 KiB

  1. <?php
  2. /**
  3. * PhpUnderControl_PhalApiMultiCryptMcrypt_Test
  4. *
  5. * 针对 ../../PhalApi/Crypt/MultiMcrypt.php PhalApi_Crypt_MultiMcrypt 类的PHPUnit单元测试
  6. *
  7. * @author: dogstar 20141211
  8. */
  9. require_once dirname(__FILE__) . '/../test_env.php';
  10. if (!class_exists('PhalApi_Crypt_MultiMcrypt')) {
  11. require dirname(__FILE__) . '/../../PhalApi/Crypt/MultiMcrypt.php';
  12. }
  13. class PhpUnderControl_PhalApiMultiCryptMcrypt_Test extends PHPUnit_Framework_TestCase
  14. {
  15. public $coreMultiCryptMcrypt;
  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->coreMultiCryptMcrypt = new PhalApi_Crypt_MultiMcrypt('12345678');
  23. }
  24. protected function tearDown()
  25. {
  26. }
  27. /**
  28. * @group testEncrypt
  29. */
  30. public function testEncrypt()
  31. {
  32. $data = 'haha~';
  33. $key = '123';
  34. $rs = $this->coreMultiCryptMcrypt->encrypt($data, $key);
  35. }
  36. /**
  37. * @group testDecrypt
  38. */
  39. public function testDecrypt()
  40. {
  41. $data = 'haha~';
  42. $key = '123';
  43. $rs = $this->coreMultiCryptMcrypt->decrypt($data, $key);
  44. }
  45. public function testMixed()
  46. {
  47. $data = 'haha!哈哈!';
  48. $key = md5('123');
  49. $encryptData = $this->coreMultiCryptMcrypt->encrypt($data, $key);
  50. $decryptData = $this->coreMultiCryptMcrypt->decrypt($encryptData, $key);
  51. $this->assertEquals($data, $decryptData);
  52. }
  53. /**
  54. * @dataProvider provideComplicateData
  55. */
  56. public function testWorkWithMoreComplicateData($data)
  57. {
  58. $key = 'phalapi';
  59. $encryptData = $this->coreMultiCryptMcrypt->encrypt($data, $key);
  60. $decryptData = $this->coreMultiCryptMcrypt->decrypt($encryptData, $key);
  61. $this->assertSame($data, $decryptData);
  62. }
  63. public function provideComplicateData()
  64. {
  65. return array(
  66. array(''),
  67. array(' '),
  68. array('0'),
  69. array(0),
  70. array(1),
  71. array('12#d_'),
  72. array(12345678),
  73. array('来点中文行不行?'),
  74. array('843435Jhe*&混合'),
  75. );
  76. }
  77. /**
  78. * 当无法对称解密时,返回原数据
  79. */
  80. public function testIllegalData()
  81. {
  82. $encryptData = '';
  83. $decryptData = $this->coreMultiCryptMcrypt->decrypt($encryptData, 'whatever');
  84. $this->assertEquals($encryptData, $decryptData);
  85. }
  86. }