1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
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 | #include <qtest.h> |
29 | #include <QSignalSpy> |
30 | #include <QTimer> |
31 | #include <QTcpSocket> |
32 | #include <QTcpServer> |
33 | #include <QDebug> |
34 | #include <QBuffer> |
35 | |
36 | #include <private/qpacketprotocol_p.h> |
37 | #include <private/qpacket_p.h> |
38 | |
39 | #include "debugutil_p.h" |
40 | |
41 | class tst_QPacketProtocol : public QObject |
42 | { |
43 | Q_OBJECT |
44 | |
45 | private: |
46 | QTcpServer *m_server; |
47 | QTcpSocket *m_client; |
48 | QTcpSocket *m_serverConn; |
49 | |
50 | private slots: |
51 | void init(); |
52 | void cleanup(); |
53 | |
54 | void send(); |
55 | void packetsAvailable(); |
56 | void packetsAvailable_data(); |
57 | void read(); |
58 | }; |
59 | |
60 | void tst_QPacketProtocol::init() |
61 | { |
62 | m_server = new QTcpServer(this); |
63 | m_serverConn = nullptr; |
64 | QVERIFY(m_server->listen(QHostAddress("127.0.0.1" ))); |
65 | |
66 | m_client = new QTcpSocket(this); |
67 | |
68 | QSignalSpy serverSpy(m_server, SIGNAL(newConnection())); |
69 | QSignalSpy clientSpy(m_client, SIGNAL(connected())); |
70 | |
71 | m_client->connectToHost(address: m_server->serverAddress(), port: m_server->serverPort()); |
72 | |
73 | QVERIFY(clientSpy.count() > 0 || clientSpy.wait()); |
74 | QVERIFY(serverSpy.count() > 0 || serverSpy.wait()); |
75 | |
76 | m_serverConn = m_server->nextPendingConnection(); |
77 | } |
78 | |
79 | void tst_QPacketProtocol::cleanup() |
80 | { |
81 | delete m_client; |
82 | delete m_serverConn; |
83 | delete m_server; |
84 | } |
85 | |
86 | void tst_QPacketProtocol::send() |
87 | { |
88 | QPacketProtocol in(m_client); |
89 | QPacketProtocol out(m_serverConn); |
90 | |
91 | QByteArray ba; |
92 | int num; |
93 | |
94 | QPacket packet(QDataStream::Qt_DefaultCompiledVersion); |
95 | packet << "Hello world" << 123; |
96 | out.send(data: packet.data()); |
97 | |
98 | QVERIFY(QQmlDebugTest::waitForSignal(&in, SIGNAL(readyRead()))); |
99 | |
100 | QPacket p(QDataStream::Qt_DefaultCompiledVersion, in.read()); |
101 | p >> ba >> num; |
102 | QCOMPARE(ba, QByteArray("Hello world" ) + '\0'); |
103 | QCOMPARE(num, 123); |
104 | } |
105 | |
106 | void tst_QPacketProtocol::packetsAvailable() |
107 | { |
108 | QFETCH(int, packetCount); |
109 | |
110 | QPacketProtocol out(m_client); |
111 | QPacketProtocol in(m_serverConn); |
112 | |
113 | QCOMPARE(out.packetsAvailable(), qint64(0)); |
114 | QCOMPARE(in.packetsAvailable(), qint64(0)); |
115 | |
116 | for (int i=0; i<packetCount; i++) { |
117 | QPacket packet(QDataStream::Qt_DefaultCompiledVersion); |
118 | packet << "Hello" ; |
119 | out.send(data: packet.data()); |
120 | } |
121 | |
122 | QVERIFY(QQmlDebugTest::waitForSignal(&in, SIGNAL(readyRead()))); |
123 | QCOMPARE(in.packetsAvailable(), qint64(packetCount)); |
124 | } |
125 | |
126 | void tst_QPacketProtocol::packetsAvailable_data() |
127 | { |
128 | QTest::addColumn<int>(name: "packetCount" ); |
129 | |
130 | QTest::newRow(dataTag: "1" ) << 1; |
131 | QTest::newRow(dataTag: "2" ) << 2; |
132 | QTest::newRow(dataTag: "10" ) << 10; |
133 | } |
134 | |
135 | void tst_QPacketProtocol::read() |
136 | { |
137 | QPacketProtocol in(m_client); |
138 | QPacketProtocol out(m_serverConn); |
139 | |
140 | QVERIFY(in.read().isEmpty()); |
141 | |
142 | QPacket packet(QDataStream::Qt_DefaultCompiledVersion); |
143 | packet << 123; |
144 | out.send(data: packet.data()); |
145 | |
146 | QPacket packet2(QDataStream::Qt_DefaultCompiledVersion); |
147 | packet2 << 456; |
148 | out.send(data: packet2.data()); |
149 | QVERIFY(QQmlDebugTest::waitForSignal(&in, SIGNAL(readyRead()))); |
150 | |
151 | int num; |
152 | |
153 | QPacket p1(QDataStream::Qt_DefaultCompiledVersion, in.read()); |
154 | QVERIFY(!p1.atEnd()); |
155 | p1 >> num; |
156 | QCOMPARE(num, 123); |
157 | |
158 | QPacket p2(QDataStream::Qt_DefaultCompiledVersion, in.read()); |
159 | QVERIFY(!p2.atEnd()); |
160 | p2 >> num; |
161 | QCOMPARE(num, 456); |
162 | |
163 | QVERIFY(in.read().isEmpty()); |
164 | } |
165 | |
166 | QTEST_MAIN(tst_QPacketProtocol) |
167 | |
168 | #include "tst_qpacketprotocol.moc" |
169 | |