Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Do not generate special purpose IPv4s #681

Merged
merged 3 commits into from
Aug 26, 2015
Merged

Conversation

ravage84
Copy link
Contributor

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).

https://en.wikipedia.org/wiki/IPv4#Special-use_addresses

0.0.0.0/8 Current network (only valid as source address) RFC 6890

https://en.wikipedia.org/wiki/List_of_assigned_/8_IPv4_address_blocks

And 255.255.255.255 (Broadcast) must not be used, also.

16777216 = 1.0.0.0 (new lowest generatable IPv4)
-2 = 255.255.255.254 (new highest generatable IPv4)

@steampilot
Copy link

How about the Loopback, shouldn't we exclude that too?

127.0.0.0/8 Loopback RFC 6890

@fzaninotto
Copy link
Owner

Could you please add a unit test?

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).
@ravage84
Copy link
Contributor Author

@fzaninotto are those tests OK for you? Writing test cases for random data feels strange 😼

What do you think about @steampilot 's proposal about also exluding 127.0.0.0/8?

@@ -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))));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand your binary voodoo here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shifts away the last three bits (B, C,& D from A.B.C.D), so only the first bit (A) remains.
This way we can compare it.
Probably there is a more efficient way, but I'm no binary calculation hero 😼

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ip2long($this->faker->ipv4()) >= 0x01000000 will disallow IP addresses starting with 0x00 (i.e. starting with 0.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimWolla you mean like this?
$this->assertTrue(ip2long($this->faker->ipv4()) >= 0x01000000);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using a regex instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fzaninotto I think @TimWolla 's proposal makes and is certainly the fastest of all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimWolla why less? 😕

I inversed the order, as it is expected and then actual in PHPUnit.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not maintainable. Even you guys don't agree on it. Please switch to a regex assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not maintainable.

That's a bit exaggerated, but fair enough.
How about $this->assertNotRegExp('/\A1\./', $this->faker->ipv4());
This checks if the IPv4 starts with 1., so 10. does not fail.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good for me

@fzaninotto
Copy link
Owner

Testing random data is OK - the CI isn't seeded so its runs with new random data every time.

I think your two cases are sufficient for exclusion.

@ravage84
Copy link
Contributor Author

@fzaninotto done.

@fzaninotto
Copy link
Owner

Awesome, thanks.

fzaninotto added a commit that referenced this pull request Aug 26, 2015
Do not generate special purpose IPv4s
@fzaninotto fzaninotto merged commit 24119f5 into fzaninotto:master Aug 26, 2015
@ravage84 ravage84 deleted the patch-1 branch October 21, 2015 11:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants