Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / IpValidatorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
14 use Symfony\Component\Validator\Constraints\Ip;
15 use Symfony\Component\Validator\Constraints\IpValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17
18 class IpValidatorTest extends ConstraintValidatorTestCase
19 {
20     protected function createValidator()
21     {
22         return new IpValidator();
23     }
24
25     public function testNullIsValid()
26     {
27         $this->validator->validate(null, new Ip());
28
29         $this->assertNoViolation();
30     }
31
32     public function testEmptyStringIsValid()
33     {
34         $this->validator->validate('', new Ip());
35
36         $this->assertNoViolation();
37     }
38
39     /**
40      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
41      */
42     public function testExpectsStringCompatibleType()
43     {
44         $this->validator->validate(new \stdClass(), new Ip());
45     }
46
47     /**
48      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
49      */
50     public function testInvalidValidatorVersion()
51     {
52         new Ip(array(
53             'version' => 666,
54         ));
55     }
56
57     /**
58      * @dataProvider getValidIpsV4
59      */
60     public function testValidIpsV4($ip)
61     {
62         $this->validator->validate($ip, new Ip(array(
63             'version' => Ip::V4,
64         )));
65
66         $this->assertNoViolation();
67     }
68
69     public function getValidIpsV4()
70     {
71         return array(
72             array('0.0.0.0'),
73             array('10.0.0.0'),
74             array('123.45.67.178'),
75             array('172.16.0.0'),
76             array('192.168.1.0'),
77             array('224.0.0.1'),
78             array('255.255.255.255'),
79             array('127.0.0.0'),
80         );
81     }
82
83     /**
84      * @dataProvider getValidIpsV6
85      */
86     public function testValidIpsV6($ip)
87     {
88         $this->validator->validate($ip, new Ip(array(
89             'version' => Ip::V6,
90         )));
91
92         $this->assertNoViolation();
93     }
94
95     public function getValidIpsV6()
96     {
97         return array(
98             array('2001:0db8:85a3:0000:0000:8a2e:0370:7334'),
99             array('2001:0DB8:85A3:0000:0000:8A2E:0370:7334'),
100             array('2001:0Db8:85a3:0000:0000:8A2e:0370:7334'),
101             array('fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c'),
102             array('fdc6:c46b:bb8f:7d4c:fdc6:c46b:bb8f:7d4c'),
103             array('fdc6:c46b:bb8f:7d4c:0000:8a2e:0370:7334'),
104             array('fe80:0000:0000:0000:0202:b3ff:fe1e:8329'),
105             array('fe80:0:0:0:202:b3ff:fe1e:8329'),
106             array('fe80::202:b3ff:fe1e:8329'),
107             array('0:0:0:0:0:0:0:0'),
108             array('::'),
109             array('0::'),
110             array('::0'),
111             array('0::0'),
112             // IPv4 mapped to IPv6
113             array('2001:0db8:85a3:0000:0000:8a2e:0.0.0.0'),
114             array('::0.0.0.0'),
115             array('::255.255.255.255'),
116             array('::123.45.67.178'),
117         );
118     }
119
120     /**
121      * @dataProvider getValidIpsAll
122      */
123     public function testValidIpsAll($ip)
124     {
125         $this->validator->validate($ip, new Ip(array(
126             'version' => Ip::ALL,
127         )));
128
129         $this->assertNoViolation();
130     }
131
132     public function getValidIpsAll()
133     {
134         return array_merge($this->getValidIpsV4(), $this->getValidIpsV6());
135     }
136
137     /**
138      * @dataProvider getInvalidIpsV4
139      */
140     public function testInvalidIpsV4($ip)
141     {
142         $constraint = new Ip(array(
143             'version' => Ip::V4,
144             'message' => 'myMessage',
145         ));
146
147         $this->validator->validate($ip, $constraint);
148
149         $this->buildViolation('myMessage')
150             ->setParameter('{{ value }}', '"'.$ip.'"')
151             ->setCode(Ip::INVALID_IP_ERROR)
152             ->assertRaised();
153     }
154
155     public function getInvalidIpsV4()
156     {
157         return array(
158             array('0'),
159             array('0.0'),
160             array('0.0.0'),
161             array('256.0.0.0'),
162             array('0.256.0.0'),
163             array('0.0.256.0'),
164             array('0.0.0.256'),
165             array('-1.0.0.0'),
166             array('foobar'),
167         );
168     }
169
170     /**
171      * @dataProvider getInvalidPrivateIpsV4
172      */
173     public function testInvalidPrivateIpsV4($ip)
174     {
175         $constraint = new Ip(array(
176             'version' => Ip::V4_NO_PRIV,
177             'message' => 'myMessage',
178         ));
179
180         $this->validator->validate($ip, $constraint);
181
182         $this->buildViolation('myMessage')
183             ->setParameter('{{ value }}', '"'.$ip.'"')
184             ->setCode(Ip::INVALID_IP_ERROR)
185             ->assertRaised();
186     }
187
188     public function getInvalidPrivateIpsV4()
189     {
190         return array(
191             array('10.0.0.0'),
192             array('172.16.0.0'),
193             array('192.168.1.0'),
194         );
195     }
196
197     /**
198      * @dataProvider getInvalidReservedIpsV4
199      */
200     public function testInvalidReservedIpsV4($ip)
201     {
202         $constraint = new Ip(array(
203             'version' => Ip::V4_NO_RES,
204             'message' => 'myMessage',
205         ));
206
207         $this->validator->validate($ip, $constraint);
208
209         $this->buildViolation('myMessage')
210             ->setParameter('{{ value }}', '"'.$ip.'"')
211             ->setCode(Ip::INVALID_IP_ERROR)
212             ->assertRaised();
213     }
214
215     public function getInvalidReservedIpsV4()
216     {
217         return array(
218             array('0.0.0.0'),
219             array('240.0.0.1'),
220             array('255.255.255.255'),
221         );
222     }
223
224     /**
225      * @dataProvider getInvalidPublicIpsV4
226      */
227     public function testInvalidPublicIpsV4($ip)
228     {
229         $constraint = new Ip(array(
230             'version' => Ip::V4_ONLY_PUBLIC,
231             'message' => 'myMessage',
232         ));
233
234         $this->validator->validate($ip, $constraint);
235
236         $this->buildViolation('myMessage')
237             ->setParameter('{{ value }}', '"'.$ip.'"')
238             ->setCode(Ip::INVALID_IP_ERROR)
239             ->assertRaised();
240     }
241
242     public function getInvalidPublicIpsV4()
243     {
244         return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidReservedIpsV4());
245     }
246
247     /**
248      * @dataProvider getInvalidIpsV6
249      */
250     public function testInvalidIpsV6($ip)
251     {
252         $constraint = new Ip(array(
253             'version' => Ip::V6,
254             'message' => 'myMessage',
255         ));
256
257         $this->validator->validate($ip, $constraint);
258
259         $this->buildViolation('myMessage')
260             ->setParameter('{{ value }}', '"'.$ip.'"')
261             ->setCode(Ip::INVALID_IP_ERROR)
262             ->assertRaised();
263     }
264
265     public function getInvalidIpsV6()
266     {
267         return array(
268             array('z001:0db8:85a3:0000:0000:8a2e:0370:7334'),
269             array('fe80'),
270             array('fe80:8329'),
271             array('fe80:::202:b3ff:fe1e:8329'),
272             array('fe80::202:b3ff::fe1e:8329'),
273             // IPv4 mapped to IPv6
274             array('2001:0db8:85a3:0000:0000:8a2e:0370:0.0.0.0'),
275             array('::0.0'),
276             array('::0.0.0'),
277             array('::256.0.0.0'),
278             array('::0.256.0.0'),
279             array('::0.0.256.0'),
280             array('::0.0.0.256'),
281         );
282     }
283
284     /**
285      * @dataProvider getInvalidPrivateIpsV6
286      */
287     public function testInvalidPrivateIpsV6($ip)
288     {
289         $constraint = new Ip(array(
290             'version' => Ip::V6_NO_PRIV,
291             'message' => 'myMessage',
292         ));
293
294         $this->validator->validate($ip, $constraint);
295
296         $this->buildViolation('myMessage')
297             ->setParameter('{{ value }}', '"'.$ip.'"')
298             ->setCode(Ip::INVALID_IP_ERROR)
299             ->assertRaised();
300     }
301
302     public function getInvalidPrivateIpsV6()
303     {
304         return array(
305             array('fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c'),
306             array('fdc6:c46b:bb8f:7d4c:fdc6:c46b:bb8f:7d4c'),
307             array('fdc6:c46b:bb8f:7d4c:0000:8a2e:0370:7334'),
308         );
309     }
310
311     /**
312      * @dataProvider getInvalidReservedIpsV6
313      */
314     public function testInvalidReservedIpsV6($ip)
315     {
316         $constraint = new Ip(array(
317             'version' => Ip::V6_NO_RES,
318             'message' => 'myMessage',
319         ));
320
321         $this->validator->validate($ip, $constraint);
322
323         $this->buildViolation('myMessage')
324             ->setParameter('{{ value }}', '"'.$ip.'"')
325             ->setCode(Ip::INVALID_IP_ERROR)
326             ->assertRaised();
327     }
328
329     public function getInvalidReservedIpsV6()
330     {
331         // Quoting after official filter documentation:
332         // "FILTER_FLAG_NO_RES_RANGE = This flag does not apply to IPv6 addresses."
333         // Full description: http://php.net/manual/en/filter.filters.flags.php
334         return $this->getInvalidIpsV6();
335     }
336
337     /**
338      * @dataProvider getInvalidPublicIpsV6
339      */
340     public function testInvalidPublicIpsV6($ip)
341     {
342         $constraint = new Ip(array(
343             'version' => Ip::V6_ONLY_PUBLIC,
344             'message' => 'myMessage',
345         ));
346
347         $this->validator->validate($ip, $constraint);
348
349         $this->buildViolation('myMessage')
350             ->setParameter('{{ value }}', '"'.$ip.'"')
351             ->setCode(Ip::INVALID_IP_ERROR)
352             ->assertRaised();
353     }
354
355     public function getInvalidPublicIpsV6()
356     {
357         return array_merge($this->getInvalidPrivateIpsV6(), $this->getInvalidReservedIpsV6());
358     }
359
360     /**
361      * @dataProvider getInvalidIpsAll
362      */
363     public function testInvalidIpsAll($ip)
364     {
365         $constraint = new Ip(array(
366             'version' => Ip::ALL,
367             'message' => 'myMessage',
368         ));
369
370         $this->validator->validate($ip, $constraint);
371
372         $this->buildViolation('myMessage')
373             ->setParameter('{{ value }}', '"'.$ip.'"')
374             ->setCode(Ip::INVALID_IP_ERROR)
375             ->assertRaised();
376     }
377
378     public function getInvalidIpsAll()
379     {
380         return array_merge($this->getInvalidIpsV4(), $this->getInvalidIpsV6());
381     }
382
383     /**
384      * @dataProvider getInvalidPrivateIpsAll
385      */
386     public function testInvalidPrivateIpsAll($ip)
387     {
388         $constraint = new Ip(array(
389             'version' => Ip::ALL_NO_PRIV,
390             'message' => 'myMessage',
391         ));
392
393         $this->validator->validate($ip, $constraint);
394
395         $this->buildViolation('myMessage')
396             ->setParameter('{{ value }}', '"'.$ip.'"')
397             ->setCode(Ip::INVALID_IP_ERROR)
398             ->assertRaised();
399     }
400
401     public function getInvalidPrivateIpsAll()
402     {
403         return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidPrivateIpsV6());
404     }
405
406     /**
407      * @dataProvider getInvalidReservedIpsAll
408      */
409     public function testInvalidReservedIpsAll($ip)
410     {
411         $constraint = new Ip(array(
412             'version' => Ip::ALL_NO_RES,
413             'message' => 'myMessage',
414         ));
415
416         $this->validator->validate($ip, $constraint);
417
418         $this->buildViolation('myMessage')
419             ->setParameter('{{ value }}', '"'.$ip.'"')
420             ->setCode(Ip::INVALID_IP_ERROR)
421             ->assertRaised();
422     }
423
424     public function getInvalidReservedIpsAll()
425     {
426         return array_merge($this->getInvalidReservedIpsV4(), $this->getInvalidReservedIpsV6());
427     }
428
429     /**
430      * @dataProvider getInvalidPublicIpsAll
431      */
432     public function testInvalidPublicIpsAll($ip)
433     {
434         $constraint = new Ip(array(
435             'version' => Ip::ALL_ONLY_PUBLIC,
436             'message' => 'myMessage',
437         ));
438
439         $this->validator->validate($ip, $constraint);
440
441         $this->buildViolation('myMessage')
442             ->setParameter('{{ value }}', '"'.$ip.'"')
443             ->setCode(Ip::INVALID_IP_ERROR)
444             ->assertRaised();
445     }
446
447     public function getInvalidPublicIpsAll()
448     {
449         return array_merge($this->getInvalidPublicIpsV4(), $this->getInvalidPublicIpsV6());
450     }
451 }