summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp
blob: a01a108591e8c349ccdcb81297d412aad15ab45f (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright (C) 2022 Intel Corporation.
// 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) {
        switch (t) {
        case QNativeIpcKey::Type::SystemV:
        case QNativeIpcKey::Type::PosixRealtime:
        case QNativeIpcKey::Type::Windows:
            return true;
        }
        return false;
    };

    // because the letter Q looked nice in Håvard's Emacs font back in the 1990s
    static_assert(qToUnderlying(QNativeIpcKey::Type::SystemV) == 'Q',
            "QNativeIpcKey::Type::SystemV must be equal to the letter Q");

    auto type = QNativeIpcKey::DefaultTypeForOs;
    auto legacy = QNativeIpcKey::legacyDefaultTypeForOs();
    QVERIFY(isKnown(type));
    QVERIFY(isKnown(legacy));

#ifdef Q_OS_WIN
    QCOMPARE(type, QNativeIpcKey::Type::Windows);
#else
    QCOMPARE(type, QNativeIpcKey::Type::PosixRealtime);
#endif

#if defined(Q_OS_WIN)
    QCOMPARE(legacy, QNativeIpcKey::Type::Windows);
#elif defined(QT_POSIX_IPC)
    QCOMPARE(legacy, QNativeIpcKey::Type::PosixRealtime);
#elif !defined(Q_OS_DARWIN)
    QCOMPARE(legacy, QNativeIpcKey::Type::SystemV);
#endif
}

void tst_QNativeIpcKey::construct()
{
    {
        QNativeIpcKey invalid(QNativeIpcKey::Type{});
        QVERIFY(!invalid.isValid());
    }

    {
        QNativeIpcKey key;
        QVERIFY(key.nativeKey().isEmpty());
        QVERIFY(key.isEmpty());
        QVERIFY(key.isValid());
        QCOMPARE(key.type(), QNativeIpcKey::DefaultTypeForOs);

        QNativeIpcKey copy(key);
        QVERIFY(copy.nativeKey().isEmpty());
        QVERIFY(copy.isEmpty());
        QVERIFY(copy.isValid());
        QCOMPARE(copy.type(), QNativeIpcKey::DefaultTypeForOs);

        QNativeIpcKey moved(std::move(copy));
        QVERIFY(moved.nativeKey().isEmpty());
        QVERIFY(moved.isEmpty());
        QVERIFY(moved.isValid());
        QCOMPARE(moved.type(), QNativeIpcKey::DefaultTypeForOs);

        key.setType({});
        key.setNativeKey("something else");
        key = std::move(moved);
        QVERIFY(key.nativeKey().isEmpty());
        QVERIFY(key.isEmpty());
        QVERIFY(key.isValid());
        QCOMPARE(key.type(), QNativeIpcKey::DefaultTypeForOs);

        copy.setType({});
        copy.setNativeKey("something else");
        copy = key;
        QVERIFY(copy.nativeKey().isEmpty());
        QVERIFY(copy.isEmpty());
        QVERIFY(copy.isValid());
        QCOMPARE(copy.type(), QNativeIpcKey::DefaultTypeForOs);
    }

    {
        QNativeIpcKey key("dummy");
        QCOMPARE(key.nativeKey(), "dummy");
        QVERIFY(!key.isEmpty());
        QVERIFY(key.isValid());
        QCOMPARE(key.type(), QNativeIpcKey::DefaultTypeForOs);

        QNativeIpcKey copy(key);
        QCOMPARE(key.nativeKey(), "dummy");
        QCOMPARE(copy.nativeKey(), "dummy");
        QVERIFY(!copy.isEmpty());
        QVERIFY(copy.isValid());
        QCOMPARE(copy.type(), QNativeIpcKey::DefaultTypeForOs);

        QNativeIpcKey moved(std::move(copy));
        QCOMPARE(key.nativeKey(), "dummy");
        QCOMPARE(moved.nativeKey(), "dummy");
        QVERIFY(!moved.isEmpty());
        QVERIFY(moved.isValid());
        QCOMPARE(moved.type(), QNativeIpcKey::DefaultTypeForOs);

        key.setType({});
        key.setNativeKey("something else");
        key = std::move(moved);
        QCOMPARE(key.nativeKey(), "dummy");
        QVERIFY(!key.isEmpty());
        QVERIFY(key.isValid());
        QCOMPARE(key.type(), QNativeIpcKey::DefaultTypeForOs);

        copy.setType({});
        copy.setNativeKey("something else");
        copy = key;
        QCOMPARE(key.nativeKey(), "dummy");
        QCOMPARE(copy.nativeKey(), "dummy");
        QVERIFY(!copy.isEmpty());
        QVERIFY(copy.isValid());
        QCOMPARE(copy.type(), QNativeIpcKey::DefaultTypeForOs);
    }
}

void tst_QNativeIpcKey::getSetCheck()
{
    QNativeIpcKey key("key1", QNativeIpcKey::Type::Windows);
    QVERIFY(key.isValid());
    QVERIFY(!key.isEmpty());
    QCOMPARE(key.nativeKey(), "key1");
    QCOMPARE(key.type(), QNativeIpcKey::Type::Windows);

    key.setType(QNativeIpcKey::Type::SystemV);
    QVERIFY(key.isValid());
    QVERIFY(!key.isEmpty());
    QCOMPARE(key.type(), QNativeIpcKey::Type::SystemV);

    key.setNativeKey("key2");
    QCOMPARE(key.nativeKey(), "key2");
}

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()
{
    QNativeIpcKey key1("key1", QNativeIpcKey::Type::PosixRealtime);
    QNativeIpcKey key2("key2", QNativeIpcKey::Type::Windows);

    // self-swaps
    key1.swap(key1);
    key2.swap(key2);
    QCOMPARE(key1.nativeKey(), "key1");
    QCOMPARE(key1.type(), QNativeIpcKey::Type::PosixRealtime);
    QCOMPARE(key2.nativeKey(), "key2");
    QCOMPARE(key2.type(), QNativeIpcKey::Type::Windows);

    key1.swap(key2);
    QCOMPARE(key2.nativeKey(), "key1");
    QCOMPARE(key2.type(), QNativeIpcKey::Type::PosixRealtime);
    QCOMPARE(key1.nativeKey(), "key2");
    QCOMPARE(key1.type(), QNativeIpcKey::Type::Windows);

    key1.swap(key2);
    QCOMPARE(key1.nativeKey(), "key1");
    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()
{
    QTest::addColumn<QString>("string");
    QTest::addColumn<QNativeIpcKey>("key");

    QTest::newRow("invalid") << QString() << QNativeIpcKey(QNativeIpcKey::Type(0));

    auto addRow = [](const char *prefix, QNativeIpcKey::Type type) {
        auto add = [=](const char *name, QLatin1StringView key, QLatin1StringView encoded = {}) {
            if (encoded.isNull())
                encoded = key;
            QTest::addRow("%s-%s", prefix, name)
                    << prefix + u":"_s + encoded << QNativeIpcKey(key, type);
        };
        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, "%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-utf8", "\xa0\xff"_L1);
        QTest::addRow("%s-%s", prefix, "non-latin1")
                << prefix + u":\u0100.\u2000.\U00010000"_s
                << QNativeIpcKey(u"\u0100.\u2000.\U00010000"_s, type);
    };
    addRow("systemv", QNativeIpcKey::Type::SystemV);
    addRow("posix", QNativeIpcKey::Type::PosixRealtime);
    addRow("windows", QNativeIpcKey::Type::Windows);

    addRow("systemv-1", QNativeIpcKey::Type(1));
    addRow("systemv-84", QNativeIpcKey::Type('T'));
    addRow("systemv-255", QNativeIpcKey::Type(0xff));
}

void tst_QNativeIpcKey::toString()
{
    QFETCH(QString, string);
    QFETCH(QNativeIpcKey, key);

    QCOMPARE(key.toString(), string);
}

void tst_QNativeIpcKey::fromString_data()
{
    toString_data();
    QTest::addRow("systemv-alias") << "systemv-81:" << QNativeIpcKey(QNativeIpcKey::Type::SystemV);
    QTest::addRow("systemv-zeropadded") << "systemv-009:" << QNativeIpcKey(QNativeIpcKey::Type(9));

    QNativeIpcKey valid("/foo", QNativeIpcKey::Type::PosixRealtime);
    QNativeIpcKey invalid(QNativeIpcKey::Type(0));

    // percent-decoding
    QTest::addRow("percent-encoded") << "posix:%2f%66o%6f" << valid;
    QTest::addRow("percent-utf8")
            << "posix:%C4%80.%E2%80%80.%F0%90%80%80"
            << QNativeIpcKey(u"\u0100.\u2000.\U00010000"_s, QNativeIpcKey::Type::PosixRealtime);

    // fragments are ignored
    QTest::addRow("with-fragment") << "posix:/foo#bar" << 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;
    QTest::addRow("invalidprefix") << "invalidprefix:" << invalid;
    QTest::addRow("systemv-nodash") << "systemv255" << invalid;
    QTest::addRow("systemv-doubledash") << "systemv--255:" << invalid;
    QTest::addRow("systemv-plus") << "systemv+255" << invalid;
    QTest::addRow("systemv-hex") << "systemv-0x01:" << invalid;
    QTest::addRow("systemv-too-low") << "systemv-0:" << invalid;
    QTest::addRow("systemv-too-high") << "systemv-256" << invalid;
    QTest::addRow("systemv-overflow-15bit") << "systemv-32769:" << invalid;
    QTest::addRow("systemv-overflow-16bit") << "systemv-65537:" << invalid;
    QTest::addRow("systemv-overflow-31bit") << "systemv-2147483649:" << invalid;
    QTest::addRow("systemv-overflow-32bit") << "systemv-4294967297:" << invalid;

    auto addRows = [=](const char *name) {
        QTest::addRow("%s-nocolon", name) << name << invalid;
        QTest::addRow("%s-junk", name) << name + u"junk"_s << invalid;
        QTest::addRow("junk-%s-colon", name) << u"junk:"_s + name + u':' << invalid;
    };
    addRows("systemv");
    addRows("posix");
    addRows("windows");
    addRows("systemv-1");
    addRows("systemv-255");
}

void tst_QNativeIpcKey::fromString()
{
    QFETCH(QString, string);
    QFETCH(QNativeIpcKey, key);

    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"