TCPLinkTest.cc 5.94 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14 15

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

16 17 18 19
#include "TCPLinkTest.h"
#include "TCPLoopBackServer.h"

TCPLinkTest::TCPLinkTest(void)
20 21 22
    : _config(NULL)
    , _link(NULL)
    , _multiSpy(NULL)
Don Gagne's avatar
Don Gagne committed
23
{
24

Don Gagne's avatar
Don Gagne committed
25 26 27
}

// Called before every test
28
void TCPLinkTest::init(void)
Don Gagne's avatar
Don Gagne committed
29
{
Don Gagne's avatar
Don Gagne committed
30 31
    UnitTest::init();
    
32 33 34
    Q_ASSERT(_link == nullptr);
    Q_ASSERT(_multiSpy == nullptr);
    Q_ASSERT(_config == nullptr);
Don Gagne's avatar
Don Gagne committed
35

36 37 38
    _config = new TCPConfiguration("MockTCP");
    _config->setAddress(QHostAddress::LocalHost);
    _config->setPort(5760);
39
    _link = new TCPLink(_config);
Don Gagne's avatar
Don Gagne committed
40 41 42 43 44 45 46
    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&));
47
    //_rgSignals[deleteLinkSignalIndex] = SIGNAL(_deleteLink(LinkInterface*));
Don Gagne's avatar
Don Gagne committed
48 49 50 51 52 53

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

// Called after every test
54
void TCPLinkTest::cleanup(void)
Don Gagne's avatar
Don Gagne committed
55 56 57
{
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_link);
58
    Q_ASSERT(_config);
Don Gagne's avatar
Don Gagne committed
59 60

    delete _multiSpy;
61 62
    _multiSpy = nullptr;

Don Gagne's avatar
Don Gagne committed
63
    delete _link;
64 65
    _link = nullptr;

66
    delete _config;
67
    _config = nullptr;
Don Gagne's avatar
Don Gagne committed
68

Don Gagne's avatar
Don Gagne committed
69
    UnitTest::cleanup();
Don Gagne's avatar
Don Gagne committed
70 71
}

72
void TCPLinkTest::_connectFail_test(void)
Don Gagne's avatar
Don Gagne committed
73
{
74
    Q_ASSERT(_config);
Don Gagne's avatar
Don Gagne committed
75 76 77 78
    Q_ASSERT(_link);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
79 80
    // With the new threading model connect will always succeed. We only get an error signal
    // for a failed connected.
81
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
82 83 84 85 86

    // 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();
87
    QCOMPARE(arguments.at(1).toString().contains(_link->getName()), true);
Don Gagne's avatar
Don Gagne committed
88
    _multiSpy->clearSignalByIndex(communicationErrorSignalIndex);
89
    
90
    _link->_disconnect();
Don Gagne's avatar
Don Gagne committed
91 92 93

    // Try to connect again to make sure everything was cleaned up correctly from previous failed connection
    
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);
    arguments = _multiSpy->getSpyByIndex(communicationErrorSignalIndex)->takeFirst();
100
    QCOMPARE(arguments.at(1).toString().contains(_link->getName()), true);
Don Gagne's avatar
Don Gagne committed
101 102 103
    _multiSpy->clearSignalByIndex(communicationErrorSignalIndex);
}

104
void TCPLinkTest::_connectSucceed_test(void)
Don Gagne's avatar
Don Gagne committed
105
{
106 107
    QSKIP("FIXME: Failing on OSX");

Don Gagne's avatar
Don Gagne committed
108 109 110 111 112
    Q_ASSERT(_link);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);

    // Start the server side
113
    TCPLoopBackServer* server = new TCPLoopBackServer(_config->address(), _config->port());
114
    Q_CHECK_PTR(server);
Don Gagne's avatar
Don Gagne committed
115 116
    
    // Connect to the server
117
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
118
    
119
    // Make sure we get the connected signals
Don Gagne's avatar
Don Gagne committed
120
    QCOMPARE(_multiSpy->waitForSignalByIndex(connectedSignalIndex, 10000), true);
121
    QCOMPARE(_multiSpy->checkOnlySignalByMask(connectedSignalMask), true);
122
    _multiSpy->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
123 124 125
    
    // Test link->server data path
    
126 127
    QByteArray bytesOut("test");

Don Gagne's avatar
Don Gagne committed
128
    // Write data from link to server
129 130 131
    const char* bytesWrittenSignal = SIGNAL(bytesWritten(qint64));
    MultiSignalSpy bytesWrittenSpy;
    QCOMPARE(bytesWrittenSpy.init(_link->getSocket(), &bytesWrittenSignal, 1), true);
132
    _link->writeBytesSafe(bytesOut.data(), bytesOut.size());
133 134 135 136 137 138
    _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
139

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

142 143 144
    QCOMPARE(_multiSpy->waitForSignalByIndex(bytesReceivedSignalIndex, 1000), true);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(bytesReceivedSignalMask), true);
    
Don Gagne's avatar
Don Gagne committed
145
    // Read the data and make sure it matches
146
    QList<QVariant> arguments = _multiSpy->getSpyByIndex(bytesReceivedSignalIndex)->takeFirst();
147
    QVERIFY(arguments.at(1).toByteArray() == bytesOut);
Don Gagne's avatar
Don Gagne committed
148
    
149 150
    _multiSpy->clearAllSignals();

Don Gagne's avatar
Don Gagne committed
151
    // Disconnect the link
152
    _link->_disconnect();
Don Gagne's avatar
Don Gagne committed
153
    
154
    // Make sure we get the disconnected signal on link side
Don Gagne's avatar
Don Gagne committed
155
    QCOMPARE(_multiSpy->waitForSignalByIndex(disconnectedSignalIndex, 1000), true);
156
    QCOMPARE(_multiSpy->checkOnlySignalByMask(disconnectedSignalMask), true);
157
    _multiSpy->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
158 159 160 161
    
    // Try to connect again to make sure everything was cleaned up correctly from previous connection
    
    // Connect to the server
162
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
163
    
164
    // Make sure we get the connected signal
Don Gagne's avatar
Don Gagne committed
165
    QCOMPARE(_multiSpy->waitForSignalByIndex(connectedSignalIndex, 1000), true);
166
    QCOMPARE(_multiSpy->checkOnlySignalByMask(connectedSignalMask), true);
167 168 169 170
    _multiSpy->clearAllSignals();
    
    server->quit();
    QTest::qWait(500);  // Wait a little for server thread to terminate
Don Gagne's avatar
Don Gagne committed
171
    delete server;
Don Gagne's avatar
Don Gagne committed
172
}