diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2021-04-14 15:14:33 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-04-15 04:09:38 +0000 |
commit | 16722743ceb59a5e60a05121773c6599a02cab20 (patch) | |
tree | d84094420954eeca9123682110e9dc7963a54a09 /tests/auto | |
parent | bcd7b0fe55355d7d4de7d38a0c8a22e5c0e1e6aa (diff) |
Check thisObject when calling various methods on URL
We should not crash when you try to call them on the wrong object.
Rather, throw a type error.
Change-Id: I1b146d9c77d838e013408988e02a65a623641f1f
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit f32436122f6ac16bfd6f23228b85a6f7c12502b6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/qml/qjsengine/tst_qjsengine.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp index 85d61a3537..e892d2dfcf 100644 --- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp @@ -273,6 +273,7 @@ private slots: void dataViewCtor(); void uiLanguage(); + void urlObject(); public: Q_INVOKABLE QJSValue throwingCppMethod1(); @@ -5270,6 +5271,69 @@ void tst_QJSEngine::uiLanguage() } } +void tst_QJSEngine::urlObject() +{ + QJSEngine engine; + + const QString href = QStringLiteral( + "http://uuu:ppp@example.com:777/foo/bar?search=stuff&other=where#hhh"); + const QUrl url(href); + + QJSManagedValue v(engine.evaluate(QStringLiteral("new URL('%1')").arg(href)), &engine); + QVERIFY(v.isObject()); + QJSManagedValue proto(v.prototype()); + + auto check = [&](const QString &prop, const QString &expected) { + QCOMPARE(v.property(prop).toString(), expected); + QVERIFY(proto.property(prop).isUndefined()); + QVERIFY(engine.hasError()); + QCOMPARE(engine.catchError().toString(), + QStringLiteral("TypeError: Value of \"this\" must be of type URL")); + }; + + check(QStringLiteral("href"), url.toString()); + check(QStringLiteral("origin"), QStringLiteral("http://example.com:777")); + check(QStringLiteral("protocol"), url.scheme()); + check(QStringLiteral("username"), url.userName()); + check(QStringLiteral("password"), url.password()); + check(QStringLiteral("host"), url.host() + u':' + QString::number(url.port())); + check(QStringLiteral("hostname"), url.host()); + check(QStringLiteral("port"), QString::number(url.port())); + check(QStringLiteral("pathname"), url.path()); + check(QStringLiteral("search"), QStringLiteral("?search=stuff&other=where")); + check(QStringLiteral("hash"), u'#' + url.fragment()); + + QJSManagedValue s(v.property("searchParams"), &engine); + QVERIFY(s.isObject()); + + const QStringList searchParamsMethods = { + QStringLiteral("append"), + QStringLiteral("delete"), + QStringLiteral("get"), + QStringLiteral("getAll"), + QStringLiteral("has"), + QStringLiteral("set"), + QStringLiteral("sort"), + QStringLiteral("entries"), + QStringLiteral("forEach"), + QStringLiteral("keys"), + QStringLiteral("values"), + QStringLiteral("toString") + }; + + for (const QString &method : searchParamsMethods) { + QJSManagedValue get(s.property(method), &engine); + + // Shoudn't crash. + // We get different error messages depending on parameters, though. + QJSValue undef = get.call({}); + QVERIFY(undef.isUndefined()); + QVERIFY(engine.hasError()); + engine.catchError(); + } + +} + QTEST_MAIN(tst_QJSEngine) #include "tst_qjsengine.moc" |