TCPLinkTest.cc 6.52 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
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
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

/// @file
///     @brief TCPLink class unit test
///
///     @author Don Gagne <don@thegagnes.com>

29 30 31 32
#include "TCPLinkTest.h"
#include "TCPLoopBackServer.h"

TCPLinkTest::TCPLinkTest(void)
33 34 35
    : _config(NULL)
    , _link(NULL)
    , _multiSpy(NULL)
Don Gagne's avatar
Don Gagne committed
36
{
37

Don Gagne's avatar
Don Gagne committed
38 39 40
}

// Called before every test
41
void TCPLinkTest::init(void)
Don Gagne's avatar
Don Gagne committed
42
{
Don Gagne's avatar
Don Gagne committed
43 44
    UnitTest::init();
    
45 46 47
    Q_ASSERT(_link == nullptr);
    Q_ASSERT(_multiSpy == nullptr);
    Q_ASSERT(_config == nullptr);
Don Gagne's avatar
Don Gagne committed
48

49 50 51
    _config = new TCPConfiguration("MockTCP");
    _config->setAddress(QHostAddress::LocalHost);
    _config->setPort(5760);
52
    _link = new TCPLink(_config);
Don Gagne's avatar
Don Gagne committed
53 54 55 56 57 58 59
    Q_ASSERT(_link != NULL);

    _rgSignals[bytesReceivedSignalIndex] = SIGNAL(bytesReceived(LinkInterface*, QByteArray));
    _rgSignals[connectedSignalIndex] = SIGNAL(connected(void));
    _rgSignals[disconnectedSignalIndex] = SIGNAL(disconnected(void));
    _rgSignals[communicationErrorSignalIndex] = SIGNAL(communicationError(const QString&, const QString&));
    _rgSignals[communicationUpdateSignalIndex] = SIGNAL(communicationUpdate(const QString&, const QString&));
60
    //_rgSignals[deleteLinkSignalIndex] = SIGNAL(_deleteLink(LinkInterface*));
Don Gagne's avatar
Don Gagne committed
61 62 63 64 65 66

    _multiSpy = new MultiSignalSpy();
    QCOMPARE(_multiSpy->init(_link, _rgSignals, _cSignals), true);
}

// Called after every test
67
void TCPLinkTest::cleanup(void)
Don Gagne's avatar
Don Gagne committed
68 69 70
{
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_link);
71
    Q_ASSERT(_config);
Don Gagne's avatar
Don Gagne committed
72 73

    delete _multiSpy;
74 75
    _multiSpy = nullptr;

Don Gagne's avatar
Don Gagne committed
76
    delete _link;
77 78
    _link = nullptr;

79
    delete _config;
80
    _config = nullptr;
Don Gagne's avatar
Don Gagne committed
81

Don Gagne's avatar
Don Gagne committed
82
    UnitTest::cleanup();
Don Gagne's avatar
Don Gagne committed
83 84
}

85
void TCPLinkTest::_connectFail_test(void)
Don Gagne's avatar
Don Gagne committed
86
{
87
    Q_ASSERT(_config);
Don Gagne's avatar
Don Gagne committed
88 89 90 91
    Q_ASSERT(_link);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
92 93
    // With the new threading model connect will always succeed. We only get an error signal
    // for a failed connected.
94
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
95 96 97 98 99

    // Make sure we get a linkError signal with the right link name
    QCOMPARE(_multiSpy->waitForSignalByIndex(communicationErrorSignalIndex, 1000), true);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(communicationErrorSignalMask), true);
    QList<QVariant> arguments = _multiSpy->getSpyByIndex(communicationErrorSignalIndex)->takeFirst();
100
    QCOMPARE(arguments.at(1).toString().contains(_link->getName()), true);
Don Gagne's avatar
Don Gagne committed
101
    _multiSpy->clearSignalByIndex(communicationErrorSignalIndex);
102
    
103
    _link->_disconnect();
Don Gagne's avatar
Don Gagne committed
104 105 106

    // Try to connect again to make sure everything was cleaned up correctly from previous failed connection
    
107
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
108 109 110 111 112
    
    // Make sure we get a linkError signal with the right link name
    QCOMPARE(_multiSpy->waitForSignalByIndex(communicationErrorSignalIndex, 1000), true);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(communicationErrorSignalMask), true);
    arguments = _multiSpy->getSpyByIndex(communicationErrorSignalIndex)->takeFirst();
113
    QCOMPARE(arguments.at(1).toString().contains(_link->getName()), true);
Don Gagne's avatar
Don Gagne committed
114 115 116
    _multiSpy->clearSignalByIndex(communicationErrorSignalIndex);
}

117
void TCPLinkTest::_connectSucceed_test(void)
Don Gagne's avatar
Don Gagne committed
118
{
119 120
    QSKIP("FIXME: Failing on OSX");

Don Gagne's avatar
Don Gagne committed
121 122 123 124 125
    Q_ASSERT(_link);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);

    // Start the server side
126
    TCPLoopBackServer* server = new TCPLoopBackServer(_config->address(), _config->port());
127
    Q_CHECK_PTR(server);
Don Gagne's avatar
Don Gagne committed
128 129
    
    // Connect to the server
130
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
131
    
132
    // Make sure we get the connected signals
Don Gagne's avatar
Don Gagne committed
133
    QCOMPARE(_multiSpy->waitForSignalByIndex(connectedSignalIndex, 10000), true);
134
    QCOMPARE(_multiSpy->checkOnlySignalByMask(connectedSignalMask), true);
135
    _multiSpy->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
136 137 138
    
    // Test link->server data path
    
139 140
    QByteArray bytesOut("test");

Don Gagne's avatar
Don Gagne committed
141
    // Write data from link to server
142 143 144
    const char* bytesWrittenSignal = SIGNAL(bytesWritten(qint64));
    MultiSignalSpy bytesWrittenSpy;
    QCOMPARE(bytesWrittenSpy.init(_link->getSocket(), &bytesWrittenSignal, 1), true);
145
    _link->writeBytesSafe(bytesOut.data(), bytesOut.size());
146 147 148 149 150 151
    _multiSpy->clearAllSignals();
    
    // We emit this signal such that it will be queued and run on the TCPLink thread. This in turn
    // allows the TCPLink object to pump the bytes through.
    connect(this, SIGNAL(waitForBytesWritten(int)), _link, SLOT(waitForBytesWritten(int)));
    emit waitForBytesWritten(1000);
Don Gagne's avatar
Don Gagne committed
152

153
    // Check for loopback, both from signal received and actual bytes returned
Don Gagne's avatar
Don Gagne committed
154

155 156 157
    QCOMPARE(_multiSpy->waitForSignalByIndex(bytesReceivedSignalIndex, 1000), true);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(bytesReceivedSignalMask), true);
    
Don Gagne's avatar
Don Gagne committed
158
    // Read the data and make sure it matches
159
    QList<QVariant> arguments = _multiSpy->getSpyByIndex(bytesReceivedSignalIndex)->takeFirst();
160
    QVERIFY(arguments.at(1).toByteArray() == bytesOut);
Don Gagne's avatar
Don Gagne committed
161
    
162 163
    _multiSpy->clearAllSignals();

Don Gagne's avatar
Don Gagne committed
164
    // Disconnect the link
165
    _link->_disconnect();
Don Gagne's avatar
Don Gagne committed
166
    
167
    // Make sure we get the disconnected signal on link side
Don Gagne's avatar
Don Gagne committed
168
    QCOMPARE(_multiSpy->waitForSignalByIndex(disconnectedSignalIndex, 1000), true);
169
    QCOMPARE(_multiSpy->checkOnlySignalByMask(disconnectedSignalMask), true);
170
    _multiSpy->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
171 172 173 174
    
    // Try to connect again to make sure everything was cleaned up correctly from previous connection
    
    // Connect to the server
175
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
176
    
177
    // Make sure we get the connected signal
Don Gagne's avatar
Don Gagne committed
178
    QCOMPARE(_multiSpy->waitForSignalByIndex(connectedSignalIndex, 1000), true);
179
    QCOMPARE(_multiSpy->checkOnlySignalByMask(connectedSignalMask), true);
180 181 182 183
    _multiSpy->clearAllSignals();
    
    server->quit();
    QTest::qWait(500);  // Wait a little for server thread to terminate
Don Gagne's avatar
Don Gagne committed
184
    delete server;
Don Gagne's avatar
Don Gagne committed
185
}