RsaKeyPairCredentialTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Feature\Credentials;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Exception\ClientException;
  6. use AlibabaCloud\Client\Exception\ServerException;
  7. use AlibabaCloud\Client\Credentials\StsCredential;
  8. use AlibabaCloud\Client\Tests\Mock\Services\Ecs\DescribeAccessPointsRequest;
  9. use AlibabaCloud\Client\Tests\Unit\Credentials\Ini\VirtualRsaKeyPairCredential;
  10. /**
  11. * Class RsaKeyPairCredentialTest
  12. *
  13. * @package AlibabaCloud\Client\Tests\Feature\Credentials
  14. */
  15. class RsaKeyPairCredentialTest extends TestCase
  16. {
  17. /**
  18. * @var string
  19. */
  20. private $clientName = 'RsaKeyPairCredentialTest';
  21. /**
  22. * @before
  23. * @throws ClientException
  24. */
  25. protected function initialize()
  26. {
  27. parent::setUp();
  28. $regionId = 'ap-northeast-1';
  29. $publicKeyId = \AlibabaCloud\Client\env('PUBLIC_KEY_ID');
  30. $privateKeyFile = VirtualRsaKeyPairCredential::privateKeyFileUrl();
  31. AlibabaCloud::rsaKeyPairClient($publicKeyId, $privateKeyFile)
  32. ->regionId($regionId)
  33. ->name($this->clientName);
  34. }
  35. /**
  36. * @after
  37. * @throws ClientException
  38. */
  39. protected function finalize()
  40. {
  41. parent::tearDown();
  42. AlibabaCloud::del($this->clientName);
  43. }
  44. /**
  45. * @throws ClientException
  46. * @throws ServerException
  47. */
  48. public function testGetSessionCredential()
  49. {
  50. $this->expectException(ServerException::class);
  51. $reg = '/InvalidAccessKeyId.NotMatchWithSignatureType/';
  52. if (method_exists($this, 'expectExceptionMessageMatches')) {
  53. $this->expectExceptionMessageMatches($reg);
  54. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  55. $this->expectExceptionMessageRegExp($reg);
  56. }
  57. $credential = AlibabaCloud::get($this->clientName)
  58. ->getSessionCredential(30, 25);
  59. // self::assertInstanceOf(StsCredential::class, $credential);
  60. }
  61. /**
  62. * @throws ClientException
  63. * @throws ServerException
  64. */
  65. public function testEcsInJapan()
  66. {
  67. $this->expectException(ServerException::class);
  68. $reg = '/InvalidAccessKeyId.NotMatchWithSignatureType/';
  69. if (method_exists($this, 'expectExceptionMessageMatches')) {
  70. $this->expectExceptionMessageMatches($reg);
  71. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  72. $this->expectExceptionMessageRegExp($reg);
  73. }
  74. $this->expectException(ServerException::class);
  75. $reg = '/InvalidAccessKeyId.NotMatchWithSignatureType/';
  76. if (method_exists($this, 'expectExceptionMessageMatches')) {
  77. $this->expectExceptionMessageMatches($reg);
  78. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  79. $this->expectExceptionMessageRegExp($reg);
  80. }
  81. $result = (new DescribeAccessPointsRequest())
  82. ->client($this->clientName)
  83. ->connectTimeout(20)
  84. ->timeout(25)
  85. ->request();
  86. // static::assertArrayHasKey('AccessPointSet', $result);
  87. }
  88. /**
  89. * @throws ClientException
  90. * @throws ServerException
  91. */
  92. public function testEcsNotInJapan()
  93. {
  94. $this->expectException(ServerException::class);
  95. $reg = '/InvalidAccessKeyId.NotMatchWithSignatureType/';
  96. if (method_exists($this, 'expectExceptionMessageMatches')) {
  97. $this->expectExceptionMessageMatches($reg);
  98. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  99. $this->expectExceptionMessageRegExp($reg);
  100. }
  101. // Setup
  102. $regionId = \AlibabaCloud\Client\env('REGION_ID', 'cn-hangzhou');
  103. // Test
  104. (new DescribeAccessPointsRequest())
  105. ->client($this->clientName)
  106. ->regionId($regionId)
  107. ->connectTimeout(25)
  108. ->timeout(30)
  109. ->request();
  110. }
  111. }