summaryrefslogtreecommitdiffstats
path: root/tests/auto/abstractoauth/tst_abstractoauth.cpp
blob: 3d229d3a15b295116ec12a03f5a66babebdb1847 (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
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtCore>
#include <QtTest>
#include <QtNetwork>

#include <QtNetworkAuth/qabstractoauth.h>

#include <private/qabstractoauth_p.h>

class tst_AbstractOAuth : public QObject
{
    Q_OBJECT

private:
    struct AbstractOAuthPrivate : public QAbstractOAuthPrivate {
        AbstractOAuthPrivate() : QAbstractOAuthPrivate("", QUrl(), QString(), nullptr)
        {}
    };

    struct AbstractOAuth : QAbstractOAuth {
        AbstractOAuth() : QAbstractOAuth(*new AbstractOAuthPrivate(),
                                         nullptr)
        {}

        QNetworkReply *head(const QUrl &, const QVariantMap &) override { return nullptr; }
        QNetworkReply *get(const QUrl &, const QVariantMap &) override { return nullptr; }
        QNetworkReply *post(const QUrl &, const QVariantMap &) override { return nullptr; }
        QNetworkReply *put(const QUrl &, const QVariantMap &) override { return nullptr; }
        QNetworkReply *deleteResource(const QUrl &, const QVariantMap &) override
        {
            return nullptr;
        }
        void grant() override {}

        void prepareRequest(QNetworkRequest *, const QByteArray &,
                            const QByteArray & = QByteArray()) override
        {
        }
    };

private Q_SLOTS:
    void authorizationUrlSignal();
    void generateRandomString_data();
    void generateRandomString();
};

void tst_AbstractOAuth::authorizationUrlSignal()
{
    AbstractOAuth obj;
    QUrl expectedValue = QUrl("http://example.net/");
    const QUrl defaultValue = obj.authorizationUrl();
    QVERIFY(expectedValue != defaultValue);
    bool emitted = false;
    connect(&obj, &QAbstractOAuth::authorizationUrlChanged, this, [&](const QUrl &value) {
        QCOMPARE(expectedValue, value);
        emitted = true;
    });
    obj.setAuthorizationUrl(expectedValue);
    QVERIFY(emitted);
}

void tst_AbstractOAuth::generateRandomString_data()
{
    QTest::addColumn<int>("length");
    for (int i = 0; i <= 255; ++i)
        QTest::addRow("%d", i) << i;
}

// copied from https://xkcd.com/221/
int getRandomNumber()
{
    return 4;   // chosen by fair dice roll.
                // guaranteed to be random.
}

void tst_AbstractOAuth::generateRandomString()
{
    struct Unprotected : public QAbstractOAuth {
        using QAbstractOAuth::generateRandomString;
    };

    QFETCH(int, length);
    QByteArray random1 = Unprotected::generateRandomString(length);
    QCOMPARE(random1.size(), length);

    // Check that it is truly random by repeating and checking that it is
    // different. We don't try it for 1 and 2 characters because the chance of
    // random coincidence is too high: 1 in 2^(6*n), so 1 in 64 and 1 in 4096
    // respectively. For 3 characters, that decreases to 1 in 262,144.
    if (length <= 2)
        return;

    QByteArray random2 = Unprotected::generateRandomString(length);
    QCOMPARE_NE(random2, random1);

    // Generate a Base64 string using getRandomNumber() random bytes. Base64
    // encodes 6 bits per byte, so a 255-character string has 1530 bits of
    // data.
    char buf[192] = {};
    int rawlen = (length * 6 + 7) / 8;
    for (int i = 0; i < rawlen; ++i)
        buf[i] = getRandomNumber();
    QByteArray random3 = QByteArray(buf, rawlen).toBase64(QByteArray::Base64UrlEncoding);
    Q_ASSERT(random3.size() >= length);
    random3.truncate(length);
    QCOMPARE_NE(random3, random1);
}

QTEST_MAIN(tst_AbstractOAuth)
#include "tst_abstractoauth.moc"