SignerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Omnipay\Alipay\Tests\Common;
  3. use Omnipay\Alipay\Common\Signer;
  4. use PHPUnit\Framework\TestCase;
  5. class SignerTest extends TestCase
  6. {
  7. protected $params;
  8. protected $key;
  9. protected $privateKey;
  10. public function testSignWithMD5()
  11. {
  12. $signer = new Signer($this->params);
  13. $sign = $signer->signWithMD5($this->key);
  14. $this->assertEquals('7e63e20bcc6ad2ba695305e340592ffd', $sign);
  15. }
  16. public function testSignWithRSA()
  17. {
  18. $this->assertFileExists($this->privateKey);
  19. $signer = new Signer($this->params);
  20. $sign = $signer->signWithRSA($this->privateKey);
  21. $this->assertEquals(
  22. 'Uwz+4a4mKJBNDsGpC+nIZnHYsA30NpoxycIjvlAKC9sLS2t3a/5H7p7EEwSOAQQV2sAu54oIH7wZ4hXXkzYoqRO1T+51hYF+r+uEb9rGYyJzbg3xzV8WFUjypGgxNd8HCAKV9qhkEGdfZ94/VCxYkS+1qxkgqD0MzzHVR20C0NI=',
  23. $sign
  24. );
  25. }
  26. public function testSort()
  27. {
  28. $params1 = [
  29. 'aaa' => '111',
  30. 'bbb' => '2222',
  31. 'ccc' => '3333',
  32. ];
  33. $params2 = [
  34. 'bbb' => '2222',
  35. 'ccc' => '3333',
  36. 'aaa' => '111',
  37. ];
  38. $signer = new Signer($params1);
  39. $sign1 = $signer->signWithMD5($this->key);
  40. $signer = new Signer($params2);
  41. $sign2 = $signer->signWithMD5($this->key);
  42. $this->assertEquals($sign1, $sign2);
  43. }
  44. public function testIgnore()
  45. {
  46. $this->assertSame(['sign', 'sign_type'], (new Signer())->getIgnores());
  47. $params1 = [
  48. 'aaa' => '111',
  49. 'bbb' => '2222',
  50. 'ccc' => '3333',
  51. 'apple' => 'jobs',
  52. ];
  53. $params2 = [
  54. 'bbb' => '2222',
  55. 'ccc' => '3333',
  56. 'aaa' => '111',
  57. ];
  58. $signer = new Signer($params1);
  59. $signer->setIgnores(['apple']);
  60. $sign1 = $signer->signWithMD5($this->key);
  61. $signer = new Signer($params2);
  62. $signer->setIgnores(['apple']);
  63. $sign2 = $signer->signWithMD5($this->key);
  64. $this->assertEquals($sign1, $sign2);
  65. $signer = new Signer($params1);
  66. $signer->setIgnores([]);
  67. $sign3 = $signer->signWithMD5($this->key);
  68. $this->assertNotEquals($sign1, $sign3);
  69. }
  70. public function testGetParamsToSign()
  71. {
  72. $params1 = [
  73. 'bbb' => '2222',
  74. 'ccc' => '3333',
  75. 'aaa' => '111',
  76. 'apple' => 'jobs',
  77. ];
  78. $signer = new Signer($params1);
  79. $signer->setIgnores(['apple']);
  80. $params = $signer->getParamsToSign();
  81. $this->assertSame(
  82. [
  83. 'aaa' => '111',
  84. 'bbb' => '2222',
  85. 'ccc' => '3333',
  86. ],
  87. $params
  88. );
  89. }
  90. public function testGetContentToSign()
  91. {
  92. $params1 = [
  93. 'bbb' => '2222',
  94. 'ccc' => '3333',
  95. 'aaa' => '111',
  96. 's' => '"."',
  97. 'e' => '',
  98. 'apple' => 'jobs',
  99. ];
  100. $signer = new Signer($params1);
  101. $signer->setIgnores(['apple']);
  102. $content = $signer->getContentToSign();
  103. $this->assertEquals('aaa=111&bbb=2222&ccc=3333&s="."', $content);
  104. }
  105. public function testConvert()
  106. {
  107. $key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB';
  108. $signer = new Signer();
  109. $key = $signer->convertKey($key, Signer::KEY_TYPE_PUBLIC);
  110. $this->assertEquals(
  111. '-----BEGIN PUBLIC KEY-----
  112. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRA
  113. FljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQE
  114. B/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5Ksi
  115. NG9zpgmLCUYuLkxpLQIDAQAB
  116. -----END PUBLIC KEY-----',
  117. $key
  118. );
  119. }
  120. protected function setUp()
  121. {
  122. parent::setUp();
  123. $this->params = [
  124. 'aaa' => '111',
  125. 'bbb' => '2222',
  126. 'ccc' => '3333',
  127. 'dd' => '',
  128. 'eee' => null,
  129. 'fff' => false,
  130. 'ggg' => true,
  131. ];
  132. $this->key = 'hello';
  133. $this->privateKey = ALIPAY_ASSET_DIR . '/dist/aop/rsa_private_key.pem';
  134. }
  135. }