Skip to content

Commit

Permalink
Client constructor now takes a Socket object instead of a location, a…
Browse files Browse the repository at this point in the history
…nd rewrote tests
  • Loading branch information
jonjo-blur committed Feb 3, 2014
1 parent a5e005d commit eb5990c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 58 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ It is recommended to install Quahog through [composer](http://getcomposer.org).
## Usage

```php
$quahog = new \Quahog\Client();
// Create a new socket instance in PHP 5.3
$factory = new Factory();
$socket = $factory->createClient('unix:///var/run/clamav/clamd.ctl');

// Create a new socket instance in PHP >=5.4
$socket = (new Factory())->createClient('unix:///var/run/clamav/clamd.ctl');

// Create a new instance of the Client
$quahog = new \Quahog\Client($socket);

// Scan a file
$result = $quahog->scanFile('/tmp/virusfile');
Expand Down
31 changes: 12 additions & 19 deletions src/Quahog/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Quahog;

use Quahog\Exception\ConnectionException;
use Socket\Raw\Factory;
use Socket\Raw\Socket;

/**
* Class Client
Expand All @@ -12,25 +12,18 @@ class Client
{

/**
* @var \Socket\Raw\Socket
* @var Socket
*/
private $_socket;
private $socket;

/**
* Instantiate a Quahog\Client instance
*
* @param string $location The hostname and port, or socket location to connect to clamd
* @throws Exception\ConnectionException
* @param Socket $socket An instance of \Socket\Raw\Socket which points to clamd
*/
public function __construct($location)
public function __construct(Socket $socket)
{
$factory = new Factory();

try {
$this->_socket = $factory->createClient($location);
} catch (\Exception $e) {
throw new ConnectionException('Could not connect to to socket at: ' . $location);
}
$this->socket = $socket;
}

/**
Expand Down Expand Up @@ -162,11 +155,11 @@ public function scanStream($stream, $maxChunkSize = 1024)

$size = pack('N', strlen($chunk));

$this->_socket->send($size, MSG_DONTROUTE);
$this->_socket->send($chunk, MSG_DONTROUTE);
$this->socket->send($size, MSG_DONTROUTE);
$this->socket->send($chunk, MSG_DONTROUTE);
}

$this->_socket->send(pack('N', 0), MSG_DONTROUTE);
$this->socket->send(pack('N', 0), MSG_DONTROUTE);

$response = $this->_receiveResponse();

Expand All @@ -180,7 +173,7 @@ public function scanStream($stream, $maxChunkSize = 1024)
*/
private function _sendCommand($command)
{
$this->_socket->send("n$command\n", MSG_DONTROUTE);
$this->socket->send("n$command\n", MSG_DONTROUTE);
}

/**
Expand All @@ -190,9 +183,9 @@ private function _sendCommand($command)
*/
private function _receiveResponse()
{
$result = $this->_socket->read(4096);
$result = $this->socket->read(4096);

$this->_socket->close();
$this->socket->close();

return trim($result);
}
Expand Down
87 changes: 49 additions & 38 deletions tests/QuahogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,93 +6,92 @@
/**
* Class QuahogTest
*/
class QuahogTest extends PHPUnit_Framework_TestCase
class QuahogTest extends \PHPUnit_Framework_TestCase
{

/**
* @var \Quahog\Client
* @var \Socket\Raw\Socket|\PHPUnit_Framework_MockObject_MockObject
*/
protected $quahog;
private $socket;

public static function setUpBeforeClass()
{
mkdir('/tmp/quahog');
file_put_contents('/tmp/quahog/EICAR', base64_decode("WDVPIVAlQEFQWzRcUFpYNTQoUF4pN0NDKTd9JEVJQ0FSLVNUQU5EQVJELUFOVElWSVJVUy1URVNU\nLUZJTEUhJEgrSCo=\n"));
file_put_contents('/tmp/quahog/EICAR2', base64_decode("WDVPIVAlQEFQWzRcUFpYNTQoUF4pN0NDKTd9JEVJQ0FSLVNUQU5EQVJELUFOVElWSVJVUy1URVNU\nLUZJTEUhJEgrSCo=\n"));
}

public static function tearDownAfterClass()
{
unlink('/tmp/quahog/EICAR');
unlink('/tmp/quahog/EICAR2');
rmdir('/tmp/quahog');
}
/**
* @var \Quahog\Client|\PHPUnit_Framework_MockObject_MockObject
*/
private $quahog;

public function setUp()
{
$this->quahog = new Client('unix:///var/run/clamav/clamd.ctl');
}

public function testConstruct()
{
$this->setExpectedException('Quahog\Exception\ConnectionException');

new Client('not-a-real-clam-instance');
$this->socket = $this->getMockBuilder('Socket\Raw\Socket')->disableOriginalConstructor()->getMock();
$this->quahog = new Client($this->socket);
}

public function testPingOK()
{
$this->socket->expects($this->any())->method('read')->will($this->returnValue('PONG'));

$result = $this->quahog->ping();

$this->assertTrue($result);
}

public function testPingFail()
{
$quahogMock = $this->getMock('Quahog\Client', array('_receiveResponse'), array('unix:///var/run/clamav/clamd.ctl'));
$quahogMock->expects($this->any())->method('_receiveResponse')->will($this->returnValue('NOPE'));

$reflection = new ReflectionClass('Quahog\Client');
$this->setExpectedException('Quahog\Exception\ConnectionException');

$method = $reflection->getMethod('_receiveResponse');
$method->setAccessible(true);
$method->invoke($quahogMock);
$this->socket->expects($this->any())->method('read')->will($this->returnValue(null));

$this->setExpectedException('Quahog\Exception\ConnectionException');
$result = $this->quahog->ping();

$quahogMock->ping();
$this->assertTrue($result);
}

public function testVersion()
{
$this->socket->expects($this->any())->method('read')->will($this->returnValue('ClamAV 1.2.3'));

$result = $this->quahog->version();

$this->assertStringStartsWith('ClamAV', $result);
}

public function testStats()
{
$this->socket->expects($this->any())->method('read')->will($this->returnValue('POOLS:'));

$result = $this->quahog->stats();

$this->assertStringStartsWith('POOLS:', $result);
}

public function testReload()
{
$this->socket->expects($this->any())->method('read')->will($this->returnValue('RELOADING'));

$result = $this->quahog->reload();

$this->assertSame('RELOADING', $result);
}

public function testScanFile()
{
$this->socket->expects($this->any())->method('read')->will(
$this->returnValue('/tmp/quahog/EICAR: Eicar-Test-Signature FOUND')
);

$result = $this->quahog->scanFile('/tmp/quahog/EICAR');

$this->assertSame(array('filename' => '/tmp/quahog/EICAR', 'reason' => 'Eicar-Test-Signature', 'status' => 'FOUND'), $result);
$this->assertSame(
array('filename' => '/tmp/quahog/EICAR', 'reason' => 'Eicar-Test-Signature', 'status' => 'FOUND'),
$result
);
}

public function testMultiscanFile()
{
$this->socket->expects($this->any())->method('read')->will(
$this->returnValue('/tmp/quahog/EICAR: Eicar-Test-Signature FOUND')
);

$result = $this->quahog->multiscanFile('/tmp/quahog');

$this->assertSame('Eicar-Test-Signature', $result['reason']);
Expand All @@ -101,18 +100,30 @@ public function testMultiscanFile()

public function testContScan()
{
$this->socket->expects($this->any())->method('read')->will(
$this->returnValue('/tmp/quahog/EICAR: Eicar-Test-Signature FOUND')
);

$result = $this->quahog->contScan('/tmp/quahog');

$this->assertSame(array('filename' => '/tmp/quahog/EICAR', 'reason' => 'Eicar-Test-Signature', 'status' => 'FOUND'), $result);
$this->assertSame(
array('filename' => '/tmp/quahog/EICAR', 'reason' => 'Eicar-Test-Signature', 'status' => 'FOUND'),
$result
);
}

public function testScanStream()
{
$stream = file_get_contents('/tmp/quahog/EICAR');
$this->socket->expects($this->any())->method('read')->will(
$this->returnValue('stream: Eicar-Test-Signature FOUND')
);

$result = $this->quahog->scanStream($stream);
$result = $this->quahog->scanStream('stream');

$this->assertSame(array('filename' => 'stream', 'reason' => 'Eicar-Test-Signature', 'status' => 'FOUND'), $result);
$this->assertSame(
array('filename' => 'stream', 'reason' => 'Eicar-Test-Signature', 'status' => 'FOUND'),
$result
);
}

public function testShutdown()
Expand Down

0 comments on commit eb5990c

Please sign in to comment.