2015-08-16 00:32:31 +08:00
|
|
|
<?php
|
2016-07-21 23:09:48 +08:00
|
|
|
|
2020-10-10 18:08:58 +08:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-06-09 04:01:29 +08:00
|
|
|
use PrivateBin\Data\Filesystem;
|
|
|
|
use PrivateBin\Persistence\ServerSalt;
|
2016-08-09 17:54:42 +08:00
|
|
|
use PrivateBin\Persistence\TrafficLimiter;
|
2016-07-21 23:09:48 +08:00
|
|
|
|
2020-10-10 18:08:58 +08:00
|
|
|
class TrafficLimiterTest extends TestCase
|
2015-08-16 00:32:31 +08:00
|
|
|
{
|
|
|
|
private $_path;
|
|
|
|
|
2020-10-10 18:22:20 +08:00
|
|
|
public function setUp(): void
|
2015-08-16 00:32:31 +08:00
|
|
|
{
|
|
|
|
/* Setup Routine */
|
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'trafficlimit';
|
2021-06-10 01:16:22 +08:00
|
|
|
$store = Filesystem::getInstance(array('dir' => $this->_path));
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore($store);
|
|
|
|
TrafficLimiter::setStore($store);
|
2015-08-16 00:32:31 +08:00
|
|
|
}
|
|
|
|
|
2020-10-10 18:22:20 +08:00
|
|
|
public function tearDown(): void
|
2015-08-16 00:32:31 +08:00
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
2016-08-09 17:54:42 +08:00
|
|
|
Helper::rmDir($this->_path . DIRECTORY_SEPARATOR);
|
2015-08-16 00:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-06-09 04:01:29 +08:00
|
|
|
public function testHtaccess()
|
|
|
|
{
|
|
|
|
$htaccess = $this->_path . DIRECTORY_SEPARATOR . '.htaccess';
|
|
|
|
@unlink($htaccess);
|
|
|
|
$_SERVER['REMOTE_ADDR'] = 'foobar';
|
|
|
|
TrafficLimiter::canPass();
|
|
|
|
$this->assertFileExists($htaccess, 'htaccess recreated');
|
|
|
|
}
|
|
|
|
|
2015-08-16 00:32:31 +08:00
|
|
|
public function testTrafficGetsLimited()
|
|
|
|
{
|
2016-08-09 17:54:42 +08:00
|
|
|
TrafficLimiter::setLimit(4);
|
2015-09-26 23:57:46 +08:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'first request may pass');
|
2016-07-06 15:01:10 +08:00
|
|
|
sleep(1);
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'second request is to fast, may not pass');
|
2016-07-06 15:01:10 +08:00
|
|
|
sleep(4);
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'third request waited long enough and may pass');
|
2015-09-26 23:57:46 +08:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '2001:1620:2057:dead:beef::cafe:babe';
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'fourth request has different ip and may pass');
|
2015-09-26 23:57:46 +08:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'fifth request is to fast, may not pass');
|
2021-05-22 16:59:47 +08:00
|
|
|
|
|
|
|
// exempted IPs configuration
|
|
|
|
TrafficLimiter::setExemptedIp('1.2.3.4,10.10.10.0/24,2001:1620:2057::/48');
|
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'still too fast and not exempted');
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '10.10.10.10';
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'IPv4 in exempted range');
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'request is to fast, but IPv4 in exempted range');
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '2001:1620:2057:dead:beef::cafe:babe';
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'IPv6 in exempted range');
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'request is to fast, but IPv6 in exempted range');
|
|
|
|
TrafficLimiter::setExemptedIp('127.*,foobar');
|
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'request is to fast, invalid range');
|
|
|
|
$_SERVER['REMOTE_ADDR'] = 'foobar';
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'non-IP address');
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'request is to fast, but non-IP address matches exempted range');
|
2015-08-16 00:32:31 +08:00
|
|
|
}
|
|
|
|
}
|