ProviderTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Credentials\Providers;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\Clients\Client;
  5. use AlibabaCloud\Client\Clients\StsClient;
  6. use AlibabaCloud\Client\Clients\AccessKeyClient;
  7. use AlibabaCloud\Client\Clients\EcsRamRoleClient;
  8. use AlibabaCloud\Client\Clients\RamRoleArnClient;
  9. use AlibabaCloud\Client\Clients\RsaKeyPairClient;
  10. use AlibabaCloud\Client\Clients\BearerTokenClient;
  11. use AlibabaCloud\Client\Exception\ClientException;
  12. use AlibabaCloud\Client\Signature\ShaHmac1Signature;
  13. use AlibabaCloud\Client\Credentials\Providers\Provider;
  14. use AlibabaCloud\Client\Credentials\AccessKeyCredential;
  15. use AlibabaCloud\Client\Tests\Unit\Credentials\Ini\VirtualRsaKeyPairCredential;
  16. /**
  17. * Class ProviderTest
  18. *
  19. * @package AlibabaCloud\Client\Tests\Unit\Credentials\Providers
  20. *
  21. * @coversDefaultClass \AlibabaCloud\Client\Credentials\Providers\EcsRamRoleProvider
  22. */
  23. class ProviderTest extends TestCase
  24. {
  25. /**
  26. * @param Client $client
  27. * @param string $key
  28. *
  29. * @throws \ReflectionException
  30. * @dataProvider key
  31. */
  32. public function testKey(Client $client, $key)
  33. {
  34. // Setup
  35. $provider = new Provider($client);
  36. // Test
  37. $method = new \ReflectionMethod(
  38. Provider::class,
  39. 'key'
  40. );
  41. $method->setAccessible(true);
  42. $actual = $method->invoke($provider);
  43. // Assert
  44. self::assertEquals($key, $actual);
  45. }
  46. /**
  47. * @return array
  48. * @throws ClientException
  49. */
  50. public function key()
  51. {
  52. return [
  53. [
  54. new AccessKeyClient('foo', 'bar'),
  55. 'foo#bar',
  56. ],
  57. [
  58. new BearerTokenClient('token'),
  59. 'bearerToken#token',
  60. ],
  61. [
  62. new Client(
  63. new AccessKeyCredential('foo', 'bar'),
  64. new ShaHmac1Signature()
  65. ),
  66. 'foo#bar',
  67. ],
  68. [
  69. new EcsRamRoleClient('name'),
  70. 'roleName#name',
  71. ],
  72. [
  73. new RamRoleArnClient('foo', 'bar', 'arn', 'name'),
  74. 'foo#bar#arn#name',
  75. ],
  76. [
  77. new RsaKeyPairClient('foo', VirtualRsaKeyPairCredential::ok()),
  78. 'publicKeyId#foo',
  79. ],
  80. [
  81. new StsClient('foo', 'bar', 'token'),
  82. 'foo#bar#token',
  83. ],
  84. ];
  85. }
  86. /**
  87. * @param Client $client
  88. * @param array $result
  89. *
  90. * @throws \ReflectionException
  91. * @dataProvider cache
  92. */
  93. public function testCache(Client $client, array $result)
  94. {
  95. // Setup
  96. $provider = new Provider($client);
  97. // Test
  98. $cacheMethod = new \ReflectionMethod(
  99. Provider::class,
  100. 'cache'
  101. );
  102. $cacheMethod->setAccessible(true);
  103. $cacheMethod->invokeArgs($provider, [$result]);
  104. $keyMethod = new \ReflectionMethod(
  105. Provider::class,
  106. 'key'
  107. );
  108. $keyMethod->setAccessible(true);
  109. $key = $keyMethod->invoke($provider);
  110. $property = new \ReflectionProperty(
  111. Provider::class,
  112. 'credentialsCache'
  113. );
  114. $property->setAccessible(true);
  115. $actual = $property->getValue();
  116. // Assert
  117. self::assertEquals($result, $actual[$key]);
  118. }
  119. /**
  120. * @param Client $client
  121. * @param array $credential
  122. * @param array|null $result
  123. *
  124. * @throws \ReflectionException
  125. * @dataProvider cache
  126. */
  127. public function testGetCredentialsInCache(Client $client, array $credential, $result)
  128. {
  129. // Setup
  130. $provider = new Provider($client);
  131. // Test
  132. $cacheMethod = new \ReflectionMethod(
  133. Provider::class,
  134. 'cache'
  135. );
  136. $cacheMethod->setAccessible(true);
  137. $cacheMethod->invokeArgs($provider, [$credential]);
  138. $cacheResult = $provider->getCredentialsInCache();
  139. // Assert
  140. self::assertEquals($cacheResult, $result);
  141. }
  142. /**
  143. * @return array
  144. * @throws ClientException
  145. */
  146. public function cache()
  147. {
  148. return [
  149. [
  150. new AccessKeyClient('foo', 'bar'),
  151. [
  152. 'Expiration' => '2049-10-01 11:11:11',
  153. ],
  154. [
  155. 'Expiration' => '2049-10-01 11:11:11',
  156. ],
  157. ],
  158. [
  159. new BearerTokenClient('token'),
  160. [
  161. 'Expiration' => \date('Y-m-d H:i:s', \time() + 4),
  162. ],
  163. null,
  164. ],
  165. [
  166. new Client(
  167. new AccessKeyCredential('foo', 'bar'),
  168. new ShaHmac1Signature()
  169. ),
  170. [
  171. 'Expiration' => '2017-02-02 11:11:11',
  172. ],
  173. null,
  174. ],
  175. [
  176. new EcsRamRoleClient('name'),
  177. [
  178. 'Expiration' => '2017-02-02 11:11:11',
  179. ],
  180. null,
  181. ],
  182. [
  183. new RamRoleArnClient('foo', 'bar', 'arn', 'name'),
  184. [
  185. 'Expiration' => '2017-02-02 11:11:11',
  186. ],
  187. null,
  188. ],
  189. [
  190. new RsaKeyPairClient('foo', VirtualRsaKeyPairCredential::ok()),
  191. [
  192. 'Expiration' => '2017-02-02 11:11:11',
  193. ],
  194. null,
  195. ],
  196. [
  197. new StsClient('foo', 'bar', 'token'),
  198. [
  199. 'Expiration' => '2017-02-02 11:11:11',
  200. ],
  201. null,
  202. ],
  203. ];
  204. }
  205. }