4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
14 use Symfony\Component\Validator\Constraints\Ip;
15 use Symfony\Component\Validator\Constraints\IpValidator;
16 use Symfony\Component\Validator\Validation;
18 class IpValidatorTest extends AbstractConstraintValidatorTest
20 protected function getApiVersion()
22 return Validation::API_VERSION_2_5;
25 protected function createValidator()
27 return new IpValidator();
30 public function testNullIsValid()
32 $this->validator->validate(null, new Ip());
34 $this->assertNoViolation();
37 public function testEmptyStringIsValid()
39 $this->validator->validate('', new Ip());
41 $this->assertNoViolation();
45 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
47 public function testExpectsStringCompatibleType()
49 $this->validator->validate(new \stdClass(), new Ip());
53 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
55 public function testInvalidValidatorVersion()
63 * @dataProvider getValidIpsV4
65 public function testValidIpsV4($ip)
67 $this->validator->validate($ip, new Ip(array(
71 $this->assertNoViolation();
74 public function getValidIpsV4()
79 array('123.45.67.178'),
83 array('255.255.255.255'),
89 * @dataProvider getValidIpsV6
91 public function testValidIpsV6($ip)
93 $this->validator->validate($ip, new Ip(array(
97 $this->assertNoViolation();
100 public function getValidIpsV6()
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'),
117 // IPv4 mapped to IPv6
118 array('2001:0db8:85a3:0000:0000:8a2e:0.0.0.0'),
120 array('::255.255.255.255'),
121 array('::123.45.67.178'),
126 * @dataProvider getValidIpsAll
128 public function testValidIpsAll($ip)
130 $this->validator->validate($ip, new Ip(array(
131 'version' => Ip::ALL,
134 $this->assertNoViolation();
137 public function getValidIpsAll()
139 return array_merge($this->getValidIpsV4(), $this->getValidIpsV6());
143 * @dataProvider getInvalidIpsV4
145 public function testInvalidIpsV4($ip)
147 $constraint = new Ip(array(
149 'message' => 'myMessage',
152 $this->validator->validate($ip, $constraint);
154 $this->buildViolation('myMessage')
155 ->setParameter('{{ value }}', '"'.$ip.'"')
156 ->setCode(Ip::INVALID_IP_ERROR)
160 public function getInvalidIpsV4()
176 * @dataProvider getInvalidPrivateIpsV4
178 public function testInvalidPrivateIpsV4($ip)
180 $constraint = new Ip(array(
181 'version' => Ip::V4_NO_PRIV,
182 'message' => 'myMessage',
185 $this->validator->validate($ip, $constraint);
187 $this->buildViolation('myMessage')
188 ->setParameter('{{ value }}', '"'.$ip.'"')
189 ->setCode(Ip::INVALID_IP_ERROR)
193 public function getInvalidPrivateIpsV4()
198 array('192.168.1.0'),
203 * @dataProvider getInvalidReservedIpsV4
205 public function testInvalidReservedIpsV4($ip)
207 $constraint = new Ip(array(
208 'version' => Ip::V4_NO_RES,
209 'message' => 'myMessage',
212 $this->validator->validate($ip, $constraint);
214 $this->buildViolation('myMessage')
215 ->setParameter('{{ value }}', '"'.$ip.'"')
216 ->setCode(Ip::INVALID_IP_ERROR)
220 public function getInvalidReservedIpsV4()
225 array('255.255.255.255'),
230 * @dataProvider getInvalidPublicIpsV4
232 public function testInvalidPublicIpsV4($ip)
234 $constraint = new Ip(array(
235 'version' => Ip::V4_ONLY_PUBLIC,
236 'message' => 'myMessage',
239 $this->validator->validate($ip, $constraint);
241 $this->buildViolation('myMessage')
242 ->setParameter('{{ value }}', '"'.$ip.'"')
243 ->setCode(Ip::INVALID_IP_ERROR)
247 public function getInvalidPublicIpsV4()
249 return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidReservedIpsV4());
253 * @dataProvider getInvalidIpsV6
255 public function testInvalidIpsV6($ip)
257 $constraint = new Ip(array(
259 'message' => 'myMessage',
262 $this->validator->validate($ip, $constraint);
264 $this->buildViolation('myMessage')
265 ->setParameter('{{ value }}', '"'.$ip.'"')
266 ->setCode(Ip::INVALID_IP_ERROR)
270 public function getInvalidIpsV6()
273 array('z001:0db8:85a3:0000:0000:8a2e:0370:7334'),
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'),
282 array('::256.0.0.0'),
283 array('::0.256.0.0'),
284 array('::0.0.256.0'),
285 array('::0.0.0.256'),
290 * @dataProvider getInvalidPrivateIpsV6
292 public function testInvalidPrivateIpsV6($ip)
294 $constraint = new Ip(array(
295 'version' => Ip::V6_NO_PRIV,
296 'message' => 'myMessage',
299 $this->validator->validate($ip, $constraint);
301 $this->buildViolation('myMessage')
302 ->setParameter('{{ value }}', '"'.$ip.'"')
303 ->setCode(Ip::INVALID_IP_ERROR)
307 public function getInvalidPrivateIpsV6()
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'),
317 * @dataProvider getInvalidReservedIpsV6
319 public function testInvalidReservedIpsV6($ip)
321 $constraint = new Ip(array(
322 'version' => Ip::V6_NO_RES,
323 'message' => 'myMessage',
326 $this->validator->validate($ip, $constraint);
328 $this->buildViolation('myMessage')
329 ->setParameter('{{ value }}', '"'.$ip.'"')
330 ->setCode(Ip::INVALID_IP_ERROR)
334 public function getInvalidReservedIpsV6()
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();
343 * @dataProvider getInvalidPublicIpsV6
345 public function testInvalidPublicIpsV6($ip)
347 $constraint = new Ip(array(
348 'version' => Ip::V6_ONLY_PUBLIC,
349 'message' => 'myMessage',
352 $this->validator->validate($ip, $constraint);
354 $this->buildViolation('myMessage')
355 ->setParameter('{{ value }}', '"'.$ip.'"')
356 ->setCode(Ip::INVALID_IP_ERROR)
360 public function getInvalidPublicIpsV6()
362 return array_merge($this->getInvalidPrivateIpsV6(), $this->getInvalidReservedIpsV6());
366 * @dataProvider getInvalidIpsAll
368 public function testInvalidIpsAll($ip)
370 $constraint = new Ip(array(
371 'version' => Ip::ALL,
372 'message' => 'myMessage',
375 $this->validator->validate($ip, $constraint);
377 $this->buildViolation('myMessage')
378 ->setParameter('{{ value }}', '"'.$ip.'"')
379 ->setCode(Ip::INVALID_IP_ERROR)
383 public function getInvalidIpsAll()
385 return array_merge($this->getInvalidIpsV4(), $this->getInvalidIpsV6());
389 * @dataProvider getInvalidPrivateIpsAll
391 public function testInvalidPrivateIpsAll($ip)
393 $constraint = new Ip(array(
394 'version' => Ip::ALL_NO_PRIV,
395 'message' => 'myMessage',
398 $this->validator->validate($ip, $constraint);
400 $this->buildViolation('myMessage')
401 ->setParameter('{{ value }}', '"'.$ip.'"')
402 ->setCode(Ip::INVALID_IP_ERROR)
406 public function getInvalidPrivateIpsAll()
408 return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidPrivateIpsV6());
412 * @dataProvider getInvalidReservedIpsAll
414 public function testInvalidReservedIpsAll($ip)
416 $constraint = new Ip(array(
417 'version' => Ip::ALL_NO_RES,
418 'message' => 'myMessage',
421 $this->validator->validate($ip, $constraint);
423 $this->buildViolation('myMessage')
424 ->setParameter('{{ value }}', '"'.$ip.'"')
425 ->setCode(Ip::INVALID_IP_ERROR)
429 public function getInvalidReservedIpsAll()
431 return array_merge($this->getInvalidReservedIpsV4(), $this->getInvalidReservedIpsV6());
435 * @dataProvider getInvalidPublicIpsAll
437 public function testInvalidPublicIpsAll($ip)
439 $constraint = new Ip(array(
440 'version' => Ip::ALL_ONLY_PUBLIC,
441 'message' => 'myMessage',
444 $this->validator->validate($ip, $constraint);
446 $this->buildViolation('myMessage')
447 ->setParameter('{{ value }}', '"'.$ip.'"')
448 ->setCode(Ip::INVALID_IP_ERROR)
452 public function getInvalidPublicIpsAll()
454 return array_merge($this->getInvalidPublicIpsV4(), $this->getInvalidPublicIpsV6());