RsaKeyPairCredentialTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Credentials\RsaKeyPairCredential;
  6. use AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials\Ini\VirtualRsaKeyPairCredential;
  7. /**
  8. * Class RsaKeyPairCredentialTest
  9. *
  10. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials
  11. *
  12. * @coversDefaultClass \AlibabaCloud\Client\Credentials\RsaKeyPairCredential
  13. */
  14. class RsaKeyPairCredentialTest extends TestCase
  15. {
  16. public static function testNotFoundFile()
  17. {
  18. // Setup
  19. $publicKeyId = 'PUBLIC_KEY_ID';
  20. if (\AlibabaCloud\Client\isWindows()) {
  21. $privateKeyFile = 'C:\\projects\\no.no';
  22. } else {
  23. $privateKeyFile = '/a/b/no.no';
  24. }
  25. // Test
  26. try {
  27. new RsaKeyPairCredential($publicKeyId, $privateKeyFile);
  28. } catch (ClientException $e) {
  29. self::assertEquals(
  30. "file_get_contents($privateKeyFile): failed to open stream: No such file or directory",
  31. $e->getErrorMessage()
  32. );
  33. }
  34. }
  35. public static function testOpenBasedirException()
  36. {
  37. // Setup
  38. $publicKeyId = 'PUBLIC_KEY_ID';
  39. if (\AlibabaCloud\Client\isWindows()) {
  40. $dirs = 'C:\\projects;C:\\Users';
  41. $privateKeyFile = 'C:\\AlibabaCloud\\no.no';
  42. } else {
  43. $dirs = 'vfs://AlibabaCloud:/home:/Users:/private:/a/b';
  44. $privateKeyFile = '/dev/no.no';
  45. }
  46. // Test
  47. ini_set('open_basedir', $dirs);
  48. try {
  49. new RsaKeyPairCredential($publicKeyId, $privateKeyFile);
  50. } catch (ClientException $e) {
  51. self::assertEquals(
  52. "file_get_contents(): open_basedir restriction in effect. File($privateKeyFile) is not within the allowed path(s): ($dirs)",
  53. $e->getErrorMessage()
  54. );
  55. }
  56. }
  57. /**
  58. * @throws ClientException
  59. */
  60. public function testConstruct()
  61. {
  62. // Setup
  63. $publicKeyId = 'public_key_id';
  64. $privateKeyFile = VirtualRsaKeyPairCredential::privateKeyFileUrl();
  65. // Test
  66. $credential = new RsaKeyPairCredential($publicKeyId, $privateKeyFile);
  67. // Assert
  68. $this->assertEquals($publicKeyId, $credential->getPublicKeyId());
  69. $this->assertStringEqualsFile($privateKeyFile, $credential->getPrivateKey());
  70. $this->assertEquals(
  71. "publicKeyId#$publicKeyId",
  72. (string)$credential
  73. );
  74. }
  75. /**
  76. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  77. * @expectedExceptionMessage Public Key ID cannot be empty
  78. * @throws ClientException
  79. */
  80. public function testPublicKeyIdEmpty()
  81. {
  82. // Setup
  83. $publicKeyId = '';
  84. $privateKeyFile = VirtualRsaKeyPairCredential::privateKeyFileUrl();
  85. // Test
  86. new RsaKeyPairCredential($publicKeyId, $privateKeyFile);
  87. }
  88. /**
  89. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  90. * @expectedExceptionMessage Public Key ID must be a string
  91. * @throws ClientException
  92. */
  93. public function testPublicKeyIdFormat()
  94. {
  95. // Setup
  96. $publicKeyId = null;
  97. $privateKeyFile = VirtualRsaKeyPairCredential::privateKeyFileUrl();
  98. // Test
  99. new RsaKeyPairCredential($publicKeyId, $privateKeyFile);
  100. }
  101. /**
  102. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  103. * @expectedExceptionMessage Private Key File cannot be empty
  104. * @throws ClientException
  105. */
  106. public function testPrivateKeyFileEmpty()
  107. {
  108. // Setup
  109. $publicKeyId = 'publicKeyId';
  110. $privateKeyFile = '';
  111. // Test
  112. new RsaKeyPairCredential($publicKeyId, $privateKeyFile);
  113. }
  114. /**
  115. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  116. * @expectedExceptionMessage Private Key File must be a string
  117. * @throws ClientException
  118. */
  119. public function testPrivateKeyFileFormat()
  120. {
  121. // Setup
  122. $publicKeyId = 'publicKeyId';
  123. $privateKeyFile = null;
  124. // Test
  125. new RsaKeyPairCredential($publicKeyId, $privateKeyFile);
  126. }
  127. }