IniCredentialTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Credentials\Ini;
  3. use ReflectionClass;
  4. use ReflectionMethod;
  5. use ReflectionException;
  6. use PHPUnit\Framework\TestCase;
  7. use AlibabaCloud\Client\Exception\ClientException;
  8. use AlibabaCloud\Client\Credentials\Ini\IniCredential;
  9. /**
  10. * Class IniCredentialTest
  11. *
  12. * @package AlibabaCloud\Client\Tests\Unit\Credentials\Ini
  13. *
  14. * @coversDefaultClass \AlibabaCloud\Client\Credentials\Ini\IniCredential
  15. */
  16. class IniCredentialTest extends TestCase
  17. {
  18. /**
  19. * @return array
  20. */
  21. public static function isNotEmpty()
  22. {
  23. return [
  24. [
  25. [],
  26. 'key',
  27. false,
  28. ],
  29. [
  30. [
  31. 'key' => '',
  32. ],
  33. 'key',
  34. false,
  35. ],
  36. [
  37. [
  38. 'key' => 'false',
  39. ],
  40. 'key',
  41. true,
  42. ],
  43. [
  44. [
  45. 'key' => true,
  46. ],
  47. 'key',
  48. true,
  49. ],
  50. [
  51. [
  52. 'key' => 'value',
  53. ],
  54. 'key',
  55. true,
  56. ],
  57. ];
  58. }
  59. /**
  60. * @throws ClientException
  61. */
  62. public static function testInOpenBaseDir()
  63. {
  64. if (!\AlibabaCloud\Client\isWindows()) {
  65. $dirs = 'vfs://AlibabaCloud:/home:/Users:/private:/a/b:/d';
  66. ini_set('open_basedir', $dirs);
  67. self::assertEquals($dirs, ini_get('open_basedir'));
  68. self::assertTrue(\AlibabaCloud\Client\inOpenBasedir('/Users/alibabacloud'));
  69. self::assertTrue(\AlibabaCloud\Client\inOpenBasedir('/private/alibabacloud'));
  70. self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/no/permission'));
  71. self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/a'));
  72. self::assertTrue(\AlibabaCloud\Client\inOpenBasedir('/a/b/'));
  73. self::assertTrue(\AlibabaCloud\Client\inOpenBasedir('/a/b/c'));
  74. self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/b'));
  75. self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/b/'));
  76. self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/x/d/c.txt'));
  77. self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/a/b.php'));
  78. }
  79. if (\AlibabaCloud\Client\isWindows()) {
  80. $dirs = 'C:\\projects;C:\\Users';
  81. ini_set('open_basedir', $dirs);
  82. self::assertEquals($dirs, ini_get('open_basedir'));
  83. }
  84. }
  85. /**
  86. * Independent method, the file must exist.
  87. *
  88. * @return array
  89. */
  90. public static function parseFile()
  91. {
  92. return [
  93. [
  94. VirtualAccessKeyCredential::ok(),
  95. 'Format error: vfs://AlibabaCloud/credentials-1',
  96. ],
  97. ];
  98. }
  99. /**
  100. * @covers ::__construct
  101. * @covers ::getDefaultFile
  102. * @covers ::getFilename
  103. * @dataProvider getFilename
  104. *
  105. * @param string $fileName
  106. * @param string $getFileName
  107. */
  108. public function testGetFilename($fileName, $getFileName)
  109. {
  110. $object = new IniCredential($fileName);
  111. self::assertEquals($getFileName, $object->getFilename());
  112. }
  113. /**
  114. * @return array
  115. */
  116. public function getFilename()
  117. {
  118. return [
  119. [
  120. '',
  121. (new IniCredential())->getDefaultFile(),
  122. ],
  123. [
  124. '/no/no/no',
  125. '/no/no/no',
  126. ],
  127. ];
  128. }
  129. /**
  130. * @dataProvider isNotEmpty
  131. *
  132. * @param array $array
  133. * @param string $key
  134. * @param bool $bool
  135. *
  136. * @throws ReflectionException
  137. */
  138. public function testIsNotEmpty($array, $key, $bool)
  139. {
  140. $object = new IniCredential();
  141. $ref = new ReflectionClass(
  142. IniCredential::class
  143. );
  144. $method = $ref->getMethod('isNotEmpty');
  145. $method->setAccessible(true);
  146. $result = $method->invokeArgs($object, [$array, $key]);
  147. self::assertEquals($result, $bool);
  148. }
  149. public function testMissingRequired()
  150. {
  151. $this->expectException(ClientException::class);
  152. $reg = "/Missing required 'key' option for 'name'/";
  153. if (method_exists($this, 'expectExceptionMessageMatches')) {
  154. $this->expectExceptionMessageMatches($reg);
  155. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  156. $this->expectExceptionMessageRegExp($reg);
  157. }
  158. $object = new IniCredential();
  159. $object->missingRequired('key', 'name');
  160. }
  161. /**
  162. * @throws ReflectionException
  163. */
  164. public function testForgetLoadedCredentialsFile()
  165. {
  166. $reflectedClass = new ReflectionClass(IniCredential::class);
  167. $reflectedProperty = $reflectedClass->getProperty('hasLoaded');
  168. $reflectedProperty->setAccessible(true);
  169. $reflectedProperty->setValue([
  170. 'FILE' => 'FILE',
  171. ]);
  172. self::assertArrayHasKey('FILE', $reflectedClass->getStaticProperties()['hasLoaded']);
  173. IniCredential::forgetLoadedCredentialsFile();
  174. self::assertEquals([], $reflectedClass->getStaticProperties()['hasLoaded']);
  175. }
  176. /**
  177. * @throws ClientException
  178. */
  179. public function testLoad()
  180. {
  181. $object = new IniCredential(VirtualAccessKeyCredential::ok());
  182. self::assertEquals($object->load(), $object->load());
  183. }
  184. /**
  185. * @dataProvider loadFile
  186. *
  187. * @param string $fileName
  188. *
  189. * @throws ReflectionException
  190. */
  191. public function testLoadFile($fileName)
  192. {
  193. $object = new IniCredential($fileName);
  194. $method = new ReflectionMethod(
  195. IniCredential::class,
  196. 'loadFile'
  197. );
  198. $method->setAccessible(true);
  199. try {
  200. $result = $method->invoke($object);
  201. if (method_exists($this, 'assertIsArray')) {
  202. self::assertIsArray($result);
  203. } elseif (method_exists($this, 'assertInternalType')) {
  204. self::assertInternalType('array', $result);
  205. }
  206. } catch (ClientException $exception) {
  207. self::assertEquals(
  208. $exception->getErrorMessage(),
  209. 'Credential file is not readable: /no/no.no'
  210. );
  211. }
  212. }
  213. /**
  214. * @return array
  215. */
  216. public function loadFile()
  217. {
  218. return [
  219. [''],
  220. ['/no/no.no'],
  221. ];
  222. }
  223. /**
  224. * @param string $fileName
  225. * @param string $exceptionMessage
  226. *
  227. * @throws ReflectionException
  228. * @dataProvider parseFile
  229. */
  230. public function testParseFile($fileName, $exceptionMessage)
  231. {
  232. $object = new IniCredential($fileName);
  233. $method = new ReflectionMethod(
  234. IniCredential::class,
  235. 'parseFile'
  236. );
  237. $method->setAccessible(true);
  238. try {
  239. self::assertArrayHasKey('ok', $method->invoke($object));
  240. } catch (ClientException $exception) {
  241. self::assertEquals(
  242. $exception->getErrorMessage(),
  243. $exceptionMessage
  244. );
  245. }
  246. }
  247. /**
  248. * @throws ReflectionException
  249. * @dataProvider parseFile
  250. */
  251. public function testParseFileBadFormat()
  252. {
  253. $object = new IniCredential(VirtualAccessKeyCredential::badFormat());
  254. $method = new ReflectionMethod(
  255. IniCredential::class,
  256. 'parseFile'
  257. );
  258. $method->setAccessible(true);
  259. try {
  260. self::assertArrayHasKey('ok', $method->invoke($object));
  261. } catch (ClientException $exception) {
  262. self::assertEquals(
  263. $exception->getErrorMessage(),
  264. 'Format error: vfs://AlibabaCloud/credentials-1-badFormat'
  265. );
  266. }
  267. }
  268. /**
  269. * @throws ReflectionException
  270. * @covers ::getHomeDirectory
  271. * @depends testParseFile
  272. */
  273. public function testGetsHomeDirectoryForWindowsUser()
  274. {
  275. putenv('HOME=');
  276. putenv('HOMEDRIVE=C:');
  277. putenv('HOMEPATH=\\Users\\Alibaba');
  278. $ref = new ReflectionClass(IniCredential::class);
  279. $method = $ref->getMethod('getHomeDirectory');
  280. $method->setAccessible(true);
  281. $this->assertEquals('C:\\Users\\Alibaba', $method->invoke(null));
  282. }
  283. /**
  284. * @throws ReflectionException
  285. * @covers ::getHomeDirectory
  286. * @depends testGetsHomeDirectoryForWindowsUser
  287. */
  288. public function testGetsHomeDirectoryForLinuxUser()
  289. {
  290. putenv('HOME=/root');
  291. putenv('HOMEDRIVE=');
  292. putenv('HOMEPATH=');
  293. $ref = new ReflectionClass(IniCredential::class);
  294. $method = $ref->getMethod('getHomeDirectory');
  295. $method->setAccessible(true);
  296. $this->assertEquals('/root', $method->invoke(null));
  297. }
  298. }