IpTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 testInvalidIpForZF4809()
  82. {
  83. $this->assertFalse($this->_validator->isValid('1.2.333'));
  84. }
  85. public function testInvalidIpForZF3435()
  86. {
  87. $this->assertFalse($this->_validator->isValid('192.168.0.2 adfs'));
  88. }
  89. /**
  90. * @see ZF-2694
  91. */
  92. public function testIPv6addresses()
  93. {
  94. if (!function_exists('inet_pton')) {
  95. $this->markTestIncomplete('No IPv6 support within this PHP release');
  96. }
  97. $this->assertTrue($this->_validator->isValid('::127.0.0.1'));
  98. }
  99. /**
  100. * @see ZF-8253
  101. */
  102. public function testMoreIPv6Addresses()
  103. {
  104. $ips = array(
  105. '2001:0db8:0000:0000:0000:0000:1428:57ab',
  106. '2001:0DB8:0000:0000:0000:0000:1428:57AB',
  107. 'a:b:c::1.2.3.4',
  108. 'a:b:c:d::1.2.3.4',
  109. 'a:b:c:d:e:f:1.2.3.4',
  110. 'a::c:d:e:f:0:1',
  111. 'a::0:1',
  112. '::e:f',
  113. 'a:b:c:d:e:f::0');
  114. foreach($ips as $ip) {
  115. $this->assertTrue($this->_validator->isValid($ip));
  116. }
  117. }
  118. /**
  119. * @see ZF-8253
  120. * @see http://bugs.php.net/bug.php?id=50117
  121. */
  122. public function testMoreIPv6AddressesPHPdidWrong()
  123. {
  124. $validIps = array('a:b:c:d:e::1.2.3.4',
  125. '::0:a:b:c:d:e:f',
  126. '0:a:b:c:d:e:f::');
  127. $invalidIps = array('::01.02.03.04',
  128. '0:0:0:255.255.255.255');
  129. foreach($validIps as $ip) {
  130. $this->assertTrue($this->_validator->isValid($ip));
  131. }
  132. foreach($invalidIps as $ip) {
  133. $this->assertFalse($this->_validator->isValid($ip));
  134. }
  135. }
  136. /**
  137. * @ZF-4352
  138. */
  139. public function testNonStringValidation()
  140. {
  141. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  142. }
  143. }