/vendor/symfony/symfony/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS · PHP · 444 lines · 312 code · 68 blank · 64 comment · 0 complexity · c643bbe94215c99d31c03492323d9104 MD5 · raw file

  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. ->assertRaised();
  134. }
  135. public function getInvalidIpsV4()
  136. {
  137. return array(
  138. array('0'),
  139. array('0.0'),
  140. array('0.0.0'),
  141. array('256.0.0.0'),
  142. array('0.256.0.0'),
  143. array('0.0.256.0'),
  144. array('0.0.0.256'),
  145. array('-1.0.0.0'),
  146. array('foobar'),
  147. );
  148. }
  149. /**
  150. * @dataProvider getInvalidPrivateIpsV4
  151. */
  152. public function testInvalidPrivateIpsV4($ip)
  153. {
  154. $constraint = new Ip(array(
  155. 'version' => Ip::V4_NO_PRIV,
  156. 'message' => 'myMessage',
  157. ));
  158. $this->validator->validate($ip, $constraint);
  159. $this->buildViolation('myMessage')
  160. ->setParameter('{{ value }}', '"'.$ip.'"')
  161. ->assertRaised();
  162. }
  163. public function getInvalidPrivateIpsV4()
  164. {
  165. return array(
  166. array('10.0.0.0'),
  167. array('172.16.0.0'),
  168. array('192.168.1.0'),
  169. );
  170. }
  171. /**
  172. * @dataProvider getInvalidReservedIpsV4
  173. */
  174. public function testInvalidReservedIpsV4($ip)
  175. {
  176. $constraint = new Ip(array(
  177. 'version' => Ip::V4_NO_RES,
  178. 'message' => 'myMessage',
  179. ));
  180. $this->validator->validate($ip, $constraint);
  181. $this->buildViolation('myMessage')
  182. ->setParameter('{{ value }}', '"'.$ip.'"')
  183. ->assertRaised();
  184. }
  185. public function getInvalidReservedIpsV4()
  186. {
  187. return array(
  188. array('0.0.0.0'),
  189. array('224.0.0.1'),
  190. array('255.255.255.255'),
  191. );
  192. }
  193. /**
  194. * @dataProvider getInvalidPublicIpsV4
  195. */
  196. public function testInvalidPublicIpsV4($ip)
  197. {
  198. $constraint = new Ip(array(
  199. 'version' => Ip::V4_ONLY_PUBLIC,
  200. 'message' => 'myMessage',
  201. ));
  202. $this->validator->validate($ip, $constraint);
  203. $this->buildViolation('myMessage')
  204. ->setParameter('{{ value }}', '"'.$ip.'"')
  205. ->assertRaised();
  206. }
  207. public function getInvalidPublicIpsV4()
  208. {
  209. return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidReservedIpsV4());
  210. }
  211. /**
  212. * @dataProvider getInvalidIpsV6
  213. */
  214. public function testInvalidIpsV6($ip)
  215. {
  216. $constraint = new Ip(array(
  217. 'version' => Ip::V6,
  218. 'message' => 'myMessage',
  219. ));
  220. $this->validator->validate($ip, $constraint);
  221. $this->buildViolation('myMessage')
  222. ->setParameter('{{ value }}', '"'.$ip.'"')
  223. ->assertRaised();
  224. }
  225. public function getInvalidIpsV6()
  226. {
  227. return array(
  228. array('z001:0db8:85a3:0000:0000:8a2e:0370:7334'),
  229. array('fe80'),
  230. array('fe80:8329'),
  231. array('fe80:::202:b3ff:fe1e:8329'),
  232. array('fe80::202:b3ff::fe1e:8329'),
  233. // IPv4 mapped to IPv6
  234. array('2001:0db8:85a3:0000:0000:8a2e:0370:0.0.0.0'),
  235. array('::0.0'),
  236. array('::0.0.0'),
  237. array('::256.0.0.0'),
  238. array('::0.256.0.0'),
  239. array('::0.0.256.0'),
  240. array('::0.0.0.256'),
  241. );
  242. }
  243. /**
  244. * @dataProvider getInvalidPrivateIpsV6
  245. */
  246. public function testInvalidPrivateIpsV6($ip)
  247. {
  248. $constraint = new Ip(array(
  249. 'version' => Ip::V6_NO_PRIV,
  250. 'message' => 'myMessage',
  251. ));
  252. $this->validator->validate($ip, $constraint);
  253. $this->buildViolation('myMessage')
  254. ->setParameter('{{ value }}', '"'.$ip.'"')
  255. ->assertRaised();
  256. }
  257. public function getInvalidPrivateIpsV6()
  258. {
  259. return array(
  260. array('fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c'),
  261. array('fdc6:c46b:bb8f:7d4c:fdc6:c46b:bb8f:7d4c'),
  262. array('fdc6:c46b:bb8f:7d4c:0000:8a2e:0370:7334'),
  263. );
  264. }
  265. /**
  266. * @dataProvider getInvalidReservedIpsV6
  267. */
  268. public function testInvalidReservedIpsV6($ip)
  269. {
  270. $constraint = new Ip(array(
  271. 'version' => Ip::V6_NO_RES,
  272. 'message' => 'myMessage',
  273. ));
  274. $this->validator->validate($ip, $constraint);
  275. $this->buildViolation('myMessage')
  276. ->setParameter('{{ value }}', '"'.$ip.'"')
  277. ->assertRaised();
  278. }
  279. public function getInvalidReservedIpsV6()
  280. {
  281. // Quoting after official filter documentation:
  282. // "FILTER_FLAG_NO_RES_RANGE = This flag does not apply to IPv6 addresses."
  283. // Full description: http://php.net/manual/en/filter.filters.flags.php
  284. return $this->getInvalidIpsV6();
  285. }
  286. /**
  287. * @dataProvider getInvalidPublicIpsV6
  288. */
  289. public function testInvalidPublicIpsV6($ip)
  290. {
  291. $constraint = new Ip(array(
  292. 'version' => Ip::V6_ONLY_PUBLIC,
  293. 'message' => 'myMessage',
  294. ));
  295. $this->validator->validate($ip, $constraint);
  296. $this->buildViolation('myMessage')
  297. ->setParameter('{{ value }}', '"'.$ip.'"')
  298. ->assertRaised();
  299. }
  300. public function getInvalidPublicIpsV6()
  301. {
  302. return array_merge($this->getInvalidPrivateIpsV6(), $this->getInvalidReservedIpsV6());
  303. }
  304. /**
  305. * @dataProvider getInvalidIpsAll
  306. */
  307. public function testInvalidIpsAll($ip)
  308. {
  309. $constraint = new Ip(array(
  310. 'version' => Ip::ALL,
  311. 'message' => 'myMessage',
  312. ));
  313. $this->validator->validate($ip, $constraint);
  314. $this->buildViolation('myMessage')
  315. ->setParameter('{{ value }}', '"'.$ip.'"')
  316. ->assertRaised();
  317. }
  318. public function getInvalidIpsAll()
  319. {
  320. return array_merge($this->getInvalidIpsV4(), $this->getInvalidIpsV6());
  321. }
  322. /**
  323. * @dataProvider getInvalidPrivateIpsAll
  324. */
  325. public function testInvalidPrivateIpsAll($ip)
  326. {
  327. $constraint = new Ip(array(
  328. 'version' => Ip::ALL_NO_PRIV,
  329. 'message' => 'myMessage',
  330. ));
  331. $this->validator->validate($ip, $constraint);
  332. $this->buildViolation('myMessage')
  333. ->setParameter('{{ value }}', '"'.$ip.'"')
  334. ->assertRaised();
  335. }
  336. public function getInvalidPrivateIpsAll()
  337. {
  338. return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidPrivateIpsV6());
  339. }
  340. /**
  341. * @dataProvider getInvalidReservedIpsAll
  342. */
  343. public function testInvalidReservedIpsAll($ip)
  344. {
  345. $constraint = new Ip(array(
  346. 'version' => Ip::ALL_NO_RES,
  347. 'message' => 'myMessage',
  348. ));
  349. $this->validator->validate($ip, $constraint);
  350. $this->buildViolation('myMessage')
  351. ->setParameter('{{ value }}', '"'.$ip.'"')
  352. ->assertRaised();
  353. }
  354. public function getInvalidReservedIpsAll()
  355. {
  356. return array_merge($this->getInvalidReservedIpsV4(), $this->getInvalidReservedIpsV6());
  357. }
  358. /**
  359. * @dataProvider getInvalidPublicIpsAll
  360. */
  361. public function testInvalidPublicIpsAll($ip)
  362. {
  363. $constraint = new Ip(array(
  364. 'version' => Ip::ALL_ONLY_PUBLIC,
  365. 'message' => 'myMessage',
  366. ));
  367. $this->validator->validate($ip, $constraint);
  368. $this->buildViolation('myMessage')
  369. ->setParameter('{{ value }}', '"'.$ip.'"')
  370. ->assertRaised();
  371. }
  372. public function getInvalidPublicIpsAll()
  373. {
  374. return array_merge($this->getInvalidPublicIpsV4(), $this->getInvalidPublicIpsV6());
  375. }
  376. }