2022-08-16 14:04:10 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
SPDX-FileCopyrightText: 2022 MBition GmbH
|
|
|
|
SPDX-FileContributor: Kai Uwe Broulik <kai_uwe.broulik@mbition.io>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config-kwin.h>
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "utils/ramfile.h"
|
|
|
|
|
2023-07-03 19:28:19 +00:00
|
|
|
#include <QTest>
|
2022-08-16 14:04:10 +00:00
|
|
|
|
|
|
|
using namespace KWin;
|
|
|
|
|
|
|
|
class TestUtils : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private Q_SLOTS:
|
|
|
|
void testRamFile();
|
|
|
|
void testSealedRamFile();
|
|
|
|
};
|
|
|
|
|
|
|
|
static const QByteArray s_testByteArray = QByteArrayLiteral("Test Data \0\1\2\3");
|
|
|
|
static const char s_writeTestArray[] = "test";
|
|
|
|
|
|
|
|
void TestUtils::testRamFile()
|
|
|
|
{
|
|
|
|
KWin::RamFile file("test", s_testByteArray.constData(), s_testByteArray.size());
|
|
|
|
QVERIFY(file.isValid());
|
|
|
|
QCOMPARE(file.size(), s_testByteArray.size());
|
|
|
|
|
|
|
|
QVERIFY(file.fd() != -1);
|
|
|
|
|
|
|
|
char buf[20];
|
|
|
|
int num = read(file.fd(), buf, sizeof buf);
|
|
|
|
QCOMPARE(num, file.size());
|
|
|
|
|
|
|
|
QCOMPARE(qstrcmp(s_testByteArray.constData(), buf), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestUtils::testSealedRamFile()
|
|
|
|
{
|
|
|
|
#if HAVE_MEMFD
|
|
|
|
KWin::RamFile file("test", s_testByteArray.constData(), s_testByteArray.size(), KWin::RamFile::Flag::SealWrite);
|
|
|
|
QVERIFY(file.isValid());
|
|
|
|
QVERIFY(file.effectiveFlags().testFlag(KWin::RamFile::Flag::SealWrite));
|
|
|
|
|
|
|
|
// Writing should not work.
|
|
|
|
auto written = write(file.fd(), s_writeTestArray, strlen(s_writeTestArray));
|
|
|
|
QCOMPARE(written, -1);
|
|
|
|
|
|
|
|
// Cannot use MAP_SHARED on sealed file descriptor.
|
2022-10-24 17:02:36 +00:00
|
|
|
void *data = mmap(nullptr, file.size(), PROT_WRITE, MAP_SHARED, file.fd(), 0);
|
2022-08-16 14:04:10 +00:00
|
|
|
QCOMPARE(data, MAP_FAILED);
|
|
|
|
|
2022-10-24 17:02:36 +00:00
|
|
|
data = mmap(nullptr, file.size(), PROT_WRITE, MAP_PRIVATE, file.fd(), 0);
|
2022-08-16 14:04:10 +00:00
|
|
|
QVERIFY(data != MAP_FAILED);
|
|
|
|
#else
|
|
|
|
QSKIP("Sealing requires memfd suport.");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_MAIN(TestUtils)
|
|
|
|
#include "test_utils.moc"
|