diff options
Diffstat (limited to 'tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp')
-rw-r--r-- | tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp | 138 |
1 files changed, 132 insertions, 6 deletions
diff --git a/tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp b/tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp index 6c30f428e9..a01a108591 100644 --- a/tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp +++ b/tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp @@ -1,28 +1,51 @@ // Copyright (C) 2022 Intel Corporation. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/QNativeIpcKey> #include <QtTest/QTest> +#include <QtTest/private/qcomparisontesthelper_p.h> #include "../ipctestcommon.h" +#if QT_CONFIG(sharedmemory) +# include <qsharedmemory.h> +#endif +#if QT_CONFIG(systemsemaphore) +# include <qsystemsemaphore.h> +#endif + +#if QT_CONFIG(sharedmemory) +static const auto makeLegacyKey = QSharedMemory::legacyNativeKey; +#else +static const auto makeLegacyKey = QSystemSemaphore::legacyNativeKey; +#endif + using namespace Qt::StringLiterals; class tst_QNativeIpcKey : public QObject { Q_OBJECT private slots: + void compareCompiles(); void defaultTypes(); void construct(); void getSetCheck(); void equality(); + void hash(); void swap(); void toString_data(); void toString(); void fromString_data(); void fromString(); + void legacyKeys_data(); + void legacyKeys(); }; +void tst_QNativeIpcKey::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile<QNativeIpcKey>(); +} + void tst_QNativeIpcKey::defaultTypes() { auto isKnown = [](QNativeIpcKey::Type t) { @@ -164,22 +187,51 @@ void tst_QNativeIpcKey::equality() QNativeIpcKey key1, key2; QCOMPARE(key1, key2); QVERIFY(!(key1 != key2)); + QT_TEST_EQUALITY_OPS(key1, key2, true); key1.setNativeKey("key1"); QCOMPARE_NE(key1, key2); QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); key2.setType({}); QCOMPARE_NE(key1, key2); QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); key2.setNativeKey(key1.nativeKey()); QCOMPARE_NE(key1, key2); QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); key2.setType(QNativeIpcKey::DefaultTypeForOs); QCOMPARE(key1, key2); QVERIFY(!(key1 != key2)); + QT_TEST_EQUALITY_OPS(key1, key2, true); + + key1 = makeLegacyKey("key1", QNativeIpcKey::DefaultTypeForOs); + QCOMPARE_NE(key1, key2); + QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); + + key2 = key1; + QCOMPARE(key1, key2); + QVERIFY(!(key1 != key2)); + QT_TEST_EQUALITY_OPS(key1, key2, true); + + // just setting the native key won't make them equal again! + key2.setNativeKey(key1.nativeKey()); + QCOMPARE_NE(key1, key2); + QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); +} + +void tst_QNativeIpcKey::hash() +{ + QNativeIpcKey key1("key1", QNativeIpcKey::DefaultTypeForOs); + QNativeIpcKey key2(key1); + QCOMPARE_EQ(qHash(key1), qHash(key2)); + QCOMPARE_EQ(qHash(key1, 123), qHash(key2, 123)); } void tst_QNativeIpcKey::swap() @@ -206,6 +258,12 @@ void tst_QNativeIpcKey::swap() QCOMPARE(key1.type(), QNativeIpcKey::Type::PosixRealtime); QCOMPARE(key2.nativeKey(), "key2"); QCOMPARE(key2.type(), QNativeIpcKey::Type::Windows); + + key1 = makeLegacyKey("key1", QNativeIpcKey::DefaultTypeForOs); + QCOMPARE(key1.type(), QNativeIpcKey::DefaultTypeForOs); + key1.swap(key2); + QCOMPARE(key1.type(), QNativeIpcKey::Type::Windows); + QCOMPARE(key2.type(), QNativeIpcKey::DefaultTypeForOs); } void tst_QNativeIpcKey::toString_data() @@ -228,13 +286,13 @@ void tst_QNativeIpcKey::toString_data() add("objectlike", "Global\\sometext"_L1); add("colon-slash", ":/"_L1); add("slash-colon", "/:"_L1); - add("non-ascii", "\xa0\xff"_L1); add("percent", "%"_L1, "%25"_L1); add("question-hash", "?#"_L1, "%3F%23"_L1); add("hash-question", "#?"_L1, "%23%3F"_L1); add("double-slash", "//"_L1, "/%2F"_L1); add("triple-slash", "///"_L1, "/%2F/"_L1); - add("non-ascii", "/\xe9"_L1); + add("non-ascii", "\xe9"_L1); + add("non-utf8", "\xa0\xff"_L1); QTest::addRow("%s-%s", prefix, "non-latin1") << prefix + u":\u0100.\u2000.\U00010000"_s << QNativeIpcKey(u"\u0100.\u2000.\U00010000"_s, type); @@ -271,12 +329,14 @@ void tst_QNativeIpcKey::fromString_data() << "posix:%C4%80.%E2%80%80.%F0%90%80%80" << QNativeIpcKey(u"\u0100.\u2000.\U00010000"_s, QNativeIpcKey::Type::PosixRealtime); - // query and fragment are ignored - QTest::addRow("with-query") << "posix:/foo?bar" << valid; + // fragments are ignored QTest::addRow("with-fragment") << "posix:/foo#bar" << valid; - QTest::addRow("with-queryfragment") << "posix:/foo?bar#baz" << valid; QTest::addRow("with-fragmentquery") << "posix:/foo#bar?baz" << valid; + // but unknown query items are not + QTest::addRow("with-query") << "posix:/foo?bar" << invalid; + QTest::addRow("with-queryfragment") << "posix:/foo?bar#baz" << invalid; + // add some ones that won't parse well QTest::addRow("positive-number") << "81" << invalid; QTest::addRow("negative-number") << "-81" << invalid; @@ -312,5 +372,71 @@ void tst_QNativeIpcKey::fromString() QCOMPARE(QNativeIpcKey::fromString(string), key); } +void tst_QNativeIpcKey::legacyKeys_data() +{ + QTest::addColumn<QNativeIpcKey::Type>("type"); + QTest::addColumn<QString>("legacyKey"); + auto addRows = [](QNativeIpcKey::Type type) { + const char *label = "<unknown-type>"; + switch (type) { + case QNativeIpcKey::Type::SystemV: + label = "systemv"; + break; + case QNativeIpcKey::Type::PosixRealtime: + label = "posix"; + break; + case QNativeIpcKey::Type::Windows: + label = "windows"; + break; + } + auto add = [=](const char *name, const QString &legacyKey) { + QTest::addRow("%s-%s", label, name) << type << legacyKey; + }; + add("empty", {}); + add("text", "foobar"_L1); + add("pathlike", "/sometext"_L1); + add("objectlike", "Global\\sometext"_L1); + add("colon-slash", ":/"_L1); + add("slash-colon", "/:"_L1); + add("percent", "%"_L1); + add("question-hash", "?#"_L1); + add("hash-question", "#?"_L1); + add("double-slash", "//"_L1); + add("triple-slash", "///"_L1); + add("non-ascii", "\xe9"_L1); + add("non-utf8", "\xa0\xff"_L1); + add("non-latin1", u":\u0100.\u2000.\U00010000"_s); + }; + + addRows(QNativeIpcKey::DefaultTypeForOs); + if (auto type = QNativeIpcKey::legacyDefaultTypeForOs(); + type != QNativeIpcKey::DefaultTypeForOs) + addRows(type); +} + +void tst_QNativeIpcKey::legacyKeys() +{ + QFETCH(QNativeIpcKey::Type, type); + QFETCH(QString, legacyKey); + + QNativeIpcKey key = makeLegacyKey(legacyKey, type); + QCOMPARE(key.type(), type); + + QString string = key.toString(); + QNativeIpcKey key2 = QNativeIpcKey::fromString(string); + QCOMPARE(key2, key); + QT_TEST_EQUALITY_OPS(key, key2, true); + + if (!legacyKey.isEmpty()) { + // confirm it shows up in the encoded form + Q_ASSERT(!legacyKey.contains(u'&')); // needs extra encoding + QUrl u; + u.setQuery("legacyKey="_L1 + legacyKey, QUrl::DecodedMode); + QString encodedLegacyKey = u.toString(QUrl::RemoveScheme | QUrl::RemoveAuthority + | QUrl::DecodeReserved); + QVERIFY2(string.contains(encodedLegacyKey), qPrintable(string)); + } +} + QTEST_MAIN(tst_QNativeIpcKey) #include "tst_qnativeipckey.moc" |