Adapt copyright header
[contrib/qtwebsockets.git] / tests / manual / compliance / tst_compliance.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42 #include <QtTest/qtestcase.h>
43 #include <QSignalSpy>
44 #include <QHostInfo>
45 #include <QSslError>
46 #include <QDebug>
47 #include <QtWebSockets/QWebSocket>
48
49 class tst_ComplianceTest : public QObject
50 {
51     Q_OBJECT
52
53 public:
54     tst_ComplianceTest();
55
56 private Q_SLOTS:
57     void initTestCase();
58     void cleanupTestCase();
59     void init();
60     void cleanup();
61     /**
62      * @brief Runs the autobahn tests against our implementation
63      */
64     void autobahnTest();
65
66 private:
67     QUrl m_url;
68
69     void runTestCases(int startNbr, int stopNbr = -1);
70     void runTestCase(int nbr, int total);
71 };
72
73 tst_ComplianceTest::tst_ComplianceTest() :
74     m_url("ws://localhost:9001")
75 {
76 }
77
78 void tst_ComplianceTest::initTestCase()
79 {
80 }
81
82 void tst_ComplianceTest::cleanupTestCase()
83 {
84 }
85
86 void tst_ComplianceTest::init()
87 {
88 }
89
90 void tst_ComplianceTest::cleanup()
91 {
92 }
93
94 void tst_ComplianceTest::runTestCase(int nbr, int total)
95 {
96     if (nbr == total)
97     {
98         return;
99     }
100
101     QWebSocket *pWebSocket = new QWebSocket;
102     QSignalSpy spy(pWebSocket, SIGNAL(disconnected()));
103
104     //next for every case, connect to url
105     //ws://ipaddress:port/runCase?case=<number>&agent=<agentname>
106     //where agent name will be QWebSocket
107     QObject::connect(pWebSocket, &QWebSocket::textMessageReceived, [=](QString message) {
108         pWebSocket->sendTextMessage(message);
109     });
110     QObject::connect(pWebSocket, &QWebSocket::binaryMessageReceived, [=](QByteArray message) {
111         pWebSocket->sendBinaryMessage(message);
112     });
113
114     qDebug() << "Executing test" << (nbr + 1) << "/" << total;
115     QUrl url = m_url;
116     url.setPath(QStringLiteral("/runCase?"));
117     QUrlQuery query;
118     query.addQueryItem(QStringLiteral("case"), QString::number(nbr + 1));
119     query.addQueryItem(QStringLiteral("agent"), QStringLiteral("QtWebSockets/1.0"));
120     url.setQuery(query);
121     pWebSocket->open(url);
122     spy.wait(60000);
123     pWebSocket->close();
124     delete pWebSocket;
125     pWebSocket = Q_NULLPTR;
126     runTestCase(nbr + 1, total);
127 }
128
129 void tst_ComplianceTest::runTestCases(int startNbr, int stopNbr)
130 {
131     runTestCase(startNbr, stopNbr);
132 }
133
134 void tst_ComplianceTest::autobahnTest()
135 {
136     //connect to autobahn server at url ws://ipaddress:port/getCaseCount
137     QWebSocket *pWebSocket = new QWebSocket;
138     QUrl url = m_url;
139     int numberOfTestCases = 0;
140     QSignalSpy spy(pWebSocket, SIGNAL(disconnected()));
141     QObject::connect(pWebSocket, &QWebSocket::textMessageReceived, [&](QString message) {
142         numberOfTestCases = message.toInt();
143     });
144
145     url.setPath(QStringLiteral("/getCaseCount"));
146     pWebSocket->open(url);
147     spy.wait(60000);
148     QVERIFY(numberOfTestCases > 0);
149
150     QObject::disconnect(pWebSocket, &QWebSocket::textMessageReceived, 0, 0);
151     runTestCases(0, numberOfTestCases);
152
153     url.setPath(QStringLiteral("/updateReports?"));
154     QUrlQuery query;
155     query.addQueryItem(QStringLiteral("agent"), QStringLiteral("QtWebSockets"));
156     url.setQuery(query);
157     pWebSocket->open(url);
158     spy.wait(60000);
159     delete pWebSocket;
160 }
161
162 QTEST_MAIN(tst_ComplianceTest)
163
164 #include "tst_compliance.moc"
165