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 | |
29 | |
30 | #include <QDomDocument> |
31 | #include <QNetworkAccessManager> |
32 | #include <QNetworkReply> |
33 | #include <QNetworkRequest> |
34 | #include <QTcpServer> |
35 | #include <QTcpSocket> |
36 | #include <QTimer> |
37 | #include <QElapsedTimer> |
38 | #include <QtDebug> |
39 | #include <QtTest/QtTest> |
40 | #include <QXmlDefaultHandler> |
41 | #include <QXmlInputSource> |
42 | #include <QXmlSimpleReader> |
43 | |
44 | class tst_QXmlInputSource : public QObject |
45 | { |
46 | Q_OBJECT |
47 | |
48 | #if QT_DEPRECATED_SINCE(5, 15) |
49 | private slots: |
50 | void reset() const; |
51 | void resetSimplified() const; |
52 | void waitForReadyIODevice() const; |
53 | void inputFromSlowDevice() const; |
54 | #endif // QT_DEPRECATED_SINCE(5, 15) |
55 | }; |
56 | |
57 | #if QT_DEPRECATED_SINCE(5, 15) |
58 | QT_WARNING_PUSH |
59 | QT_WARNING_DISABLE_DEPRECATED |
60 | /*! |
61 | \internal |
62 | \since 4.4 |
63 | |
64 | See task 166278. |
65 | */ |
66 | void tst_QXmlInputSource::reset() const |
67 | { |
68 | const QString input(QString::fromLatin1(str: "<element attribute1='value1' attribute2='value2'/>" )); |
69 | |
70 | QXmlSimpleReader reader; |
71 | QXmlDefaultHandler handler; |
72 | reader.setContentHandler(&handler); |
73 | |
74 | QXmlInputSource source; |
75 | source.setData(input); |
76 | |
77 | QCOMPARE(source.data(), input); |
78 | |
79 | source.reset(); |
80 | QCOMPARE(source.data(), input); |
81 | |
82 | source.reset(); |
83 | QVERIFY(reader.parse(source)); |
84 | source.reset(); |
85 | QCOMPARE(source.data(), input); |
86 | } |
87 | |
88 | /*! |
89 | \internal |
90 | \since 4.4 |
91 | |
92 | See task 166278. |
93 | */ |
94 | void tst_QXmlInputSource::resetSimplified() const |
95 | { |
96 | const QString input(QString::fromLatin1(str: "<element/>" )); |
97 | |
98 | QXmlSimpleReader reader; |
99 | |
100 | QXmlInputSource source; |
101 | source.setData(input); |
102 | |
103 | QVERIFY(reader.parse(source)); |
104 | source.reset(); |
105 | QCOMPARE(source.data(), input); |
106 | } |
107 | |
108 | class ServerAndClient : public QObject |
109 | { |
110 | Q_OBJECT |
111 | |
112 | public: |
113 | ServerAndClient(QEventLoop &ev) : success(false) |
114 | , eventLoop(ev) |
115 | , bodyBytesRead(0) |
116 | , bodyLength(-1) |
117 | , isBody(false) |
118 | { |
119 | setObjectName("serverAndClient" ); |
120 | tcpServer = new QTcpServer(this); |
121 | connect(sender: tcpServer, SIGNAL(newConnection()), receiver: this, SLOT(newConnection())); |
122 | tcpServer->listen(address: QHostAddress::LocalHost, port: 1088); |
123 | httpClient = new QNetworkAccessManager(this); |
124 | connect(asender: httpClient, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*))); |
125 | } |
126 | |
127 | bool success; |
128 | QEventLoop &eventLoop; |
129 | |
130 | public slots: |
131 | void doIt() |
132 | { |
133 | QUrl url("http://127.0.0.1:1088" ); |
134 | QNetworkRequest req(url); |
135 | req.setRawHeader(headerName: "POST" , value: url.path().toLatin1()); |
136 | req.setRawHeader(headerName: "user-agent" , value: "xml-test" ); |
137 | req.setRawHeader(headerName: "keep-alive" , value: "false" ); |
138 | req.setRawHeader(headerName: "host" , value: url.host().toLatin1()); |
139 | |
140 | QByteArray xmlrpc("<methodCall>\r\n\ |
141 | <methodName>SFD.GetVersion</methodName>\r\n\ |
142 | <params/>\r\n\ |
143 | </methodCall>" ); |
144 | req.setHeader(header: QNetworkRequest::ContentLengthHeader, value: xmlrpc.size()); |
145 | req.setHeader(header: QNetworkRequest::ContentTypeHeader, value: "text/xml" ); |
146 | |
147 | httpClient->post(request: req, data: xmlrpc); |
148 | } |
149 | |
150 | void requestFinished(QNetworkReply *reply) |
151 | { |
152 | QCOMPARE(reply->error(), QNetworkReply::NoError); |
153 | reply->deleteLater(); |
154 | } |
155 | |
156 | private slots: |
157 | void newConnection() |
158 | { |
159 | QTcpSocket *const s = tcpServer->nextPendingConnection(); |
160 | |
161 | if(s) |
162 | connect(sender: s, SIGNAL(readyRead()), receiver: this, SLOT(readyRead())); |
163 | } |
164 | |
165 | void readyRead() |
166 | { |
167 | QTcpSocket *const s = static_cast<QTcpSocket *>(sender()); |
168 | |
169 | while (s->bytesAvailable()) |
170 | { |
171 | const QString line(s->readLine()); |
172 | |
173 | if (line.startsWith(s: "Content-Length:" )) |
174 | bodyLength = line.mid(position: 15).toInt(); |
175 | |
176 | if (isBody) |
177 | { |
178 | body.append(a: line.toUtf8()); |
179 | bodyBytesRead += line.length(); |
180 | } |
181 | else if (line == "\r\n" ) |
182 | { |
183 | isBody = true; |
184 | if (bodyLength == -1) |
185 | { |
186 | qFatal(msg: "No length was specified in the header." ); |
187 | } |
188 | } |
189 | } |
190 | |
191 | if (bodyBytesRead == bodyLength) |
192 | { |
193 | QDomDocument domDoc; |
194 | success = domDoc.setContent(text: body); |
195 | eventLoop.exit(); |
196 | } |
197 | } |
198 | |
199 | private: |
200 | QByteArray body; |
201 | int bodyBytesRead, bodyLength; |
202 | bool isBody; |
203 | QTcpServer *tcpServer; |
204 | QNetworkAccessManager* httpClient; |
205 | }; |
206 | |
207 | void tst_QXmlInputSource::waitForReadyIODevice() const |
208 | { |
209 | QEventLoop el; |
210 | ServerAndClient sv(el); |
211 | QTimer::singleShot(msec: 1, receiver: &sv, SLOT(doIt())); |
212 | |
213 | el.exec(); |
214 | QVERIFY(sv.success); |
215 | } |
216 | |
217 | // This class is used to emulate a case where less than 4 bytes are sent in |
218 | // a single packet to ensure it is still parsed correctly |
219 | class SlowIODevice : public QIODevice |
220 | { |
221 | public: |
222 | SlowIODevice(const QString &expectedData, QObject *parent = 0) |
223 | : QIODevice(parent), currentPos(0), readyToSend(true) |
224 | { |
225 | stringData = expectedData.toUtf8(); |
226 | dataTimer = new QTimer(this); |
227 | connect(sender: dataTimer, signal: &QTimer::timeout, slot: [=]() { |
228 | readyToSend = true; |
229 | emit readyRead(); |
230 | dataTimer->stop(); |
231 | }); |
232 | dataTimer->start(msec: 1000); |
233 | } |
234 | bool open(SlowIODevice::OpenMode) override |
235 | { |
236 | setOpenMode(ReadOnly); |
237 | return true; |
238 | } |
239 | bool isSequential() const override |
240 | { |
241 | return true; |
242 | } |
243 | qint64 bytesAvailable() const override |
244 | { |
245 | if (readyToSend && stringData.size() != currentPos) |
246 | return qMax(a: 3, b: stringData.size() - currentPos); |
247 | return 0; |
248 | } |
249 | qint64 readData(char *data, qint64 maxSize) override |
250 | { |
251 | if (!readyToSend) |
252 | return 0; |
253 | const qint64 readSize = qMin(a: qMin(a: (qint64)3, b: maxSize), b: (qint64)(stringData.size() - currentPos)); |
254 | if (readSize > 0) |
255 | memcpy(dest: data, src: &stringData.constData()[currentPos], n: readSize); |
256 | currentPos += readSize; |
257 | readyToSend = false; |
258 | if (currentPos != stringData.size()) |
259 | dataTimer->start(msec: 1000); |
260 | return readSize; |
261 | } |
262 | qint64 writeData(const char *, qint64) override { return 0; } |
263 | bool waitForReadyRead(int msecs) override |
264 | { |
265 | // Delibrately wait a maximum of 10 seconds for the sake |
266 | // of the test, so it doesn't unduly hang |
267 | const int waitTime = qMax(a: 10000, b: msecs); |
268 | QElapsedTimer t; |
269 | t.start(); |
270 | while (t.elapsed() < waitTime) { |
271 | QCoreApplication::processEvents(); |
272 | if (readyToSend) |
273 | return true; |
274 | } |
275 | return false; |
276 | } |
277 | private: |
278 | QByteArray stringData; |
279 | int currentPos; |
280 | bool readyToSend; |
281 | QTimer *dataTimer; |
282 | }; |
283 | |
284 | void tst_QXmlInputSource::inputFromSlowDevice() const |
285 | { |
286 | QString expectedData = QStringLiteral("<foo><bar>kake</bar><bar>ja</bar></foo>" ); |
287 | SlowIODevice slowDevice(expectedData); |
288 | QXmlInputSource source(&slowDevice); |
289 | QString data; |
290 | while (true) { |
291 | const QChar nextChar = source.next(); |
292 | if (nextChar == QXmlInputSource::EndOfDocument) |
293 | break; |
294 | else if (nextChar != QXmlInputSource::EndOfData) |
295 | data += nextChar; |
296 | } |
297 | QCOMPARE(data, expectedData); |
298 | } |
299 | |
300 | QT_WARNING_POP |
301 | #endif // QT_DEPRECATED_SINCE(5, 15) |
302 | |
303 | QTEST_MAIN(tst_QXmlInputSource) |
304 | #include "tst_qxmlinputsource.moc" |
305 | |