From 639ce10071c90eab0d4935cf6bc2fa172b34ed04 Mon Sep 17 00:00:00 2001 From: Martin Kampas Date: Fri, 29 Mar 2013 12:19:19 +0100 Subject: [PATCH] tests/sandbox: initial commit --- tests/sandbox/sandbox.pro | 15 ++++ tests/sandbox/sandboxfileenginehandler.h | 90 ++++++++++++++++++++++++ tests/sandbox/sandboxhook.cpp | 12 ++++ tests/tests.pri | 2 + tests/tests.pro | 1 + 5 files changed, 120 insertions(+) create mode 100644 tests/sandbox/sandbox.pro create mode 100644 tests/sandbox/sandboxfileenginehandler.h create mode 100644 tests/sandbox/sandboxhook.cpp diff --git a/tests/sandbox/sandbox.pro b/tests/sandbox/sandbox.pro new file mode 100644 index 0000000..f4a83bf --- /dev/null +++ b/tests/sandbox/sandbox.pro @@ -0,0 +1,15 @@ +SOURCES = sandboxhook.cpp + +TEMPLATE = lib +TARGET = sandboxhook +CONFIG -= app_bundle +CONFIG += console qtestlib +QT -= gui +QT += network testlib + +!include( ../tests.pri ) { error("Unable to find tests include") } + +unix:target.path = $${PREFIX}/$$TESTS_PATH +INSTALLS += target + +!include( ../../buildpath.pri ) { error("Unable to find build path specification") } diff --git a/tests/sandbox/sandboxfileenginehandler.h b/tests/sandbox/sandboxfileenginehandler.h new file mode 100644 index 0000000..a2e2312 --- /dev/null +++ b/tests/sandbox/sandboxfileenginehandler.h @@ -0,0 +1,90 @@ +/** + * @file sandboxfileenginehandler.h + * @copyright 2013 Jolla Ltd. + * @author Martin Kampas + * @date 2013 + */ + +#ifndef _SANDBOXINGFILEENGINEHANDLER_H +#define _SANDBOXINGFILEENGINEHANDLER_H + +#include +#include +#include +#include +#include +#include + +#include +#include "../../constants.h" + +/** + * Redirects all file operations on system configuration files to files under + * directory specified by SSU_TESTS_SANDBOX environment variable. + */ +class SandboxFileEngineHandler : public QAbstractFileEngineHandler { + public: + QAbstractFileEngine *create(const QString &fileName) const{ + static bool enabled = false; + static bool firstCall = true; + + if (!enabled && !firstCall){ + return 0; + } + + static QString sandboxPath = + QProcessEnvironment::systemEnvironment().value("SSU_TESTS_SANDBOX"); + + if (firstCall){ + firstCall = false; + + if (sandboxPath.isEmpty()){ + return 0; + } + + if (!QFileInfo(sandboxPath).exists()){ + qFatal("%s: Invalid SSU_TESTS_SANDBOX value: No such file or directory", + qPrintable(sandboxPath)); + } + + if (!QFileInfo(sandboxPath).isDir()){ + qFatal("%s: Invalid SSU_TESTS_SANDBOX value: Not a directory", + qPrintable(sandboxPath)); + } + + enabled = true; + } + + if (!fileName.startsWith('/')){ + return 0; + } + + static QSet ssuConfigFiles = QSet() + << SSU_CONFIGURATION + << SSU_REPO_CONFIGURATION + << SSU_DEFAULT_CONFIGURATION + << SSU_BOARD_MAPPING_CONFIGURATION; + + static QSet ssuConfigDirectories = QSet() + << SSU_BOARD_MAPPING_CONFIGURATION_DIR; + + if (!ssuConfigFiles.contains(fileName)){ + bool match = false; + foreach (const QString &ssuConfigDirectory, ssuConfigDirectories){ + if (fileName.startsWith(ssuConfigDirectory + '/')){ + match = true; + break; + } + } + if (!match){ + return 0; + } + } + + const QString fileName_ = QDir(sandboxPath).absoluteFilePath(QString(fileName).remove(0, 1)); + + return new QFSFileEngine(fileName_); + } +}; + +#endif diff --git a/tests/sandbox/sandboxhook.cpp b/tests/sandbox/sandboxhook.cpp new file mode 100644 index 0000000..7a9423c --- /dev/null +++ b/tests/sandbox/sandboxhook.cpp @@ -0,0 +1,12 @@ +#include + +#include "sandboxfileenginehandler.h" + +extern "C" void qt_startup_hook() +{ + SandboxFileEngineHandler *const handler = new SandboxFileEngineHandler(); + Q_UNUSED(handler); + + static void(*next_qt_startup_hook)() = (void (*)()) dlsym(RTLD_NEXT, "qt_startup_hook"); + next_qt_startup_hook(); +} diff --git a/tests/tests.pri b/tests/tests.pri index 62db2f3..29b0ac5 100644 --- a/tests/tests.pri +++ b/tests/tests.pri @@ -1,3 +1,5 @@ +DEPENDPATH *= $${PWD}/sandbox + TESTS_PATH = /opt/tests/ssu DEFINES += TESTS_PATH="'\"$${TESTS_PATH}\"'" diff --git a/tests/tests.pro b/tests/tests.pro index 8767aac..371c9cc 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -1,6 +1,7 @@ TEMPLATE = subdirs CONFIG += qt ordered coverage debug SUBDIRS = \ + sandbox \ ut_settings \ ut_urlresolver \ ut_variables \