IpValidatorTest.php 8.9 KB

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