File: test_wsdiscoveryclient.cpp

package info (click to toggle)
kdsoap-ws-discovery-client 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 356 kB
  • sloc: cpp: 794; sh: 8; makefile: 5
file content (304 lines) | stat: -rw-r--r-- 13,248 bytes parent folder | download
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

// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2019-2020 Casper Meijn <casper@meijn.net>
// SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org>

#include <KDSoapClient/KDQName>
#include <QNetworkDatagram>
#include <QNetworkInterface>
#include <QRegularExpression>
#include <QSignalSpy>
#include <QTest>
#include <QUdpSocket>
#include <QXmlStreamReader>

#include "wsdiscoveryclient.h"
#include "wsdiscoverytargetservice.h"

Q_DECLARE_METATYPE(WSDiscoveryTargetService)

static constexpr auto clientPort = 15744;

class testWSDiscoveryClient : public QObject
{
    Q_OBJECT
public:
    explicit testWSDiscoveryClient(QObject *parent = nullptr)
        : QObject(parent)
    {
        ;
    }

private Q_SLOTS:
    static void testSendProbe();
    static void testSendResolve();
    static void testReceiveProbeMatch();
    static void testReceiveResolveMatch();

private:
    static QByteArray zeroOutUuid(const QByteArray &original);
    static QByteArray expectedSendProbeData();
    static QByteArray expectedSendResolveData();
    static QByteArray toBeSendProbeMatchData();
    static QByteArray toBeSendResolveMatchData();
    static QByteArray formatXml(const QByteArray &original);
};

void testWSDiscoveryClient::testSendProbe()
{
    QUdpSocket testSocket;
    QVERIFY(testSocket.bind(QHostAddress::Any, 3702, QAbstractSocket::ShareAddress));
    const auto ifaces = QNetworkInterface::allInterfaces();
    for (const auto &iface : ifaces) {
        QVERIFY(testSocket.joinMulticastGroup(QHostAddress(QStringLiteral("FF02::C")), iface));
    }

    KDQName type(QStringLiteral("tdn:NetworkVideoTransmitter"));
    type.setNameSpace(QStringLiteral("http://www.onvif.org/ver10/network/wsdl"));
    auto typeList = QList<KDQName>() << type;

    auto scopeList = QList<QUrl>() << QUrl(QStringLiteral("onvif://www.onvif.org/Profile/Streaming"));

    WSDiscoveryClient discoveryClient;
    discoveryClient.start();
    discoveryClient.sendProbe(typeList, scopeList);

    QVERIFY(testSocket.hasPendingDatagrams());
    auto datagram = testSocket.receiveDatagram();
    auto zeroedDatagram = zeroOutUuid(datagram.data());
    QCOMPARE(formatXml(zeroedDatagram), formatXml(expectedSendProbeData()));
}

QByteArray testWSDiscoveryClient::expectedSendProbeData()
{
    return QByteArray(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        "<soap:Envelope"
        "  xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\""
        "  xmlns:soap-enc=\"http://www.w3.org/2003/05/soap-encoding\""
        "  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
        "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
        "  xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\""
        "  xmlns:n1=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\">"
        "  <soap:Header>"
        "    <wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>"
        "    <wsa:ReplyTo><wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address></wsa:ReplyTo>"
        "    <wsa:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</wsa:Action>"
        "    <wsa:MessageID>urn:uuid:00000000-0000-0000-0000-000000000000</wsa:MessageID>"
        "  </soap:Header>"
        "  <soap:Body>"
        "    <n1:Probe>"
        "      <n1:Types xmlns:tdn=\"http://www.onvif.org/ver10/network/wsdl\">tdn:NetworkVideoTransmitter</n1:Types>"
        "      <n1:Scopes>onvif://www.onvif.org/Profile/Streaming</n1:Scopes>"
        "    </n1:Probe>"
        "  </soap:Body>"
        "</soap:Envelope>");
}

void testWSDiscoveryClient::testSendResolve()
{
    QUdpSocket testSocket;
    QVERIFY(testSocket.bind(QHostAddress::Any, 3702, QAbstractSocket::ShareAddress));
    const auto ifaces = QNetworkInterface::allInterfaces();
    for (const auto &iface : ifaces) {
        QVERIFY(testSocket.joinMulticastGroup(QHostAddress(QStringLiteral("FF02::C")), iface));
    }

    WSDiscoveryClient discoveryClient;
    discoveryClient.start();
    discoveryClient.sendResolve(QStringLiteral("A_Unique_Reference"));

    QVERIFY(testSocket.hasPendingDatagrams());
    auto datagram = testSocket.receiveDatagram();
    auto zeroedDatagram = zeroOutUuid(datagram.data());
    QCOMPARE(formatXml(zeroedDatagram), formatXml(expectedSendResolveData()));
}

QByteArray testWSDiscoveryClient::expectedSendResolveData()
{
    return QByteArray(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        "<soap:Envelope"
        "  xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\""
        "  xmlns:soap-enc=\"http://www.w3.org/2003/05/soap-encoding\""
        "  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
        "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
        "  xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\""
        "  xmlns:n1=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\">"
        "  <soap:Header>"
        "    <wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>"
        "    <wsa:ReplyTo><wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address></wsa:ReplyTo>"
        "    <wsa:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/Resolve</wsa:Action>"
        "    <wsa:MessageID>urn:uuid:00000000-0000-0000-0000-000000000000</wsa:MessageID>"
        "  </soap:Header>"
        "  <soap:Body>"
        "    <n1:Resolve>"
        "      <wsa:EndpointReference>"
        "        <wsa:Address>A_Unique_Reference</wsa:Address>"
        "      </wsa:EndpointReference>"
        "    </n1:Resolve>"
        "  </soap:Body>"
        "</soap:Envelope>");
}

void testWSDiscoveryClient::testReceiveProbeMatch()
{
    WSDiscoveryClient discoveryClient;
    discoveryClient.start(clientPort);

    qRegisterMetaType<WSDiscoveryTargetService>();
    QSignalSpy spy(&discoveryClient, &WSDiscoveryClient::probeMatchReceived);
    QVERIFY(spy.isValid());

    QUdpSocket testSocket;
    QVERIFY(testSocket.bind(QHostAddress::Any, 3702, QAbstractSocket::ShareAddress));
    const auto ifaces = QNetworkInterface::allInterfaces();
    for (const auto &iface : ifaces) {
        QVERIFY(testSocket.joinMulticastGroup(QHostAddress(QStringLiteral("FF02::C")), iface));
    }
    testSocket.writeDatagram(toBeSendProbeMatchData(), QHostAddress::LocalHost, clientPort);

    QVERIFY(spy.wait(1000));

    QCOMPARE(spy.count(), 1); // make sure the signal was emitted exactly one time
    QList<QVariant> arguments = spy.takeFirst(); // take the first signal

    const WSDiscoveryTargetService &probeMatchService = qvariant_cast<WSDiscoveryTargetService>(arguments.at(0));

    QCOMPARE(probeMatchService.endpointReference(), "Incomming_unique_reference");
    QCOMPARE(probeMatchService.scopeList().size(), 1);
    QCOMPARE(probeMatchService.scopeList().at(0), QUrl(QStringLiteral("ldap:///ou=engineering,o=examplecom,c=us")));
    QCOMPARE(probeMatchService.typeList().size(), 1);
    QCOMPARE(probeMatchService.typeList().at(0), KDQName(QStringLiteral("http://printer.example.org/2003/imaging"), QStringLiteral("PrintBasic")));
    QCOMPARE(probeMatchService.xAddrList().size(), 1);
    QCOMPARE(probeMatchService.xAddrList().at(0), QUrl(QStringLiteral("http://prn-example/PRN42/b42-1668-a")));
    QVERIFY(probeMatchService.lastSeen().msecsTo(QDateTime::currentDateTime()) < 500);
}

QByteArray testWSDiscoveryClient::toBeSendProbeMatchData()
{
    return QByteArray(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        "<soap:Envelope"
        "  xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\""
        "  xmlns:soap-enc=\"http://www.w3.org/2003/05/soap-encoding\""
        "  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
        "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
        "  xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\""
        "  xmlns:n1=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\">"
        "  <soap:Header>"
        "    <wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>"
        "    <wsa:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/ProbeMatches</wsa:Action>"
        "    <wsa:MessageID>urn:uuid:00000000-0000-0000-0000-000000000000</wsa:MessageID>"
        "    <wsa:RelatesTo>xs:anyURI</wsa:RelatesTo>"
        "    <n1:AppSequence InstanceId=\"12\" MessageNumber=\"12\"/>"
        "  </soap:Header>"
        "  <soap:Body>"
        "    <n1:ProbeMatches>"
        "      <n1:ProbeMatch>"
        "        <wsa:EndpointReference><wsa:Address>Incomming_unique_reference</wsa:Address></wsa:EndpointReference>"
        "        <n1:Types xmlns:i=\"http://printer.example.org/2003/imaging\">i:PrintBasic</n1:Types>"
        "        <n1:Scopes>ldap:///ou=engineering,o=examplecom,c=us</n1:Scopes>"
        "        <n1:XAddrs>http://prn-example/PRN42/b42-1668-a</n1:XAddrs>"
        "        <n1:MetadataVersion>12</n1:MetadataVersion>"
        "      </n1:ProbeMatch>"
        "    </n1:ProbeMatches>"
        "  </soap:Body>"
        "</soap:Envelope>");
}

void testWSDiscoveryClient::testReceiveResolveMatch()
{
    WSDiscoveryClient discoveryClient;
    discoveryClient.start(clientPort);

    qRegisterMetaType<WSDiscoveryTargetService>();
    QSignalSpy spy(&discoveryClient, &WSDiscoveryClient::resolveMatchReceived);
    QVERIFY(spy.isValid());

    QUdpSocket testSocket;
    QVERIFY(testSocket.bind(QHostAddress::Any, 3702, QAbstractSocket::ShareAddress));
    const auto ifaces = QNetworkInterface::allInterfaces();
    for (const auto &iface : ifaces) {
        QVERIFY(testSocket.joinMulticastGroup(QHostAddress(QStringLiteral("FF02::C")), iface));
    }
    testSocket.writeDatagram(toBeSendResolveMatchData(), QHostAddress::LocalHost, clientPort);

    QVERIFY(spy.wait(1000));

    QCOMPARE(spy.count(), 1); // make sure the signal was emitted exactly one time
    QList<QVariant> arguments = spy.takeFirst(); // take the first signal

    const WSDiscoveryTargetService &probeMatchService = qvariant_cast<WSDiscoveryTargetService>(arguments.at(0));

    QCOMPARE(probeMatchService.endpointReference(), "Incomming_resolve_reference");
    QCOMPARE(probeMatchService.scopeList().size(), 1);
    QCOMPARE(probeMatchService.scopeList().at(0), QUrl(QStringLiteral("ldap:///ou=floor1,ou=b42,ou=anytown,o=examplecom,c=us")));
    QCOMPARE(probeMatchService.typeList().size(), 1);
    QCOMPARE(probeMatchService.typeList().at(0), KDQName(QStringLiteral("http://printer.example.org/2003/imaging"), QStringLiteral("PrintAdvanced")));
    QCOMPARE(probeMatchService.xAddrList().size(), 1);
    QCOMPARE(probeMatchService.xAddrList().at(0), QUrl(QStringLiteral("http://printer.local:8080")));
    QVERIFY(probeMatchService.lastSeen().msecsTo(QDateTime::currentDateTime()) < 500);
}

QByteArray testWSDiscoveryClient::toBeSendResolveMatchData()
{
    return QByteArray(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        "<soap:Envelope"
        "  xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\""
        "  xmlns:soap-enc=\"http://www.w3.org/2003/05/soap-encoding\""
        "  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
        "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
        "  xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\""
        "  xmlns:n1=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\">"
        "  <soap:Header>"
        "    <wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>"
        "    <wsa:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/ResolveMatches</wsa:Action>"
        "    <wsa:MessageID>urn:uuid:00000000-0000-0000-0000-000000000000</wsa:MessageID>"
        "    <wsa:RelatesTo>xs:anyURI</wsa:RelatesTo>"
        "    <n1:AppSequence InstanceId=\"12\" MessageNumber=\"13\"/>"
        "  </soap:Header>"
        "  <soap:Body>"
        "    <n1:ResolveMatches>"
        "      <n1:ResolveMatch>"
        "        <wsa:EndpointReference><wsa:Address>Incomming_resolve_reference</wsa:Address></wsa:EndpointReference>"
        "        <n1:Types xmlns:i=\"http://printer.example.org/2003/imaging\">i:PrintAdvanced</n1:Types>"
        "        <n1:Scopes>ldap:///ou=floor1,ou=b42,ou=anytown,o=examplecom,c=us</n1:Scopes>"
        "        <n1:XAddrs>http://printer.local:8080</n1:XAddrs>"
        "        <n1:MetadataVersion>12</n1:MetadataVersion>"
        "      </n1:ResolveMatch>"
        "    </n1:ResolveMatches>"
        "  </soap:Body>"
        "</soap:Envelope>");
}

QByteArray testWSDiscoveryClient::formatXml(const QByteArray &original)
{
    QByteArray xmlOut;

    QXmlStreamReader reader(original);
    QXmlStreamWriter writer(&xmlOut);
    writer.setAutoFormatting(true);

    while (!reader.atEnd()) {
        reader.readNext();
        if (!reader.isWhitespace()) {
            writer.writeCurrentToken(reader);
        }
    }

    return xmlOut;
}

QByteArray testWSDiscoveryClient::zeroOutUuid(const QByteArray &original)
{
    QString originalString = original;
    static QRegularExpression regExp(QStringLiteral("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"));
    originalString.replace(regExp, QStringLiteral("00000000-0000-0000-0000-000000000000"));
    return originalString.toLatin1();
}

QTEST_MAIN(testWSDiscoveryClient)

#include "test_wsdiscoveryclient.moc"