mirror of
https://github.com/phpseclib/phpseclib.git
synced 2024-11-16 02:07:09 +00:00
Merge pull request #529 from bantu/scp-tests
Some SCP tests. * bantu/scp-tests: SCP: Allow for null byte in tests as long as #146 is under investigation. Some SCP tests. Infrastructure for obtaining temporary files.
This commit is contained in:
commit
1c949e47c8
93
tests/Functional/Net/SCPSSH2UserStoryTest.php
Normal file
93
tests/Functional/Net/SCPSSH2UserStoryTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @author Andreas Fischer <bantu@phpbb.com>
|
||||
* @copyright MMXIV Andreas Fischer
|
||||
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
||||
*/
|
||||
|
||||
class Functional_Net_SCPSSH2UserStoryTest extends PhpseclibFunctionalTestCase
|
||||
{
|
||||
static protected $remoteFile;
|
||||
static protected $exampleData;
|
||||
static protected $exampleDataLength;
|
||||
|
||||
static public function setUpBeforeClass()
|
||||
{
|
||||
if (getenv('TRAVIS') && version_compare(PHP_VERSION, '5.3.0', '<')) {
|
||||
self::markTestIncomplete(
|
||||
'This test fails on Travis CI on PHP 5.2 due to requiring GMP.'
|
||||
);
|
||||
}
|
||||
parent::setUpBeforeClass();
|
||||
self::$remoteFile = uniqid('phpseclib-scp-ssh2-') . '.txt';
|
||||
self::$exampleData = str_repeat('abscp12345', 1000);
|
||||
self::$exampleDataLength = 10000;
|
||||
}
|
||||
|
||||
public function testConstructSSH2()
|
||||
{
|
||||
$ssh = new Net_SSH2($this->getEnv('SSH_HOSTNAME'));
|
||||
$this->assertTrue(
|
||||
$ssh->login(
|
||||
$this->getEnv('SSH_USERNAME'),
|
||||
$this->getEnv('SSH_PASSWORD')
|
||||
)
|
||||
);
|
||||
return $ssh;
|
||||
}
|
||||
|
||||
/** @depends testConstructSSH2 */
|
||||
public function testConstructor($ssh)
|
||||
{
|
||||
$scp = new Net_SCP($ssh);
|
||||
$this->assertTrue(
|
||||
is_object($scp),
|
||||
'Could not construct Net_SCP object.'
|
||||
);
|
||||
return $scp;
|
||||
}
|
||||
|
||||
/** @depends testConstructor */
|
||||
public function testPutGetString($scp)
|
||||
{
|
||||
$this->assertTrue(
|
||||
$scp->put(self::$remoteFile, self::$exampleData),
|
||||
'Failed asserting that data could successfully be put() into file.'
|
||||
);
|
||||
$content = $scp->get(self::$remoteFile);
|
||||
// TODO: Address https://github.com/phpseclib/phpseclib/issues/146
|
||||
$this->assertContains(
|
||||
strlen($content),
|
||||
array(self::$exampleDataLength, self::$exampleDataLength + 1),
|
||||
'Failed asserting that string length matches expected length.'
|
||||
);
|
||||
$this->assertContains(
|
||||
$content,
|
||||
array(self::$exampleData, self::$exampleData . "\0"),
|
||||
'Failed asserting that string content matches expected content.'
|
||||
);
|
||||
return $scp;
|
||||
}
|
||||
|
||||
/** @depends testPutGetString */
|
||||
public function testGetFile($scp)
|
||||
{
|
||||
$localFilename = $this->createTempFile();
|
||||
$this->assertTrue(
|
||||
$scp->get(self::$remoteFile, $localFilename),
|
||||
'Failed asserting that get() into file was successful.'
|
||||
);
|
||||
// TODO: Address https://github.com/phpseclib/phpseclib/issues/146
|
||||
$this->assertContains(
|
||||
filesize($localFilename),
|
||||
array(self::$exampleDataLength, self::$exampleDataLength + 1),
|
||||
'Failed asserting that filesize matches expected data size.'
|
||||
);
|
||||
$this->assertContains(
|
||||
file_get_contents($localFilename),
|
||||
array(self::$exampleData, self::$exampleData . "\0"),
|
||||
'Failed asserting that file content matches expected content.'
|
||||
);
|
||||
}
|
||||
}
|
@ -7,6 +7,32 @@
|
||||
|
||||
abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $tempFilesToUnlinkOnTearDown = array();
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
foreach ($this->tempFilesToUnlinkOnTearDown as $filename) {
|
||||
if (!file_exists($filename) || unlink($filename)) {
|
||||
unset($this->tempFilesToUnlinkOnTearDown[$filename]);
|
||||
}
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a temporary file on the local filesystem and returns its path.
|
||||
* All files created using this method will be deleted from the filesystem
|
||||
* on tearDown(), i.e. after each test method was run.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function createTempFile()
|
||||
{
|
||||
$filename = tempnam(sys_get_temp_dir(), 'phpseclib-test-');
|
||||
$this->tempFilesToUnlinkOnTearDown[] = $filename;
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $constant
|
||||
* @param mixed $expected
|
||||
|
Loading…
Reference in New Issue
Block a user