| | 105 | |
| | 106 | /** |
| | 107 | * @dataProvider ip_address_testcases |
| | 108 | */ |
| | 109 | function test_is_ip_address( $maybe_ip, $expected ) { |
| | 110 | $actual = WP_Http::is_ip_address( $maybe_ip ); |
| | 111 | $this->assertEquals( $expected, $actual ); |
| | 112 | } |
| | 113 | |
| | 114 | function ip_address_testcases() { |
| | 115 | // 0: The IP Address, 1: The expected resulting |
| | 116 | return array( |
| | 117 | // Valid IPv4 address |
| | 118 | // See: http://tools.ietf.org/html/rfc5737 |
| | 119 | array( '0.0.0.0', 4 ), |
| | 120 | array( '127.0.0.1', 4 ), // The loopback address |
| | 121 | array( '192.0.2.0', 4 ), // RFC 5737 - IPv4 Address Blocks Reserved for Documentation |
| | 122 | array( '198.51.100.0', 4 ), // RFC 5737 - IPv4 Address Blocks Reserved for Documentation |
| | 123 | array( '203.0.113.0', 4 ), // RFC 5737 - IPv4 Address Blocks Reserved for Documentation |
| | 124 | array( '255.255.255.255', 4 ), |
| | 125 | |
| | 126 | // Invalid IPv4 address |
| | 127 | array( '256.255.255.255', false ), // The first octet is out of range |
| | 128 | array( '255.256.255.255', false ), // The second octet is out of range |
| | 129 | array( '255.255.256.255', false ), // The third octet is out of range |
| | 130 | array( '255.255.255.256', false ), // The fourth octet is out of range |
| | 131 | array( '999.999.999.999', false ), // All octet is out of range |
| | 132 | array( '2000.1.1.1', false ), |
| | 133 | array( '1.2000.1.1', false ), |
| | 134 | array( '1.1.2000.1', false ), |
| | 135 | array( '1.1.1.2000', false ), |
| | 136 | array( '2000.2000.2000.2000', false ), |
| | 137 | |
| | 138 | // Valid IPv6 address |
| | 139 | // See: http://tools.ietf.org/html/rfc4291 |
| | 140 | array( '0:0:0:0:0:0:0:0', 6 ), // The unspecified address |
| | 141 | array( '::', 6 ), // The unspecified address (in compressed form) |
| | 142 | array( '0:0:0:0:0:0:0:1', 6 ), // The loopback address |
| | 143 | array( '::1', 6 ), // The loopback address (in compressed form) |
| | 144 | array( '2001:db8::', 6 ), // RFC 3849 - IPv6 Address Prefix Reserved for Documentation |
| | 145 | array( '2001:0db8:0000:0000:0000:0000:dead:beaf', 6 ), |
| | 146 | array( '2001:db8:0:0:0:0:dead:beaf', 6 ), |
| | 147 | array( '2001:db8::dead:beaf', 6 ), |
| | 148 | array( 'ABCD:EF01:2345:6789:ABCD:EF01:2345:6789', 6 ), // RFC 4291 - Example |
| | 149 | array( '2001:DB8:0:0:8:800:200C:417A', 6 ), // RFC 4291 - Example of unicast address |
| | 150 | array( '2001:DB8::8:800:200C:417A', 6 ), // RFC 4291 - Example of unicast address (in compressed form) |
| | 151 | array( 'FF01:0:0:0:0:0:0:101', 6 ), // RFC 4291 - Example of multicast address |
| | 152 | array( 'FF01::101', 6 ), // RFC 4291 - Example of multicast address (in compressed form) |
| | 153 | array( '0:0:0:0:0:0:13.1.68.3', 6 ), // RFC 4291 - Example of an alternative form with a mixed environment of IPv4 and IPv6 nodes |
| | 154 | array( '::13.1.68.3', 6 ), // RFC 4291 - Example of an alternative form with a mixed environment of IPv4 and IPv6 nodes (in compressed form) |
| | 155 | array( '0:0:0:0:0:FFFF:129.144.52.38', 6 ), // RFC 4291 - Example of an alternative form with a mixed environment of IPv4 and IPv6 nodes |
| | 156 | array( '::FFFF:129.144.52.38', 6 ), // RFC 4291 - Example of an alternative form with a mixed environment of IPv4 and IPv6 nodes (in compressed form) |
| | 157 | |
| | 158 | // Invalid IPv6 address |
| | 159 | // See: https://github.com/richb-intermapper/IPv6-Regex |
| | 160 | array( '2001:DB8:0:0:8:800:200C:417A:221', false ), // unicast full |
| | 161 | array( 'FF01::101::2', false ), // multicast compressed |
| | 162 | array( '02001:0000:1234:0000:0000:C1C0:ABCD:0876', false ), // extra 0 not allowed |
| | 163 | array( '2001:0000:1234:0000:00001:C1C0:ABCD:0876', false ), // extra 0 not allowed |
| | 164 | array( '2001:0000:1234:0000:0000:C1C0:ABCD:0876 0', false ), // junk after valid address |
| | 165 | array( '2001:0000:1234: 0000:0000:C1C0:ABCD:0876', false ), // internal space |
| | 166 | array( '3ffe:0b00:0000:0001:0000:0000:000a', false ), // Segments is less than 8 |
| | 167 | array( 'FF02:0000:0000:0000:0000:0000:0000:0000:0001', false ), // Segments is over 8 |
| | 168 | array( '3ffe:b00::1::a', false ), // Compressed double |
| | 169 | array( '::1111:2222:3333:4444:5555:6666::', false ), // Compressed double |
| | 170 | array( '1::5:400.2.3.4', false ), // Embedded ipv4 address is out of range |
| | 171 | array( '1::5:192.168.0.256', false ), // Embedded ipv4 address is out of range |
| | 172 | array( '2001:1:1:1:1:1:255Z255X255Y255', false ), // garbage instead of "." in IPv4 |
| | 173 | array( '::ffff:192x168.1.26', false ), // ditto |
| | 174 | array( '::ffff:2.3.4', false ), // has ipv4 octet lost |
| | 175 | array( '1.2.3.4:1111:2222:3333:4444::5555', false ), // Aeron |
| | 176 | array( ':', false ), |
| | 177 | array( ':::', false ), |
| | 178 | |
| | 179 | // other |
| | 180 | array( '123', false ), |
| | 181 | array( 'ldkfj', false ), |
| | 182 | array( '.', false ), |
| | 183 | array( '..', false ), |
| | 184 | array( '...', false ), |
| | 185 | |
| | 186 | ); |
| | 187 | } |