summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-08-16 15:31:06 +0200
committerQt by Nokia <qt-info@nokia.com>2012-08-20 21:59:32 +0200
commitce9b010ec619aa6e5f19b6ae208b76a4e398b20b (patch)
tree7eccc6c7a4a5654dae1956df4a3f262dd8c0f109 /tests/auto
parent60818231d82ca34f1d33ccb9ba7500b5470a3d0d (diff)
Fix decoding of QByteArray in the deprecated "encoded" setters in QUrl
The asymmetry is intentional: the getters can use toLatin1() because the called functions, with a QUrl::FullyEncoded parameter, return ASCII only. This gives a small performance improvement over the need to run the UTF-8 encoder. However, the data passed to setters could contain non-ASCII binary data, in addition to the percent-encoded data. We can't use fromUtf8 because it's binary and we can't use toPercentEncoded because it already encoded. Change-Id: I5ecdb49be5af51ac86fd9764eb3a6aa96385f512 Reviewed-by: David Faure <faure@kde.org>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/corelib/io/qurl/tst_qurl.cpp13
-rw-r--r--tests/auto/corelib/io/qurlinternal/tst_qurlinternal.cpp32
2 files changed, 45 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp
index 6b7a8025ff..12e6e4a01d 100644
--- a/tests/auto/corelib/io/qurl/tst_qurl.cpp
+++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp
@@ -2247,6 +2247,8 @@ void tst_QUrl::setEncodedFragment_data()
QTest::newRow("initial url has fragment") << BA("http://www.kde.org#old") << BA("new") << BA("http://www.kde.org#new");
QTest::newRow("encoded fragment") << BA("http://www.kde.org") << BA("a%20c") << BA("http://www.kde.org#a%20c");
QTest::newRow("with #") << BA("http://www.kde.org") << BA("a#b") << BA("http://www.kde.org#a#b");
+ QTest::newRow("unicode") << BA("http://www.kde.org") << BA("\xc3\xa9") << BA("http://www.kde.org#%C3%A9");
+ QTest::newRow("binary") << BA("http://www.kde.org") << BA("\x00\xc0\x80", 3) << BA("http://www.kde.org#%00%C0%80");
}
void tst_QUrl::setEncodedFragment()
@@ -2926,6 +2928,17 @@ void tst_QUrl::componentEncodings()
QCOMPARE(url.toString(formatting),
(((QString(toString ))))); // the weird () and space is to align the output
+ if (formatting == QUrl::FullyEncoded) {
+ QCOMPARE(url.encodedUserName(), userName.toUtf8());
+ QCOMPARE(url.encodedPassword(), password.toUtf8());
+ // no encodedUserInfo
+ QCOMPARE(url.encodedHost(), host.toUtf8());
+ // no encodedAuthority
+ QCOMPARE(url.encodedPath(), path.toUtf8());
+ QCOMPARE(url.encodedQuery(), query.toUtf8());
+ QCOMPARE(url.encodedFragment(), fragment.toUtf8());
+ }
+
// repeat with the URL we got from toString
QUrl url2(toString);
QCOMPARE(url2.userName(formatting), userName);
diff --git a/tests/auto/corelib/io/qurlinternal/tst_qurlinternal.cpp b/tests/auto/corelib/io/qurlinternal/tst_qurlinternal.cpp
index e008133d87..5e7fc2c7b6 100644
--- a/tests/auto/corelib/io/qurlinternal/tst_qurlinternal.cpp
+++ b/tests/auto/corelib/io/qurlinternal/tst_qurlinternal.cpp
@@ -95,6 +95,8 @@ private Q_SLOTS:
void encodingRecode();
void encodingRecodeInvalidUtf8_data();
void encodingRecodeInvalidUtf8();
+ void recodeByteArray_data();
+ void recodeByteArray();
};
#include "tst_qurlinternal.moc"
@@ -1006,4 +1008,34 @@ void tst_QUrlInternal::encodingRecodeInvalidUtf8()
QCOMPARE(output, QTest::currentDataTag() + input);
}
+void tst_QUrlInternal::recodeByteArray_data()
+{
+ QTest::addColumn<QByteArray>("input");
+ QTest::addColumn<QString>("expected");
+
+ QTest::newRow("null") << QByteArray() << QString();
+ QTest::newRow("empty") << QByteArray("") << QString("");
+ QTest::newRow("normal") << QByteArray("Hello") << "Hello";
+ QTest::newRow("valid-utf8") << QByteArray("\xc3\xa9") << "%C3%A9";
+ QTest::newRow("percent-encoded") << QByteArray("%C3%A9%00%C0%80") << "%C3%A9%00%C0%80";
+ QTest::newRow("invalid-utf8-1") << QByteArray("\xc3\xc3") << "%C3%C3";
+ QTest::newRow("invalid-utf8-2") << QByteArray("\xc0\x80") << "%C0%80";
+
+ // note: percent-encoding the control characters ("\0" -> "%00") would also
+ // be correct, but it's unnecessary for this function
+ QTest::newRow("binary") << QByteArray("\0\x1f", 2) << QString::fromLatin1("\0\x1f", 2);;
+ QTest::newRow("binary+percent-encoded") << QByteArray("\0%25", 4) << QString::fromLatin1("\0%25", 4);
+}
+
+void tst_QUrlInternal::recodeByteArray()
+{
+ QFETCH(QByteArray, input);
+ QFETCH(QString, expected);
+ QString output = qt_urlRecodeByteArray(input);
+
+ QCOMPARE(output.isNull(), input.isNull());
+ QCOMPARE(output.isEmpty(), input.isEmpty());
+ QCOMPARE(output, expected);
+}
+
QTEST_APPLESS_MAIN(tst_QUrlInternal)