php-projecthoneypot/tests/ProjectHoneyPotTest.php

311 lines
7.8 KiB
PHP

<?php
require_once './src/ProjectHoneyPot.php';
if (
!class_exists('\PHPUnit_Framework_TestCase')
&& class_exists('\PHPUnit\Framework\TestCase')
) {
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
}
class ProjectHoneyPotTest extends PHPUnit_Framework_TestCase
{
public function testCreateMock()
{
$this->assertTrue(true);
if (
method_exists('PHPUnit_Runner_Version','id')
&& version_compare(PHPUnit_Runner_Version::id(), '5.4', '<=')
) {
return 'getMock';
}
return 'createMock';
}
// Due to version juggling, did this a bit more manually as the
// declarations were being removed entirely and I wasn't sure which
// versions supported the suggested methods.
public function testInvalidKey()
{
$threw = false;
try {
new joshtronic\ProjectHoneyPot('foo');
} catch (Exception $e) {
$threw = true;
$this->assertEquals(
'You must specify a valid API key.',
$e->getMessage(),
);
}
$this->assertTrue($threw);
}
public function testInvalidIP()
{
$object = new joshtronic\ProjectHoneyPot('foobarfoobar');
$this->assertEquals(
array('error' => 'Invalid IP address.'),
$object->query('foo')
);
}
/**
* @depends testCreateMock
*/
public function testMissingResults($createMock)
{
$mock = $this->$createMock('joshtronic\ProjectHoneyPot');
$mock
->expects($this->once())
->method('dns_get_record')
->with($this->equalTo('4.3.2.1'))
->will($this->returnValue('foo'));
var_dump($mock->query('1.2.3.4'));
$this->assertNull($mock->query('1.2.3.4'));
// $this->assertFalse($mock->query('1.2.3.4'));
}
/**
* @depends testCreateMock
*/
public function testCategory0($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '127.0.0.0'))));
$results = $mock->query('1.2.3.4');
$this->assertEquals(array('Search Engine'), $results['categories']);
}
/**
* @depends testCreateMock
*/
public function testCategory1($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '127.0.0.1'))));
$results = $mock->query('1.2.3.4');
$this->assertEquals(array('Suspicious'), $results['categories']);
}
/**
* @depends testCreateMock
*/
public function testCategory2($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '127.0.0.2'))));
$results = $mock->query('1.2.3.4');
$this->assertEquals(array('Harvester'), $results['categories']);
}
/**
* @depends testCreateMock
*/
public function testCategory3($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '127.0.0.3'))));
$results = $mock->query('1.2.3.4');
$this->assertEquals(
array('Suspicious', 'Harvester'),
$results['categories']
);
}
/**
* @depends testCreateMock
*/
public function testCategory4($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '127.0.0.4'))));
$results = $mock->query('1.2.3.4');
$this->assertEquals(
array('Comment Spammer'),
$results['categories']
);
}
/**
* @depends testCreateMock
*/
public function testCategory5($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '127.0.0.5'))));
$results = $mock->query('1.2.3.4');
$this->assertEquals(
array('Suspicious', 'Comment Spammer'),
$results['categories']
);
}
/**
* @depends testCreateMock
*/
public function testCategory6($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '127.0.0.6'))));
$results = $mock->query('1.2.3.4');
$this->assertEquals(
array('Harvester', 'Comment Spammer'),
$results['categories']
);
}
/**
* @depends testCreateMock
*/
public function testCategory7($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '127.0.0.7'))));
$results = $mock->query('1.2.3.4');
$this->assertEquals(
array('Suspicious', 'Harvester', 'Comment Spammer'),
$results['categories']
);
}
/**
* @depends testCreateMock
*/
public function testCategoryDefault($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '127.0.0.255'))));
$results = $mock->query('1.2.3.4');
$this->assertEquals(
array('Reserved for Future Use'),
$results['categories']
);
}
/**
* @depends testCreateMock
*/
public function testWithout127($createMock)
{
$mock = $this->$createMock(
'joshtronic\ProjectHoneyPot',
array('dns_get_record'),
array('foobarfoobar')
);
$mock
->expects($this->once())
->method('dns_get_record')
->will($this->returnValue(array(array('ip' => '1.0.0.0'))));
$this->assertFalse($mock->query('1.2.3.4'));
}
// Doesn't serve much purpose aside from helping achieve 100% coverage
public function testDnsGetRecord()
{
$object = new joshtronic\ProjectHoneyPot('foobarfoobar');
$object->dns_get_record('1.2.3.4');
}
}