From ae3d5a915a7a284c478e8c511fe568a01007a4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=BCrth?= Date: Tue, 25 Aug 2015 19:09:35 +0200 Subject: [PATCH 1/3] Do not generate special purpose IPv4s Do not genate 255.255.255.255 (Broadcast) and IPs between 0.0.0.0 and 0.255.255.255 (0.0.0.0/8, current network). --- src/Faker/Provider/Internet.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Faker/Provider/Internet.php b/src/Faker/Provider/Internet.php index 9e92f1466e..9dde1a5109 100644 --- a/src/Faker/Provider/Internet.php +++ b/src/Faker/Provider/Internet.php @@ -288,7 +288,7 @@ public function slug($nbWords = 6, $variableNbWords = true) */ public function ipv4() { - return long2ip(mt_rand(0, 1) == 0 ? mt_rand(-2147483648, 0) : mt_rand(1, 2147483647)); + return long2ip(mt_rand(0, 1) == 0 ? mt_rand(-2147483648, -2) : mt_rand(16777216, 2147483647)); } /** From c2d1030ac345bccc2e61d32ce96dc698f671101d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=BCrth?= Date: Wed, 26 Aug 2015 13:50:29 +0200 Subject: [PATCH 2/3] Add tests for changed ipv4 behavior --- test/Faker/Provider/InternetTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/Faker/Provider/InternetTest.php b/test/Faker/Provider/InternetTest.php index fcd750b149..3a8a04647c 100644 --- a/test/Faker/Provider/InternetTest.php +++ b/test/Faker/Provider/InternetTest.php @@ -108,6 +108,16 @@ public function testIpv4() $this->assertNotFalse(filter_var($this->faker->ipv4(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)); } + public function testIpv4NotLocalNetwork() + { + $this->assertNotEquals('1.0.0.0', long2ip(ip2long($this->faker->ipv4()) & (-1 << (32 - 8)))); + } + + public function testIpv4NotBroadcast() + { + $this->assertNotEquals('255.255.255.255', $this->faker->ipv4()); + } + public function testIpv6() { $this->assertNotFalse(filter_var($this->faker->ipv6(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)); From d38e8c16de1820bd9505fb14234ff9c020741640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=BCrth?= Date: Wed, 26 Aug 2015 16:32:30 +0200 Subject: [PATCH 3/3] Use RegExp instead of binary shifting Refs https://github.com/fzaninotto/Faker/pull/681/files#r37978932 --- test/Faker/Provider/InternetTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Faker/Provider/InternetTest.php b/test/Faker/Provider/InternetTest.php index 3a8a04647c..e715e82e73 100644 --- a/test/Faker/Provider/InternetTest.php +++ b/test/Faker/Provider/InternetTest.php @@ -110,7 +110,7 @@ public function testIpv4() public function testIpv4NotLocalNetwork() { - $this->assertNotEquals('1.0.0.0', long2ip(ip2long($this->faker->ipv4()) & (-1 << (32 - 8)))); + $this->assertNotRegExp('/\A1\./', $this->faker->ipv4()); } public function testIpv4NotBroadcast()