1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* antibot |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Antibot |
8
|
|
|
* @subpackage Jkphl\Antibot\Tests\Ports |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2020 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2020 Joschi Kuphal <[email protected]> |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Antibot\Tests\Ports; |
38
|
|
|
|
39
|
|
|
use Jkphl\Antibot\Domain\Exceptions\BlacklistValidationException; |
40
|
|
|
use Jkphl\Antibot\Domain\Exceptions\WhitelistValidationException; |
41
|
|
|
use Jkphl\Antibot\Ports\LookupStrategy\ArrayLookupStrategy; |
42
|
|
|
use Jkphl\Antibot\Ports\Validators\IpBlacklistValidator; |
43
|
|
|
use Jkphl\Antibot\Ports\Validators\IpWhitelistValidator; |
44
|
|
|
use Jkphl\Antibot\Ports\Validators\ParameterBlacklistValidator; |
45
|
|
|
use Jkphl\Antibot\Tests\AbstractTestBase; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Lookup Validator Tests |
49
|
|
|
* |
50
|
|
|
* @package Jkphl\Antibot |
51
|
|
|
* @subpackage Jkphl\Antibot\Tests\Ports |
52
|
|
|
*/ |
53
|
|
|
class LookupValidatorTest extends AbstractTestBase |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* Test the IP whitelist validator |
57
|
|
|
*/ |
58
|
|
|
public function testIpWhitelist(): void |
59
|
|
|
{ |
60
|
|
|
$this->expectException(WhitelistValidationException::class); |
61
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
62
|
|
|
$arrayWhitelist1 = new ArrayLookupStrategy(['4.3.2.1']); |
63
|
|
|
$ipWhitelistValidator1 = new IpWhitelistValidator($arrayWhitelist1); |
64
|
|
|
$this->assertTrue($ipWhitelistValidator1->validate($request, $this->createAntibot())); |
65
|
|
|
|
66
|
|
|
$arrayWhitelist2 = new ArrayLookupStrategy(['1.2.3.4']); |
67
|
|
|
$ipWhitelistValidator2 = new IpWhitelistValidator($arrayWhitelist2); |
68
|
|
|
$ipWhitelistValidator2->validate($request, $this->createAntibot()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Test the IP blacklist validator |
73
|
|
|
*/ |
74
|
|
|
public function testIpBlacklist(): void |
75
|
|
|
{ |
76
|
|
|
$this->expectException(BlacklistValidationException::class); |
77
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
78
|
|
|
$arrayBlacklist1 = new ArrayLookupStrategy(['4.3.2.1']); |
79
|
|
|
$ipBlacklistValidator1 = new IpBlacklistValidator($arrayBlacklist1); |
80
|
|
|
$this->assertTrue($ipBlacklistValidator1->validate($request, $this->createAntibot())); |
81
|
|
|
|
82
|
|
|
$arrayBlacklist2 = new ArrayLookupStrategy(['1.2.3.4']); |
83
|
|
|
$ipBlacklistValidator2 = new IpBlacklistValidator($arrayBlacklist2); |
84
|
|
|
$ipBlacklistValidator2->validate($request, $this->createAntibot()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test the parameter blacklist validator with a GET parameter |
89
|
|
|
*/ |
90
|
|
|
public function testGetParamBlacklist(): void |
91
|
|
|
{ |
92
|
|
|
$this->expectException(BlacklistValidationException::class); |
93
|
|
|
$request = $this->createRequest( |
94
|
|
|
['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4'], |
95
|
|
|
['name' => 'John Doe', 'email' => '[email protected]'] |
96
|
|
|
); |
97
|
|
|
$arrayBlacklist1 = new ArrayLookupStrategy(['[email protected]']); |
98
|
|
|
$paramBlacklistValidator1 = new ParameterBlacklistValidator($arrayBlacklist1, 'email'); |
99
|
|
|
$this->assertTrue($paramBlacklistValidator1->validate($request, $this->createAntibot())); |
100
|
|
|
|
101
|
|
|
$arrayBlacklist2 = new ArrayLookupStrategy(['[email protected]']); |
102
|
|
|
$paramBlacklistValidator2 = new ParameterBlacklistValidator($arrayBlacklist2, 'email'); |
103
|
|
|
$paramBlacklistValidator2->validate($request, $this->createAntibot()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Test the parameter blacklist validator with a POST parameter |
108
|
|
|
*/ |
109
|
|
|
public function testPostParamBlacklist(): void |
110
|
|
|
{ |
111
|
|
|
$this->expectException(BlacklistValidationException::class); |
112
|
|
|
$request = $this->createRequest( |
113
|
|
|
['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4'], |
114
|
|
|
[], |
115
|
|
|
['name' => 'John Doe', 'email' => '[email protected]'] |
116
|
|
|
); |
117
|
|
|
$arrayBlacklist1 = new ArrayLookupStrategy(['[email protected]']); |
118
|
|
|
$paramBlacklistValidator1 = new ParameterBlacklistValidator( |
119
|
|
|
$arrayBlacklist1, |
120
|
|
|
'email', |
121
|
|
|
ParameterBlacklistValidator::POST |
122
|
|
|
); |
123
|
|
|
$this->assertTrue($paramBlacklistValidator1->validate($request, $this->createAntibot())); |
124
|
|
|
|
125
|
|
|
$arrayBlacklist2 = new ArrayLookupStrategy(['[email protected]']); |
126
|
|
|
$paramBlacklistValidator2 = new ParameterBlacklistValidator( |
127
|
|
|
$arrayBlacklist2, |
128
|
|
|
'email', |
129
|
|
|
ParameterBlacklistValidator::POST |
130
|
|
|
); |
131
|
|
|
$paramBlacklistValidator2->validate($request, $this->createAntibot()); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|