|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PragmaRX\Firewall\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
|
6
|
|
|
use PragmaRX\Firewall\Vendor\Laravel\Facade as Firewall; |
|
7
|
|
|
|
|
8
|
|
|
class FirewallTestCase extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public function test_firewall_is_instantiable() |
|
11
|
|
|
{ |
|
12
|
|
|
$false = Firewall::isBlackListed('impossible'); |
|
13
|
|
|
|
|
14
|
|
|
$this->assertFalse($false); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function test_disable_firewall() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->setConfig('enabled', false); |
|
20
|
|
|
|
|
21
|
|
|
Firewall::blacklist($ip = '172.17.0.100'); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertTrue(Firewall::isBlackListed($ip)); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function test_can_blacklist_ips() |
|
27
|
|
|
{ |
|
28
|
|
|
Firewall::blacklist($ip = '172.17.0.100'); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertTrue(Firewall::isBlackListed($ip)); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertFalse(Firewall::isWhitelisted($ip)); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertFalse(Firewall::isBlackListed('172.17.0.101')); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertFalse(Firewall::isWhitelisted('172.17.0.101')); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertEquals(Firewall::whichList($ip), 'blacklist'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function test_can_whitelist_ips() |
|
42
|
|
|
{ |
|
43
|
|
|
Firewall::whitelist($ip = '172.17.0.101'); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertFalse(Firewall::isBlackListed($ip)); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertTrue(Firewall::isWhitelisted($ip)); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertFalse(Firewall::isWhitelisted('172.17.0.102')); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertFalse(Firewall::isBlacklisted('172.17.0.102')); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertEquals(Firewall::whichList($ip), 'whitelist'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function test_can_list_cidrs() |
|
57
|
|
|
{ |
|
58
|
|
|
Firewall::whitelist('172.17.0.0/24'); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertTrue(Firewall::isWhitelisted($ip = '172.17.0.1')); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertTrue(Firewall::isWhitelisted('172.17.0.100')); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertTrue(Firewall::isWhitelisted('172.17.0.255')); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertFalse(Firewall::isBlacklisted($ip)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function test_refuses_wrong_ip_addresses() |
|
70
|
|
|
{ |
|
71
|
|
|
$false = Firewall::whitelist('172.17.0.256'); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertFalse($false); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertEquals(Firewall::getMessages()->toArray(), ['172.17.0.256 is not a valid IP address']); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertFalse($false); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function test_force_to_a_list() |
|
81
|
|
|
{ |
|
82
|
|
|
Firewall::whitelist($ip = '172.17.0.1'); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertTrue(Firewall::isWhitelisted($ip)); |
|
85
|
|
|
|
|
86
|
|
|
Firewall::blacklist($ip = '172.17.0.1'); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertTrue(Firewall::isWhitelisted($ip)); |
|
89
|
|
|
|
|
90
|
|
|
Firewall::blacklist($ip = '172.17.0.1', true); // force |
|
91
|
|
|
|
|
92
|
|
|
$this->assertFalse(Firewall::isWhitelisted($ip)); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertTrue(Firewall::isBlacklisted($ip)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function test_find_ip() |
|
98
|
|
|
{ |
|
99
|
|
|
Firewall::whitelist($ip = '172.17.0.1'); |
|
100
|
|
|
|
|
101
|
|
|
$model = Firewall::find($ip); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertInstanceOf(\PragmaRX\Firewall\Vendor\Laravel\Models\Firewall::class, $model); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertNull(Firewall::find('impossible')); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function test_get_all_ips() |
|
109
|
|
|
{ |
|
110
|
|
|
Firewall::whitelist('172.17.0.1'); |
|
111
|
|
|
Firewall::whitelist('172.17.0.2'); |
|
112
|
|
|
Firewall::whitelist('172.17.0.3'); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertCount(3, Firewall::all()); |
|
115
|
|
|
|
|
116
|
|
|
Firewall::remove('172.17.0.3'); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertCount(2, Firewall::all()); |
|
119
|
|
|
|
|
120
|
|
|
Firewall::clear('172.17.0.3'); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertCount(0, Firewall::all()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function test_block_access() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->assertInstanceOf(Response::class, Firewall::blockAccess()); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function test_log() |
|
131
|
|
|
{ |
|
132
|
|
|
Firewall::log('whatever'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function test_setip() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->assertEquals('127.0.0.1', Firewall::getIp()); |
|
138
|
|
|
|
|
139
|
|
|
Firewall::setIp($ip = '127.0.0.2'); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertEquals($ip, Firewall::getIp()); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function test_ip_validation() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->assertTrue(Firewall::ipIsValid('172.17.0.100')); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertFalse(Firewall::ipIsValid('172.17.0.256')); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function test_report() |
|
152
|
|
|
{ |
|
153
|
|
|
Firewall::whitelist('172.17.0.1'); |
|
154
|
|
|
Firewall::blacklist('172.17.0.2'); |
|
155
|
|
|
|
|
156
|
|
|
$expected = [ |
|
157
|
|
|
[ |
|
158
|
|
|
'ip_address' => '172.17.0.1', |
|
159
|
|
|
'whitelisted' => 1, |
|
160
|
|
|
], |
|
161
|
|
|
[ |
|
162
|
|
|
'ip_address' => '172.17.0.2', |
|
163
|
|
|
'whitelisted' => 0, |
|
164
|
|
|
], |
|
165
|
|
|
]; |
|
166
|
|
|
|
|
167
|
|
|
$report = Firewall::report()->map(function ($item, $key) { |
|
168
|
|
|
return collect($item->toArray())->only(['ip_address', 'whitelisted']); |
|
169
|
|
|
})->toArray(); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertEquals($expected, $report); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function test_attack() |
|
175
|
|
|
{ |
|
176
|
|
|
$this->setConfig('notifications.enabled', false); |
|
177
|
|
|
|
|
178
|
|
|
$this->setConfig('attack_blocker.allowed_frequency.ip.requests', 2); |
|
179
|
|
|
|
|
180
|
|
|
$this->assertFalse(Firewall::isBeingAttacked('172.17.0.1')); |
|
181
|
|
|
$this->assertFalse(Firewall::isBeingAttacked('172.17.0.1')); |
|
182
|
|
|
|
|
183
|
|
|
$this->assertNull(Firewall::responseToAttack()); |
|
184
|
|
|
|
|
185
|
|
|
$this->assertTrue(Firewall::isBeingAttacked('172.17.0.1')); |
|
186
|
|
|
|
|
187
|
|
|
$this->assertInstanceOf(Response::class, Firewall::responseToAttack()); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|