appConfig = $this->createMock(IAppConfig::class); $this->factory = new Factory(); $this->allowList = new BruteforceAllowList( $this->appConfig, $this->factory, ); } public static function dataIsBypassListed(): array { return [ [ '10.10.10.10', [ 'whitelist_0' => '10.10.10.0/24', ], true, ], [ '10.10.10.10', [ 'whitelist_0' => '192.168.0.0/16', ], false, ], [ '10.10.10.10', [ 'whitelist_0' => '192.168.0.0/16', 'whitelist_1' => '10.10.10.0/24', ], true, ], [ '10.10.10.10', [ 'whitelist_0' => '10.10.10.11/31', ], true, ], [ '10.10.10.10', [ 'whitelist_0' => '10.10.10.9/31', ], false, ], [ '10.10.10.10', [ 'whitelist_0' => '10.10.10.15/29', ], true, ], [ 'dead:beef:cafe::1', [ 'whitelist_0' => '192.168.0.0/16', 'whitelist_1' => '10.10.10.0/24', 'whitelist_2' => 'deaf:beef:cafe:1234::/64' ], false, ], [ 'dead:beef:cafe::1', [ 'whitelist_0' => '192.168.0.0/16', 'whitelist_1' => '10.10.10.0/24', 'whitelist_2' => 'deaf:beef::/64' ], false, ], [ 'dead:beef:cafe::1', [ 'whitelist_0' => '192.168.0.0/16', 'whitelist_1' => '10.10.10.0/24', 'whitelist_2' => 'deaf:cafe::/8' ], true, ], [ 'dead:beef:cafe::1111', [ 'whitelist_0' => 'dead:beef:cafe::1100/123', ], true, ], [ 'invalid', [], false, ], ]; } /** * @dataProvider dataIsBypassListed * * @param string[] $allowList */ public function testIsBypassListed( string $ip, array $allowList, bool $isAllowListed, ): void { $this->appConfig->method('getKeys') ->with($this->equalTo('bruteForce')) ->willReturn(array_keys($allowList)); $this->appConfig->method('getValueString') ->willReturnCallback(function ($app, $key, $default) use ($allowList) { if ($app !== 'bruteForce') { return $default; } if (isset($allowList[$key])) { return $allowList[$key]; } return $default; }); $this->assertSame( $isAllowListed, $this->allowList->isBypassListed($ip) ); } }