IpValidatorTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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\Tests\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraints\Ip;
  12. use Symfony\Component\Validator\Constraints\IpValidator;
  13. class IpValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $validator;
  16. protected function setUp()
  17. {
  18. $this->validator = new IpValidator();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->validator = null;
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid(null, new Ip()));
  27. }
  28. public function testEmptyStringIsValid()
  29. {
  30. $this->assertTrue($this->validator->isValid('', new Ip()));
  31. }
  32. public function testExpectsStringCompatibleType()
  33. {
  34. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  35. $this->validator->isValid(new \stdClass(), new Ip());
  36. }
  37. public function testInvalidValidatorVersion()
  38. {
  39. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  40. $ip = new Ip(array(
  41. 'version' => 666,
  42. ));
  43. }
  44. /**
  45. * @dataProvider getValidIpsV4
  46. */
  47. public function testValidIpsV4($ip)
  48. {
  49. $this->assertTrue($this->validator->isValid($ip, new Ip(array(
  50. 'version' => Ip::V4,
  51. ))));
  52. }
  53. public function getValidIpsV4()
  54. {
  55. return array(
  56. array('0.0.0.0'),
  57. array('10.0.0.0'),
  58. array('123.45.67.178'),
  59. array('172.16.0.0'),
  60. array('192.168.1.0'),
  61. array('224.0.0.1'),
  62. array('255.255.255.255'),
  63. array('127.0.0.0'),
  64. );
  65. }
  66. /**
  67. * @dataProvider getValidIpsV6
  68. */
  69. public function testValidIpsV6($ip)
  70. {
  71. $this->assertTrue($this->validator->isValid($ip, new Ip(array(
  72. 'version' => Ip::V6,
  73. ))));
  74. }
  75. public function getValidIpsV6()
  76. {
  77. return array(
  78. array('2001:0db8:85a3:0000:0000:8a2e:0370:7334'),
  79. array('2001:0DB8:85A3:0000:0000:8A2E:0370:7334'),
  80. array('2001:0Db8:85a3:0000:0000:8A2e:0370:7334'),
  81. array('fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c'),
  82. array('fdc6:c46b:bb8f:7d4c:fdc6:c46b:bb8f:7d4c'),
  83. array('fdc6:c46b:bb8f:7d4c:0000:8a2e:0370:7334'),
  84. array('fe80:0000:0000:0000:0202:b3ff:fe1e:8329'),
  85. array('fe80:0:0:0:202:b3ff:fe1e:8329'),
  86. array('fe80::202:b3ff:fe1e:8329'),
  87. array('0:0:0:0:0:0:0:0'),
  88. array('::'),
  89. array('0::'),
  90. array('::0'),
  91. array('0::0'),
  92. // IPv4 mapped to IPv6
  93. array('2001:0db8:85a3:0000:0000:8a2e:0.0.0.0'),
  94. array('::0.0.0.0'),
  95. array('::255.255.255.255'),
  96. array('::123.45.67.178'),
  97. );
  98. }
  99. /**
  100. * @dataProvider getValidIpsAll
  101. */
  102. public function testValidIpsAll($ip)
  103. {
  104. $this->assertTrue($this->validator->isValid($ip, new Ip(array(
  105. 'version' => Ip::ALL,
  106. ))));
  107. }
  108. public function getValidIpsAll()
  109. {
  110. return array_merge($this->getValidIpsV4(), $this->getValidIpsV6());
  111. }
  112. /**
  113. * @dataProvider getInvalidIpsV4
  114. */
  115. public function testInvalidIpsV4($ip)
  116. {
  117. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  118. 'version' => Ip::V4,
  119. ))));
  120. }
  121. public function getInvalidIpsV4()
  122. {
  123. return array(
  124. array('0'),
  125. array('0.0'),
  126. array('0.0.0'),
  127. array('256.0.0.0'),
  128. array('0.256.0.0'),
  129. array('0.0.256.0'),
  130. array('0.0.0.256'),
  131. array('-1.0.0.0'),
  132. array('foobar'),
  133. );
  134. }
  135. /**
  136. * @dataProvider getInvalidPrivateIpsV4
  137. */
  138. public function testInvalidPrivateIpsV4($ip)
  139. {
  140. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  141. 'version' => Ip::V4_NO_PRIV,
  142. ))));
  143. }
  144. public function getInvalidPrivateIpsV4()
  145. {
  146. return array(
  147. array('10.0.0.0'),
  148. array('172.16.0.0'),
  149. array('192.168.1.0'),
  150. );
  151. }
  152. /**
  153. * @dataProvider getInvalidReservedIpsV4
  154. */
  155. public function testInvalidReservedIpsV4($ip)
  156. {
  157. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  158. 'version' => Ip::V4_NO_RES,
  159. ))));
  160. }
  161. public function getInvalidReservedIpsV4()
  162. {
  163. return array(
  164. array('0.0.0.0'),
  165. array('224.0.0.1'),
  166. array('255.255.255.255'),
  167. );
  168. }
  169. /**
  170. * @dataProvider getInvalidPublicIpsV4
  171. */
  172. public function testInvalidPublicIpsV4($ip)
  173. {
  174. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  175. 'version' => Ip::V4_ONLY_PUBLIC,
  176. ))));
  177. }
  178. public function getInvalidPublicIpsV4()
  179. {
  180. return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidReservedIpsV4());
  181. }
  182. /**
  183. * @dataProvider getInvalidIpsV6
  184. */
  185. public function testInvalidIpsV6($ip)
  186. {
  187. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  188. 'version' => Ip::V6,
  189. ))));
  190. }
  191. public function getInvalidIpsV6()
  192. {
  193. return array(
  194. array('z001:0db8:85a3:0000:0000:8a2e:0370:7334'),
  195. array('fe80'),
  196. array('fe80:8329'),
  197. array('fe80:::202:b3ff:fe1e:8329'),
  198. array('fe80::202:b3ff::fe1e:8329'),
  199. // IPv4 mapped to IPv6
  200. array('2001:0db8:85a3:0000:0000:8a2e:0370:0.0.0.0'),
  201. array('::0.0'),
  202. array('::0.0.0'),
  203. array('::256.0.0.0'),
  204. array('::0.256.0.0'),
  205. array('::0.0.256.0'),
  206. array('::0.0.0.256'),
  207. );
  208. }
  209. /**
  210. * @dataProvider getInvalidPrivateIpsV6
  211. */
  212. public function testInvalidPrivateIpsV6($ip)
  213. {
  214. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  215. 'version' => Ip::V6_NO_PRIV,
  216. ))));
  217. }
  218. public function getInvalidPrivateIpsV6()
  219. {
  220. return array(
  221. array('fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c'),
  222. array('fdc6:c46b:bb8f:7d4c:fdc6:c46b:bb8f:7d4c'),
  223. array('fdc6:c46b:bb8f:7d4c:0000:8a2e:0370:7334'),
  224. );
  225. }
  226. /**
  227. * @dataProvider getInvalidReservedIpsV6
  228. */
  229. public function testInvalidReservedIpsV6($ip)
  230. {
  231. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  232. 'version' => Ip::V6_NO_RES,
  233. ))));
  234. }
  235. public function getInvalidReservedIpsV6()
  236. {
  237. // Quoting after official filter documentation:
  238. // "FILTER_FLAG_NO_RES_RANGE = This flag does not apply to IPv6 addresses."
  239. // Full description: http://php.net/manual/en/filter.filters.flags.php
  240. return $this->getInvalidIpsV6();
  241. }
  242. /**
  243. * @dataProvider getInvalidPublicIpsV6
  244. */
  245. public function testInvalidPublicIpsV6($ip)
  246. {
  247. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  248. 'version' => Ip::V6_ONLY_PUBLIC,
  249. ))));
  250. }
  251. public function getInvalidPublicIpsV6()
  252. {
  253. return array_merge($this->getInvalidPrivateIpsV6(), $this->getInvalidReservedIpsV6());
  254. }
  255. /**
  256. * @dataProvider getInvalidIpsAll
  257. */
  258. public function testInvalidIpsAll($ip)
  259. {
  260. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  261. 'version' => Ip::ALL,
  262. ))));
  263. }
  264. public function getInvalidIpsAll()
  265. {
  266. return array_merge($this->getInvalidIpsV4(), $this->getInvalidIpsV6());
  267. }
  268. /**
  269. * @dataProvider getInvalidPrivateIpsAll
  270. */
  271. public function testInvalidPrivateIpsAll($ip)
  272. {
  273. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  274. 'version' => Ip::ALL_NO_PRIV,
  275. ))));
  276. }
  277. public function getInvalidPrivateIpsAll()
  278. {
  279. return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidPrivateIpsV6());
  280. }
  281. /**
  282. * @dataProvider getInvalidReservedIpsAll
  283. */
  284. public function testInvalidReservedIpsAll($ip)
  285. {
  286. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  287. 'version' => Ip::ALL_NO_RES,
  288. ))));
  289. }
  290. public function getInvalidReservedIpsAll()
  291. {
  292. return array_merge($this->getInvalidReservedIpsV4(), $this->getInvalidReservedIpsV6());
  293. }
  294. /**
  295. * @dataProvider getInvalidPublicIpsAll
  296. */
  297. public function testInvalidPublicIpsAll($ip)
  298. {
  299. $this->assertFalse($this->validator->isValid($ip, new Ip(array(
  300. 'version' => Ip::ALL_ONLY_PUBLIC,
  301. ))));
  302. }
  303. public function getInvalidPublicIpsAll()
  304. {
  305. return array_merge($this->getInvalidPublicIpsV4(), $this->getInvalidPublicIpsV6());
  306. }
  307. public function testMessageIsSet()
  308. {
  309. $constraint = new Ip(array(
  310. 'message' => 'myMessage'
  311. ));
  312. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  313. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  314. $this->assertEquals($this->validator->getMessageParameters(), array(
  315. '{{ value }}' => 'foobar',
  316. ));
  317. }
  318. }