From 75a56ce44908eb14ef058d111129ef3d285c5364 Mon Sep 17 00:00:00 2001 From: "Bradley T. Hughes" Date: Thu, 5 Aug 2010 12:48:44 +0200 Subject: Add QUdpSocket::setMulticastInterface() and ::multicastInterface() This API allows the programmer to set/query the outgoing interface for multicast packets for the socket. Both functions need an initialized socket to work. Autotest updated to test setting each interface in the system as the multicast interface for IPv4 and IPv6 UDP sockets. --- tests/auto/qudpsocket/tst_qudpsocket.cpp | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'tests/auto/qudpsocket') diff --git a/tests/auto/qudpsocket/tst_qudpsocket.cpp b/tests/auto/qudpsocket/tst_qudpsocket.cpp index a1dd20037d..2b78706deb 100644 --- a/tests/auto/qudpsocket/tst_qudpsocket.cpp +++ b/tests/auto/qudpsocket/tst_qudpsocket.cpp @@ -50,11 +50,13 @@ #include #include #include +#include #include #include "../network-settings.h" Q_DECLARE_METATYPE(QHostAddress) +Q_DECLARE_METATYPE(QNetworkInterface) //TESTED_CLASS= //TESTED_FILES= @@ -102,6 +104,8 @@ private slots: void multicastJoinBeforeBind(); void multicastLeaveAfterClose_data(); void multicastLeaveAfterClose(); + void setMulticastInterface_data(); + void setMulticastInterface(); void multicast_data(); void multicast(); @@ -954,6 +958,46 @@ void tst_QUdpSocket::multicastLeaveAfterClose() QVERIFY(!udpSocket.leaveMulticastGroup(groupAddress)); } +void tst_QUdpSocket::setMulticastInterface_data() +{ + QTest::addColumn("iface"); + QTest::addColumn("address"); + QList interfaces = QNetworkInterface::allInterfaces(); + foreach (const QNetworkInterface &iface, interfaces) { + foreach (const QNetworkAddressEntry &entry, iface.addressEntries()) { + QTest::newRow(QString("%1:%2").arg(iface.name()).arg(entry.ip().toString()).toAscii()) + << iface + << entry.ip(); + } + } +} + +void tst_QUdpSocket::setMulticastInterface() +{ + QFETCH_GLOBAL(bool, setProxy); + QFETCH(QNetworkInterface, iface); + QFETCH(QHostAddress, address); + + QUdpSocket udpSocket; + // bind initializes the socket + bool bound = udpSocket.bind((address.protocol() == QAbstractSocket::IPv6Protocol + ? QHostAddress(QHostAddress::AnyIPv6) + : QHostAddress(QHostAddress::Any)), + 0); + if (!bound) + QTest::ignoreMessage(QtWarningMsg, "QUdpSocket::setMulticastInterface() called on a QUdpSocket when not in QUdpSocket::BoundState"); + udpSocket.setMulticastInterface(iface); + if (!bound) + QTest::ignoreMessage(QtWarningMsg, "QUdpSocket::multicastInterface() called on a QUdpSocket when not in QUdpSocket::BoundState"); + QNetworkInterface iface2 = udpSocket.multicastInterface(); + if (!setProxy) { + QVERIFY(iface2.isValid()); + QCOMPARE(iface.name(), iface2.name()); + } else { + QVERIFY(!iface2.isValid()); + } +} + void tst_QUdpSocket::multicast_data() { QHostAddress anyAddress = QHostAddress(QHostAddress::Any); -- cgit v1.2.3