IpTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-2009 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. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * @see Zend_Validate_Ip
  28. */
  29. require_once 'Zend/Validate/Ip.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Validate
  37. */
  38. class Zend_Validate_IpTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Zend_Validate_Ip object
  42. *
  43. * @var Zend_Validate_Ip
  44. */
  45. protected $_validator;
  46. /**
  47. * Creates a new Zend_Validate_Ip object for each test method
  48. *
  49. * @return void
  50. */
  51. public function setUp()
  52. {
  53. $this->_validator = new Zend_Validate_Ip();
  54. }
  55. /**
  56. * Ensures that the validator follows expected behavior
  57. *
  58. * @return void
  59. */
  60. public function testBasic()
  61. {
  62. $this->assertTrue($this->_validator->isValid('1.2.3.4'));
  63. $this->assertTrue($this->_validator->isValid('10.0.0.1'));
  64. $this->assertTrue($this->_validator->isValid('255.255.255.255'));
  65. $this->assertFalse($this->_validator->isValid('0.0.0.256'));
  66. $this->assertFalse($this->_validator->isValid('1.2.3.4.5'));
  67. }
  68. public function testZeroIpForZF2420()
  69. {
  70. $this->assertTrue($this->_validator->isValid('0.0.0.0'));
  71. }
  72. /**
  73. * Ensures that getMessages() returns expected default value
  74. *
  75. * @return void
  76. */
  77. public function testGetMessages()
  78. {
  79. $this->assertEquals(array(), $this->_validator->getMessages());
  80. }
  81. public function testOnlyIpv4()
  82. {
  83. $this->_validator->setOptions(array('allowipv6' => false));
  84. $this->assertTrue($this->_validator->isValid('1.2.3.4'));
  85. $this->assertFalse($this->_validator->isValid('a:b:c:d:e::1.2.3.4'));
  86. }
  87. public function testOnlyIpv6()
  88. {
  89. $this->_validator->setOptions(array('allowipv4' => false));
  90. $this->assertFalse($this->_validator->isValid('1.2.3.4'));
  91. $this->assertTrue($this->_validator->isValid('a:b:c:d:e::1.2.3.4'));
  92. }
  93. public function testNoValidation()
  94. {
  95. try {
  96. $this->_validator->setOptions(array('allowipv4' => false, 'allowipv6' => false));
  97. $this->fail();
  98. } catch (Zend_Validate_Exception $e) {
  99. $this->assertContains('Nothing to validate', $e->getMessage());
  100. }
  101. }
  102. public function testInvalidIpForZF4809()
  103. {
  104. $this->assertFalse($this->_validator->isValid('1.2.333'));
  105. }
  106. public function testInvalidIpForZF3435()
  107. {
  108. $this->assertFalse($this->_validator->isValid('192.168.0.2 adfs'));
  109. }
  110. /**
  111. * @see ZF-2694
  112. * @see ZF-8253
  113. */
  114. public function testIPv6addresses()
  115. {
  116. $IPs = array(
  117. '2001:0db8:0000:0000:0000:0000:1428:57ab' => true,
  118. '2001:0DB8:0000:0000:0000:0000:1428:57AB' => true,
  119. '2001:00db8:0000:0000:0000:0000:1428:57ab' => false,
  120. '2001:0db8:xxxx:0000:0000:0000:1428:57ab' => false,
  121. '2001:db8::1428:57ab' => true,
  122. '2001:db8::1428::57ab' => false,
  123. '2001:dx0::1234' => false,
  124. '2001:db0::12345' => false,
  125. '' => false,
  126. ':' => false,
  127. '::' => true,
  128. ':::' => false,
  129. '::::' => false,
  130. '::1' => true,
  131. ':::1' => false,
  132. '::1.2.3.4' => true,
  133. '::127.0.0.1' => true,
  134. '::256.0.0.1' => false,
  135. '::01.02.03.04' => false,
  136. 'a:b:c::1.2.3.4' => true,
  137. 'a:b:c:d::1.2.3.4' => true,
  138. 'a:b:c:d:e::1.2.3.4' => true,
  139. 'a:b:c:d:e:f:1.2.3.4' => true,
  140. 'a:b:c:d:e:f:1.256.3.4' => false,
  141. 'a:b:c:d:e:f::1.2.3.4' => false,
  142. 'a:b:c:d:e:f:0:1:2' => false,
  143. 'a:b:c:d:e:f:0:1' => true,
  144. 'a::b:c:d:e:f:0:1' => false,
  145. 'a::c:d:e:f:0:1' => true,
  146. 'a::d:e:f:0:1' => true,
  147. 'a::e:f:0:1' => true,
  148. 'a::f:0:1' => true,
  149. 'a::0:1' => true,
  150. 'a::1' => true,
  151. 'a::' => true,
  152. '::0:1:a:b:c:d:e:f' => false,
  153. '::0:a:b:c:d:e:f' => true,
  154. '::a:b:c:d:e:f' => true,
  155. '::b:c:d:e:f' => true,
  156. '::c:d:e:f' => true,
  157. '::d:e:f' => true,
  158. '::e:f' => true,
  159. '::f' => true,
  160. '0:1:a:b:c:d:e:f::' => false,
  161. '0:a:b:c:d:e:f::' => true,
  162. 'a:b:c:d:e:f::' => true,
  163. 'b:c:d:e:f::' => true,
  164. 'c:d:e:f::' => true,
  165. 'd:e:f::' => true,
  166. 'e:f::' => true,
  167. 'f::' => true,
  168. 'a:b:::e:f' => false,
  169. '::a:' => false,
  170. '::a::' => false,
  171. ':a::b' => false,
  172. 'a::b:' => false,
  173. '::a:b::c' => false,
  174. 'abcde::f' => false,
  175. ':10.0.0.1' => false,
  176. '0:0:0:255.255.255.255' => false,
  177. '1fff::a88:85a3::172.31.128.1' => false,
  178. 'a:b:c:d:e:f:0::1' => false,
  179. 'a:b:c:d:e:f:0::' => true,
  180. 'a:b:c:d:e:f::0' => true,
  181. 'a:b:c:d:e:f::' => true,
  182. 'total gibberish' => false
  183. );
  184. foreach($IPs as $ip => $expectedOutcome) {
  185. if($expectedOutcome) {
  186. $this->assertTrue($this->_validator->isValid($ip));
  187. } else {
  188. $this->assertFalse($this->_validator->isValid($ip));
  189. }
  190. }
  191. }
  192. /**
  193. * @ZF-4352
  194. */
  195. public function testNonStringValidation()
  196. {
  197. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  198. }
  199. }