IniCredentialFeatureTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Credentials;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Exception\ClientException;
  6. use AlibabaCloud\Client\Credentials\Ini\IniCredential;
  7. use AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials\Ini\VirtualAccessKeyCredential;
  8. use AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials\Ini\VirtualRamRoleArnCredential;
  9. use AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials\Ini\VirtualRsaKeyPairCredential;
  10. use AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials\Ini\VirtualEcsRamRoleCredential;
  11. use AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials\Ini\VirtualBearerTokenCredential;
  12. /**
  13. * Class IniCredentialFeatureTest
  14. *
  15. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Credentials
  16. *
  17. * @coversDefaultClass \AlibabaCloud\Client\Credentials\Ini\IniCredential
  18. */
  19. class IniCredentialFeatureTest extends TestCase
  20. {
  21. /**
  22. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  23. * @expectedExceptionMessage Format error: vfs://AlibabaCloud/credentials-2
  24. */
  25. public static function testBadFormat()
  26. {
  27. AlibabaCloud::load(VirtualAccessKeyCredential::badFormat());
  28. }
  29. /**
  30. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  31. * @expectedExceptionCode 0
  32. * @expectedExceptionMessageRegExp /Credential file is not readable: \w+/
  33. */
  34. public static function testLoadCredentialsFile()
  35. {
  36. AlibabaCloud::load('no/file');
  37. }
  38. /**
  39. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  40. * @expectedExceptionMessage Credential file is not readable: /root/AlibabaCloud/NoneFile
  41. */
  42. public static function testLoadDirectory()
  43. {
  44. AlibabaCloud::load('/root/AlibabaCloud/NoneFile');
  45. }
  46. /**
  47. * @throws ClientException
  48. */
  49. public static function testDisableClient()
  50. {
  51. AlibabaCloud::load(VirtualAccessKeyCredential::disable());
  52. self::assertFalse(AlibabaCloud::has('phpunit'));
  53. }
  54. /**
  55. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  56. * @expectedExceptionCode 0
  57. * @expectedExceptionMessage Missing required 'type' option for 'phpunit'
  58. */
  59. public static function testNoType()
  60. {
  61. AlibabaCloud::load(VirtualAccessKeyCredential::noType());
  62. }
  63. /**
  64. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  65. * @expectedExceptionCode 0
  66. * @expectedExceptionMessage Invalid type 'invalidType' for 'phpunit'
  67. */
  68. public static function testInvalidType()
  69. {
  70. AlibabaCloud::load(VirtualAccessKeyCredential::invalidType());
  71. }
  72. /**
  73. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  74. * @expectedExceptionCode 0
  75. * @expectedExceptionMessage Missing required 'access_key_id' option for 'phpunit'
  76. */
  77. public static function testNoKey()
  78. {
  79. AlibabaCloud::load(VirtualAccessKeyCredential::noKey());
  80. }
  81. /**
  82. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  83. * @expectedExceptionCode 0
  84. * @expectedExceptionMessage Missing required 'access_key_secret' option for 'phpunit'
  85. */
  86. public static function testNoSecret()
  87. {
  88. AlibabaCloud::load(VirtualAccessKeyCredential::noSecret());
  89. }
  90. /**
  91. * @throws ClientException
  92. */
  93. public function tearDown()
  94. {
  95. AlibabaCloud::del('phpunit');
  96. IniCredential::forgetLoadedCredentialsFile();
  97. }
  98. /**
  99. * @throws ClientException
  100. */
  101. public function testAccessKeyOk()
  102. {
  103. AlibabaCloud::load(VirtualAccessKeyCredential::ok());
  104. $client = AlibabaCloud::get('ok');
  105. self::assertEquals('foo', $client->getCredential()->getAccessKeyId());
  106. self::assertEquals('bar', $client->getCredential()->getAccessKeySecret());
  107. self::assertEquals(0.2, $client->options['timeout']);
  108. self::assertEquals(0.03, $client->options['connect_timeout']);
  109. self::assertEquals(true, $client->options['debug']);
  110. self::assertEquals('tcp://localhost:8125', $client->options['proxy']['http']);
  111. self::assertEquals('tcp://localhost:9124', $client->options['proxy']['https']);
  112. self::assertEquals(['.mit.edu', 'foo.com'], $client->options['proxy']['no']);
  113. self::assertEquals(['/path/server.pem', 'password'], $client->options['cert']);
  114. self::assertEquals('cn-hangzhou', $client->regionId);
  115. }
  116. /**
  117. * @throws ClientException
  118. */
  119. public function testEcsRamRoleClient()
  120. {
  121. AlibabaCloud::load(VirtualEcsRamRoleCredential::client());
  122. self::assertTrue(AlibabaCloud::has('phpunit'));
  123. }
  124. /**
  125. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  126. * @expectedExceptionCode 0
  127. * @expectedExceptionMessage Missing required 'role_name' option for 'phpunit'
  128. */
  129. public function testEcsRamRoleClientNoRoleName()
  130. {
  131. AlibabaCloud::load(VirtualEcsRamRoleCredential::noRoleName());
  132. }
  133. /**
  134. * @throws ClientException
  135. */
  136. public function testRamRoleArnClient()
  137. {
  138. AlibabaCloud::load(VirtualRamRoleArnCredential::client());
  139. self::assertTrue(AlibabaCloud::has('phpunit'));
  140. }
  141. /**
  142. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  143. * @expectedExceptionCode 0
  144. * @expectedExceptionMessage Missing required 'access_key_id' option for 'phpunit'
  145. */
  146. public function testRamRoleArnClientNoKey()
  147. {
  148. AlibabaCloud::load(VirtualRamRoleArnCredential::noKey());
  149. }
  150. /**
  151. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  152. * @expectedExceptionCode 0
  153. * @expectedExceptionMessage Missing required 'access_key_secret' option for 'phpunit'
  154. */
  155. public function testRamRoleArnClientNoSecret()
  156. {
  157. AlibabaCloud::load(VirtualRamRoleArnCredential::noSecret());
  158. }
  159. /**
  160. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  161. * @expectedExceptionCode 0
  162. * @expectedExceptionMessage Missing required 'role_arn' option for 'phpunit'
  163. */
  164. public function testRamRoleArnClientNoRoleArn()
  165. {
  166. AlibabaCloud::load(VirtualRamRoleArnCredential::noRoleArn());
  167. }
  168. /**
  169. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  170. * @expectedExceptionCode 0
  171. * @expectedExceptionMessage Missing required 'role_session_name' option for 'phpunit'
  172. */
  173. public function testRamRoleArnClientNoRoleSessionName()
  174. {
  175. AlibabaCloud::load(VirtualRamRoleArnCredential::noRoleSessionName());
  176. }
  177. /**
  178. * @throws ClientException
  179. */
  180. public function testBearerTokenClient()
  181. {
  182. AlibabaCloud::load(VirtualBearerTokenCredential::client());
  183. self::assertTrue(AlibabaCloud::has('phpunit'));
  184. }
  185. /**
  186. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  187. * @expectedExceptionCode 0
  188. * @expectedExceptionMessage Missing required 'bearer_token' option for 'phpunit'
  189. */
  190. public function testBearerTokenClientNoToken()
  191. {
  192. AlibabaCloud::load(VirtualBearerTokenCredential::noToken());
  193. }
  194. /**
  195. * @throws ClientException
  196. */
  197. public function testAkClientWithAttributes()
  198. {
  199. AlibabaCloud::load(VirtualAccessKeyCredential::akClientWithAttributes('akClientWithAttributes'));
  200. AlibabaCloud::load(VirtualAccessKeyCredential::akClientWithAttributesNoCertPassword('NoCertPassword'));
  201. static::assertTrue(AlibabaCloud::has('akClientWithAttributes'));
  202. static::assertFalse(AlibabaCloud::has('NoCertPassword'));
  203. }
  204. /**
  205. * @throws ClientException
  206. */
  207. public function testAkClientWithAttributesNoCertPassword()
  208. {
  209. AlibabaCloud::load(VirtualAccessKeyCredential::akClientWithAttributesNoCertPassword('NoCertPassword'));
  210. static::assertTrue(AlibabaCloud::has('NoCertPassword'));
  211. }
  212. /**
  213. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  214. * @expectedExceptionCode 0
  215. * @expectedExceptionMessage Missing required 'public_key_id' option for 'phpunit'
  216. */
  217. public function testNoPublicKeyId()
  218. {
  219. AlibabaCloud::load(VirtualRsaKeyPairCredential::noPublicKeyId());
  220. }
  221. /**
  222. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  223. * @expectedExceptionCode 0
  224. * @expectedExceptionMessage Missing required 'private_key_file' option for 'phpunit'
  225. */
  226. public function testNoPrivateKeyFile()
  227. {
  228. AlibabaCloud::load(VirtualRsaKeyPairCredential::noPrivateKeyFile());
  229. }
  230. /**
  231. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  232. * @expectedExceptionCode 0
  233. * @expectedExceptionMessage file_get_contents(/bad/path.pem): failed to open stream: No such file or
  234. * directory
  235. */
  236. public function testBadPrivateKeyFilePath()
  237. {
  238. AlibabaCloud::load(VirtualRsaKeyPairCredential::badPrivateKeyFilePath());
  239. }
  240. }