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.
 
 
 
 
 
 

133 lines
3.5 KiB

  1. <?php
  2. /**
  3. * PhpUnderControl_PhalApiCryptRSAMultiPri2Pub_Test
  4. *
  5. * 针对 ../../../PhalApi/Crypt/RSA/MultiPri2Pub.php PhalApi_Crypt_RSA_MultiPri2Pub 类的PHPUnit单元测试
  6. *
  7. * @author: dogstar 20150314
  8. */
  9. require_once dirname(__FILE__) . '/../../test_env.php';
  10. if (!class_exists('PhalApi_Crypt_RSA_MultiPri2Pub')) {
  11. require dirname(__FILE__) . '/../../../PhalApi/Crypt/RSA/MultiPri2Pub.php';
  12. }
  13. class PhpUnderControl_PhalApiCryptRSAMultiPri2Pub_Test extends PHPUnit_Framework_TestCase
  14. {
  15. public $phalApiCryptRSAMultiPri2Pub;
  16. public $privkey;
  17. public $pubkey;
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. /**
  22. $res = openssl_pkey_new();
  23. openssl_pkey_export($res, $privkey);
  24. $this->privkey = $privkey;
  25. $pubkey = openssl_pkey_get_details($res);
  26. $this->pubkey = $pubkey["key"];
  27. */
  28. $keyG = new PhalApi_Crypt_RSA_KeyGenerator();
  29. $this->privkey = $keyG->getPriKey();
  30. $this->pubkey = $keyG->getPubKey();
  31. $this->phalApiCryptRSAMultiPri2Pub = new PhalApi_Crypt_RSA_MultiPri2Pub();
  32. }
  33. protected function tearDown()
  34. {
  35. }
  36. /**
  37. * @group testEncrypt
  38. */
  39. public function testEncrypt()
  40. {
  41. $data = 'something important here ...';
  42. $key = $this->privkey;
  43. $rs = $this->phalApiCryptRSAMultiPri2Pub->encrypt($data, $key);
  44. $this->assertNotEmpty($rs);
  45. return $rs;
  46. }
  47. /**
  48. * @group testDecrypt
  49. */
  50. public function testDecrypt()
  51. {
  52. //we need to encrypt the data again, since pubkey is different every time
  53. $data = $this->phalApiCryptRSAMultiPri2Pub->encrypt('something important here ...', $this->privkey);
  54. $key = $this->pubkey;
  55. $rs = $this->phalApiCryptRSAMultiPri2Pub->decrypt($data, $key);
  56. $this->assertEquals('something important here ...', $rs);
  57. }
  58. /**
  59. * demo
  60. */
  61. public function testDecryptAfterEncrypt()
  62. {
  63. $keyG = new PhalApi_Crypt_RSA_KeyGenerator();
  64. $privkey = $keyG->getPriKey();
  65. $pubkey = $keyG->getPubKey();
  66. DI()->crypt = new PhalApi_Crypt_RSA_MultiPri2Pub();
  67. $data = 'AHA! I have $2.22 dollars!';
  68. $encryptData = DI()->crypt->encrypt($data, $privkey);
  69. $decryptData = DI()->crypt->decrypt($encryptData, $pubkey);
  70. $this->assertEquals($data, $decryptData);
  71. }
  72. /**
  73. * @dataProvider provideComplicateData
  74. */
  75. public function testWorkWithMoreComplicateData($data)
  76. {
  77. $encryptData = $this->phalApiCryptRSAMultiPri2Pub->encrypt($data, $this->privkey);
  78. $decryptData = $this->phalApiCryptRSAMultiPri2Pub->decrypt($encryptData, $this->pubkey);
  79. $this->assertNotNull($decryptData);
  80. $this->assertEquals($data, $decryptData);
  81. $wrongDecryptData = $this->phalApiCryptRSAMultiPri2Pub->decrypt($encryptData, 'whatever');
  82. $this->assertNotSame($data, $wrongDecryptData);
  83. }
  84. public function provideComplicateData()
  85. {
  86. return array(
  87. array(''),
  88. array(' '),
  89. array('0'),
  90. array(0),
  91. array(1),
  92. array('12#d_'),
  93. array(12345678),
  94. array('来点中文行不行?'),
  95. array('843435Jhe*&混合'),
  96. array(json_encode(array('name' => 'dogstar', 'ext' => '来点中文行不行?'))),
  97. array('something important here ...'),
  98. array(str_repeat('something long long here ...', 130)),
  99. );
  100. }
  101. }