From dbe8debe306c2453ed00ea381269ce044c889e44 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 20 Feb 2022 09:35:05 +0100 Subject: [PATCH] add creator unit tests for refactoring target, currently failing --- tst/Persistence/TrafficLimiterTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tst/Persistence/TrafficLimiterTest.php b/tst/Persistence/TrafficLimiterTest.php index 289598e..4e56b8a 100644 --- a/tst/Persistence/TrafficLimiterTest.php +++ b/tst/Persistence/TrafficLimiterTest.php @@ -66,4 +66,23 @@ class TrafficLimiterTest extends PHPUnit_Framework_TestCase $this->assertTrue(TrafficLimiter::canPass(), 'non-IP address'); $this->assertTrue(TrafficLimiter::canPass(), 'request is to fast, but non-IP address matches exempted range'); } + + public function testTrafficLimitCreators() + { + TrafficLimiter::setCreators('1.2.3.4,10.10.10.0/24,2001:1620:2057::/48'); + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + $this->assertFalse(TrafficLimiter::canPass(), 'not a creator'); + $_SERVER['REMOTE_ADDR'] = '10.10.10.10'; + $this->assertTrue(TrafficLimiter::canPass(), 'IPv4 in creator range'); + $this->assertTrue(TrafficLimiter::canPass(), 'request is to fast, but IPv4 in creator range'); + $_SERVER['REMOTE_ADDR'] = '2001:1620:2057:dead:beef::cafe:babe'; + $this->assertTrue(TrafficLimiter::canPass(), 'IPv6 in creator range'); + $this->assertTrue(TrafficLimiter::canPass(), 'request is to fast, but IPv6 in creator range'); + TrafficLimiter::setExempted('127.*,foobar'); + $this->assertTrue(TrafficLimiter::canPass(), 'first cached request may pass'); + $this->assertFalse(TrafficLimiter::canPass(), 'request is to fast, not a creator'); + $_SERVER['REMOTE_ADDR'] = 'foobar'; + $this->assertTrue(TrafficLimiter::canPass(), 'non-IP address'); + $this->assertTrue(TrafficLimiter::canPass(), 'request is to fast, but non-IP address matches creator'); + } }