File: basic.cpp

package info (click to toggle)
qtkeychain 0.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 488 kB
  • sloc: cpp: 2,799; xml: 276; makefile: 24
file content (73 lines) | stat: -rw-r--r-- 2,482 bytes parent folder | download | duplicates (2)
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
#include <QtTest>

#include "qtkeychain/keychain.h"

namespace
{
    QByteArray generateRandomString(qsizetype size)
    {
        std::vector<quint32> buffer(size, 0);
        QRandomGenerator::global()->fillRange(buffer.data(), size);
        return QByteArray(reinterpret_cast<char *>(buffer.data()), static_cast<int>(size * sizeof(quint32))).toBase64(QByteArray::Base64UrlEncoding).mid(0, size);
    }

}
class BasicTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void test_data()
    {
        QTest::addColumn<QByteArray>("password");
        QTest::newRow("normal password") << QByteArrayLiteral("this is a password");
        QTest::newRow("1000") << generateRandomString(1000);
        QTest::newRow("2000") << generateRandomString(2000);
        QTest::newRow("3000") << generateRandomString(3000);
        QTest::newRow("10000") << generateRandomString(10000);
        QTest::newRow("18944") << generateRandomString(18944);

    }

    void test()
    {
#ifdef Q_OS_MACOS
        QSKIP("This test case has no access to the keychain");
#endif
        const QString serviceKey = QStringLiteral("QtKeychainTest-%1").arg(QTest::currentDataTag());
        QFETCH(QByteArray, password);
        {
            QKeychain::WritePasswordJob writeJob(serviceKey);
            writeJob.setKey(serviceKey);
            writeJob.setBinaryData(password);
            QSignalSpy writeSpy(&writeJob, &QKeychain::WritePasswordJob::finished);
            writeJob.start();
            writeSpy.wait();
#ifdef Q_OS_WIN
            QEXPECT_FAIL("18944", "Maximum for Windows is exceeded", Abort);
#endif
            qDebug() << writeJob.errorString();
            QCOMPARE(writeJob.error(), QKeychain::NoError);
        }
        {
            QKeychain::ReadPasswordJob readJob(serviceKey);
            readJob.setKey(serviceKey);
            QSignalSpy readSpy(&readJob, &QKeychain::ReadPasswordJob::finished);
            readJob.start();
            readSpy.wait();
            QCOMPARE(readJob.error(), QKeychain::NoError);
            QCOMPARE(readJob.binaryData(), password);
        }
        {
            QKeychain::DeletePasswordJob deleteJob(serviceKey);
            deleteJob.setKey(serviceKey);
            QSignalSpy deleteSpy(&deleteJob, &QKeychain::DeletePasswordJob::finished);
            deleteJob.start();
            deleteSpy.wait();
            QCOMPARE(deleteJob.error(), QKeychain::NoError);
        }
    }
};

QTEST_MAIN(BasicTest)
#include "basic.moc"