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