PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpseclib/phpseclib/tests/Functional/Net/SFTPUserStoryTest.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 478 lines | 326 code | 78 blank | 74 comment | 1 complexity | 3657fdc5c599b4a4035172c62ca5d651 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@phpbb.com>
  4. * @copyright 2014 Andreas Fischer
  5. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  6. */
  7. class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase
  8. {
  9. static protected $scratchDir;
  10. static protected $exampleData;
  11. static protected $exampleDataLength;
  12. static public function setUpBeforeClass()
  13. {
  14. parent::setUpBeforeClass();
  15. self::$scratchDir = uniqid('phpseclib-sftp-scratch-');
  16. self::$exampleData = str_repeat('abcde12345', 1000);
  17. self::$exampleDataLength = 10000;
  18. }
  19. public function testConstructor()
  20. {
  21. $sftp = new Net_SFTP($this->getEnv('SSH_HOSTNAME'));
  22. $this->assertTrue(
  23. is_object($sftp),
  24. 'Could not construct NET_SFTP object.'
  25. );
  26. return $sftp;
  27. }
  28. /**
  29. * @depends testConstructor
  30. */
  31. public function testPasswordLogin($sftp)
  32. {
  33. $username = $this->getEnv('SSH_USERNAME');
  34. $password = $this->getEnv('SSH_PASSWORD');
  35. $this->assertTrue(
  36. $sftp->login($username, $password),
  37. 'SSH2/SFTP login using password failed.'
  38. );
  39. return $sftp;
  40. }
  41. /**
  42. * @depends testPasswordLogin
  43. */
  44. public function testPwdHome($sftp)
  45. {
  46. $this->assertEquals(
  47. $this->getEnv('SSH_HOME'),
  48. $sftp->pwd(),
  49. 'Failed asserting that pwd() returns home directory after login.'
  50. );
  51. return $sftp;
  52. }
  53. /**
  54. * @depends testPwdHome
  55. */
  56. public function testMkDirScratch($sftp)
  57. {
  58. $dirname = self::$scratchDir;
  59. $this->assertTrue(
  60. $sftp->mkdir($dirname),
  61. "Failed asserting that a new scratch directory $dirname could " .
  62. 'be created.'
  63. );
  64. $this->assertFalse(
  65. $sftp->mkdir($dirname),
  66. "Failed asserting that a new scratch directory $dirname could " .
  67. 'not be created (because it already exists).'
  68. );
  69. return $sftp;
  70. }
  71. /**
  72. * @depends testMkDirScratch
  73. */
  74. public function testChDirScratch($sftp)
  75. {
  76. $this->assertTrue(
  77. $sftp->chdir(self::$scratchDir),
  78. sprintf(
  79. 'Failed asserting that working directory could be changed ' .
  80. 'to scratch directory %s.',
  81. self::$scratchDir
  82. )
  83. );
  84. $pwd = $sftp->pwd();
  85. $this->assertStringStartsWith(
  86. $this->getEnv('SSH_HOME'),
  87. $pwd,
  88. 'Failed asserting that the home directory is a prefix of the ' .
  89. 'current working directory.'
  90. );
  91. $this->assertStringEndsWith(
  92. self::$scratchDir,
  93. $pwd,
  94. 'Failed asserting that the scratch directory name is a suffix ' .
  95. 'of the current working directory.'
  96. );
  97. return $sftp;
  98. }
  99. /**
  100. * @depends testChDirScratch
  101. */
  102. public function testStatOnDir($sftp)
  103. {
  104. $this->assertNotSame(
  105. array(),
  106. $sftp->stat('.'),
  107. 'Failed asserting that the cwd has a non-empty stat.'
  108. );
  109. return $sftp;
  110. }
  111. /**
  112. * @depends testStatOnDir
  113. */
  114. public function testPutSizeGetFile($sftp)
  115. {
  116. $this->assertTrue(
  117. $sftp->put('file1.txt', self::$exampleData),
  118. 'Failed asserting that example data could be successfully put().'
  119. );
  120. $this->assertSame(
  121. self::$exampleDataLength,
  122. $sftp->size('file1.txt'),
  123. 'Failed asserting that put example data has the expected length'
  124. );
  125. $this->assertSame(
  126. self::$exampleData,
  127. $sftp->get('file1.txt'),
  128. 'Failed asserting that get() returns expected example data.'
  129. );
  130. return $sftp;
  131. }
  132. /**
  133. * @depends testPutSizeGetFile
  134. */
  135. public function testTouch($sftp)
  136. {
  137. $this->assertTrue(
  138. $sftp->touch('file2.txt'),
  139. 'Failed asserting that touch() successfully ran.'
  140. );
  141. $this->assertTrue(
  142. $sftp->file_exists('file2.txt'),
  143. 'Failed asserting that touch()\'d file exists'
  144. );
  145. return $sftp;
  146. }
  147. /**
  148. * @depends testTouch
  149. */
  150. public function testTruncate($sftp)
  151. {
  152. $this->assertTrue(
  153. $sftp->touch('file3.txt'),
  154. 'Failed asserting that touch() successfully ran.'
  155. );
  156. $this->assertTrue(
  157. $sftp->truncate('file3.txt', 1024 * 1024),
  158. 'Failed asserting that touch() successfully ran.'
  159. );
  160. $this->assertSame(
  161. 1024 * 1024,
  162. $sftp->size('file3.txt'),
  163. 'Failed asserting that truncate()\'d file has the expected length'
  164. );
  165. return $sftp;
  166. }
  167. /**
  168. * @depends testTruncate
  169. */
  170. public function testChDirOnFile($sftp)
  171. {
  172. $this->assertFalse(
  173. $sftp->chdir('file1.txt'),
  174. 'Failed to assert that the cwd cannot be changed to a file'
  175. );
  176. return $sftp;
  177. }
  178. /**
  179. * @depends testChDirOnFile
  180. */
  181. public function testFileExistsIsFileIsDirFile($sftp)
  182. {
  183. $this->assertTrue(
  184. $sftp->file_exists('file1.txt'),
  185. 'Failed asserting that file_exists() on example file returns true.'
  186. );
  187. $this->assertTrue(
  188. $sftp->is_file('file1.txt'),
  189. 'Failed asserting that is_file() on example file returns true.'
  190. );
  191. $this->assertFalse(
  192. $sftp->is_dir('file1.txt'),
  193. 'Failed asserting that is_dir() on example file returns false.'
  194. );
  195. return $sftp;
  196. }
  197. /**
  198. * @depends testFileExistsIsFileIsDirFile
  199. */
  200. public function testFileExistsIsFileIsDirFileNonexistent($sftp)
  201. {
  202. $this->assertFalse(
  203. $sftp->file_exists('file4.txt'),
  204. 'Failed asserting that a nonexistent file does not exist.'
  205. );
  206. $this->assertFalse(
  207. $sftp->is_file('file4.txt'),
  208. 'Failed asserting that is_file() on nonexistent file returns false.'
  209. );
  210. $this->assertFalse(
  211. $sftp->is_dir('file4.txt'),
  212. 'Failed asserting that is_dir() on nonexistent file returns false.'
  213. );
  214. return $sftp;
  215. }
  216. /**
  217. * @depends testFileExistsIsFileIsDirFileNonexistent
  218. */
  219. public function testSortOrder($sftp)
  220. {
  221. $this->assertTrue(
  222. $sftp->mkdir('temp'),
  223. "Failed asserting that a new scratch directory temp could " .
  224. 'be created.'
  225. );
  226. $sftp->setListOrder('filename', SORT_DESC);
  227. $list = $sftp->nlist();
  228. $expected = array('.', '..', 'temp', 'file3.txt', 'file2.txt', 'file1.txt');
  229. $this->assertSame(
  230. $list,
  231. $expected,
  232. 'Failed asserting that list sorted correctly.'
  233. );
  234. $sftp->setListOrder('filename', SORT_ASC);
  235. $list = $sftp->nlist();
  236. $expected = array('.', '..', 'temp', 'file1.txt', 'file2.txt', 'file3.txt');
  237. $this->assertSame(
  238. $list,
  239. $expected,
  240. 'Failed asserting that list sorted correctly.'
  241. );
  242. $sftp->setListOrder('size', SORT_DESC);
  243. $files = $sftp->nlist();
  244. $last_size = 0x7FFFFFFF;
  245. foreach ($files as $file) {
  246. if ($sftp->is_file($file)) {
  247. $cur_size = $sftp->size($file);
  248. $this->assertLessThanOrEqual(
  249. $last_size, $cur_size,
  250. 'Failed asserting that nlist() is in descending order'
  251. );
  252. $last_size = $cur_size;
  253. }
  254. }
  255. return $sftp;
  256. }
  257. /**
  258. * @depends testSortOrder
  259. */
  260. public function testResourceXfer($sftp)
  261. {
  262. $fp = fopen('res.txt', 'w+');
  263. $sftp->get('file1.txt', $fp);
  264. rewind($fp);
  265. $sftp->put('file4.txt', $fp);
  266. fclose($fp);
  267. $this->assertSame(
  268. self::$exampleData,
  269. $sftp->get('file4.txt'),
  270. 'Failed asserting that a file downloaded into a resource and reuploaded from a resource has the correct data'
  271. );
  272. return $sftp;
  273. }
  274. /**
  275. * @depends testResourceXfer
  276. */
  277. public function testSymlink($sftp)
  278. {
  279. $this->assertTrue(
  280. $sftp->symlink('file3.txt', 'symlink'),
  281. 'Failed asserting that a symlink could be created'
  282. );
  283. return $sftp;
  284. }
  285. /**
  286. * @depends testSymlink
  287. */
  288. public function testReadlink($sftp)
  289. {
  290. $this->assertInternalType('string', $sftp->readlink('symlink'),
  291. 'Failed asserting that a symlink\'s target could be read'
  292. );
  293. return $sftp;
  294. }
  295. /**
  296. * on older versions this would result in a fatal error
  297. * @depends testReadlink
  298. * @group github402
  299. */
  300. public function testStatcacheFix($sftp)
  301. {
  302. // Name used for both directory and file.
  303. $name = 'stattestdir';
  304. $this->assertTrue($sftp->mkdir($name));
  305. $this->assertTrue($sftp->is_dir($name));
  306. $this->assertTrue($sftp->chdir($name));
  307. $this->assertStringEndsWith(self::$scratchDir . '/' . $name, $sftp->pwd());
  308. $this->assertFalse($sftp->file_exists($name));
  309. $this->assertTrue($sftp->touch($name));
  310. $this->assertTrue($sftp->is_file($name));
  311. $this->assertTrue($sftp->chdir('..'));
  312. $this->assertStringEndsWith(self::$scratchDir, $sftp->pwd());
  313. $this->assertTrue($sftp->is_dir($name));
  314. $this->assertTrue($sftp->is_file("$name/$name"));
  315. $this->assertTrue($sftp->delete($name, true));
  316. return $sftp;
  317. }
  318. /**
  319. * @depends testStatcacheFix
  320. */
  321. public function testChDirUpHome($sftp)
  322. {
  323. $this->assertTrue(
  324. $sftp->chdir('../'),
  325. 'Failed asserting that directory could be changed one level up.'
  326. );
  327. $this->assertEquals(
  328. $this->getEnv('SSH_HOME'),
  329. $sftp->pwd(),
  330. 'Failed asserting that pwd() returns home directory.'
  331. );
  332. return $sftp;
  333. }
  334. /**
  335. * @depends testChDirUpHome
  336. */
  337. public function testFileExistsIsFileIsDirDir($sftp)
  338. {
  339. $this->assertTrue(
  340. $sftp->file_exists(self::$scratchDir),
  341. 'Failed asserting that file_exists() on scratch dir returns true.'
  342. );
  343. $this->assertFalse(
  344. $sftp->is_file(self::$scratchDir),
  345. 'Failed asserting that is_file() on example file returns false.'
  346. );
  347. $this->assertTrue(
  348. $sftp->is_dir(self::$scratchDir),
  349. 'Failed asserting that is_dir() on example file returns true.'
  350. );
  351. return $sftp;
  352. }
  353. /**
  354. * @depends testFileExistsIsFileIsDirDir
  355. */
  356. public function testTruncateLargeFile($sftp)
  357. {
  358. $filesize = (4 * 1024 + 16) * 1024 * 1024;
  359. $filename = 'file-large-from-truncate-4112MiB.txt';
  360. $this->assertTrue($sftp->touch($filename));
  361. $this->assertTrue($sftp->truncate($filename, $filesize));
  362. $this->assertSame($filesize, $sftp->size($filename));
  363. return $sftp;
  364. }
  365. /**
  366. * @depends testTruncateLargeFile
  367. */
  368. public function testRmDirScratch($sftp)
  369. {
  370. $this->assertFalse(
  371. $sftp->rmdir(self::$scratchDir),
  372. 'Failed asserting that non-empty scratch directory could ' .
  373. 'not be deleted using rmdir().'
  374. );
  375. return $sftp;
  376. }
  377. /**
  378. * @depends testRmDirScratch
  379. */
  380. public function testDeleteRecursiveScratch($sftp)
  381. {
  382. $this->assertTrue(
  383. $sftp->delete(self::$scratchDir),
  384. 'Failed asserting that non-empty scratch directory could ' .
  385. 'be deleted using recursive delete().'
  386. );
  387. return $sftp;
  388. }
  389. /**
  390. * @depends testDeleteRecursiveScratch
  391. */
  392. public function testRmDirScratchNonexistent($sftp)
  393. {
  394. $this->assertFalse(
  395. $sftp->rmdir(self::$scratchDir),
  396. 'Failed asserting that nonexistent scratch directory could ' .
  397. 'not be deleted using rmdir().'
  398. );
  399. }
  400. }