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