2015-08-15 12:32:31 -04:00
|
|
|
<?php
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2020-10-10 06:08:58 -04:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-06-08 16:01:29 -04:00
|
|
|
use PrivateBin\Data\Filesystem;
|
|
|
|
use PrivateBin\Persistence\ServerSalt;
|
2016-08-09 05:54:42 -04:00
|
|
|
use PrivateBin\Persistence\TrafficLimiter;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2020-10-10 06:08:58 -04:00
|
|
|
class TrafficLimiterTest extends TestCase
|
2015-08-15 12:32:31 -04:00
|
|
|
{
|
|
|
|
private $_path;
|
|
|
|
|
2020-10-10 06:22:20 -04:00
|
|
|
public function setUp(): void
|
2015-08-15 12:32:31 -04:00
|
|
|
{
|
|
|
|
/* Setup Routine */
|
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'trafficlimit';
|
2021-06-09 13:16:22 -04:00
|
|
|
$store = Filesystem::getInstance(array('dir' => $this->_path));
|
2021-06-08 16:01:29 -04:00
|
|
|
ServerSalt::setStore($store);
|
|
|
|
TrafficLimiter::setStore($store);
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|
|
|
|
|
2020-10-10 06:22:20 -04:00
|
|
|
public function tearDown(): void
|
2015-08-15 12:32:31 -04:00
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::rmDir($this->_path . DIRECTORY_SEPARATOR);
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|
|
|
|
|
2021-06-08 16:01:29 -04: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-15 12:32:31 -04:00
|
|
|
public function testTrafficGetsLimited()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
TrafficLimiter::setLimit(4);
|
2015-09-26 11:57:46 -04:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'first request may pass');
|
2016-07-06 03:01:10 -04:00
|
|
|
sleep(1);
|
2022-02-20 05:25:19 -05:00
|
|
|
try {
|
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'expected an exception');
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->assertEquals($e->getMessage(), 'Please wait 4 seconds between each post.', 'second request is to fast, may not pass');
|
|
|
|
}
|
2016-07-06 03:01:10 -04:00
|
|
|
sleep(4);
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'third request waited long enough and may pass');
|
2015-09-26 11:57:46 -04:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '2001:1620:2057:dead:beef::cafe:babe';
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'fourth request has different ip and may pass');
|
2015-09-26 11:57:46 -04:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
2022-02-20 05:25:19 -05:00
|
|
|
try {
|
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'expected an exception');
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->assertEquals($e->getMessage(), 'Please wait 4 seconds between each post.', 'fifth request is to fast, may not pass');
|
|
|
|
}
|
2022-02-20 03:30:41 -05:00
|
|
|
}
|
2021-05-22 04:59:47 -04:00
|
|
|
|
2022-02-20 03:30:41 -05:00
|
|
|
public function testTrafficLimitExempted()
|
|
|
|
{
|
2022-02-28 10:24:06 -05:00
|
|
|
TrafficLimiter::setExempted('1.2.3.4,10.10.10/24,2001:1620:2057::/48');
|
2022-02-20 03:30:41 -05:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'first request may pass');
|
2022-02-20 05:25:19 -05:00
|
|
|
try {
|
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'expected an exception');
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->assertEquals($e->getMessage(), 'Please wait 4 seconds between each post.', 'not exempted');
|
|
|
|
}
|
2021-05-22 04:59:47 -04:00
|
|
|
$_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');
|
2022-02-20 03:09:20 -05:00
|
|
|
TrafficLimiter::setExempted('127.*,foobar');
|
2022-02-20 03:30:41 -05:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'first cached request may pass');
|
2022-02-20 05:25:19 -05:00
|
|
|
try {
|
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'expected an exception');
|
|
|
|
} catch (Exception $e) {
|
2022-02-26 00:58:41 -05:00
|
|
|
$this->assertEquals($e->getMessage(), 'Please wait 4 seconds between each post.', 'request is too fast, invalid range');
|
2022-02-20 05:25:19 -05:00
|
|
|
}
|
2021-05-22 04:59:47 -04:00
|
|
|
$_SERVER['REMOTE_ADDR'] = 'foobar';
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'non-IP address');
|
2022-02-26 00:58:54 -05:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'request is too fast, but non-IP address matches exempted range');
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|
2022-02-20 03:35:05 -05:00
|
|
|
|
|
|
|
public function testTrafficLimitCreators()
|
|
|
|
{
|
2022-02-28 10:24:06 -05:00
|
|
|
TrafficLimiter::setCreators('1.2.3.4,10.10.10/24,2001:1620:2057::/48');
|
2022-02-20 03:35:05 -05:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
2022-02-20 05:25:19 -05:00
|
|
|
try {
|
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'expected an exception');
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->assertEquals($e->getMessage(), 'Your IP is not authorized to create pastes.', 'not a creator');
|
|
|
|
}
|
2022-02-20 03:35:05 -05:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '10.10.10.10';
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'IPv4 in creator range');
|
2022-02-26 00:59:02 -05:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'request is too fast, but IPv4 in creator range');
|
2022-02-20 03:35:05 -05:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '2001:1620:2057:dead:beef::cafe:babe';
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'IPv6 in creator range');
|
2022-02-26 00:59:11 -05:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'request is too fast, but IPv6 in creator range');
|
2022-02-20 05:25:19 -05:00
|
|
|
TrafficLimiter::setCreators('127.*,foobar');
|
|
|
|
try {
|
|
|
|
$this->assertFalse(TrafficLimiter::canPass(), 'expected an exception');
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->assertEquals($e->getMessage(), 'Your IP is not authorized to create pastes.', 'request is to fast, not a creator');
|
|
|
|
}
|
2021-05-22 04:59:47 -04:00
|
|
|
$_SERVER['REMOTE_ADDR'] = 'foobar';
|
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'non-IP address');
|
2022-02-20 03:35:05 -05:00
|
|
|
$this->assertTrue(TrafficLimiter::canPass(), 'request is to fast, but non-IP address matches creator');
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|
|
|
|
}
|