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

Unit TestingTcpIp

Unit Testing Problem Overview


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?

Unit Testing Solutions


Solution 1 - Unit Testing

According to RFC 5737:

> 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

Solution 2 - Unit Testing

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

Solution 3 - Unit Testing

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.

Solution 4 - Unit Testing

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)

Solution 5 - Unit Testing

254.254.254.254

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

RFC 1700

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyan R.View Question on Stackoverflow
Solution 1 - Unit TestingJonathanView Answer on Stackoverflow
Solution 2 - Unit TestingFrédéric HamidiView Answer on Stackoverflow
Solution 3 - Unit TestingChris RuddView Answer on Stackoverflow
Solution 4 - Unit TestingMarc BView Answer on Stackoverflow
Solution 5 - Unit TestingWilliam DwyerView Answer on Stackoverflow