Add test function for private network

Add a function QHostAddress::isPrivateUse to test
for IPv4 private networks and IPv6 unique local unicast addresses.

Task-number: QTBUG-111211
Change-Id: Ic8abefa7aa974fa83118aeb9f2506a5713f79f0d
Reviewed-by: Tasuku Suzuki <tasuku.suzuki@signal-slot.co.jp>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
YAMAMOTO Atsushi 2023-02-20 11:45:47 +09:00
parent a25644aecf
commit f80b0aee60
3 changed files with 28 additions and 7 deletions

View File

@ -164,8 +164,12 @@ AddressClassification QHostAddressPrivate::classify() const
return BroadcastAddress;
return UnknownAddress;
}
if (((a & 0xff000000U) == 0x0a000000U) // 10.0.0.0/8
|| ((a & 0xfff00000U) == 0xac100000U) // 172.16.0.0/12
|| ((a & 0xffff0000U) == 0xc0a80000U)) // 192.168.0.0/16
return PrivateNetworkAddress;
// Not testing for PrivateNetworkAddress and TestNetworkAddress
// Not testing for TestNetworkAddress
// since we don't need them yet.
return GlobalAddress;
}
@ -1113,7 +1117,7 @@ bool QHostAddress::isLoopback() const
considered as global in new applications. This function returns true for
site-local addresses too.
\sa isLoopback(), isSiteLocal(), isUniqueLocalUnicast()
\sa isLoopback(), isSiteLocal(), isUniqueLocalUnicast(), isPrivateUse()
*/
bool QHostAddress::isGlobal() const
{
@ -1131,7 +1135,7 @@ bool QHostAddress::isGlobal() const
\l{https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml}{IANA
IPv6 Address Space} registry for more information.
\sa isLoopback(), isGlobal(), isMulticast(), isSiteLocal(), isUniqueLocalUnicast()
\sa isLoopback(), isGlobal(), isMulticast(), isSiteLocal(), isUniqueLocalUnicast(), isPrivateUse()
*/
bool QHostAddress::isLinkLocal() const
{
@ -1154,7 +1158,7 @@ bool QHostAddress::isLinkLocal() const
isGlobal() also returns true). Site-local addresses were replaced by Unique
Local Addresses (ULA).
\sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast()
\sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isPrivateUse()
*/
bool QHostAddress::isSiteLocal() const
{
@ -1175,7 +1179,7 @@ bool QHostAddress::isSiteLocal() const
4193 says that, in practice, "applications may treat these addresses like
global scoped addresses." Only routers need care about the distinction.
\sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast()
\sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isPrivateUse()
*/
bool QHostAddress::isUniqueLocalUnicast() const
{
@ -1188,7 +1192,7 @@ bool QHostAddress::isUniqueLocalUnicast() const
Returns \c true if the address is an IPv4 or IPv6 multicast address, \c
false otherwise.
\sa isLoopback(), isGlobal(), isLinkLocal(), isSiteLocal(), isUniqueLocalUnicast()
\sa isLoopback(), isGlobal(), isLinkLocal(), isSiteLocal(), isUniqueLocalUnicast(), isPrivateUse()
*/
bool QHostAddress::isMulticast() const
{
@ -1205,13 +1209,27 @@ bool QHostAddress::isMulticast() const
broadcast address. For that, please use \l QNetworkInterface to obtain the
broadcast addresses of the local machine.
\sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast()
\sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isPrivateUse()
*/
bool QHostAddress::isBroadcast() const
{
return d->classify() == BroadcastAddress;
}
/*!
\since 6.6
Returns \c true if the address is an IPv6 unique local unicast address or
IPv4 address reserved for local networks by \l {RFC 1918}, \c false otherwise.
\sa isLoopback(), isGlobal(), isMulticast(), isLinkLocal(), isUniqueLocalUnicast(), isBroadcast()
*/
bool QHostAddress::isPrivateUse() const
{
const AddressClassification classification = d->classify();
return (classification == PrivateNetworkAddress) || (classification == UniqueLocalAddress);
}
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d, const QHostAddress &address)
{

View File

@ -127,6 +127,7 @@ public:
bool isUniqueLocalUnicast() const;
bool isMulticast() const;
bool isBroadcast() const;
bool isPrivateUse() const;
static QPair<QHostAddress, int> parseSubnet(const QString &subnet);

View File

@ -703,6 +703,7 @@ void tst_QHostAddress::classification()
bool isUniqueLocalAddress = (result == UniqueLocalAddress);
bool isMulticast = (result == MulticastAddress);
bool isBroadcast = (result == BroadcastAddress);
bool isPrivateUse = (result == PrivateNetworkAddress || result == UniqueLocalAddress);
QCOMPARE(address.isLoopback(), isLoopback);
QCOMPARE(address.isGlobal(), isGlobal);
@ -711,6 +712,7 @@ void tst_QHostAddress::classification()
QCOMPARE(address.isUniqueLocalUnicast(), isUniqueLocalAddress);
QCOMPARE(address.isMulticast(), isMulticast);
QCOMPARE(address.isBroadcast(), isBroadcast);
QCOMPARE(address.isPrivateUse(), isPrivateUse);
}
void tst_QHostAddress::convertv4v6_data()