From f06b629bfb42f50b9562704b3be49f5d3f8510bb Mon Sep 17 00:00:00 2001 From: David Faure Date: Tue, 22 May 2012 17:43:50 +0200 Subject: Fix handling of invalid urls in QDataStream << QUrl When given an invalid url, the output shouldn't be a valid url. KDE's kurltest detected this regression compared to Qt4, where all invalid urls were empty in toString() -- but we don't want that, to give as much feedback as possible to the user. Change-Id: Ie53e6e1c0a1d4bb9e12b820220dfb7e2f7753959 Reviewed-by: Thiago Macieira --- src/corelib/io/qurl.cpp | 4 +++- tests/auto/corelib/io/qurl/tst_qurl.cpp | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 6e17740869..1bad2a95e9 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -2879,7 +2879,9 @@ bool QUrl::isParentOf(const QUrl &childUrl) const */ QDataStream &operator<<(QDataStream &out, const QUrl &url) { - QByteArray u = url.toString(QUrl::FullyEncoded).toLatin1(); + QByteArray u; + if (url.isValid()) + u = url.toEncoded(); out << u; return out; } diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index 29b88980ad..7ee4f74999 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -165,6 +165,8 @@ private slots: void componentEncodings(); void setComponents_data(); void setComponents(); + void streaming_data(); + void streaming(); }; // Testing get/set functions @@ -3146,5 +3148,37 @@ void tst_QUrl::setComponents() } } +void tst_QUrl::streaming_data() +{ + QTest::addColumn("urlStr"); + + QTest::newRow("origURL") << "http://www.website.com/directory/?#ref"; + QTest::newRow("urlWithPassAndNoUser") << "ftp://:password@ftp.kde.org/path"; + QTest::newRow("accentuated") << QString::fromUtf8("trash:/été"); + QTest::newRow("withPercents") << "http://host/path%25path?%3Fque%25ry#%23ref%25"; + QTest::newRow("empty") << ""; + QVERIFY(!QUrl("ptal://mlc:usb").isValid()); + QTest::newRow("invalid") << "ptal://mlc:usb"; + QTest::newRow("ipv6") << "http://[::ffff:129.144.52.38]:81?query"; +} + +void tst_QUrl::streaming() +{ + QFETCH(QString, urlStr); + QUrl url(urlStr); + + QByteArray buffer; + QDataStream writeStream( &buffer, QIODevice::WriteOnly ); + writeStream << url; + + QDataStream stream( buffer ); + QUrl restored; + stream >> restored; + if (url.isValid()) + QCOMPARE(restored.url(), url.url()); + else + QVERIFY(!restored.isValid()); +} + QTEST_MAIN(tst_QUrl) #include "tst_qurl.moc" -- cgit v1.2.3