ShaHmac256WithRsaSignatureTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Signature;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Signature\ShaHmac256WithRsaSignature;
  6. use AlibabaCloud\Client\Tests\Unit\Credentials\Ini\VirtualRsaKeyPairCredential;
  7. /**
  8. * Class ShaHmac256WithRsaSignatureTest
  9. *
  10. * @package AlibabaCloud\Client\Tests\Unit\Signature
  11. *
  12. * @coversDefaultClass \AlibabaCloud\Client\Signature\ShaHmac256WithRsaSignature
  13. */
  14. class ShaHmac256WithRsaSignatureTest extends TestCase
  15. {
  16. /**
  17. * @covers ::sign
  18. * @covers ::getMethod
  19. * @covers ::getVersion
  20. * @covers ::getType
  21. * @throws ClientException
  22. */
  23. public function testShaHmac256Signature()
  24. {
  25. // Setup
  26. $string = 'string';
  27. $privateKeyFile = VirtualRsaKeyPairCredential::privateKeyFileUrl();
  28. $expected =
  29. 'gjWgwRf/BjOYbjrPleU9qzNrZXNO+Z9aiwxmbBj1TPF2/PEOjBy5/YSk+GfL2GGg5pkupzrKiG+4FQ4r+EjeQcdByRDv1x1eBrQHwAbieKmjLc1++vJWQQpSKJykMl5dRzADUwsXYzvCCvVCIXjYZJNsrdt/0G+gaRVX7oelAX+d1MiTjRam7Ugzxcr1nELz2dc3DOyhXqCw8loNtsFVNcrDC5B/urx4eYdAFWRYVbORdTTgPdOF/gNJOWPqQgvFQsICJpScwIXP2OntCjYj8EBGNafBK3bCe4jxHwtxBA72PmuJ/ZHxUqSstwbcVk5S40PlRIhqtrfn6ajxYk41SQ==';
  30. // Test
  31. $signature = new ShaHmac256WithRsaSignature();
  32. // Assert
  33. static::assertInstanceOf(ShaHmac256WithRsaSignature::class, $signature);
  34. static::assertEquals('SHA256withRSA', $signature->getMethod());
  35. static::assertEquals('1.0', $signature->getVersion());
  36. static::assertEquals('PRIVATEKEY', $signature->getType());
  37. static::assertEquals(
  38. $expected,
  39. $signature->sign($string, \file_get_contents($privateKeyFile))
  40. );
  41. }
  42. public function testShaHmac256SignatureBadPrivateKey()
  43. {
  44. $this->expectException(ClientException::class);
  45. $this->expectExceptionCode(0);
  46. $reg = '/openssl_sign()/';
  47. if (method_exists($this, 'expectExceptionMessageMatches')) {
  48. $this->expectExceptionMessageMatches($reg);
  49. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  50. $this->expectExceptionMessageRegExp($reg);
  51. }
  52. // Setup
  53. $string = 'string';
  54. $privateKeyFile = VirtualRsaKeyPairCredential::badPrivateKey();
  55. // Test
  56. $signature = new ShaHmac256WithRsaSignature();
  57. // Assert
  58. $signature->sign($string, \file_get_contents($privateKeyFile));
  59. }
  60. }