gluon-web-remote

Web remote Administration for big size of gluon routers
git clone git://archive.git.mtrnord.blog/MTRNord/gluon-web-remote.git
Log | Files | Refs | README

SftpTest.php (6763B)


      1 <?php
      2 
      3 
      4 namespace Ssh\FunctionalTests;
      5 
      6 
      7 use Ssh\Authentication\Password;
      8 use Ssh\Configuration;
      9 use Ssh\Session;
     10 use Ssh\Sftp;
     11 use Symfony\Component\Filesystem\Filesystem;
     12 
     13 /**
     14  * @author Julius Beckmann
     15  *
     16  * @group functional
     17  *
     18  * @covers \Ssh\Sftp
     19  */
     20 class SftpTest extends \PHPUnit_Framework_TestCase
     21 {
     22     /** @var Configuration */
     23     private $configuration;
     24 
     25     /** @var  Password */
     26     private $auth;
     27 
     28     /** @var  Session */
     29     private $session;
     30 
     31     /** @var  Sftp */
     32     private $sftp;
     33 
     34     /** @var  string */
     35     private $tmpDir;
     36 
     37     protected function setUp()
     38     {
     39         $this->configuration = new Configuration('localhost');
     40         $this->auth = new Password(TEST_USER, TEST_PASSWORD);
     41         $this->session = new Session($this->configuration, $this->auth);
     42 
     43         $this->sftp = $this->session->getSftp();
     44 
     45         $this->createFixtures();
     46     }
     47 
     48     protected function createFixtures()
     49     {
     50         $this->tmpDir = rtrim(sys_get_temp_dir(), '/') . '/' . 'HerzultPHPSSH';
     51 
     52         $fs = new Filesystem();
     53 
     54         $fs->remove($this->tmpDir);
     55         $fs->mkdir($this->tmpDir);
     56 
     57         $fs->touch($this->tmpDir . '/foo.txt');
     58 
     59         $fs->mkdir($this->tmpDir . '/bar');
     60         $fs->mkdir($this->tmpDir . '/bar/alice');
     61         $fs->dumpFile($this->tmpDir . '/bar/bob.txt', 'SomeContent');
     62 
     63         $fs->chmod($this->tmpDir, 0777, 0000, true);
     64     }
     65 
     66     protected function createLargeDirectoryTree()
     67     {
     68         $basePath = $this->tmpDir . '/tree';
     69         $fs = new Filesystem();
     70 
     71         // Create a larger directory tree
     72         foreach (range('a', 'c') as $a) {
     73             $fs->mkdir($basePath . '/' . $a);
     74             foreach (range('a', 'c') as $b) {
     75                 $fs->mkdir($basePath . '/' . $a . '/' . $b);
     76                 foreach (range('a', 'c') as $c) {
     77                     $fs->mkdir($basePath . '/' . $a . '/' . $b . '/' . $c);
     78                     foreach (range('1', '3') as $file) {
     79                         $fs->touch($basePath . '/' . $a . '/' . $b . '/' . $c . '/' . $file . '.txt');
     80                     }
     81                 }
     82             }
     83         }
     84         $fs->chmod($basePath, 0777, 0000, true);
     85     }
     86 
     87     public function testSymlinks()
     88     {
     89         $path = $this->tmpDir . '/symlink';
     90         $pathTarget = $this->tmpDir . '/bar/bob.txt';
     91 
     92         $this->assertTrue($this->sftp->symlink($pathTarget, $path));
     93         $this->assertEquals($pathTarget, $this->sftp->readlink($path));
     94         $this->assertEquals(
     95           array(
     96             7,
     97             'size',
     98             4,
     99             'uid',
    100             5,
    101             'gid',
    102             2,
    103             'mode',
    104             8,
    105             'atime',
    106             9,
    107             'mtime'
    108           ),
    109           array_keys($this->sftp->lstat($path))
    110         );
    111     }
    112 
    113     public function testListlargeDirectory()
    114     {
    115         $this->createLargeDirectoryTree();
    116 
    117         $list = $this->sftp->listDirectory($this->tmpDir . '/tree', true);
    118 
    119         $this->assertCount(81, $list['files']);
    120         $this->assertCount(39, $list['directories']);
    121     }
    122 
    123     public function testReadFile()
    124     {
    125         $this->assertEquals('SomeContent', $this->sftp->read($this->tmpDir . '/bar/bob.txt'));
    126 
    127         $this->assertEquals(false, $this->sftp->read($this->tmpDir . '/not_existing.txt'));
    128     }
    129 
    130     public function testWriteAndReadFile()
    131     {
    132         $path = $this->tmpDir . '/new.txt';
    133 
    134         // Creating new file.
    135         $this->assertEquals(true, $this->sftp->write($path, 'MyContent'));
    136         $this->assertEquals('MyContent', $this->sftp->read($path));
    137 
    138         // Overwriting.
    139         $this->assertEquals(true, $this->sftp->write($path, 'AnotherContent'));
    140         $this->assertEquals('AnotherContent', $this->sftp->read($path));
    141     }
    142 
    143     public function testRealpath()
    144     {
    145         // Will work.
    146         $this->assertEquals('/etc/hosts', $this->sftp->realpath('/usr//../../../../etc//hosts'));
    147 
    148         // This should not work.
    149         $this->assertEquals(false, $this->sftp->realpath('/foo/bar//../../../../etc//hosts'));
    150     }
    151 
    152     public function testUnlinkFile()
    153     {
    154         $path = $this->tmpDir . '/bar/bob.txt';
    155 
    156         $this->assertTrue(is_array($this->sftp->stat($path)));
    157 
    158         $this->assertTrue($this->sftp->unlink($path));
    159 
    160         $this->assertFalse($this->sftp->stat($path));
    161 
    162         // Unlinking a second time should not work.
    163         $this->assertFalse($this->sftp->unlink($path));
    164     }
    165 
    166     public function testRenameFile()
    167     {
    168         $path = $this->tmpDir . '/bar/bob.txt';
    169         $pathTarget = $this->tmpDir . '/bar/bob.txt_moved';
    170 
    171         $this->assertTrue(is_array($this->sftp->stat($path)));
    172         $this->assertFalse($this->sftp->stat($pathTarget));
    173 
    174         $this->assertTrue($this->sftp->rename($path, $pathTarget));
    175 
    176         $this->assertTrue(is_array($this->sftp->stat($pathTarget)));
    177         $this->assertFalse($this->sftp->stat($path));
    178     }
    179 
    180     public function testListDirectory()
    181     {
    182         $path = $this->tmpDir . '/bar';
    183 
    184         $list = $this->sftp->listDirectory($path);
    185 
    186         $this->assertEquals(
    187           array(
    188             'files' => array($path . '/bob.txt'),
    189             'directories' => array($path . '/alice'),
    190             ),
    191             $list
    192         );
    193     }
    194 
    195     /**
    196      * @expectedException \RuntimeException
    197      */
    198     public function testListNotExistingDirectory()
    199     {
    200         $path = $this->tmpDir . '/does_not_exist';
    201 
    202         $this->sftp->listDirectory($path);
    203     }
    204 
    205     public function testCreateStatAndRemoveDir()
    206     {
    207         $path = $this->tmpDir . '/a_dir';
    208 
    209         // Check
    210         $this->assertFalse($this->sftp->stat($path));
    211 
    212         // Create
    213         $this->assertTrue($this->sftp->mkdir($path));
    214 
    215         //var_dump($this->sftp->stat($path)); die();
    216 
    217         $this->assertEquals(
    218           array(
    219             7,
    220             'size',
    221             4,
    222             'uid',
    223             5,
    224             'gid',
    225             2,
    226             'mode',
    227             8,
    228             'atime',
    229             9,
    230             'mtime'
    231           ),
    232           array_keys($this->sftp->stat($path))
    233         );
    234 
    235 
    236         // Remove
    237         $this->assertTrue($this->sftp->rmdir($path));
    238         $this->assertFalse($this->sftp->stat($path));
    239     }
    240 
    241     public function testTestNotExistingFile()
    242     {
    243         $path = $this->tmpDir . '/foo.bar';
    244 
    245         $this->assertFalse($this->sftp->exists($path));
    246 
    247         $list = $this->sftp->listDirectory($this->tmpDir);
    248         $this->assertNotContains($path, $list['files']);
    249     }
    250 
    251     public function testTestExistingFile()
    252     {
    253         $path = $this->tmpDir . '/foo.txt';
    254 
    255         $this->assertTrue($this->sftp->exists($path));
    256 
    257         $list = $this->sftp->listDirectory($this->tmpDir);
    258         $this->assertContains($path, $list['files']);
    259     }
    260 }
    261