1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
<?php
use dokuwiki\Ip;
class ip_test extends DokuWikiTest {
/**
* The data provider for ipToNumber() tests.
*
* @return mixed[][] Returns an array of test cases.
*/
public function ip_to_number_provider() : array
{
$tests = [
['127.0.0.1', 4, 0x00000000, 0x7f000001],
['::127.0.0.1', 6, 0x00000000, 0x7f000001],
['::1', 6, 0x00000000, 0x00000001],
['38AF:3033:AA39:CDE3:1A46:094C:44ED:5300', 6, 0x38AF3033AA39CDE3, 0x1A46094C44ED5300],
['193.53.125.7', 4, 0x00000000, 0xC1357D07],
];
return $tests;
}
/**
* Test ipToNumber().
*
* @dataProvider ip_to_number_provider
*
* @param string $ip The IP address to convert.
* @param int $version The IP version, either 4 or 6.
* @param int $upper The upper 64 bits of the IP.
* @param int $lower The lower 64 bits of the IP.
*
* @return void
*/
public function test_ip_to_number(string $ip, int $version, int $upper, int $lower): void
{
$result = Ip::ipToNumber($ip);
$this->assertSame($version, $result['version']);
$this->assertSame($upper, $result['upper']);
$this->assertSame($lower, $result['lower']);
}
/**
* The data provider for test_ip_in_range().
*
* @return mixed[][] Returns an array of test cases.
*/
public function ip_in_range_provider(): array
{
$tests = [
['192.168.11.2', '192.168.0.0/16', true],
['192.168.11.2', '192.168.64.1/16', true],
['192.168.11.2', '192.168.64.1/18', false],
['192.168.11.2', '192.168.11.0/20', true],
['127.0.0.1', '127.0.0.0/7', true],
['127.0.0.1', '127.0.0.0/8', true],
['127.0.0.1', '127.200.0.0/8', true],
['127.0.0.1', '127.200.0.0/9', false],
['127.0.0.1', '127.0.0.0/31', true],
['127.0.0.1', '127.0.0.0/32', false],
['127.0.0.1', '127.0.0.1/32', true],
['1111:2222:3333:4444:5555:6666:7777:8888', '1110::/12', true],
['1110:2222:3333:4444:5555:6666:7777:8888', '1110::/12', true],
['1100:2222:3333:4444:5555:6666:7777:8888', '1110::/12', false],
['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3300::/40', true],
['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3200::/40', false],
['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3333:4444:5555:6666:7777:8889/127', true],
['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3333:4444:5555:6666:7777:8889/128', false],
['1111:2222:3333:4444:5555:6666:7777:8889', '1111:2222:3333:4444:5555:6666:7777:8889/128', true],
['abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abcd', 'abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abcd/128', true],
['abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abce', 'abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abcd/128', false],
];
return $tests;
}
/**
* Test ipInRange().
*
* @dataProvider ip_in_range_provider
*
* @param string $ip The IP to test.
* @param string $range The IP range to test against.
* @param bool $expected The expected result from ipInRange().
*
* @return void
*/
public function test_ip_in_range(string $ip, string $range, bool $expected): void
{
$result = Ip::ipInRange($ip, $range);
$this->assertSame($expected, $result);
}
/**
* Data provider for test_ip_matches().
*
* @return mixed[][] Returns an array of test cases.
*/
public function ip_matches_provider(): array
{
// Tests for a CIDR range.
$rangeTests = $this->ip_in_range_provider();
// Tests for an exact IP match.
$exactTests = [
['127.0.0.1', '127.0.0.1', true],
['127.0.0.1', '127.0.0.0', false],
['aaaa:bbbb:cccc:dddd:eeee::', 'aaaa:bbbb:cccc:dddd:eeee:0000:0000:0000', true],
['aaaa:bbbb:cccc:dddd:eeee:0000:0000:0000', 'aaaa:bbbb:cccc:dddd:eeee::', true],
['aaaa:bbbb:0000:0000:0000:0000:0000:0001', 'aaaa:bbbb::1', true],
['aaaa:bbbb::0001', 'aaaa:bbbb::1', true],
['aaaa:bbbb::0001', 'aaaa:bbbb::', false],
['::ffff:127.0.0.1', '127.0.0.1', false],
['::ffff:127.0.0.1', '::0:ffff:127.0.0.1', true],
];
return array_merge($rangeTests, $exactTests);
}
/**
* Test ipMatches().
*
* @dataProvider ip_matches_provider
*
* @param string $ip The IP to test.
* @param string $ipOrRange The IP or IP range to test against.
* @param bool $expected The expeced result from ipMatches().
*
* @return void
*/
public function test_ip_matches(string $ip, string $ipOrRange, bool $expected): void
{
$result = Ip::ipMatches($ip, $ipOrRange);
$this->assertSame($expected, $result);
}
/**
* Data provider for proxyIsTrusted().
*
* @return mixed[][] Returns an array of test cases.
*/
public function proxy_is_trusted_provider(): array
{
// The new default configuration value.
$default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'];
// Adding some custom trusted proxies.
$custom = array_merge($default, ['1.2.3.4', '1122::', '3.0.0.1/8', '1111:2222::/32']);
$tests = [
// Empty configuration.
['', '127.0.0.1', false],
// Configuration with an array of IPs/CIDRs.
[$default, '127.0.0.1', true],
[$default, '127.1.2.3', true],
[$default, '10.1.2.3', true],
[$default, '11.1.2.3', false],
[$default, '172.16.0.1', true],
[$default, '172.160.0.1', false],
[$default, '172.31.255.255', true],
[$default, '172.32.0.0', false],
[$default, '172.200.0.0', false],
[$default, '192.168.2.3', true],
[$default, '192.169.1.2', false],
[$default, '::1', true],
[$default, '0000:0000:0000:0000:0000:0000:0000:0001', true],
// With custom proxies set.
[$custom, '127.0.0.1', true],
[$custom, '1.2.3.4', true],
[$custom, '3.0.1.2', true],
[$custom, '1122::', true],
[$custom, '1122:0000:0000:0000:0000:0000:0000:0000', true],
[$custom, '1111:2223::', false],
[$custom, '1111:2222::', true],
[$custom, '1111:2222:3333::', true],
[$custom, '1111:2222:3333::1', true],
];
return $tests;
}
/**
* Test proxyIsTrusted().
*
* @dataProvider proxy_is_trusted_provider
*
* @param string|string[] $config The value for $conf[trustedproxies].
* @param string $ip The proxy IP to test.
* @param bool $expected The expected result from proxyIsTrusted().
*/
public function test_proxy_is_trusted($config, string $ip, bool $expected): void
{
global $conf;
$conf['trustedproxies'] = $config;
$result = Ip::proxyIsTrusted($ip);
$this->assertSame($expected, $result);
}
/**
* Data provider for test_forwarded_for().
*
* @return mixed[][] Returns an array of test cases.
*/
public function forwarded_for_provider(): array
{
// The new default configuration value.
$default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'];
// Adding some custom trusted proxies.
$custom = array_merge($default, ['1.2.3.4', '1122::', '3.0.0.1/8', '1111:2222::/32']);
$tests = [
// Empty config value should always return empty array.
[[], '', '127.0.0.1', []],
[[], '127.0.0.1', '127.0.0.1', []],
// The new default configuration.
[$default, '', '127.0.0.1', []],
[$default, '1.2.3.4', '127.0.0.1', ['1.2.3.4', '127.0.0.1']],
[$default, '1.2.3.4', '192.168.1.1', ['1.2.3.4', '192.168.1.1']],
[$default, '1.2.3.4,172.16.0.1', '192.168.1.1', ['1.2.3.4', '172.16.0.1', '192.168.1.1']],
[$default, '1.2.3.4,172.16.0.1', '::1', ['1.2.3.4', '172.16.0.1', '::1']],
[$default, '1.2.3.4,172.16.0.1', '::0001', ['1.2.3.4', '172.16.0.1', '::0001']],
// Directly from an untrusted proxy.
[$default, '', '127.0.0.1', []],
[$default, '1.2.3.4', '11.22.33.44', []],
[$default, '::1', '11.22.33.44', []],
[$default, '::1', '::2', []],
// From a trusted proxy, but via an untrusted proxy.
[$default, '1.2.3.4,11.22.33.44,172.16.0.1', '192.168.1.1', []],
[$default, '1.2.3.4,::2,172.16.0.1', '::1', []],
// A custom configuration.
[$custom, '', '127.0.0.1', []],
[$custom, '1.2.3.4', '127.0.0.1', ['1.2.3.4', '127.0.0.1']],
[$custom, '1.2.3.4', '192.168.1.1', ['1.2.3.4', '192.168.1.1']],
[$custom, '1.2.3.4,172.16.0.1', '192.168.1.1', ['1.2.3.4', '172.16.0.1', '192.168.1.1']],
[$custom, '1.2.3.4,172.16.0.1', '::1', ['1.2.3.4', '172.16.0.1', '::1']],
[$custom, '1.2.3.4,172.16.0.1', '::0001', ['1.2.3.4', '172.16.0.1', '::0001']],
// Directly from an untrusted proxy.
[$custom, '', '127.0.0.1', []],
[$custom, '1.2.3.4', '11.22.33.44', []],
[$custom, '::1', '11.22.33.44', []],
[$custom, '::1', '::2', []],
// From a trusted proxy, but via an untrusted proxy.
[$custom, '1.2.3.4,11.22.33.44,172.16.0.1', '192.168.1.1', []],
[$custom, '1.2.3.4,::2,172.16.0.1', '::1', []],
// Via a custom proxy.
[$custom, '11.2.3.4,3.1.2.3,172.16.0.1', '192.168.1.1', ['11.2.3.4', '3.1.2.3', '172.16.0.1', '192.168.1.1']],
[$custom, '11.2.3.4,1122::,172.16.0.1', '3.0.0.1', ['11.2.3.4', '1122::', '172.16.0.1', '3.0.0.1']],
[$custom, '11.2.3.4,1122::,172.16.0.1', '1111:2222:3333::', ['11.2.3.4', '1122::', '172.16.0.1', '1111:2222:3333::']],
];
return $tests;
}
/**
* Test forwardedFor().
*
* @dataProvider forwarded_for_provider
*
* @param string|string[] $config The trustedproxies config value.
* @param string $header The X-Forwarded-For header value.
* @param string $remoteAddr The TCP/IP peer address.
* @param array $expected The expected result from forwardedFor().
*
* @return void
*/
public function test_forwarded_for($config, string $header, string $remoteAddr, array $expected): void
{
/* @var Input $INPUT */
global $INPUT, $conf;
$conf['trustedproxies'] = $config;
$INPUT->server->set('HTTP_X_FORWARDED_FOR', $header);
$INPUT->server->set('REMOTE_ADDR', $remoteAddr);
$result = Ip::forwardedFor();
$this->assertSame($expected, $result);
}
}
|