TCPLinkTest.cc 5.98 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
    : _link(NULL)
21
    , _multiSpy(NULL)
Don Gagne's avatar
Don Gagne committed
22
{
23

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

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

34 35 36 37 38
    TCPConfiguration* tcpConfig = new TCPConfiguration("MockTCP");
    tcpConfig->setAddress(QHostAddress::LocalHost);
    tcpConfig->setPort(5760);
    _sharedConfig = SharedLinkConfigurationPointer(tcpConfig);
    _link = new TCPLink(_sharedConfig);
Don Gagne's avatar
Don Gagne committed
39 40 41 42 43 44

    _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&));
45
    //_rgSignals[deleteLinkSignalIndex] = SIGNAL(_deleteLink(LinkInterface*));
Don Gagne's avatar
Don Gagne committed
46 47 48 49 50 51

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

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

    delete _multiSpy;
58 59
    _multiSpy = nullptr;

Don Gagne's avatar
Don Gagne committed
60
    delete _link;
61 62
    _link = nullptr;

63
    _sharedConfig.clear();
Don Gagne's avatar
Don Gagne committed
64

Don Gagne's avatar
Don Gagne committed
65
    UnitTest::cleanup();
Don Gagne's avatar
Don Gagne committed
66 67
}

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

    // 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();
82
    QCOMPARE(arguments.at(1).toString().contains(_link->getName()), true);
Don Gagne's avatar
Don Gagne committed
83
    _multiSpy->clearSignalByIndex(communicationErrorSignalIndex);
84
    
85
    _link->_disconnect();
Don Gagne's avatar
Don Gagne committed
86 87 88

    // Try to connect again to make sure everything was cleaned up correctly from previous failed connection
    
89
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
90 91 92 93 94
    
    // 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();
95
    QCOMPARE(arguments.at(1).toString().contains(_link->getName()), true);
Don Gagne's avatar
Don Gagne committed
96 97 98
    _multiSpy->clearSignalByIndex(communicationErrorSignalIndex);
}

99
void TCPLinkTest::_connectSucceed_test(void)
Don Gagne's avatar
Don Gagne committed
100
{
101 102
    QSKIP("FIXME: Failing on OSX");

Don Gagne's avatar
Don Gagne committed
103 104 105 106 107
    Q_ASSERT(_link);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);

    // Start the server side
108 109
    TCPConfiguration* tcpConfig = qobject_cast<TCPConfiguration*>(_sharedConfig.data());
    TCPLoopBackServer* server = new TCPLoopBackServer(tcpConfig->address(), tcpConfig->port());
110
    Q_CHECK_PTR(server);
Don Gagne's avatar
Don Gagne committed
111 112
    
    // Connect to the server
113
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
114
    
115
    // Make sure we get the connected signals
Don Gagne's avatar
Don Gagne committed
116
    QCOMPARE(_multiSpy->waitForSignalByIndex(connectedSignalIndex, 10000), true);
117
    QCOMPARE(_multiSpy->checkOnlySignalByMask(connectedSignalMask), true);
118
    _multiSpy->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
119 120 121
    
    // Test link->server data path
    
122 123
    QByteArray bytesOut("test");

Don Gagne's avatar
Don Gagne committed
124
    // Write data from link to server
125 126 127
    const char* bytesWrittenSignal = SIGNAL(bytesWritten(qint64));
    MultiSignalSpy bytesWrittenSpy;
    QCOMPARE(bytesWrittenSpy.init(_link->getSocket(), &bytesWrittenSignal, 1), true);
128
    _link->writeBytesSafe(bytesOut.data(), bytesOut.size());
129 130 131 132
    _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.
133
    connect(this, &TCPLinkTest::waitForBytesWritten, _link, &TCPLink::waitForBytesWritten);
134
    emit waitForBytesWritten(1000);
Don Gagne's avatar
Don Gagne committed
135

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

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

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