Skip to content

Commit

Permalink
Add proper tests
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Reschke <[email protected]>
  • Loading branch information
LukasReschke committed Dec 19, 2016
1 parent e536313 commit 3eb3e43
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 49 deletions.
30 changes: 14 additions & 16 deletions lib/private/IntegrityCheck/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,18 @@ public function writeAppSignature($path,
X509 $certificate,
RSA $privateKey) {
$appInfoDir = $path . '/appinfo';
$this->fileAccessHelper->assertDirectoryExists($path);
$this->fileAccessHelper->assertDirectoryExists($appInfoDir);

$iterator = $this->getFolderIterator($path);
$hashes = $this->generateHashes($iterator, $path);
$signature = $this->createSignatureData($hashes, $certificate, $privateKey);
try {
$this->fileAccessHelper->file_put_contents(
$appInfoDir . '/signature.json',
$this->fileAccessHelper->assertDirectoryExists($appInfoDir);

$iterator = $this->getFolderIterator($path);
$hashes = $this->generateHashes($iterator, $path);
$signature = $this->createSignatureData($hashes, $certificate, $privateKey);
$this->fileAccessHelper->file_put_contents(
$appInfoDir . '/signature.json',
json_encode($signature, JSON_PRETTY_PRINT)
);
} catch (\Exception $e){
if (!$this->fileAccessHelper->is_writeable($appInfoDir)){
if (!$this->fileAccessHelper->is_writable($appInfoDir)) {
throw new \Exception($appInfoDir . ' is not writable');
}
throw $e;
Expand All @@ -303,19 +302,18 @@ public function writeCoreSignature(X509 $certificate,
RSA $rsa,
$path) {
$coreDir = $path . '/core';
$this->fileAccessHelper->assertDirectoryExists($path);
$this->fileAccessHelper->assertDirectoryExists($coreDir);

$iterator = $this->getFolderIterator($path, $path);
$hashes = $this->generateHashes($iterator, $path);
$signatureData = $this->createSignatureData($hashes, $certificate, $rsa);
try {

$this->fileAccessHelper->assertDirectoryExists($coreDir);
$iterator = $this->getFolderIterator($path, $path);
$hashes = $this->generateHashes($iterator, $path);
$signatureData = $this->createSignatureData($hashes, $certificate, $rsa);
$this->fileAccessHelper->file_put_contents(
$coreDir . '/signature.json',
json_encode($signatureData, JSON_PRETTY_PRINT)
);
} catch (\Exception $e){
if (!$this->fileAccessHelper->is_writeable($coreDir)){
if (!$this->fileAccessHelper->is_writable($coreDir)) {
throw new \Exception($coreDir . ' is not writable');
}
throw $e;
Expand Down
8 changes: 4 additions & 4 deletions lib/private/IntegrityCheck/Helpers/FileAccessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function file_exists($filename) {
* @throws \Exception
*/
public function file_put_contents($filename, $data) {
$bytesWritten = file_put_contents($filename, $data);
$bytesWritten = @file_put_contents($filename, $data);
if ($bytesWritten === false || $bytesWritten !== strlen($data)){
throw new \Exception('Failed to write into ' . $filename);
}
Expand All @@ -69,15 +69,15 @@ public function file_put_contents($filename, $data) {
* @param string $path
* @return bool
*/
public function is_writeable($path){
return is_writeable($path);
public function is_writable($path) {
return is_writable($path);
}

/**
* @param string $path
* @throws \Exception
*/
public function assertDirectoryExists($path){
public function assertDirectoryExists($path) {
if (!is_dir($path)) {
throw new \Exception('Directory ' . $path . ' does not exist.');
}
Expand Down
62 changes: 53 additions & 9 deletions tests/lib/Command/Integrity/SignAppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
use Test\TestCase;

class SignAppTest extends TestCase {
/** @var Checker */
/** @var Checker|\PHPUnit_Framework_MockObject_MockObject */
private $checker;
/** @var SignApp */
private $signApp;
/** @var FileAccessHelper */
/** @var FileAccessHelper|\PHPUnit_Framework_MockObject_MockObject */
private $fileAccessHelper;
/** @var IURLGenerator */
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator;

public function setUp() {
Expand Down Expand Up @@ -75,7 +75,7 @@ public function testExecuteWithMissingPath() {
->method('writeln')
->with('This command requires the --path, --privateKey and --certificate.');

$this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]);
$this->assertNull(self::invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecuteWithMissingPrivateKey() {
Expand Down Expand Up @@ -103,7 +103,7 @@ public function testExecuteWithMissingPrivateKey() {
->method('writeln')
->with('This command requires the --path, --privateKey and --certificate.');

$this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]);
$this->assertNull(self::invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecuteWithMissingCertificate() {
Expand Down Expand Up @@ -131,7 +131,7 @@ public function testExecuteWithMissingCertificate() {
->method('writeln')
->with('This command requires the --path, --privateKey and --certificate.');

$this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]);
$this->assertNull(self::invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecuteWithNotExistingPrivateKey() {
Expand Down Expand Up @@ -165,7 +165,7 @@ public function testExecuteWithNotExistingPrivateKey() {
->method('writeln')
->with('Private key "privateKey" does not exists.');

$this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]);
$this->assertNull(self::invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecuteWithNotExistingCertificate() {
Expand Down Expand Up @@ -204,7 +204,51 @@ public function testExecuteWithNotExistingCertificate() {
->method('writeln')
->with('Certificate "certificate" does not exists.');

$this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]);
$this->assertNull(self::invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecuteWithException() {
$inputInterface = $this->createMock(InputInterface::class);
$outputInterface = $this->createMock(OutputInterface::class);

$inputInterface
->expects($this->at(0))
->method('getOption')
->with('path')
->will($this->returnValue('AppId'));
$inputInterface
->expects($this->at(1))
->method('getOption')
->with('privateKey')
->will($this->returnValue('privateKey'));
$inputInterface
->expects($this->at(2))
->method('getOption')
->with('certificate')
->will($this->returnValue('certificate'));

$this->fileAccessHelper
->expects($this->at(0))
->method('file_get_contents')
->with('privateKey')
->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key'));
$this->fileAccessHelper
->expects($this->at(1))
->method('file_get_contents')
->with('certificate')
->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt'));

$this->checker
->expects($this->once())
->method('writeAppSignature')
->willThrowException(new \Exception('My error message'));

$outputInterface
->expects($this->at(0))
->method('writeln')
->with('Error: My error message');

$this->assertSame(1, self::invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecute() {
Expand Down Expand Up @@ -247,6 +291,6 @@ public function testExecute() {
->method('writeln')
->with('Successfully signed "AppId"');

$this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]);
$this->assertSame(0, self::invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]));
}
}
60 changes: 52 additions & 8 deletions tests/lib/Command/Integrity/SignCoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
use Test\TestCase;

class SignCoreTest extends TestCase {
/** @var Checker */
/** @var Checker|\PHPUnit_Framework_MockObject_MockObject */
private $checker;
/** @var FileAccessHelper|\PHPUnit_Framework_MockObject_MockObject */
private $fileAccessHelper;
/** @var SignCore */
private $signCore;
/** @var FileAccessHelper */
private $fileAccessHelper;

public function setUp() {
parent::setUp();
Expand Down Expand Up @@ -65,7 +65,7 @@ public function testExecuteWithMissingPrivateKey() {
->method('writeln')
->with('--privateKey, --certificate and --path are required.');

$this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]);
$this->assertNull(self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecuteWithMissingCertificate() {
Expand All @@ -88,7 +88,7 @@ public function testExecuteWithMissingCertificate() {
->method('writeln')
->with('--privateKey, --certificate and --path are required.');

$this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]);
$this->assertNull(self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecuteWithNotExistingPrivateKey() {
Expand Down Expand Up @@ -122,7 +122,7 @@ public function testExecuteWithNotExistingPrivateKey() {
->method('writeln')
->with('Private key "privateKey" does not exists.');

$this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]);
$this->assertNull(self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecuteWithNotExistingCertificate() {
Expand Down Expand Up @@ -161,7 +161,51 @@ public function testExecuteWithNotExistingCertificate() {
->method('writeln')
->with('Certificate "certificate" does not exists.');

$this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]);
$this->assertNull(self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecuteWithException() {
$inputInterface = $this->createMock(InputInterface::class);
$outputInterface = $this->createMock(OutputInterface::class);

$inputInterface
->expects($this->at(0))
->method('getOption')
->with('privateKey')
->will($this->returnValue('privateKey'));
$inputInterface
->expects($this->at(1))
->method('getOption')
->with('certificate')
->will($this->returnValue('certificate'));
$inputInterface
->expects($this->at(2))
->method('getOption')
->with('path')
->will($this->returnValue('certificate'));

$this->fileAccessHelper
->expects($this->at(0))
->method('file_get_contents')
->with('privateKey')
->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key'));
$this->fileAccessHelper
->expects($this->at(1))
->method('file_get_contents')
->with('certificate')
->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt'));

$this->checker
->expects($this->once())
->method('writeCoreSignature')
->willThrowException(new \Exception('My exception message'));

$outputInterface
->expects($this->at(0))
->method('writeln')
->with('Error: My exception message');

$this->assertEquals(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
}

public function testExecute() {
Expand Down Expand Up @@ -204,6 +248,6 @@ public function testExecute() {
->method('writeln')
->with('Successfully signed "core"');

$this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]);
$this->assertEquals(0, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
}
}
Loading

0 comments on commit 3eb3e43

Please sign in to comment.