From 1e32ade79ced64597ef4148f16e0d8a329733c4e Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Fri, 11 Sep 2015 12:15:03 +0300 Subject: QIODevice: fix interleaving read() and write() in text mode under Windows Skip the correct number of bytes in the read buffer when expanding '\n' into "\r\n" upon writing. Change-Id: I5b01fc47c330dee5c83001abf0acd7d63d790b96 Reviewed-by: Oswald Buddenhagen --- tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'tests/auto/corelib/io/qiodevice') diff --git a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp index 565ca18899..334f5aba05 100644 --- a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp +++ b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp @@ -59,6 +59,7 @@ private slots: void peekBug(); void readAllKeepPosition(); + void writeInTextMode(); }; void tst_QIODevice::initTestCase() @@ -628,5 +629,45 @@ void tst_QIODevice::readAllKeepPosition() QCOMPARE(resultArray, buffer.buffer()); } +class RandomAccessBuffer : public QIODevice +{ +public: + RandomAccessBuffer(const char *data) : QIODevice(), buf(data) { } + +protected: + qint64 readData(char *data, qint64 maxSize) Q_DECL_OVERRIDE + { + maxSize = qMin(maxSize, qint64(buf.size() - pos())); + memcpy(data, buf.constData() + pos(), maxSize); + return maxSize; + } + qint64 writeData(const char *data, qint64 maxSize) Q_DECL_OVERRIDE + { + maxSize = qMin(maxSize, qint64(buf.size() - pos())); + memcpy(buf.data() + pos(), data, maxSize); + return maxSize; + } + +private: + QByteArray buf; +}; + +// Test write() on skipping correct number of bytes in read buffer +void tst_QIODevice::writeInTextMode() +{ + // Unlike other platforms, Windows implementation expands '\n' into + // "\r\n" sequence in write(). Ensure that write() properly works with + // a read buffer on random-access devices. +#ifndef Q_OS_WIN + QSKIP("This is a Windows-only test"); +#else + RandomAccessBuffer buffer("one\r\ntwo\r\nthree\r\n"); + buffer.open(QBuffer::ReadWrite | QBuffer::Text); + QCOMPARE(buffer.readLine(), QByteArray("one\n")); + QCOMPARE(buffer.write("two\n"), 4); + QCOMPARE(buffer.readLine(), QByteArray("three\n")); +#endif +} + QTEST_MAIN(tst_QIODevice) #include "tst_qiodevice.moc" -- cgit v1.2.3