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\Test\ConstraintValidatorTestCase;
18 class IpValidatorTest extends ConstraintValidatorTestCase
20 protected function createValidator()
22 return new IpValidator();
25 public function testNullIsValid()
27 $this->validator->validate(null, new Ip());
29 $this->assertNoViolation();
32 public function testEmptyStringIsValid()
34 $this->validator->validate('', new Ip());
36 $this->assertNoViolation();
40 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
42 public function testExpectsStringCompatibleType()
44 $this->validator->validate(new \stdClass(), new Ip());
48 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
50 public function testInvalidValidatorVersion()
58 * @dataProvider getValidIpsV4
60 public function testValidIpsV4($ip)
62 $this->validator->validate($ip, new Ip(array(
66 $this->assertNoViolation();
69 public function getValidIpsV4()
74 array('123.45.67.178'),
78 array('255.255.255.255'),
84 * @dataProvider getValidIpsV6
86 public function testValidIpsV6($ip)
88 $this->validator->validate($ip, new Ip(array(
92 $this->assertNoViolation();
95 public function getValidIpsV6()
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'),
112 // IPv4 mapped to IPv6
113 array('2001:0db8:85a3:0000:0000:8a2e:0.0.0.0'),
115 array('::255.255.255.255'),
116 array('::123.45.67.178'),
121 * @dataProvider getValidIpsAll
123 public function testValidIpsAll($ip)
125 $this->validator->validate($ip, new Ip(array(
126 'version' => Ip::ALL,
129 $this->assertNoViolation();
132 public function getValidIpsAll()
134 return array_merge($this->getValidIpsV4(), $this->getValidIpsV6());
138 * @dataProvider getInvalidIpsV4
140 public function testInvalidIpsV4($ip)
142 $constraint = new Ip(array(
144 'message' => 'myMessage',
147 $this->validator->validate($ip, $constraint);
149 $this->buildViolation('myMessage')
150 ->setParameter('{{ value }}', '"'.$ip.'"')
151 ->setCode(Ip::INVALID_IP_ERROR)
155 public function getInvalidIpsV4()
171 * @dataProvider getInvalidPrivateIpsV4
173 public function testInvalidPrivateIpsV4($ip)
175 $constraint = new Ip(array(
176 'version' => Ip::V4_NO_PRIV,
177 'message' => 'myMessage',
180 $this->validator->validate($ip, $constraint);
182 $this->buildViolation('myMessage')
183 ->setParameter('{{ value }}', '"'.$ip.'"')
184 ->setCode(Ip::INVALID_IP_ERROR)
188 public function getInvalidPrivateIpsV4()
193 array('192.168.1.0'),
198 * @dataProvider getInvalidReservedIpsV4
200 public function testInvalidReservedIpsV4($ip)
202 $constraint = new Ip(array(
203 'version' => Ip::V4_NO_RES,
204 'message' => 'myMessage',
207 $this->validator->validate($ip, $constraint);
209 $this->buildViolation('myMessage')
210 ->setParameter('{{ value }}', '"'.$ip.'"')
211 ->setCode(Ip::INVALID_IP_ERROR)
215 public function getInvalidReservedIpsV4()
220 array('255.255.255.255'),
225 * @dataProvider getInvalidPublicIpsV4
227 public function testInvalidPublicIpsV4($ip)
229 $constraint = new Ip(array(
230 'version' => Ip::V4_ONLY_PUBLIC,
231 'message' => 'myMessage',
234 $this->validator->validate($ip, $constraint);
236 $this->buildViolation('myMessage')
237 ->setParameter('{{ value }}', '"'.$ip.'"')
238 ->setCode(Ip::INVALID_IP_ERROR)
242 public function getInvalidPublicIpsV4()
244 return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidReservedIpsV4());
248 * @dataProvider getInvalidIpsV6
250 public function testInvalidIpsV6($ip)
252 $constraint = new Ip(array(
254 'message' => 'myMessage',
257 $this->validator->validate($ip, $constraint);
259 $this->buildViolation('myMessage')
260 ->setParameter('{{ value }}', '"'.$ip.'"')
261 ->setCode(Ip::INVALID_IP_ERROR)
265 public function getInvalidIpsV6()
268 array('z001:0db8:85a3:0000:0000:8a2e:0370:7334'),
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'),
277 array('::256.0.0.0'),
278 array('::0.256.0.0'),
279 array('::0.0.256.0'),
280 array('::0.0.0.256'),
285 * @dataProvider getInvalidPrivateIpsV6
287 public function testInvalidPrivateIpsV6($ip)
289 $constraint = new Ip(array(
290 'version' => Ip::V6_NO_PRIV,
291 'message' => 'myMessage',
294 $this->validator->validate($ip, $constraint);
296 $this->buildViolation('myMessage')
297 ->setParameter('{{ value }}', '"'.$ip.'"')
298 ->setCode(Ip::INVALID_IP_ERROR)
302 public function getInvalidPrivateIpsV6()
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'),
312 * @dataProvider getInvalidReservedIpsV6
314 public function testInvalidReservedIpsV6($ip)
316 $constraint = new Ip(array(
317 'version' => Ip::V6_NO_RES,
318 'message' => 'myMessage',
321 $this->validator->validate($ip, $constraint);
323 $this->buildViolation('myMessage')
324 ->setParameter('{{ value }}', '"'.$ip.'"')
325 ->setCode(Ip::INVALID_IP_ERROR)
329 public function getInvalidReservedIpsV6()
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();
338 * @dataProvider getInvalidPublicIpsV6
340 public function testInvalidPublicIpsV6($ip)
342 $constraint = new Ip(array(
343 'version' => Ip::V6_ONLY_PUBLIC,
344 'message' => 'myMessage',
347 $this->validator->validate($ip, $constraint);
349 $this->buildViolation('myMessage')
350 ->setParameter('{{ value }}', '"'.$ip.'"')
351 ->setCode(Ip::INVALID_IP_ERROR)
355 public function getInvalidPublicIpsV6()
357 return array_merge($this->getInvalidPrivateIpsV6(), $this->getInvalidReservedIpsV6());
361 * @dataProvider getInvalidIpsAll
363 public function testInvalidIpsAll($ip)
365 $constraint = new Ip(array(
366 'version' => Ip::ALL,
367 'message' => 'myMessage',
370 $this->validator->validate($ip, $constraint);
372 $this->buildViolation('myMessage')
373 ->setParameter('{{ value }}', '"'.$ip.'"')
374 ->setCode(Ip::INVALID_IP_ERROR)
378 public function getInvalidIpsAll()
380 return array_merge($this->getInvalidIpsV4(), $this->getInvalidIpsV6());
384 * @dataProvider getInvalidPrivateIpsAll
386 public function testInvalidPrivateIpsAll($ip)
388 $constraint = new Ip(array(
389 'version' => Ip::ALL_NO_PRIV,
390 'message' => 'myMessage',
393 $this->validator->validate($ip, $constraint);
395 $this->buildViolation('myMessage')
396 ->setParameter('{{ value }}', '"'.$ip.'"')
397 ->setCode(Ip::INVALID_IP_ERROR)
401 public function getInvalidPrivateIpsAll()
403 return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidPrivateIpsV6());
407 * @dataProvider getInvalidReservedIpsAll
409 public function testInvalidReservedIpsAll($ip)
411 $constraint = new Ip(array(
412 'version' => Ip::ALL_NO_RES,
413 'message' => 'myMessage',
416 $this->validator->validate($ip, $constraint);
418 $this->buildViolation('myMessage')
419 ->setParameter('{{ value }}', '"'.$ip.'"')
420 ->setCode(Ip::INVALID_IP_ERROR)
424 public function getInvalidReservedIpsAll()
426 return array_merge($this->getInvalidReservedIpsV4(), $this->getInvalidReservedIpsV6());
430 * @dataProvider getInvalidPublicIpsAll
432 public function testInvalidPublicIpsAll($ip)
434 $constraint = new Ip(array(
435 'version' => Ip::ALL_ONLY_PUBLIC,
436 'message' => 'myMessage',
439 $this->validator->validate($ip, $constraint);
441 $this->buildViolation('myMessage')
442 ->setParameter('{{ value }}', '"'.$ip.'"')
443 ->setCode(Ip::INVALID_IP_ERROR)
447 public function getInvalidPublicIpsAll()
449 return array_merge($this->getInvalidPublicIpsV4(), $this->getInvalidPublicIpsV6());