IpValidatorTest.php 12 KB

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