diff options
author | Lukas Reschke <lukas@statuscode.ch> | 2016-12-19 15:35:31 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@statuscode.ch> | 2016-12-19 15:35:31 +0100 |
commit | 3eb3e437c8a0520192ec7c1018d4d1c55e780dc0 (patch) | |
tree | f436ecbdddcd9924277f0ecca24a140be289bc78 /tests/lib/IntegrityCheck | |
parent | e536313451d071aa9693411b3964ef26ce75c904 (diff) | |
download | nextcloud-server-3eb3e437c8a0520192ec7c1018d4d1c55e780dc0.tar.gz nextcloud-server-3eb3e437c8a0520192ec7c1018d4d1c55e780dc0.zip |
Add proper tests
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
Diffstat (limited to 'tests/lib/IntegrityCheck')
-rw-r--r-- | tests/lib/IntegrityCheck/CheckerTest.php | 84 | ||||
-rw-r--r-- | tests/lib/IntegrityCheck/Helpers/FileAccessHelperTest.php | 25 |
2 files changed, 97 insertions, 12 deletions
diff --git a/tests/lib/IntegrityCheck/CheckerTest.php b/tests/lib/IntegrityCheck/CheckerTest.php index 963a638bfd7..049017cb5e8 100644 --- a/tests/lib/IntegrityCheck/CheckerTest.php +++ b/tests/lib/IntegrityCheck/CheckerTest.php @@ -34,19 +34,19 @@ use OCP\ICacheFactory; use OCP\App\IAppManager; class CheckerTest extends TestCase { - /** @var EnvironmentHelper */ + /** @var EnvironmentHelper|\PHPUnit_Framework_MockObject_MockObject */ private $environmentHelper; - /** @var AppLocator */ + /** @var AppLocator|\PHPUnit_Framework_MockObject_MockObject */ private $appLocator; /** @var Checker */ private $checker; - /** @var FileAccessHelper */ + /** @var FileAccessHelper|\PHPUnit_Framework_MockObject_MockObject */ private $fileAccessHelper; - /** @var IConfig */ + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ private $config; - /** @var ICacheFactory */ + /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */ private $cacheFactory; - /** @var IAppManager */ + /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */ private $appManager; public function setUp() { @@ -77,8 +77,20 @@ class CheckerTest extends TestCase { /** * @expectedException \Exception + * @expectedExceptionMessage Exception message */ public function testWriteAppSignatureOfNotExistingApp() { + $this->fileAccessHelper + ->expects($this->at(0)) + ->method('assertDirectoryExists') + ->with('NotExistingApp/appinfo') + ->willThrowException(new \Exception('Exception message')); + $this->fileAccessHelper + ->expects($this->at(1)) + ->method('is_writable') + ->with('NotExistingApp/appinfo') + ->willReturn(true); + $keyBundle = file_get_contents(__DIR__ .'/../../data/integritycheck/SomeApp.crt'); $rsaPrivateKey = file_get_contents(__DIR__ .'/../../data/integritycheck/SomeApp.key'); $rsa = new RSA(); @@ -90,13 +102,14 @@ class CheckerTest extends TestCase { /** * @expectedException \Exception + * @expectedExceptionMessageRegExp /[a-zA-Z\/_-]+ is not writable/ */ - public function testWriteAppSignatureWrongPermissions(){ + public function testWriteAppSignatureWrongPermissions() { $this->fileAccessHelper ->expects($this->once()) ->method('file_put_contents') - ->will($this->throwException(new \Exception)) - ; + ->will($this->throwException(new \Exception('Exception message'))); + $keyBundle = file_get_contents(__DIR__ .'/../../data/integritycheck/SomeApp.crt'); $rsaPrivateKey = file_get_contents(__DIR__ .'/../../data/integritycheck/SomeApp.key'); $rsa = new RSA(); @@ -460,6 +473,54 @@ class CheckerTest extends TestCase { $this->assertSame([], $this->checker->verifyAppSignature('SomeApp')); } + /** + * @expectedException \Exception + * @expectedExceptionMessage Exception message + */ + public function testWriteCoreSignatureWithException() { + $this->fileAccessHelper + ->expects($this->at(0)) + ->method('assertDirectoryExists') + ->will($this->throwException(new \Exception('Exception message'))); + $this->fileAccessHelper + ->expects($this->at(1)) + ->method('is_writable') + ->with(__DIR__ . '/core') + ->willReturn(true); + + $keyBundle = file_get_contents(__DIR__ .'/../../data/integritycheck/SomeApp.crt'); + $rsaPrivateKey = file_get_contents(__DIR__ .'/../../data/integritycheck/SomeApp.key'); + $rsa = new RSA(); + $rsa->loadKey($rsaPrivateKey); + $x509 = new X509(); + $x509->loadX509($keyBundle); + $this->checker->writeCoreSignature($x509, $rsa, __DIR__); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessageRegExp /[a-zA-Z\/_-]+ is not writable/ + */ + public function testWriteCoreSignatureWrongPermissions() { + $this->fileAccessHelper + ->expects($this->at(0)) + ->method('assertDirectoryExists') + ->will($this->throwException(new \Exception('Exception message'))); + $this->fileAccessHelper + ->expects($this->at(1)) + ->method('is_writable') + ->with(__DIR__ . '/core') + ->willReturn(false); + + $keyBundle = file_get_contents(__DIR__ .'/../../data/integritycheck/SomeApp.crt'); + $rsaPrivateKey = file_get_contents(__DIR__ .'/../../data/integritycheck/SomeApp.key'); + $rsa = new RSA(); + $rsa->loadKey($rsaPrivateKey); + $x509 = new X509(); + $x509->loadX509($keyBundle); + $this->checker->writeCoreSignature($x509, $rsa, __DIR__); + } + public function testWriteCoreSignature() { $expectedSignatureFileData = '{ "hashes": { @@ -965,7 +1026,7 @@ class CheckerTest extends TestCase { ->method('verifyCoreSignature'); $this->appLocator ->expects($this->at(0)) - ->Method('getAllApps') + ->method('getAllApps') ->will($this->returnValue([ 'files', 'calendar', @@ -1091,7 +1152,6 @@ class CheckerTest extends TestCase { ->with('integrity.check.disabled', false) ->will($this->returnValue(true)); - $result = $this->invokePrivate($this->checker, 'isCodeCheckEnforced'); - $this->assertSame(false, $result); + $this->assertFalse(self::invokePrivate($this->checker, 'isCodeCheckEnforced')); } } diff --git a/tests/lib/IntegrityCheck/Helpers/FileAccessHelperTest.php b/tests/lib/IntegrityCheck/Helpers/FileAccessHelperTest.php index 740b14e61c4..de4aeec78cc 100644 --- a/tests/lib/IntegrityCheck/Helpers/FileAccessHelperTest.php +++ b/tests/lib/IntegrityCheck/Helpers/FileAccessHelperTest.php @@ -40,4 +40,29 @@ class FileAccessHelperTest extends TestCase { $this->fileAccessHelper->file_put_contents($filePath, $data); $this->assertSame($data, $this->fileAccessHelper->file_get_contents($filePath)); } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Failed to write into /anabsolutelynotexistingfolder/on/the/system.txt + */ + public function testFile_put_contentsWithException() { + $this->fileAccessHelper->file_put_contents('/anabsolutelynotexistingfolder/on/the/system.txt', 'MyFiles'); + } + + public function testIs_writable() { + $this->assertFalse($this->fileAccessHelper->is_writable('/anabsolutelynotexistingfolder/on/the/system.txt')); + $this->assertTrue($this->fileAccessHelper->is_writable(\OC::$server->getTempManager()->getTemporaryFile('MyFile'))); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Directory /anabsolutelynotexistingfolder/on/the/system does not exist. + */ + public function testAssertDirectoryExistsWithException() { + $this->fileAccessHelper->assertDirectoryExists('/anabsolutelynotexistingfolder/on/the/system'); + } + + public function testAssertDirectoryExists() { + $this->fileAccessHelper->assertDirectoryExists(\OC::$server->getTempManager()->getTemporaryFolder('/testfolder/')); + } } |