sandboxtest.cpp 3.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
 * @file sandboxtest.cpp
 * @copyright 2013 Jolla Ltd.
 * @author Martin Kampas <martin.kampas@tieto.com>
 * @date 2013
 */

#include "sandboxtest.h"

#include <QtTest/QtTest>

#include "libssu/sandbox_p.h"

14 15
void SandboxTest::test()
{
16

17
    const QDir::Filters noHidden = QDir::AllEntries | QDir::NoDotAndDotDot;
18

19 20 21 22 23
    QCOMPARE(QDir(Sandbox::map(LOCATE_DATA_PATH, "/world")).entryList(noHidden, QDir::Name),
             QStringList()
             << "world-and-sandbox"
             << "world-only"
             << "world-only-to-be-copied-into-sandbox");
24

25 26 27
    QVERIFY(!QFileInfo(Sandbox::map(LOCATE_DATA_PATH, "/world/world-only")).isWritable());
    QCOMPARE(readAll(Sandbox::map(LOCATE_DATA_PATH, "/world/world-only")).trimmed(),
             QString("world/world-only"));
28

29 30 31
    QVERIFY(!QFileInfo(Sandbox::map(LOCATE_DATA_PATH, "/world/world-and-sandbox")).isWritable());
    QCOMPARE(readAll(Sandbox::map(LOCATE_DATA_PATH, "/world/world-and-sandbox")).trimmed(),
             QString("world/world-and-sandbox"));
32

33 34 35 36
    QVERIFY(!QFileInfo(Sandbox::map(LOCATE_DATA_PATH, "/world/world-only-to-be-copied-into-sandbox"))
            .isWritable());
    QCOMPARE(readAll(Sandbox::map(LOCATE_DATA_PATH, "/world/world-only-to-be-copied-into-sandbox"))
             .trimmed(), QString("world/world-only-to-be-copied-into-sandbox"));
37

38
    QVERIFY(!QFileInfo(Sandbox::map(LOCATE_DATA_PATH, "/world/sandbox-only")).exists());
39 40


41 42 43 44 45
    Sandbox sandbox(Sandbox::map(LOCATE_DATA_PATH, "/sandbox"),
                    Sandbox::UseAsSkeleton, Sandbox::ThisProcess | Sandbox::ChildProcesses);
    sandbox.addWorldFiles(Sandbox::map(LOCATE_DATA_PATH, "/world"), QDir::AllEntries,
                          QStringList() << "*-to-be-copied-into-sandbox");
    QVERIFY(sandbox.activate());
46 47


48 49 50 51 52
    QCOMPARE(QDir(Sandbox::map(LOCATE_DATA_PATH, "/world")).entryList(noHidden, QDir::Name),
             QStringList()
             << "sandbox-only"
             << "world-and-sandbox"
             << "world-only-to-be-copied-into-sandbox");
53

54
    QVERIFY(!QFileInfo(Sandbox::map(LOCATE_DATA_PATH, "/world/world-only")).exists());
55

56 57 58
    QVERIFY(QFileInfo(Sandbox::map(LOCATE_DATA_PATH, "/world/world-and-sandbox")).isWritable());
    QCOMPARE(readAll(Sandbox::map(LOCATE_DATA_PATH, "/world/world-and-sandbox")).trimmed(),
             QString("sandbox/world-and-sandbox"));
59

60 61 62 63
    QVERIFY(QFileInfo(Sandbox::map(LOCATE_DATA_PATH, "/world/world-only-to-be-copied-into-sandbox"))
            .isWritable());
    QCOMPARE(readAll(Sandbox::map(LOCATE_DATA_PATH, "/world/world-only-to-be-copied-into-sandbox"))
             .trimmed(), QString("world/world-only-to-be-copied-into-sandbox"));
64

65 66 67 68
    QVERIFY(QFileInfo(Sandbox::map(LOCATE_DATA_PATH, "/world/sandbox-only")).exists());
    QVERIFY(QFileInfo(Sandbox::map(LOCATE_DATA_PATH, "/world/sandbox-only")).isWritable());
    QCOMPARE(readAll(Sandbox::map(LOCATE_DATA_PATH, "/world/sandbox-only")).trimmed(),
             QString("sandbox/sandbox-only"));
69 70
}

71 72 73 74 75 76 77 78
QString SandboxTest::readAll(const QString &fileName)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning("%s: Failed to open file for reading: '%s': %s", Q_FUNC_INFO, qPrintable(fileName),
                 qPrintable(file.errorString()));
        return QString();
    }
79

80
    return file.readAll();
81
}