Debian Codegraph @

tests/auto/core5/sax/qxmlinputsource/tst_qxmlinputsource.cpp

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