ShaHmac256WithRsaSignatureTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Signature;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Signature\ShaHmac256WithRsaSignature;
  6. use AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials\Ini\VirtualRsaKeyPairCredential;
  7. /**
  8. * Class ShaHmac256WithRsaSignatureTest
  9. *
  10. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\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. 'W78DF0crfkZlhKE89dz9V5O03LkzfAZlhGltxdAYzJSL+z0YwgxNgmV15cEkqVONgSDSOqGzceqpBSSnIeoKVHny5TCe7OxBVuWu2nUsqN/PkQQqWDuuBsxqcDiSJ6lchQAqDR/P5BgwiSX2BnRAFQeAiX27lqVPpkYRi2U2ejksSw4e+RfNAPIKBftrpjnVHtFSbwiLsi0kfV/3a1SdXcVf2yUoL3dOs9akp4wQuALWuiBJ+5VNK3a4t4GQm3sfhEC+5tUQeQmdOPlHew8FjR/hXidYEUMHo6t/qNL7kciNy3Zk0iiVLLuhGMsu9tfotSJZKKpeD+4w9VYjv8qptQ==';
  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. /**
  43. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  44. * @expectedExceptionCode 0
  45. * @expectedExceptionMessage openssl_sign(): supplied key param cannot be coerced into a private key
  46. */
  47. public function testShaHmac256SignatureBadPrivateKey()
  48. {
  49. // Setup
  50. $string = 'string';
  51. $privateKeyFile = VirtualRsaKeyPairCredential::badPrivateKey();
  52. // Test
  53. $signature = new ShaHmac256WithRsaSignature();
  54. // Assert
  55. $signature->sign($string, \file_get_contents($privateKeyFile));
  56. }
  57. }