RsaKeyPairProviderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Credentials\Providers;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Tests\Mock\Mock;
  6. use AlibabaCloud\Client\Clients\RsaKeyPairClient;
  7. use AlibabaCloud\Client\Credentials\StsCredential;
  8. use AlibabaCloud\Client\Exception\ClientException;
  9. use AlibabaCloud\Client\Exception\ServerException;
  10. use AlibabaCloud\Client\Credentials\AccessKeyCredential;
  11. use AlibabaCloud\Client\Credentials\Providers\RsaKeyPairProvider;
  12. use AlibabaCloud\Client\Tests\Unit\Credentials\Ini\VirtualRsaKeyPairCredential;
  13. /**
  14. * Class RsaKeyPairProviderTest
  15. *
  16. * @package AlibabaCloud\Client\Tests\Unit\Credentials\Providers
  17. *
  18. * @coversDefaultClass \AlibabaCloud\Client\Credentials\Providers\RamRoleArnProvider
  19. */
  20. class RsaKeyPairProviderTest extends TestCase
  21. {
  22. /**
  23. * @before
  24. */
  25. protected function initialize()
  26. {
  27. parent::setUp();
  28. AlibabaCloud::cancelMock();
  29. }
  30. /**
  31. * @after
  32. */
  33. protected function finalize()
  34. {
  35. parent::tearDown();
  36. AlibabaCloud::cancelMock();
  37. }
  38. /**
  39. * @throws ClientException
  40. * @throws ServerException
  41. */
  42. public function testGet()
  43. {
  44. $this->expectException(ClientException::class);
  45. $reg = '/openssl_sign/';
  46. if (method_exists($this, 'expectExceptionMessageMatches')) {
  47. $this->expectExceptionMessageMatches($reg);
  48. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  49. $this->expectExceptionMessageRegExp($reg);
  50. }
  51. // Setup
  52. $client = new RsaKeyPairClient(
  53. 'foo',
  54. VirtualRsaKeyPairCredential::ok()
  55. );
  56. $provider = new RsaKeyPairProvider($client);
  57. // Test
  58. $actual = $provider->get();
  59. // Assert
  60. self::assertInstanceOf(AccessKeyCredential::class, $actual);
  61. }
  62. /**
  63. * @throws ClientException
  64. * @throws ServerException
  65. * @throws \ReflectionException
  66. */
  67. public function testGetInCache()
  68. {
  69. // Setup
  70. $client = new RsaKeyPairClient(
  71. 'foo',
  72. VirtualRsaKeyPairCredential::ok()
  73. );
  74. $provider = new RsaKeyPairProvider($client);
  75. // Test
  76. $cacheMethod = new \ReflectionMethod(
  77. RsaKeyPairProvider::class,
  78. 'cache'
  79. );
  80. $cacheMethod->setAccessible(true);
  81. $result = [
  82. 'Expiration' => '2049-10-01 11:11:11',
  83. 'SessionAccessKeyId' => 'foo',
  84. 'SessionAccessKeySecret' => 'bar',
  85. ];
  86. $cacheMethod->invokeArgs($provider, [$result]);
  87. $actual = $provider->get();
  88. // Assert
  89. self::assertInstanceOf(StsCredential::class, $actual);
  90. }
  91. /**
  92. * @throws ClientException
  93. * @throws ServerException
  94. */
  95. public function testNoCredentials()
  96. {
  97. $this->expectException(ServerException::class);
  98. $this->expectExceptionMessage("SDK.InvalidCredential: Result contains no credentials RequestId:");
  99. AlibabaCloud::mockResponse();
  100. $client = AlibabaCloud::rsaKeyPairClient(
  101. 'publicKeyId',
  102. VirtualRsaKeyPairCredential::privateKeyFileUrl()
  103. );
  104. $provider = new RsaKeyPairProvider($client);
  105. $provider->get();
  106. }
  107. /**
  108. * @throws ClientException
  109. * @throws ServerException
  110. */
  111. public function testSuccess()
  112. {
  113. AlibabaCloud::mockResponse(
  114. 200,
  115. [],
  116. '{
  117. "RequestId": "F702286E-F231-4F40-BB86-XXXXXX",
  118. "SessionAccessKey": {
  119. "SessionAccessKeyId": "TMPSK.**************",
  120. "Expiration": "2019-02-19T07:02:36.225Z",
  121. "SessionAccessKeySecret": "**************"
  122. }
  123. }'
  124. );
  125. $client = AlibabaCloud::rsaKeyPairClient(
  126. 'public_key_id',
  127. VirtualRsaKeyPairCredential::privateKeyFileUrl()
  128. );
  129. $provider = new RsaKeyPairProvider($client);
  130. $credential = $provider->get();
  131. self::assertInstanceOf(StsCredential::class, $credential);
  132. }
  133. }