From 01f547ba173a1615a86b31d0f3adb2dcfe46080b Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 25 Jun 2015 00:15:19 +0200 Subject: [PATCH 1/4] Add SFTPStreamTest::testFopenFcloseCreatesFile() --- tests/Functional/Net/SFTPLargeFileTest.php | 27 +-------------- tests/Functional/Net/SFTPStreamTest.php | 33 ++++++++++++++++++ tests/Functional/Net/SFTPTestCase.php | 39 ++++++++++++++++++++++ 3 files changed, 73 insertions(+), 26 deletions(-) create mode 100644 tests/Functional/Net/SFTPStreamTest.php create mode 100644 tests/Functional/Net/SFTPTestCase.php diff --git a/tests/Functional/Net/SFTPLargeFileTest.php b/tests/Functional/Net/SFTPLargeFileTest.php index 69a757da..1e85b1fa 100644 --- a/tests/Functional/Net/SFTPLargeFileTest.php +++ b/tests/Functional/Net/SFTPLargeFileTest.php @@ -8,11 +8,8 @@ require_once 'Crypt/Base.php'; -class Functional_Net_SFTPLargeFileTest extends PhpseclibFunctionalTestCase +class Functional_Net_SFTPLargeFileTest extends Functional_Net_SFTPTestCase { - protected $sftp; - protected $scratchDir; - static public function setUpBeforeClass() { if (!extension_loaded('mcrypt') && !extension_loaded('openssl')) { @@ -21,28 +18,6 @@ class Functional_Net_SFTPLargeFileTest extends PhpseclibFunctionalTestCase parent::setUpBeforeClass(); } - public function setUp() - { - $this->scratchDir = uniqid('phpseclib-sftp-large-scratch-'); - - $this->sftp = new Net_SFTP($this->getEnv('SSH_HOSTNAME')); - $this->assertTrue($this->sftp->login( - $this->getEnv('SSH_USERNAME'), - $this->getEnv('SSH_PASSWORD') - )); - $this->assertTrue($this->sftp->mkdir($this->scratchDir)); - $this->assertTrue($this->sftp->chdir($this->scratchDir)); - } - - public function tearDown() - { - if ($this->sftp) { - $this->sftp->chdir($this->getEnv('SSH_HOME')); - $this->sftp->delete($this->scratchDir); - } - parent::tearDown(); - } - /** * @group github298 * @group github455 diff --git a/tests/Functional/Net/SFTPStreamTest.php b/tests/Functional/Net/SFTPStreamTest.php new file mode 100644 index 00000000..6d731449 --- /dev/null +++ b/tests/Functional/Net/SFTPStreamTest.php @@ -0,0 +1,33 @@ + + * @copyright 2015 Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +// Registers sftp:// as a side effect. +require_once 'Net/SFTP/Stream.php'; + +class Functional_Net_SFTPStreamTest extends Functional_Net_SFTPTestCase +{ + public function testFopenFcloseCreatesFile() + { + $context = stream_context_create(array( + 'sftp' => array('session' => $this->sftp), + )); + $fp = fopen($this->buildUrl('fooo.txt'), 'wb', false, $context); + $this->assertTrue(is_resource($fp)); + fclose($fp); + $this->assertSame(0, $this->sftp->size('fooo.txt')); + } + + protected function buildUrl($suffix) + { + return sprintf( + 'sftp://via-context/%s/%s', + $this->sftp->pwd(), + $suffix + ); + } +} diff --git a/tests/Functional/Net/SFTPTestCase.php b/tests/Functional/Net/SFTPTestCase.php new file mode 100644 index 00000000..080e4f8f --- /dev/null +++ b/tests/Functional/Net/SFTPTestCase.php @@ -0,0 +1,39 @@ + + * @copyright 2015 Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +/** + * This class provides each test method with a new and empty $this->scratchDir. + */ +abstract class Functional_Net_SFTPTestCase extends PhpseclibFunctionalTestCase +{ + protected $sftp; + protected $scratchDir; + + public function setUp() + { + parent::setUp(); + $this->scratchDir = uniqid('phpseclib-sftp-scratch-'); + + $this->sftp = new Net_SFTP($this->getEnv('SSH_HOSTNAME')); + $this->assertTrue($this->sftp->login( + $this->getEnv('SSH_USERNAME'), + $this->getEnv('SSH_PASSWORD') + )); + $this->assertTrue($this->sftp->mkdir($this->scratchDir)); + $this->assertTrue($this->sftp->chdir($this->scratchDir)); + } + + public function tearDown() + { + if ($this->sftp) { + $this->sftp->chdir($this->getEnv('SSH_HOME')); + $this->sftp->delete($this->scratchDir); + } + parent::tearDown(); + } +} From f0957021a185f2d0290ff86da52bad27e827eb42 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 25 Jun 2015 14:24:18 +0200 Subject: [PATCH 2/4] Need to create the file when it does not exist and mode[0] is not 'r'. --- phpseclib/Net/SFTP/Stream.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpseclib/Net/SFTP/Stream.php b/phpseclib/Net/SFTP/Stream.php index 7db29979..d209b092 100644 --- a/phpseclib/Net/SFTP/Stream.php +++ b/phpseclib/Net/SFTP/Stream.php @@ -280,6 +280,8 @@ class Net_SFTP_Stream if ($this->size === false) { if ($this->mode[0] == 'r') { return false; + } else { + $this->sftp->touch($path); } } else { switch ($this->mode[0]) { From 9e07fb704b89bf92f13eb2d326adda93411a763e Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 25 Jun 2015 14:24:58 +0200 Subject: [PATCH 3/4] mode[0] of 'c' is not supposed to truncate. --- phpseclib/Net/SFTP/Stream.php | 1 - 1 file changed, 1 deletion(-) diff --git a/phpseclib/Net/SFTP/Stream.php b/phpseclib/Net/SFTP/Stream.php index d209b092..3d583fa9 100644 --- a/phpseclib/Net/SFTP/Stream.php +++ b/phpseclib/Net/SFTP/Stream.php @@ -288,7 +288,6 @@ class Net_SFTP_Stream case 'x': return false; case 'w': - case 'c': $this->sftp->truncate($path, 0); } } From d71da6b02bbbe8ce64dfaff2686c452c9093dbe5 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 25 Jun 2015 14:26:35 +0200 Subject: [PATCH 4/4] Explicitly set size to 0 when creating or truncating. --- phpseclib/Net/SFTP/Stream.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpseclib/Net/SFTP/Stream.php b/phpseclib/Net/SFTP/Stream.php index 3d583fa9..0085b959 100644 --- a/phpseclib/Net/SFTP/Stream.php +++ b/phpseclib/Net/SFTP/Stream.php @@ -282,6 +282,7 @@ class Net_SFTP_Stream return false; } else { $this->sftp->touch($path); + $this->size = 0; } } else { switch ($this->mode[0]) { @@ -289,6 +290,7 @@ class Net_SFTP_Stream return false; case 'w': $this->sftp->truncate($path, 0); + $this->size = 0; } }