Update license headers and add new license files
[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:LGPL21$
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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** In addition, as a special exception, Digia gives you certain additional
27 ** rights. These rights are described in the Digia Qt LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** $QT_END_LICENSE$
31 **
32 ****************************************************************************/
33 #include <QtTest/QtTest>
34 #include <QtTest/qtestcase.h>
35 #include <QSignalSpy>
36 #include <QHostInfo>
37 #include <QSslError>
38 #include <QDebug>
39 #include <QtWebSockets/QWebSocket>
40
41 class tst_ComplianceTest : public QObject
42 {
43     Q_OBJECT
44
45 public:
46     tst_ComplianceTest();
47
48 private Q_SLOTS:
49     void initTestCase();
50     void cleanupTestCase();
51     void init();
52     void cleanup();
53     /**
54      * @brief Runs the autobahn tests against our implementation
55      */
56     void autobahnTest();
57
58 private:
59     QUrl m_url;
60
61     void runTestCases(int startNbr, int stopNbr = -1);
62     void runTestCase(int nbr, int total);
63 };
64
65 tst_ComplianceTest::tst_ComplianceTest() :
66     m_url("ws://localhost:9001")
67 {
68 }
69
70 void tst_ComplianceTest::initTestCase()
71 {
72 }
73
74 void tst_ComplianceTest::cleanupTestCase()
75 {
76 }
77
78 void tst_ComplianceTest::init()
79 {
80 }
81
82 void tst_ComplianceTest::cleanup()
83 {
84 }
85
86 void tst_ComplianceTest::runTestCase(int nbr, int total)
87 {
88     if (nbr == total)
89     {
90         return;
91     }
92
93     QWebSocket *pWebSocket = new QWebSocket;
94     QSignalSpy spy(pWebSocket, SIGNAL(disconnected()));
95
96     //next for every case, connect to url
97     //ws://ipaddress:port/runCase?case=<number>&agent=<agentname>
98     //where agent name will be QWebSocket
99     QObject::connect(pWebSocket, &QWebSocket::textMessageReceived, [=](QString message) {
100         pWebSocket->sendTextMessage(message);
101     });
102     QObject::connect(pWebSocket, &QWebSocket::binaryMessageReceived, [=](QByteArray message) {
103         pWebSocket->sendBinaryMessage(message);
104     });
105
106     qDebug() << "Executing test" << (nbr + 1) << "/" << total;
107     QUrl url = m_url;
108     url.setPath(QStringLiteral("/runCase?"));
109     QUrlQuery query;
110     query.addQueryItem(QStringLiteral("case"), QString::number(nbr + 1));
111     query.addQueryItem(QStringLiteral("agent"), QStringLiteral("QtWebSockets/1.0"));
112     url.setQuery(query);
113     pWebSocket->open(url);
114     spy.wait(60000);
115     pWebSocket->close();
116     delete pWebSocket;
117     pWebSocket = Q_NULLPTR;
118     runTestCase(nbr + 1, total);
119 }
120
121 void tst_ComplianceTest::runTestCases(int startNbr, int stopNbr)
122 {
123     runTestCase(startNbr, stopNbr);
124 }
125
126 void tst_ComplianceTest::autobahnTest()
127 {
128     //connect to autobahn server at url ws://ipaddress:port/getCaseCount
129     QWebSocket *pWebSocket = new QWebSocket;
130     QUrl url = m_url;
131     int numberOfTestCases = 0;
132     QSignalSpy spy(pWebSocket, SIGNAL(disconnected()));
133     QObject::connect(pWebSocket, &QWebSocket::textMessageReceived, [&](QString message) {
134         numberOfTestCases = message.toInt();
135     });
136
137     url.setPath(QStringLiteral("/getCaseCount"));
138     pWebSocket->open(url);
139     spy.wait(60000);
140     QVERIFY(numberOfTestCases > 0);
141
142     QObject::disconnect(pWebSocket, &QWebSocket::textMessageReceived, 0, 0);
143     runTestCases(0, numberOfTestCases);
144
145     url.setPath(QStringLiteral("/updateReports?"));
146     QUrlQuery query;
147     query.addQueryItem(QStringLiteral("agent"), QStringLiteral("QtWebSockets"));
148     url.setQuery(query);
149     pWebSocket->open(url);
150     spy.wait(60000);
151     delete pWebSocket;
152 }
153
154 QTEST_MAIN(tst_ComplianceTest)
155
156 #include "tst_compliance.moc"
157