sandboxtest.cpp 2.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/**
 * @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"

void SandboxTest::test(){

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

  QCOMPARE(QDir(TESTS_DATA_PATH "/world").entryList(noHidden, QDir::Name),
      QStringList()
      << "world-and-sandbox"
      << "world-only"
      << "world-only-to-be-copied-into-sandbox");

  QVERIFY(!QFileInfo(TESTS_DATA_PATH "/world/world-only").isWritable());
  QCOMPARE(readAll(TESTS_DATA_PATH "/world/world-only").trimmed(),
      QString("world/world-only"));

  QVERIFY(!QFileInfo(TESTS_DATA_PATH "/world/world-and-sandbox").isWritable());
  QCOMPARE(readAll(TESTS_DATA_PATH "/world/world-and-sandbox").trimmed(),
      QString("world/world-and-sandbox"));

  QVERIFY(!QFileInfo(TESTS_DATA_PATH "/world/world-only-to-be-copied-into-sandbox").isWritable());
  QCOMPARE(readAll(TESTS_DATA_PATH "/world/world-only-to-be-copied-into-sandbox").trimmed(),
      QString("world/world-only-to-be-copied-into-sandbox"));

  QVERIFY(!QFileInfo(TESTS_DATA_PATH "/world/sandbox-only").exists());


  Sandbox sandbox(TESTS_DATA_PATH "/sandbox",
      Sandbox::UseAsSkeleton, Sandbox::ThisProcess | Sandbox::ChildProcesses);
  sandbox.addWorldFiles(TESTS_DATA_PATH "/world", QDir::AllEntries,
      QStringList() << "*-to-be-copied-into-sandbox");
  QVERIFY(sandbox.activate());


  QCOMPARE(QDir(TESTS_DATA_PATH "/world").entryList(noHidden, QDir::Name),
      QStringList()
      << "world-and-sandbox"
      << "world-only"
      << "world-only-to-be-copied-into-sandbox");

  QVERIFY(!QFileInfo(TESTS_DATA_PATH "/world/world-only").isWritable());
  QCOMPARE(readAll(TESTS_DATA_PATH "/world/world-only").trimmed(),
      QString("world/world-only"));

  QVERIFY(QFileInfo(TESTS_DATA_PATH "/world/world-and-sandbox").isWritable());
  QCOMPARE(readAll(TESTS_DATA_PATH "/world/world-and-sandbox").trimmed(),
      QString("sandbox/world-and-sandbox"));

  QVERIFY(QFileInfo(TESTS_DATA_PATH "/world/world-only-to-be-copied-into-sandbox").isWritable());
  QCOMPARE(readAll(TESTS_DATA_PATH "/world/world-only-to-be-copied-into-sandbox").trimmed(),
      QString("world/world-only-to-be-copied-into-sandbox"));

  QVERIFY(QFileInfo(TESTS_DATA_PATH "/world/sandbox-only").exists());
  QVERIFY(QFileInfo(TESTS_DATA_PATH "/world/sandbox-only").isWritable());
  QCOMPARE(readAll(TESTS_DATA_PATH "/world/sandbox-only").trimmed(),
      QString("sandbox/sandbox-only"));
}

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();
  }

  return file.readAll();
}