2015-08-16 00:32:31 +08:00
|
|
|
<?php
|
2016-07-21 23:09:48 +08:00
|
|
|
|
|
|
|
use PrivateBin\trafficlimiter;
|
|
|
|
|
2015-08-16 00:32:31 +08:00
|
|
|
class trafficlimiterTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
private $_path;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'trafficlimit';
|
|
|
|
trafficlimiter::setPath($this->_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
|
|
|
helper::rmdir($this->_path . DIRECTORY_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTrafficGetsLimited()
|
|
|
|
{
|
|
|
|
$this->assertEquals($this->_path, trafficlimiter::getPath());
|
|
|
|
$file = 'baz';
|
|
|
|
$this->assertEquals($this->_path . DIRECTORY_SEPARATOR . $file, trafficlimiter::getPath($file));
|
|
|
|
trafficlimiter::setLimit(4);
|
2015-09-26 23:57:46 +08:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
|
|
$this->assertTrue(trafficlimiter::canPass(), 'first request may pass');
|
2016-07-06 15:01:10 +08:00
|
|
|
sleep(1);
|
2015-09-26 23:57:46 +08:00
|
|
|
$this->assertFalse(trafficlimiter::canPass(), 'second request is to fast, may not pass');
|
2016-07-06 15:01:10 +08:00
|
|
|
sleep(4);
|
2015-09-26 23:57:46 +08:00
|
|
|
$this->assertTrue(trafficlimiter::canPass(), 'third request waited long enough and may pass');
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '2001:1620:2057:dead:beef::cafe:babe';
|
|
|
|
$this->assertTrue(trafficlimiter::canPass(), 'fourth request has different ip and may pass');
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
|
|
$this->assertFalse(trafficlimiter::canPass(), 'fifth request is to fast, may not pass');
|
2015-08-16 00:32:31 +08:00
|
|
|
}
|
|
|
|
}
|