Skip to content

Commit 4a7e72f

Browse files
committed
Merged unit tests
2 parents 2edee4f + 02fc102 commit 4a7e72f

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

src/Ip.php

+45
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,51 @@ public static function isValidv6($ip)
7373
return (bool)filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
7474
}
7575

76+
/**
77+
* Checks if an IP is local
78+
*
79+
* @param string $ip IP
80+
* @return boolean true if the IP is local, otherwise false
81+
*/
82+
public static function isLocal($ip)
83+
{
84+
$localIpv4Ranges = array(
85+
'10.*.*.*',
86+
'127.*.*.*',
87+
'192.168.*.*',
88+
'169.254.*.*',
89+
'172.16.0.0-172.31.255.255',
90+
'224.*.*.*',
91+
);
92+
93+
$localIpv6Ranges = array(
94+
'fe80::/10',
95+
'::1/128',
96+
'fc00::/7'
97+
);
98+
99+
if (self::isValidv4($ip)) {
100+
return self::match($ip, $localIpv4Ranges);
101+
}
102+
103+
if (self::isValidv6($ip)) {
104+
return self::match($ip, $localIpv6Ranges);
105+
}
106+
107+
return false;
108+
}
109+
110+
/**
111+
* Checks if an IP is remot
112+
*
113+
* @param string $ip IP
114+
* @return boolean true if the IP is remote, otherwise false
115+
*/
116+
public static function isRemote($ip)
117+
{
118+
return !self::isLocal($ip);
119+
}
120+
76121
/**
77122
* Checks if an IP is part of an IP range.
78123
*

tests/Unit/IpTest.php

+28-7
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function test8()
153153
$this->assertTrue($status);
154154

155155
$status = Ip::match('2001:cdba:0000:0000:0000:0000:3257:9652',
156-
'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:9999');
156+
'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:9999');
157157
$this->assertTrue($status);
158158

159159

@@ -166,7 +166,7 @@ public function test8()
166166

167167

168168
$status = Ip::match('2001:cdba:0000:0000:0000:0000:3257:7778',
169-
'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:7777');
169+
'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:7777');
170170
$this->assertFalse($status);
171171
}
172172

@@ -193,15 +193,36 @@ public function test9()
193193
/**
194194
* @test
195195
*/
196-
197-
public function test_match_range(){
198-
$range = Ip::matchRange('192.168.100.','192.168..');
196+
197+
198+
public function test_match_range()
199+
{
200+
$range = Ip::matchRange('192.168.100.', '192.168..');
199201
$this->assertTrue($range);
200202

201-
$range = Ip::matchRange('192.168.1.200','192.168.1.');
203+
$range = Ip::matchRange('192.168.1.200', '192.168.1.');
202204
$this->assertTrue($range);
203205

204-
$range = Ip::matchRange('192.168.1.200','192.168.2.');
206+
$range = Ip::matchRange('192.168.1.200', '192.168.2.');
205207
$this->assertFalse($range);
206208
}
209+
210+
public function testLocal()
211+
{
212+
$status = Ip::isLocal('192.168.5.5');
213+
$this->assertTrue($status);
214+
215+
$status = Ip::isLocal('fe80::202:b3ff:fe1e:8329');
216+
$this->assertTrue($status);
217+
}
218+
219+
/**
220+
* @test
221+
*/
222+
public function testRemote()
223+
{
224+
$status = Ip::isRemote('8.8.8.8');
225+
$this->assertTrue($status);
226+
}
227+
207228
}

0 commit comments

Comments
 (0)