IpTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Validate_Ip
  24. */
  25. require_once 'Zend/Validate/Ip.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Validate
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Validate
  33. */
  34. class Zend_Validate_IpTest extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * Zend_Validate_Ip object
  38. *
  39. * @var Zend_Validate_Ip
  40. */
  41. protected $_validator;
  42. /**
  43. * Creates a new Zend_Validate_Ip object for each test method
  44. *
  45. * @return void
  46. */
  47. public function setUp()
  48. {
  49. $this->_validator = new Zend_Validate_Ip();
  50. }
  51. /**
  52. * Ensures that the validator follows expected behavior
  53. *
  54. * @return void
  55. */
  56. public function testBasic()
  57. {
  58. $this->assertTrue($this->_validator->isValid('1.2.3.4'));
  59. $this->assertTrue($this->_validator->isValid('10.0.0.1'));
  60. $this->assertTrue($this->_validator->isValid('255.255.255.255'));
  61. $this->assertFalse($this->_validator->isValid('0.0.0.256'));
  62. $this->assertFalse($this->_validator->isValid('1.2.3.4.5'));
  63. }
  64. public function testZeroIpForZF2420()
  65. {
  66. $this->assertTrue($this->_validator->isValid('0.0.0.0'));
  67. }
  68. /**
  69. * Ensures that getMessages() returns expected default value
  70. *
  71. * @return void
  72. */
  73. public function testGetMessages()
  74. {
  75. $this->assertEquals(array(), $this->_validator->getMessages());
  76. }
  77. public function testOnlyIpv4()
  78. {
  79. $this->_validator->setOptions(array('allowipv6' => false));
  80. $this->assertTrue($this->_validator->isValid('1.2.3.4'));
  81. $this->assertFalse($this->_validator->isValid('a:b:c:d:e::1.2.3.4'));
  82. }
  83. public function testOnlyIpv6()
  84. {
  85. $this->_validator->setOptions(array('allowipv4' => false));
  86. $this->assertFalse($this->_validator->isValid('1.2.3.4'));
  87. $this->assertTrue($this->_validator->isValid('a:b:c:d:e::1.2.3.4'));
  88. }
  89. public function testNoValidation()
  90. {
  91. try {
  92. $this->_validator->setOptions(array('allowipv4' => false, 'allowipv6' => false));
  93. $this->fail();
  94. } catch (Zend_Validate_Exception $e) {
  95. $this->assertContains('Nothing to validate', $e->getMessage());
  96. }
  97. }
  98. public function testInvalidIpForZF4809()
  99. {
  100. $this->assertFalse($this->_validator->isValid('1.2.333'));
  101. }
  102. public function testInvalidIpForZF3435()
  103. {
  104. $this->assertFalse($this->_validator->isValid('192.168.0.2 adfs'));
  105. }
  106. /**
  107. * @group ZF-2694
  108. * @group ZF-8253
  109. */
  110. public function testIPv6addresses()
  111. {
  112. $IPs = array(
  113. '2001:0db8:0000:0000:0000:0000:1428:57ab' => true,
  114. '2001:0DB8:0000:0000:0000:0000:1428:57AB' => true,
  115. '2001:00db8:0000:0000:0000:0000:1428:57ab' => false,
  116. '2001:0db8:xxxx:0000:0000:0000:1428:57ab' => false,
  117. '2001:db8::1428:57ab' => true,
  118. '2001:db8::1428::57ab' => false,
  119. '2001:dx0::1234' => false,
  120. '2001:db0::12345' => false,
  121. '' => false,
  122. ':' => false,
  123. '::' => true,
  124. ':::' => false,
  125. '::::' => false,
  126. '::1' => true,
  127. ':::1' => false,
  128. '::1.2.3.4' => true,
  129. '::127.0.0.1' => true,
  130. '::256.0.0.1' => false,
  131. '::01.02.03.04' => false,
  132. 'a:b:c::1.2.3.4' => true,
  133. 'a:b:c:d::1.2.3.4' => true,
  134. 'a:b:c:d:e::1.2.3.4' => true,
  135. 'a:b:c:d:e:f:1.2.3.4' => true,
  136. 'a:b:c:d:e:f:1.256.3.4' => false,
  137. 'a:b:c:d:e:f::1.2.3.4' => false,
  138. 'a:b:c:d:e:f:0:1:2' => false,
  139. 'a:b:c:d:e:f:0:1' => true,
  140. 'a::b:c:d:e:f:0:1' => false,
  141. 'a::c:d:e:f:0:1' => true,
  142. 'a::d:e:f:0:1' => true,
  143. 'a::e:f:0:1' => true,
  144. 'a::f:0:1' => true,
  145. 'a::0:1' => true,
  146. 'a::1' => true,
  147. 'a::' => true,
  148. '::0:1:a:b:c:d:e:f' => false,
  149. '::0:a:b:c:d:e:f' => true,
  150. '::a:b:c:d:e:f' => true,
  151. '::b:c:d:e:f' => true,
  152. '::c:d:e:f' => true,
  153. '::d:e:f' => true,
  154. '::e:f' => true,
  155. '::f' => true,
  156. '0:1:a:b:c:d:e:f::' => false,
  157. '0:a:b:c:d:e:f::' => true,
  158. 'a:b:c:d:e:f::' => true,
  159. 'b:c:d:e:f::' => true,
  160. 'c:d:e:f::' => true,
  161. 'd:e:f::' => true,
  162. 'e:f::' => true,
  163. 'f::' => true,
  164. 'a:b:::e:f' => false,
  165. '::a:' => false,
  166. '::a::' => false,
  167. ':a::b' => false,
  168. 'a::b:' => false,
  169. '::a:b::c' => false,
  170. 'abcde::f' => false,
  171. ':10.0.0.1' => false,
  172. '0:0:0:255.255.255.255' => false,
  173. '1fff::a88:85a3::172.31.128.1' => false,
  174. 'a:b:c:d:e:f:0::1' => false,
  175. 'a:b:c:d:e:f:0::' => true,
  176. 'a:b:c:d:e:f::0' => true,
  177. 'a:b:c:d:e:f::' => true,
  178. 'total gibberish' => false
  179. );
  180. foreach($IPs as $ip => $expectedOutcome) {
  181. if($expectedOutcome) {
  182. $this->assertTrue($this->_validator->isValid($ip));
  183. } else {
  184. $this->assertFalse($this->_validator->isValid($ip));
  185. }
  186. }
  187. }
  188. /**
  189. * @ZF-4352
  190. */
  191. public function testNonStringValidation()
  192. {
  193. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  194. }
  195. /**
  196. * @ZF-8640
  197. */
  198. public function testNonNewlineValidation()
  199. {
  200. $this->assertFalse($this->_validator->isValid("::C0A8:2\n"));
  201. }
  202. }