Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...