From 0740ab56d7c3b540677e33e28924d47bce87c1cf Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 13 Oct 2022 10:52:34 -0700 Subject: IPC: add support for multiple backends to QSharedMemory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simultaneously. Change-Id: If4c23ea3719947d790d4fffd17152760989b9bc6 Reviewed-by: Tor Arne Vestbø Reviewed-by: Fabian Kosmale --- .../ipc/qsharedmemory/tst_qsharedmemory.cpp | 71 +++++++++++++++++----- 1 file changed, 56 insertions(+), 15 deletions(-) (limited to 'tests') diff --git a/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp b/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp index 44f5cee82d..4c871e0269 100644 --- a/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp +++ b/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp @@ -19,6 +19,7 @@ #endif #include "private/qtcore-config_p.h" +#include "../ipctestcommon.h" #define EXISTING_SIZE 1024 @@ -31,15 +32,11 @@ class tst_QSharedMemory : public QObject { Q_OBJECT -public: - tst_QSharedMemory(); - virtual ~tst_QSharedMemory(); - public Q_SLOTS: + void initTestCase(); void init(); void cleanup(); - private slots: // basics void constructor(); @@ -51,6 +48,8 @@ private slots: void create(); void attach_data(); void attach(); + void changeKeyType_data() { attach_data(); } + void changeKeyType(); void lock(); // custom edge cases @@ -92,7 +91,7 @@ protected: QNativeIpcKey platformSafeKey(const QString &key) { - QNativeIpcKey::Type keyType = QNativeIpcKey::DefaultTypeForOs; + QFETCH_GLOBAL(QNativeIpcKey::Type, keyType); return QSharedMemory::platformSafeKey(mangleKey(key), keyType); } @@ -108,21 +107,16 @@ protected: QList keys; QList jail; - QSharedMemory *existingSharedMemory; + QSharedMemory *existingSharedMemory = nullptr; int seq = 0; private: - const QString m_helperBinary; + const QString m_helperBinary = "./producerconsumer_helper"; }; -tst_QSharedMemory::tst_QSharedMemory() - : existingSharedMemory(0) - , m_helperBinary("./producerconsumer_helper") -{ -} - -tst_QSharedMemory::~tst_QSharedMemory() +void tst_QSharedMemory::initTestCase() { + IpcTestCommon::addGlobalTestRows(); } void tst_QSharedMemory::init() @@ -285,6 +279,25 @@ void tst_QSharedMemory::nativeKey() QCOMPARE(sm.size(), 0); QCOMPARE(sm.detach(), false); + + // change the key type + QNativeIpcKey::Type nextKeyType = IpcTestCommon::nextKeyType(setIpcKey.type()); + if (nextKeyType != setIpcKey.type()) { + QNativeIpcKey setIpcKey2 = QSharedMemory::platformSafeKey(setKey, nextKeyType); + sm.setNativeKey(setIpcKey2); + + QCOMPARE(sm.nativeIpcKey(), setIpcKey2); + QCOMPARE(sm.nativeKey(), setIpcKey2.nativeKey()); + + QCOMPARE(sm.isAttached(), false); + + QCOMPARE(sm.error(), QSharedMemory::NoError); + QCOMPARE(sm.errorString(), QString()); + QVERIFY(!sm.data()); + QCOMPARE(sm.size(), 0); + + QCOMPARE(sm.detach(), false); + } } QT_WARNING_PUSH @@ -408,6 +421,34 @@ void tst_QSharedMemory::attach() } } +void tst_QSharedMemory::changeKeyType() +{ + QFETCH(QString, key); + QFETCH(bool, exists); + QFETCH(QSharedMemory::SharedMemoryError, error); + + QNativeIpcKey nativeKey = platformSafeKey(key); + QNativeIpcKey::Type nextKeyType = IpcTestCommon::nextKeyType(nativeKey.type()); + if (nextKeyType == nativeKey.type()) + QSKIP("System only supports one key type"); +// qDebug() << "Changing from" << nativeKey.type() << "to" << nextKeyType; + + QSharedMemory sm(nativeKey); + QCOMPARE(sm.attach(), exists); + QCOMPARE(sm.error(), error); + + QNativeIpcKey nextKey = + QSharedMemory::platformSafeKey(mangleKey(key), nextKeyType); + sm.setNativeKey(nextKey); + QCOMPARE(sm.isAttached(), false); + QVERIFY(!sm.attach()); + + if (exists) + QCOMPARE(sm.error(), QSharedMemory::NotFound); + else + QCOMPARE(sm.error(), error); +} + void tst_QSharedMemory::lock() { QSharedMemory shm; -- cgit v1.2.3