summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/ipc/ipctestcommon.h
blob: 25c7b8ce4400911378f53ea15aa704d88a771be0 (plain)
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
80
81
82
83
// Copyright (C) 2022 Intel Corporation.

#include <QtTest/QTest>
#include <QtCore/QNativeIpcKey>

namespace IpcTestCommon {
static QList<QNativeIpcKey::Type> supportedKeyTypes;

template <typename IpcClass> void addGlobalTestRows()
{
    qDebug() << "Default key type is" << QNativeIpcKey::DefaultTypeForOs
             << "and legacy key type is" << QNativeIpcKey::legacyDefaultTypeForOs();

#if defined(Q_OS_FREEBSD) || defined(Q_OS_DARWIN) || defined(Q_OS_WIN) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID))
    // only enforce that IPC works on the platforms above; other platforms may
    // have no working backends (notably, Android)
    QVERIFY(IpcClass::isKeyTypeSupported(QNativeIpcKey::DefaultTypeForOs));
    QVERIFY(IpcClass::isKeyTypeSupported(QNativeIpcKey::legacyDefaultTypeForOs()));
#endif

    auto addRowIfSupported = [](const char *name, QNativeIpcKey::Type type) {
        if (IpcClass::isKeyTypeSupported(type)) {
            supportedKeyTypes << type;
            QTest::newRow(name) << type;
        }
    };

    QTest::addColumn<QNativeIpcKey::Type>("keyType");

    addRowIfSupported("Windows", QNativeIpcKey::Type::Windows);
    addRowIfSupported("POSIX", QNativeIpcKey::Type::PosixRealtime);
    addRowIfSupported("SystemV-Q", QNativeIpcKey::Type::SystemV);
    addRowIfSupported("SystemV-T", QNativeIpcKey::Type('T'));

    if (supportedKeyTypes.isEmpty())
        QSKIP("System reports no supported IPC types.");
}

// rotate through the supported types and find another
inline QNativeIpcKey::Type nextKeyType(QNativeIpcKey::Type type)
{
    qsizetype idx = supportedKeyTypes.indexOf(type);
    Q_ASSERT(idx >= 0);

    ++idx;
    if (idx == supportedKeyTypes.size())
        idx = 0;
    return supportedKeyTypes.at(idx);
}
} // namespace IpcTestCommon

QT_BEGIN_NAMESPACE
namespace QTest {
template<> inline char *toString(const QNativeIpcKey::Type &type)
{
    switch (type) {
    case QNativeIpcKey::Type::SystemV:  return qstrdup("SystemV");
    case QNativeIpcKey::Type::PosixRealtime: return qstrdup("PosixRealTime");
    case QNativeIpcKey::Type::Windows:  return qstrdup("Windows");
    }
    if (type == QNativeIpcKey::Type{})
        return qstrdup("Invalid");

    char buf[32];
    qsnprintf(buf, sizeof(buf), "%u", unsigned(type));
    return qstrdup(buf);
}

template<> inline char *toString(const QNativeIpcKey &key)
{
    if (!key.isValid())
        return qstrdup("<invalid>");

    const char *type = toString(key.type());
    const char *text = toString(key.nativeKey());
    char buf[256];
    qsnprintf(buf, sizeof(buf), "QNativeIpcKey(%s, %s)", text, type);
    delete[] type;
    delete[] text;
    return qstrdup(buf);
}
} // namespace QTest
QT_END_NAMESPACE