1/****************************************************************************
2**
3** Copyright (C) 2016 Intel Corporation.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QNetworkDatagram>
30#include <QtTest>
31#include <QCoreApplication>
32
33class tst_QNetworkDatagram : public QObject
34{
35 Q_OBJECT
36
37public:
38 tst_QNetworkDatagram();
39
40private Q_SLOTS:
41 void getSetCheck();
42 void makeReply_data();
43 void makeReply();
44};
45
46tst_QNetworkDatagram::tst_QNetworkDatagram()
47{
48}
49
50void tst_QNetworkDatagram::getSetCheck()
51{
52 QNetworkDatagram dg;
53
54 QVERIFY(dg.isNull());
55 QVERIFY(!dg.isValid());
56 QCOMPARE(dg.senderAddress(), QHostAddress());
57 QCOMPARE(dg.destinationAddress(), QHostAddress());
58 QCOMPARE(dg.senderPort(), -1);
59 QCOMPARE(dg.destinationPort(), -1);
60 QCOMPARE(dg.hopLimit(), -1);
61 QCOMPARE(dg.interfaceIndex(), 0U);
62
63 dg.setHopLimit(1);
64 QCOMPARE(dg.hopLimit(), 1);
65 dg.setHopLimit(255);
66 QCOMPARE(dg.hopLimit(), 255);
67
68 dg.setInterfaceIndex(1);
69 QCOMPARE(dg.interfaceIndex(), 1U);
70 dg.setInterfaceIndex(1234567U);
71 QCOMPARE(dg.interfaceIndex(), 1234567U);
72
73 dg.setSender(address: QHostAddress::Any, port: 12345);
74 QCOMPARE(dg.senderAddress(), QHostAddress(QHostAddress::Any));
75 QCOMPARE(dg.senderPort(), 12345);
76 dg.setSender(address: QHostAddress::LocalHost);
77 QCOMPARE(dg.senderAddress(), QHostAddress(QHostAddress::LocalHost));
78 QCOMPARE(dg.senderPort(), 0);
79
80 dg.setDestination(address: QHostAddress::LocalHostIPv6, port: 12345);
81 QCOMPARE(dg.destinationAddress(), QHostAddress(QHostAddress::LocalHostIPv6));
82 QCOMPARE(dg.destinationPort(), 12345);
83 dg.setDestination(address: QHostAddress::Broadcast, port: 137);
84 QCOMPARE(dg.destinationAddress(), QHostAddress(QHostAddress::Broadcast));
85 QCOMPARE(dg.destinationPort(), 137);
86}
87
88void tst_QNetworkDatagram::makeReply_data()
89{
90 qRegisterMetaType<QNetworkDatagram>();
91 QTest::addColumn<QNetworkDatagram>(name: "dgram");
92 QTest::addColumn<QString>(name: "localAddress");
93
94 QNetworkDatagram dgram("some data", QHostAddress("192.0.2.1"), 10001);
95 dgram.setHopLimit(64);
96 dgram.setSender(address: QHostAddress::LocalHost, port: 12345);
97 QTest::newRow(dataTag: "ipv4") << dgram << "192.0.2.1";
98
99 dgram.setDestination(address: QHostAddress("224.0.0.1"), port: 10002);
100 QTest::newRow(dataTag: "ipv4-multicast") << dgram << QString();
101
102 dgram.setSender(address: QHostAddress::LocalHostIPv6, port: 12346);
103 dgram.setDestination(address: QHostAddress("2001:db8::1"), port: 12347);
104 QTest::newRow(dataTag: "ipv6") << dgram << "2001:db8::1";
105
106 dgram.setSender(address: QHostAddress("fe80::1%1"), port: 10003);
107 dgram.setDestination(address: QHostAddress("fe80::2%1"), port: 10004);
108 dgram.setInterfaceIndex(1);
109 QTest::newRow(dataTag: "ipv6-linklocal") << dgram << "fe80::2%1";
110
111 dgram.setDestination(address: QHostAddress("ff02::1%1"), port: 10005);
112 QTest::newRow(dataTag: "ipv6-multicast") << dgram << QString();
113}
114
115void tst_QNetworkDatagram::makeReply()
116{
117 QFETCH(QNetworkDatagram, dgram);
118 QFETCH(QString, localAddress);
119
120 {
121 QNetworkDatagram reply = dgram.makeReply(payload: "World");
122 QCOMPARE(reply.data(), QByteArray("World"));
123 QCOMPARE(reply.senderAddress(), QHostAddress(localAddress));
124 QCOMPARE(reply.senderPort(), localAddress.isEmpty() ? -1 : dgram.destinationPort());
125 QCOMPARE(reply.destinationAddress(), dgram.senderAddress());
126 QCOMPARE(reply.destinationPort(), dgram.senderPort());
127 QCOMPARE(reply.interfaceIndex(), dgram.interfaceIndex());
128 QCOMPARE(reply.hopLimit(), -1);
129 }
130
131 QNetworkDatagram copy = dgram;
132 copy.setData(copy.data());
133 {
134 QNetworkDatagram reply = std::move(copy).makeReply(payload: "World");
135 QCOMPARE(reply.data(), QByteArray("World"));
136 QCOMPARE(reply.senderAddress(), QHostAddress(localAddress));
137 QCOMPARE(reply.senderPort(), localAddress.isEmpty() ? -1 : dgram.destinationPort());
138 QCOMPARE(reply.destinationAddress(), dgram.senderAddress());
139 QCOMPARE(reply.destinationPort(), dgram.senderPort());
140 QCOMPARE(reply.interfaceIndex(), dgram.interfaceIndex());
141 QCOMPARE(reply.hopLimit(), -1);
142 }
143}
144
145QTEST_MAIN(tst_QNetworkDatagram)
146#include "tst_qnetworkdatagram.moc"
147

source code of qtbase/tests/auto/network/kernel/qnetworkdatagram/tst_qnetworkdatagram.cpp