Fix Display\ResultsTest test on Windows
[phpmyadmin.git] / test / classes / IpAllowDenyTest.php
blob10de275a19ce1dfa0a47e1d07f1ecbfe84fa1526
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * tests for PhpMyAdmin\IpAllowDeny
6 * @package PhpMyAdmin-test
7 */
8 declare(strict_types=1);
10 namespace PhpMyAdmin\Tests;
12 use PhpMyAdmin\Core;
13 use PhpMyAdmin\IpAllowDeny;
14 use PHPUnit\Framework\TestCase;
16 /**
17 * PhpMyAdmin\Tests\IpAllowDenyTest class
19 * this class is for testing PhpMyAdmin\IpAllowDeny
21 * @package PhpMyAdmin-test
23 class IpAllowDenyTest extends TestCase
25 /**
26 * @var IpAllowDeny
28 private $ipAllowDeny;
30 /**
31 * Prepares environment for the test.
33 * @return void
35 protected function setUp(): void
37 $GLOBALS['cfg']['Server']['user'] = "pma_username";
38 $GLOBALS['cfg']['Server']['AllowDeny']['rules'][]
39 = "allow % 255.255.255.0/4";
40 $GLOBALS['cfg']['Server']['AllowDeny']['rules'][]
41 = "allow % from 255.255.2.0/4";
42 $GLOBALS['cfg']['Server']['AllowDeny']['rules'][]
43 = "allow % from 2001:4998:c:a0d:0000:0000:4998:1020";
44 $GLOBALS['cfg']['Server']['AllowDeny']['rules'][]
45 = "allow % from 2001:4998:c:a0d:0000:0000:4998:[1001-2010]";
46 $GLOBALS['cfg']['Server']['AllowDeny']['rules'][]
47 = "allow % from 2001:4998:c:a0d:0000:0000:4998:3020/24";
48 $GLOBALS['cfg']['Server']['AllowDeny']['rules'][] = "deny % 255.255.0.0/8";
49 $GLOBALS['cfg']['Server']['AllowDeny']['rules'][]
50 = "deny % from 255.255.0.0/8";
52 $this->ipAllowDeny = new IpAllowDeny();
55 /**
56 * Test for Core::getIp
58 * @param string $remote remote
59 * @param string $header header
60 * @param string $expected expected result
61 * @param string $proxyip proxyip
63 * @return void
65 * @dataProvider proxyIPs
67 public function testGetIp($remote, $header, $expected, $proxyip = null): void
69 unset($_SERVER['REMOTE_ADDR']);
70 unset($_SERVER['TEST_FORWARDED_HEADER']);
71 $GLOBALS['cfg']['TrustedProxies'] = [];
73 if ($remote !== null) {
74 $_SERVER['REMOTE_ADDR'] = $remote;
77 if ($header !== null) {
78 if ($proxyip === null) {
79 $proxyip = $remote;
81 $GLOBALS['cfg']['TrustedProxies'][$proxyip] = 'TEST_FORWARDED_HEADER';
82 $_SERVER['TEST_FORWARDED_HEADER'] = $header;
85 $this->assertEquals(
86 $expected,
87 Core::getIp()
90 unset($_SERVER['REMOTE_ADDR']);
91 unset($_SERVER['TEST_FORWARDED_HEADER']);
92 $GLOBALS['cfg']['TrustedProxies'] = [];
95 /**
96 * Data provider for Core::getIp tests
98 * @return array
100 public function proxyIPs()
102 return [
103 // Nothing set
105 null,
106 null,
107 false,
109 // Remote IP set
111 '101.0.0.25',
112 null,
113 '101.0.0.25',
115 // Proxy
117 '101.0.0.25',
118 '192.168.10.10',
119 '192.168.10.10',
121 // Several proxies
123 '101.0.0.25',
124 '192.168.10.1, 192.168.100.100',
125 '192.168.10.1',
127 // Invalid proxy
129 '101.0.0.25',
130 'invalid',
131 false,
133 // Direct IP with proxy enabled
135 '101.0.0.25',
136 '192.168.10.10',
137 '101.0.0.25',
138 '10.10.10.10',
144 * Test for ipMaskTest
146 * @return void
148 public function testIpMaskTest()
150 //IPV4 testing
151 $testRange = "255.255.0.0/8";
152 $ipToTest = "10.0.0.0";
153 $this->assertEquals(
154 false,
155 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
158 $testRange = "255.255.0.0/4";
159 $ipToTest = "255.3.0.0";
160 $this->assertEquals(
161 true,
162 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
165 $testRange = "255.255.0.[0-10]";
166 $ipToTest = "255.3.0.3";
167 $this->assertEquals(
168 false,
169 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
171 $ipToTest = "255.3.0.12";
172 $this->assertEquals(
173 false,
174 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
177 //IPV6 testing
178 //not range
179 $ipToTest = "2001:4998:c:a0d:0000:0000:4998:1020";
180 $testRange = "2001:4998:c:a0d:0000:0000:4998:1020";
181 $this->assertEquals(
182 true,
183 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
185 $ipToTest = "2001:4998:c:a0d:0000:0000:4998:1020";
186 $testRange = "2001:4998:c:a0d:0000:0000:4998:2020";
187 $this->assertEquals(
188 false,
189 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
192 //range
193 $ipToTest = "2001:4998:c:a0d:0000:0000:4998:1020";
194 $testRange = "2001:4998:c:a0d:0000:0000:4998:[1001-2010]";
195 $this->assertEquals(
196 true,
197 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
199 $ipToTest = "2001:4998:c:a0d:0000:0000:4998:3020";
200 $testRange = "2001:4998:c:a0d:0000:0000:4998:[1001-2010]";
201 $this->assertEquals(
202 false,
203 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
206 //CDIR
207 $ipToTest = "2001:4998:c:a0d:0000:0000:4998:1020";
208 $testRange = "2001:4998:c:a0d:0000:0000:4998:[1001-2010]";
209 $this->assertEquals(
210 true,
211 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
213 $ipToTest = "2001:4998:c:a0d:0000:0000:4998:1000";
214 $testRange = "2001:4998:c:a0d:0000:0000:4998:3020/24";
215 $this->assertEquals(
216 false,
217 $this->ipAllowDeny->ipMaskTest($testRange, $ipToTest)
222 * Test for allowDeny
224 * @return void
226 public function testAllowDeny()
228 $_SERVER['REMOTE_ADDR'] = "";
229 $this->assertEquals(
230 false,
231 $this->ipAllowDeny->allow()
234 $_SERVER['REMOTE_ADDR'] = "255.0.1.0";
235 $this->assertEquals(
236 true,
237 $this->ipAllowDeny->allow()
239 $_SERVER['REMOTE_ADDR'] = "10.0.0.0";
240 $this->assertEquals(
241 false,
242 $this->ipAllowDeny->allow()
245 $_SERVER['REMOTE_ADDR'] = "255.255.0.1";
246 $this->assertEquals(
247 true,
248 $this->ipAllowDeny->deny()
250 $_SERVER['REMOTE_ADDR'] = "255.124.0.5";
251 $this->assertEquals(
252 true,
253 $this->ipAllowDeny->deny()
255 $_SERVER['REMOTE_ADDR'] = "122.124.0.5";
256 $this->assertEquals(
257 false,
258 $this->ipAllowDeny->deny()
261 //IPV6
262 $_SERVER['REMOTE_ADDR'] = "2001:4998:c:a0d:0000:0000:4998:1020";
263 $this->assertEquals(
264 true,
265 $this->ipAllowDeny->allow()
267 $_SERVER['REMOTE_ADDR'] = "2001:4998:c:a0d:0000:0000:4998:1000";
268 $this->assertEquals(
269 false,
270 $this->ipAllowDeny->allow()
272 $_SERVER['REMOTE_ADDR'] = "2001:4998:c:a0d:0000:0000:4998:1020";
273 $this->assertEquals(
274 true,
275 $this->ipAllowDeny->allow()