aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qcoapreply/tst_qcoapreply.cpp
blob: d9a75620950ada5309f292c0af9b884f8bd1c36d (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
/****************************************************************************
**
** Copyright (C) 2017 Witekio.
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCoap module.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest>
#include <QCoreApplication>

#include <QtCoap/qcoapreply.h>
#include <private/qcoapreply_p.h>

class tst_QCoapReply : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void updateReply_data();
    void updateReply();
    void requestData();
    void abortRequest();
};

class QCoapReplyForTests : public QCoapReply
{
public:
    QCoapReplyForTests(const QCoapRequest &req) : QCoapReply (req) {}

    void setRunning(const QCoapToken &token, QCoapMessageId messageId)
    {
        Q_D(QCoapReply);
        d->_q_setRunning(token, messageId);
    }
};

void tst_QCoapReply::updateReply_data()
{
    QTest::addColumn<QByteArray>("payload");
    QTest::addColumn<QtCoap::ResponseCode>("responseCode");
    QTest::addColumn<QtCoap::Error>("error");

    QTest::newRow("success")
            << QByteArray("Some data")
            << QtCoap::ResponseCode::Content
            << QtCoap::Error::Ok;
    QTest::newRow("content error")
            << QByteArray("Error")
            << QtCoap::ResponseCode::BadRequest
            << QtCoap::Error::Ok;
    QTest::newRow("finished error")
            << QByteArray("Error")
            << QtCoap::ResponseCode::Content
            << QtCoap::Error::BadRequest;
    QTest::newRow("content & finished errors")
            << QByteArray("2Errors")
            << QtCoap::ResponseCode::BadGateway
            << QtCoap::Error::BadRequest;
}

void tst_QCoapReply::updateReply()
{
    QFETCH(QByteArray, payload);
    QFETCH(QtCoap::ResponseCode, responseCode);
    QFETCH(QtCoap::Error, error);

    const QByteArray token = "\xAF\x01\xC2";
    const quint16 id = 645;

    QCoapReply reply(QCoapRequest{});
    QCoapMessage message;
    message.setToken(token);
    message.setMessageId(id);
    message.setPayload(payload);

    QSignalSpy spyReplyFinished(&reply, &QCoapReply::finished);
    QSignalSpy spyReplyNotified(&reply, &QCoapReply::notified);
    QSignalSpy spyReplyError(&reply, &QCoapReply::error);
    QSignalSpy spyReplyAborted(&reply, &QCoapReply::aborted);

    QMetaObject::invokeMethod(&reply, "_q_setContent",
                              Q_ARG(QHostAddress, QHostAddress()),
                              Q_ARG(QCoapMessage, message),
                              Q_ARG(QtCoap::ResponseCode, responseCode));
    QMetaObject::invokeMethod(&reply, "_q_setFinished",
                              Q_ARG(QtCoap::Error, error));

    QCOMPARE(spyReplyFinished.count(), 1);
    QCOMPARE(spyReplyNotified.count(), 0);
    QCOMPARE(spyReplyAborted.count(), 0);
    if (error != QtCoap::Error::Ok || QtCoap::isError(responseCode)) {
        QVERIFY(spyReplyError.count() > 0);
        QCOMPARE(reply.isSuccessful(), false);
    } else {
        QCOMPARE(spyReplyError.count(), 0);
        QCOMPARE(reply.isSuccessful(), true);
    }

    QCOMPARE(reply.readAll(), payload);
    QCOMPARE(reply.readAll(), QByteArray());
    QCOMPARE(reply.responseCode(), responseCode);
    QCOMPARE(reply.message().token(), token);
    QCOMPARE(reply.message().messageId(), id);
}

void tst_QCoapReply::requestData()
{
#ifdef QT_BUILD_INTERNAL
    QCoapReplyForTests reply((QCoapRequest()));
    reply.setRunning("token", 543);

    QCOMPARE(reply.request().token(), QByteArray("token"));
    QCOMPARE(reply.request().messageId(), 543);
#else
    QSKIP("Not an internal build, skipping this test");
#endif
}

void tst_QCoapReply::abortRequest()
{
#ifdef QT_BUILD_INTERNAL
    QCoapReplyForTests reply((QCoapRequest()));
    reply.setRunning("token", 543);

    QSignalSpy spyAborted(&reply, &QCoapReply::aborted);
    QSignalSpy spyFinished(&reply, &QCoapReply::finished);
    reply.abortRequest();

    QTRY_COMPARE_WITH_TIMEOUT(spyAborted.count(), 1, 1000);
    QList<QVariant> arguments = spyAborted.takeFirst();
    QTRY_COMPARE_WITH_TIMEOUT(spyFinished.count(), 1, 1000);
    QVERIFY(arguments.at(0).toByteArray() == "token");
    QCOMPARE(reply.isSuccessful(), false);
#else
    QSKIP("Not an internal build, skipping this test");
#endif
}

QTEST_MAIN(tst_QCoapReply)

#include "tst_qcoapreply.moc"