From 09222ab2cd57d2412ce6cbf9b32a6b1d88cd719a Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Wed, 12 Aug 2020 09:41:40 +0200 Subject: Check for ssl when compling qml tests Do not include certificate error qml test if no ssl. This change does some copy-paste but this will be handled in qt6. Change-Id: I8cc6d37074d78ca9f55333f479fb410ef927385d Reviewed-by: Allan Sandfeld Jensen --- .../quick/qmltests/data/tst_certificateError.qml | 114 --------------- tests/auto/quick/qmltests/qmltests.pro | 1 - tests/auto/quick/qmltests/tst_qmltests.cpp | 5 - .../quick/qmltests_ssl/data/TestWebEngineView.qml | 122 ++++++++++++++++ .../qmltests_ssl/data/tst_certificateError.qml | 114 +++++++++++++++ tests/auto/quick/qmltests_ssl/qmltests_ssl.pro | 10 ++ tests/auto/quick/qmltests_ssl/tst_qmltests_ssl.cpp | 160 +++++++++++++++++++++ tests/auto/quick/quick.pro | 2 + 8 files changed, 408 insertions(+), 120 deletions(-) delete mode 100644 tests/auto/quick/qmltests/data/tst_certificateError.qml create mode 100644 tests/auto/quick/qmltests_ssl/data/TestWebEngineView.qml create mode 100644 tests/auto/quick/qmltests_ssl/data/tst_certificateError.qml create mode 100644 tests/auto/quick/qmltests_ssl/qmltests_ssl.pro create mode 100644 tests/auto/quick/qmltests_ssl/tst_qmltests_ssl.cpp diff --git a/tests/auto/quick/qmltests/data/tst_certificateError.qml b/tests/auto/quick/qmltests/data/tst_certificateError.qml deleted file mode 100644 index 0629be175..000000000 --- a/tests/auto/quick/qmltests/data/tst_certificateError.qml +++ /dev/null @@ -1,114 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWebEngine module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -import QtQuick 2.2 -import QtTest 1.0 -import QtWebEngine 1.9 - -import Test.Shared 1.0 as Shared - -TestWebEngineView { - id: view; width: 320; height: 320 - - property bool deferError: false - property bool acceptCertificate: false - - onCertificateError: function(error) { - if (deferError) - error.defer() - else if (acceptCertificate) - error.ignoreCertificateError() - else - error.rejectCertificate() - } - - SignalSpy { - id: spyError - target: view - signalName: 'certificateError' - } - - TestCase { - name: 'CertificateError' - when: windowShown - - function initTestCase() { - Shared.HttpsServer.setExpectError(true) - Shared.HttpsServer.newRequest.connect(function (request) { - request.setResponseBody('Test') - request.sendResponse() - }) - view.settings.errorPageEnabled = false - } - - function init() { - verify(Shared.HttpsServer.start()) - } - - function cleanup() { - Shared.HttpsServer.stop() - view.deferError = false - view.acceptCertificate = false - spyError.clear() - } - - function test_error_data() { - return [ - { tag: 'reject', deferError: false, acceptCertificate: false, expectedContent: '' }, - { tag: 'defer_reject', deferError: true, acceptCertificate: false, expectedContent: '' }, - { tag: 'defer_accept', deferError: true, acceptCertificate: true, expectedContent: 'Test' }, - ] - } - - function test_error(data) { - view.deferError = data.deferError - view.acceptCertificate = data.acceptCertificate - view.url = Shared.HttpsServer.url() - - if (data.deferError) { - spyError.wait() - compare(spyError.count, 1) - compare('', view.getBodyText()) - - let error = spyError.signalArguments[0][0] - if (data.acceptCertificate) - error.ignoreCertificateError() - else - error.rejectCertificate() - } - - if (data.acceptCertificate) - verify(view.waitForLoadSucceeded()) - else - verify(view.waitForLoadFailed()) - - compare(spyError.count, 1) - compare(data.expectedContent, view.getBodyText()) - } - } -} diff --git a/tests/auto/quick/qmltests/qmltests.pro b/tests/auto/quick/qmltests/qmltests.pro index 6bec6dc0d..5c57f7ad9 100644 --- a/tests/auto/quick/qmltests/qmltests.pro +++ b/tests/auto/quick/qmltests/qmltests.pro @@ -1,5 +1,4 @@ include(../tests.pri) -include(../../shared/https.pri) QT += qmltest diff --git a/tests/auto/quick/qmltests/tst_qmltests.cpp b/tests/auto/quick/qmltests/tst_qmltests.cpp index 0d830931d..819f0b07c 100644 --- a/tests/auto/quick/qmltests/tst_qmltests.cpp +++ b/tests/auto/quick/qmltests/tst_qmltests.cpp @@ -26,8 +26,6 @@ ** ****************************************************************************/ -#include - #include #include #include @@ -145,9 +143,6 @@ int main(int argc, char **argv) qmlRegisterType("Test.util", 1, 0, "TempDir"); QTEST_SET_MAIN_SOURCE_PATH - - qmlRegisterSingletonType("Test.Shared", 1, 0, "HttpsServer", [&] (QQmlEngine *, QJSEngine *) { return new HttpsServer; }); - int i = quick_test_main(argc, argv, "qmltests", QUICK_TEST_SOURCE_DIR); return i; } diff --git a/tests/auto/quick/qmltests_ssl/data/TestWebEngineView.qml b/tests/auto/quick/qmltests_ssl/data/TestWebEngineView.qml new file mode 100644 index 000000000..6db076ae8 --- /dev/null +++ b/tests/auto/quick/qmltests_ssl/data/TestWebEngineView.qml @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import QtTest 1.1 +import QtWebEngine 1.7 + +WebEngineView { + property var loadStatus: null + property bool windowCloseRequestedSignalEmitted: false + settings.focusOnNavigationEnabled: true + + function waitForLoadSucceeded(timeout) { + var success = _waitFor(function() { return loadStatus == WebEngineView.LoadSucceededStatus }, timeout) + loadStatus = null + return success + } + function waitForLoadFailed(timeout) { + var failure = _waitFor(function() { return loadStatus == WebEngineView.LoadFailedStatus }, timeout) + loadStatus = null + return failure + } + function waitForLoadStopped(timeout) { + var stop = _waitFor(function() { return loadStatus == WebEngineView.LoadStoppedStatus }, timeout) + loadStatus = null + return stop + } + function waitForWindowCloseRequested() { + return _waitFor(function() { return windowCloseRequestedSignalEmitted; }); + } + function _waitFor(predicate, timeout) { + if (timeout === undefined) + timeout = 12000; + var i = 0 + while (i < timeout && !predicate()) { + testResult.wait(50) + i += 50 + } + return predicate() + } + + function getActiveElementId() { + var activeElementId; + runJavaScript("document.activeElement.id", function(result) { + activeElementId = result; + }); + testCase.tryVerify(function() { return activeElementId != undefined }); + return activeElementId; + } + + function verifyElementHasFocus(element) { + testCase.tryVerify(function() { return getActiveElementId() == element; }, 5000, + "Element \"" + element + "\" has focus"); + } + + function setFocusToElement(element) { + runJavaScript("document.getElementById('" + element + "').focus()"); + verifyElementHasFocus(element); + } + + function getElementCenter(element) { + var center; + runJavaScript("(function() {" + + " var elem = document.getElementById('" + element + "');" + + " var rect = elem.getBoundingClientRect();" + + " return { 'x': (rect.left + rect.right) / 2, 'y': (rect.top + rect.bottom) / 2 };" + + "})();", function(result) { center = result } ); + testCase.tryVerify(function() { return center !== undefined; }); + return center; + } + + function getTextSelection() { + var textSelection; + runJavaScript("window.getSelection().toString()", function(result) { textSelection = result }); + testCase.tryVerify(function() { return textSelection !== undefined; }); + return textSelection; + } + + TestResult { id: testResult } + TestCase { id: testCase } + + onLoadingChanged: { + loadStatus = loadRequest.status + } + + onWindowCloseRequested: { + windowCloseRequestedSignalEmitted = true; + } + + function getBodyText() { + let text + runJavaScript('document.body.innerText', function(t) { text = t }) + testCase.tryVerify(function() { return text !== undefined }) + return text + } +} + diff --git a/tests/auto/quick/qmltests_ssl/data/tst_certificateError.qml b/tests/auto/quick/qmltests_ssl/data/tst_certificateError.qml new file mode 100644 index 000000000..0629be175 --- /dev/null +++ b/tests/auto/quick/qmltests_ssl/data/tst_certificateError.qml @@ -0,0 +1,114 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.2 +import QtTest 1.0 +import QtWebEngine 1.9 + +import Test.Shared 1.0 as Shared + +TestWebEngineView { + id: view; width: 320; height: 320 + + property bool deferError: false + property bool acceptCertificate: false + + onCertificateError: function(error) { + if (deferError) + error.defer() + else if (acceptCertificate) + error.ignoreCertificateError() + else + error.rejectCertificate() + } + + SignalSpy { + id: spyError + target: view + signalName: 'certificateError' + } + + TestCase { + name: 'CertificateError' + when: windowShown + + function initTestCase() { + Shared.HttpsServer.setExpectError(true) + Shared.HttpsServer.newRequest.connect(function (request) { + request.setResponseBody('Test') + request.sendResponse() + }) + view.settings.errorPageEnabled = false + } + + function init() { + verify(Shared.HttpsServer.start()) + } + + function cleanup() { + Shared.HttpsServer.stop() + view.deferError = false + view.acceptCertificate = false + spyError.clear() + } + + function test_error_data() { + return [ + { tag: 'reject', deferError: false, acceptCertificate: false, expectedContent: '' }, + { tag: 'defer_reject', deferError: true, acceptCertificate: false, expectedContent: '' }, + { tag: 'defer_accept', deferError: true, acceptCertificate: true, expectedContent: 'Test' }, + ] + } + + function test_error(data) { + view.deferError = data.deferError + view.acceptCertificate = data.acceptCertificate + view.url = Shared.HttpsServer.url() + + if (data.deferError) { + spyError.wait() + compare(spyError.count, 1) + compare('', view.getBodyText()) + + let error = spyError.signalArguments[0][0] + if (data.acceptCertificate) + error.ignoreCertificateError() + else + error.rejectCertificate() + } + + if (data.acceptCertificate) + verify(view.waitForLoadSucceeded()) + else + verify(view.waitForLoadFailed()) + + compare(spyError.count, 1) + compare(data.expectedContent, view.getBodyText()) + } + } +} diff --git a/tests/auto/quick/qmltests_ssl/qmltests_ssl.pro b/tests/auto/quick/qmltests_ssl/qmltests_ssl.pro new file mode 100644 index 000000000..a8325e497 --- /dev/null +++ b/tests/auto/quick/qmltests_ssl/qmltests_ssl.pro @@ -0,0 +1,10 @@ +include(../tests.pri) +include(../../shared/https.pri) +QT += qmltest + +IMPORTPATH += $$PWD/data + +OTHER_FILES += $$PWD/data/tst_certificateError.qml + +load(qt_build_paths) +DEFINES += QUICK_TEST_SOURCE_DIR=\\\"$$re_escape($$PWD$${QMAKE_DIR_SEP}data)\\\" diff --git a/tests/auto/quick/qmltests_ssl/tst_qmltests_ssl.cpp b/tests/auto/quick/qmltests_ssl/tst_qmltests_ssl.cpp new file mode 100644 index 000000000..1f54ffd8e --- /dev/null +++ b/tests/auto/quick/qmltests_ssl/tst_qmltests_ssl.cpp @@ -0,0 +1,160 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#if QT_CONFIG(ssl) +#include +#endif + +#include +#include +#include +#include +#include +#include "qt_webengine_quicktest.h" + +#if defined(Q_OS_LINUX) && defined(QT_DEBUG) +#include +#include +#include +#endif + +#if defined(Q_OS_LINUX) && defined(QT_DEBUG) +static bool debuggerPresent() +{ + int fd = open("/proc/self/status", O_RDONLY); + if (fd == -1) + return false; + char buffer[2048]; + ssize_t size = read(fd, buffer, sizeof(buffer) - 1); + if (size == -1) { + close(fd); + return false; + } + buffer[size] = 0; + const char tracerPidToken[] = "\nTracerPid:"; + char *tracerPid = strstr(buffer, tracerPidToken); + if (!tracerPid) { + close(fd); + return false; + } + tracerPid += sizeof(tracerPidToken); + long int pid = strtol(tracerPid, &tracerPid, 10); + close(fd); + return pid != 0; +} + +static void stackTrace() +{ + bool ok = false; + const int disableStackDump = qEnvironmentVariableIntValue("QTEST_DISABLE_STACK_DUMP", &ok); + if (ok && disableStackDump == 1) + return; + + if (debuggerPresent()) + return; + + fprintf(stderr, "\n========= Received signal, dumping stack ==============\n"); + char cmd[512]; + qsnprintf(cmd, 512, "gdb --pid %d 2>/dev/null < app; + + // Force to use English language for testing due to error message checks + QLocale::setDefault(QLocale("en")); + + static QByteArrayList params = {QByteArrayLiteral("--use-fake-device-for-media-stream")}; + QVector w_argv(argc); \ + for (int i = 0; i < argc; ++i) \ + w_argv[i] = argv[i]; \ + for (int i = 0; i < params.size(); ++i) \ + w_argv.append(params[i].data()); \ + int w_argc = w_argv.size(); \ + + if (!QCoreApplication::instance()) { + app.reset(new Application(w_argc, const_cast(w_argv.data()))); + } + QtWebEngine::initialize(); + QQuickWebEngineProfile::defaultProfile()->setOffTheRecord(true); + qmlRegisterType("Test.util", 1, 0, "TempDir"); + + QTEST_SET_MAIN_SOURCE_PATH +#if QT_CONFIG(ssl) + qmlRegisterSingletonType("Test.Shared", 1, 0, "HttpsServer", [&] (QQmlEngine *, QJSEngine *) { return new HttpsServer; }); +#endif + int i = quick_test_main(argc, argv, "qmltests", QUICK_TEST_SOURCE_DIR); + return i; +} + +#include "tst_qmltests_ssl.moc" diff --git a/tests/auto/quick/quick.pro b/tests/auto/quick/quick.pro index 865ec718e..41ea5c4c1 100644 --- a/tests/auto/quick/quick.pro +++ b/tests/auto/quick/quick.pro @@ -18,5 +18,7 @@ qtConfig(webengine-testsupport) { qquickwebengineviewgraphics } +qtConfig(ssl): SUBDIRS += qmltests_ssl + # QTBUG-66055 boot2qt: SUBDIRS -= inspectorserver qquickwebengineview qmltests qmltests2 -- cgit v1.2.3