what is a good invalid IP address to use for unit tests?
Asked Answered
P

5

74

I am writing unit tests for a client library. I want to test connecting with an invalid port and an invalid ip. What is a good ip address to use that won't potentially be routed somewhere? I don't want to make any assumptions about the network the machine running the unit tests is on. LOCALHOST seems like a bad choice since that is the valid machine running the server component and I want to test an invalid port separately. Is there an INVALID-IP reserved somewhere in the IPv4 spec?

Psychologist answered 4/5, 2012 at 20:57 Comment(0)
V
96

According to RFC 5737 §3 Documentation Address Blocks:

The blocks 192.0.2.0/24 (TEST-NET-1), 198.51.100.0/24 (TEST-NET-2), and 203.0.113.0/24 (TEST-NET-3) are provided for use in documentation.

This means you can use pick an IP address from these ranges:

  • 192.0.2.0 - 192.0.2.255
  • 198.51.100.0 - 198.51.100.255
  • 203.0.113.0 - 203.0.113.255
Veterinary answered 4/5, 2012 at 20:59 Comment(6)
Documentation and unit tests are two distinct things: "These blocks are not for local use, and the filters may be used in both local and public contexts.". I would hope a unit test to have the ability to function, even if it's only for the duration of the test. (If this unit test doesn't involve any connection at all, this would work.)Transport
@Bruno: Question: "What is a good ip address to use that won't potentially be routed somewhere?" From the linked document: "Network operators SHOULD add [the TEST-NET-1, TEST-NET-2, and TEST-NET-3 blocks] to the list of non-routeable address spaces, and if packet filters are deployed, then this address block SHOULD be added to packet filters." Seems good to me.Veterinary
Good point, I just wasn't sure if the OP didn't want to make some actual internal connections.Transport
For completeness I would think 2001:db8::/32 should be in this answer as well.Redintegration
I have been using documentation addresses for unit tests myself. But later I have started wondering whether 198.18.0.0/15 defined in RFC 2544 might be a better choice.Redintegration
Wondering if I can expect actual connections to these addresses to return immediately during testing? Seems not the case on OSX.Saraband
E
14

If you're looking for a truly invalid IP address (as opposed to an unrouteable one), you can take advantage of the fact that the first byte of a Class A address cannot be 0.

For example:

0.42.42.42
Emarginate answered 4/5, 2012 at 21:0 Comment(4)
I think it might be risky to use an invalid IP address in this case. What if an external component/API, used by the application that's being tested, behaves different when an invalid IP address is supplied? (Hard to explain what I mean. But let's say you save the IP address in a database, in a column with an IP address type. The database might return an error because the supplied IP address is invalid. When that happens your unit test fails, but not because of your own faults.)Veterinary
@Jonathan, I suspect the questioner actually wants the unit test to behave that way (note that he also wants to use an invalid port, so he might be writing a fail-test in the first place). It doesn't really matter anyway, as our answers complement each other: yours provide technically valid but unrouteable addresses, mine provides invalid ones :)Anthropocentric
I am looking to get a connection timed out exception so @Veterinary had what I was looking for. Using a truly "invalid" IP results in a different exception from my socket library... maybe test that too. This is a good point though (+1).Psychologist
To enforce getting a "connection timed out" exception during unit testing, the easier (and faster) route may be to mock the method in your socket library so that it simply throws that exception. See, e.g,. mockito.Malebranche
A
5

The answer from Jonathan (and it's comments) are good for IPv4

If your tests support IPv6 it has an explicit black hole you can use: 0100::/64 as defined in RFC 6666

It's tempting to use 254.254.254.254 since it's easy to remember, but on some platforms it gives a immediate "transmit failed. General Failure" rather than an actual timeout which may be an invalid unit test.

Agleam answered 3/4, 2019 at 17:25 Comment(2)
I phrased it as to the original asker, but keep in mind this is a necro. I only added it because it's an explicit black hole that exactly fits this scenario (and would have done as a comment to Jonathan's top rated answer, but I don't have enough reputation yet)Agleam
This is only answer with IPv6, so this really should be an answer, and not a comment. And this is a good answer too.Simferopol
R
2

There's 3 private IP blocks you can use for such things:

10/8 (10.0.0.0 -> 10.255.255.255) (an old school Class A netblock)

172.16/12 (172.16.0.0 -> 172.131.255.255

196.168/16 (192.168.0.0 -> 192.168.255.255) (an old school Class B netblock)

Reticulation answered 4/5, 2012 at 21:0 Comment(0)
S
0

254.254.254.254

Should be in the reserved for future use domain...

RFC 1700

Scurry answered 4/5, 2012 at 20:59 Comment(2)
239.0.0.0 - > 255.255.255.255 are all Reserved for future use and won't work anywhere generally speaking.Scurry
Actually 255.255.255.255 has been assigned a meaning. So the reserved range is only 239.0.0.0 - 255.255.255.254.Redintegration

© 2022 - 2024 — McMap. All rights reserved.