Merge pull request #727 from bantu/SFTPStreamTest-2.0

[2.0] Various fixes to fopen mode handling in SFTP Stream

* bantu/SFTPStreamTest-2.0:
  Explicitly set size to 0 when creating or truncating.
  mode[0] of 'c' is not supposed to truncate.
  Need to create the file when it does not exist and mode[0] is not 'r'.
  Add SFTPStreamTest::testFopenFcloseCreatesFile()
This commit is contained in:
Andreas Fischer 2015-07-04 14:28:15 +02:00
commit 4724249f50
4 changed files with 84 additions and 27 deletions

View File

@ -262,14 +262,17 @@ class Stream
if ($this->size === false) { if ($this->size === false) {
if ($this->mode[0] == 'r') { if ($this->mode[0] == 'r') {
return false; return false;
} else {
$this->sftp->touch($path);
$this->size = 0;
} }
} else { } else {
switch ($this->mode[0]) { switch ($this->mode[0]) {
case 'x': case 'x':
return false; return false;
case 'w': case 'w':
case 'c':
$this->sftp->truncate($path, 0); $this->sftp->truncate($path, 0);
$this->size = 0;
} }
} }

View File

@ -9,11 +9,8 @@
use phpseclib\Crypt\Base; use phpseclib\Crypt\Base;
use phpseclib\Net\SFTP; use phpseclib\Net\SFTP;
class Functional_Net_SFTPLargeFileTest extends PhpseclibFunctionalTestCase class Functional_Net_SFTPLargeFileTest extends Functional_Net_SFTPTestCase
{ {
protected $sftp;
protected $scratchDir;
static public function setUpBeforeClass() static public function setUpBeforeClass()
{ {
if (!extension_loaded('mcrypt') && !extension_loaded('openssl')) { if (!extension_loaded('mcrypt') && !extension_loaded('openssl')) {
@ -22,28 +19,6 @@ class Functional_Net_SFTPLargeFileTest extends PhpseclibFunctionalTestCase
parent::setUpBeforeClass(); parent::setUpBeforeClass();
} }
public function setUp()
{
$this->scratchDir = uniqid('phpseclib-sftp-large-scratch-');
$this->sftp = new 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 github298
* @group github455 * @group github455

View File

@ -0,0 +1,38 @@
<?php
/**
* @author Andreas Fischer <bantu@phpbb.com>
* @copyright 2015 Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
use phpseclib\Net\SFTP\Stream;
class Functional_Net_SFTPStreamTest extends Functional_Net_SFTPTestCase
{
static public function setUpBeforeClass()
{
Stream::register();
parent::setUpBeforeClass();
}
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
);
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* @author Andreas Fischer <bantu@phpbb.com>
* @copyright 2015 Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
use phpseclib\Net\SFTP;
/**
* 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 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();
}
}