FileManagerTest.cc 16.7 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

/// @file
///     @author Don Gagne <don@thegagnes.com>

14
#include "FileManagerTest.h"
15
#include "MultiVehicleManager.h"
16
#include "UAS.h"
17
#include "QGCApplication.h"
18

19 20 21 22
FileManagerTest::FileManagerTest(void)
    : _fileServer(NULL)
    , _fileManager(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 case
28
void FileManagerTest::init(void)
Don Gagne's avatar
Don Gagne committed
29
{
Don Gagne's avatar
Don Gagne committed
30 31
    UnitTest::init();
    
32
    _connectMockLink();
33 34 35

    _fileServer = _mockLink->getFileServer();
    QVERIFY(_fileServer != NULL);
Don Gagne's avatar
Don Gagne committed
36
    
37
    _fileManager = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()->uas()->getFileManager();
38 39 40
    QVERIFY(_fileManager != NULL);
    
    Q_ASSERT(_multiSpy == NULL);
Don Gagne's avatar
Don Gagne committed
41
    
42
    // Reset any internal state back to normal
43
    _fileServer->setErrorMode(MockLinkFileServer::errModeNone);
44
    _fileListReceived.clear();
Don Gagne's avatar
Don Gagne committed
45

46
    connect(_fileManager, &FileManager::listEntry, this, &FileManagerTest::listEntry);
Don Gagne's avatar
Don Gagne committed
47

48
    _rgSignals[listEntrySignalIndex] = SIGNAL(listEntry(const QString&));
49 50
    _rgSignals[commandCompleteSignalIndex] = SIGNAL(commandComplete(void));
    _rgSignals[commandErrorSignalIndex] = SIGNAL(commandError(const QString&));
51

Don Gagne's avatar
Don Gagne committed
52 53 54 55 56 57
    _multiSpy = new MultiSignalSpy();
    Q_CHECK_PTR(_multiSpy);
    QCOMPARE(_multiSpy->init(_fileManager, _rgSignals, _cSignals), true);
}

// Called after every test case
58
void FileManagerTest::cleanup(void)
Don Gagne's avatar
Don Gagne committed
59 60 61 62
{
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_fileManager);
    
63 64
    // Disconnecting the link will prompt for log file save
    setExpectedFileDialog(getSaveFileName, QStringList());
65 66
    _disconnectMockLink();

67 68 69
    _fileServer = NULL;
    _fileManager = NULL;
    
Don Gagne's avatar
Don Gagne committed
70 71 72
    delete _multiSpy;
    
    _multiSpy = NULL;
Don Gagne's avatar
Don Gagne committed
73 74
    
    UnitTest::cleanup();
Don Gagne's avatar
Don Gagne committed
75 76
}

77 78
/// @brief Connected to FileManager listEntry signal in order to catch list entries
void FileManagerTest::listEntry(const QString& entry)
Don Gagne's avatar
Don Gagne committed
79 80
{
    // Keep a list of all names received so we can test it for correctness
81
    _fileListReceived += entry;
Don Gagne's avatar
Don Gagne committed
82 83 84
}


85
void FileManagerTest::_ackTest(void)
Don Gagne's avatar
Don Gagne committed
86 87 88 89 90 91 92
{
    Q_ASSERT(_fileManager);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
    // If the file manager doesn't receive an ack it will timeout and emit an error. So make sure
    // we don't get any error signals.
93 94 95 96
    //TODO: FIX
    //QVERIFY(_fileManager->_sendCmdTestAck());
    //QTest::qWait(_ackTimerTimeoutMsecs); // Let the file manager timeout
    //QVERIFY(_multiSpy->checkNoSignals());
97
    
98
    // Setup for no response from ack. This should cause a timeout error
99
    _fileServer->setErrorMode(MockLinkFileServer::errModeNoResponse);
100
    QVERIFY(_fileManager->_sendCmdTestAck());
101 102
    _multiSpy->waitForSignalByIndex(commandErrorSignalIndex, _ackTimerTimeoutMsecs);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
103 104 105
    _multiSpy->clearAllSignals();

    // Setup for a bad sequence number in the ack. This should cause an error;
106
    _fileServer->setErrorMode(MockLinkFileServer::errModeBadSequence);
107
    QVERIFY(_fileManager->_sendCmdTestAck());
108 109
    _multiSpy->waitForSignalByIndex(commandErrorSignalIndex, _ackTimerTimeoutMsecs);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
110
    _multiSpy->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
111 112
}

113
void FileManagerTest::_noAckTest(void)
Don Gagne's avatar
Don Gagne committed
114 115 116 117 118 119 120
{
    Q_ASSERT(_fileManager);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
    // This should not get the ack back and timeout.
    QVERIFY(_fileManager->_sendCmdTestNoAck());
121
    QTest::qWait(_ackTimerTimeoutMsecs); // Let the file manager timeout
122
    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
Don Gagne's avatar
Don Gagne committed
123 124
}

125
void FileManagerTest::_listTest(void)
Don Gagne's avatar
Don Gagne committed
126 127 128 129
{
    Q_ASSERT(_fileManager);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
130 131 132

    // test the automatic retry behavior by enabling random drops
    _fileServer->enableRandromDrops(true);
Don Gagne's avatar
Don Gagne committed
133
    
134
    // FileManager::listDirectory signalling as follows:
135
    //  Emits a listEntry signal for each list entry
136
    //  Emits an commandError signal if:
137 138 139 140
    //      It gets a Nak back
    //      Sequence number is incorrrect on any response
    //      CRC is incorrect on any responses
    //      List entry is formatted incorrectly
141 142 143
    //  It is possible to get a number of good listEntry signals, followed by an commandError signal
    //  Emits commandComplete after it receives the final list entry
    //      If an commandError signal is signalled no listComplete is signalled
144
    
Don Gagne's avatar
Don Gagne committed
145
    // Send a bogus path
146
    //  We should get a single commandError signal
147
    _fileManager->listDirectory("/bogus");
148 149
    _multiSpy->waitForSignalByIndex(commandErrorSignalIndex, _ackTimerTimeoutMsecs);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
Don Gagne's avatar
Don Gagne committed
150 151
    _multiSpy->clearAllSignals();

152
    // Setup the mock file server with a valid directory list
Don Gagne's avatar
Don Gagne committed
153 154
    QStringList fileList;
    fileList << "Ddir" << "Ffoo" << "Fbar";
155 156 157 158 159 160 161 162 163 164 165 166 167
    _fileServer->setFileList(fileList);
    
    // Send a list command at the root of the directory tree which should succeed
    _fileManager->listDirectory("/");
    QTest::qWait(_ackTimerTimeoutMsecs); // Let the file manager timeout
    QCOMPARE(_multiSpy->checkSignalByMask(commandCompleteSignalMask), true);
    QCOMPARE(_multiSpy->checkNoSignalByMask(commandErrorSignalMask), true);
    QCOMPARE(_multiSpy->getSpyByIndex(listEntrySignalIndex)->count(), fileList.count());
    QVERIFY(_fileListReceived == fileList);
    
    // Set everything back to initial state
    _fileListReceived.clear();
    _multiSpy->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
168
    
169
    // Run through the various server side failure modes
170 171
    for (size_t i=0; i<MockLinkFileServer::cFailureModes; i++) {
        MockLinkFileServer::ErrorMode_t errMode = MockLinkFileServer::rgFailureModes[i];
172
        qDebug() << "Testing failure mode:" << errMode;
173
        _fileServer->setErrorMode(errMode);
174 175 176 177
        
        _fileManager->listDirectory("/");
        QTest::qWait(_ackTimerTimeoutMsecs); // Let the file manager timeout
        
178
        if (errMode == MockLinkFileServer::errModeNoSecondResponse || errMode == MockLinkFileServer::errModeNakSecondResponse) {
179 180 181
            // For simulated server errors on subsequent Acks, the first Ack will go through. This means we should have gotten some
            // partial results. In the case of the directory list test set, all entries fit into the first ack, so we should have
            // gotten back all of them.
182 183
            QCOMPARE(_multiSpy->getSpyByIndex(listEntrySignalIndex)->count(), fileList.count());
            _multiSpy->clearSignalByIndex(listEntrySignalIndex);
184 185
            
            // And then it should have errored out because the next list Request would have failed.
186
            QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
187
        } else {
nopeppermint's avatar
nopeppermint committed
188
            // For the simulated errors which failed the initial response we should not have gotten any results back at all.
189
            // Just an error.
190
            QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
191 192 193 194 195
        }

        // Set everything back to initial state
        _fileListReceived.clear();
        _multiSpy->clearAllSignals();
196
        _fileServer->setErrorMode(MockLinkFileServer::errModeNone);
197
    }
198 199

    _fileServer->enableRandromDrops(false);
Don Gagne's avatar
Don Gagne committed
200
}
201

202 203 204
#if 0
// Trying to write test code for read and burst mode download as well as implement support in MockLineFileServer reached a point
// of diminishing returns where the test code and mock server were generating more bugs in themselves than finding problems.
205
void FileManagerTest::_readDownloadTest(void)
206 207 208 209 210
{
    Q_ASSERT(_fileManager);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
211 212 213
    // We setup a spy on the Reset command signal of the mock file server so that we can determine that a
    // Reset command was correctly sent after the Open/Read commands complete.
    QSignalSpy resetSpy(_fileServer, SIGNAL(resetCommandReceived()));
214
    
215 216
    // Send a bogus path
    _fileManager->downloadPath("bogus", QDir::temp());
217 218
    _multiSpy->waitForSignalByIndex(commandErrorSignalIndex, _ackTimerTimeoutMsecs);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
219
    _multiSpy->clearAllSignals();
220 221
    QCOMPARE(resetSpy.count(), 0);

222
    // Clean previous downloads
223 224
    for (size_t i=0; i<MockLinkFileServer::cFileTestCases; i++) {
        QString filePath = QDir::temp().absoluteFilePath(MockLinkFileServer::rgFileTestCases[i].filename);
225 226 227
        if (QFile::exists(filePath)) {
            Q_ASSERT(QFile::remove(filePath));
        }
228
    }
229
    
230
    // Run through the set of file test cases
231 232
    for (size_t i=0; i<MockLinkFileServer::cFileTestCases; i++) {
        const MockLinkFileServer::FileTestCase* testCase = &MockLinkFileServer::rgFileTestCases[i];
233 234
        
        // Run through the various failure modes for this test case
235 236
        for (size_t j=0; j<MockLinkFileServer::cFailureModes; j++) {
            qDebug() << "Testing successful download";
237
			
238 239 240 241 242
            // Run what should be a successful file download test case. No servers errors are being simulated.
            _fileServer->setErrorMode(MockLinkFileServer::errModeNone);
            _fileManager->downloadPath(testCase->filename, QDir::temp());
            QTest::qWait(_ackTimerTimeoutMsecs); // Let the file manager timeout
            
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
243
            // This should be a successful download
244 245 246 247 248 249 250 251 252 253 254 255
            QCOMPARE(_multiSpy->checkOnlySignalByMask(commandCompleteSignalMask), true);
            _multiSpy->clearAllSignals();
            
            // We should get a single Reset command to close the session
            QCOMPARE(resetSpy.count(), 1);
            resetSpy.clear();
            
            // Validate file contents
            QString filePath = QDir::temp().absoluteFilePath(MockLinkFileServer::rgFileTestCases[i].filename);
            _validateFileContents(filePath, MockLinkFileServer::rgFileTestCases[i].length);
            MockLinkFileServer::ErrorMode_t errMode = MockLinkFileServer::rgFailureModes[j];
            
256
            qDebug() << "Testing failure mode:" << errMode;
257
            _fileServer->setErrorMode(errMode);
258
            
259
            _fileManager->downloadPath(testCase->filename, QDir::temp());
260 261
            QTest::qWait(_ackTimerTimeoutMsecs); // Let the file manager timeout
            
262 263 264 265 266 267
            if (errMode == MockLinkFileServer::errModeNakResponse) {
                // This will Nak the Open call which will fail the download, but not cause a Reset
                QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
                QCOMPARE(resetSpy.count(), 0);
            } else {
                if (testCase->packetCount == 1 && (errMode == MockLinkFileServer::errModeNoSecondResponse || errMode == MockLinkFileServer::errModeNakSecondResponse)) {
268
                    // The downloaded file fits within a single Ack response, hence there is no second Read issued.
269 270 271
                    // This should result in a successful download, followed by a Reset
                    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandCompleteSignalMask), true);
                    QCOMPARE(resetSpy.count(), 1);
272 273
                    
                    // Validate file contents
274 275
                    QString filePath = QDir::temp().absoluteFilePath(testCase->filename);
                    _validateFileContents(filePath, testCase->length);
276 277 278 279
                } else {
                    // Download should have failed, followed by a aReset
                    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
                    QCOMPARE(resetSpy.count(), 1);
280 281 282 283 284
                }
            }

            // Cleanup for next iteration
            _multiSpy->clearAllSignals();
285 286
            resetSpy.clear();
            _fileServer->setErrorMode(MockLinkFileServer::errModeNone);
287
        }
288
    }
289
}
290 291 292

void FileManagerTest::_streamDownloadTest(void)
{
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    Q_ASSERT(_fileManager);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
    // We setup a spy on the Reset command signal of the mock file server so that we can determine that a
    // Reset command was correctly sent after the Open/Read commands complete.
    QSignalSpy resetSpy(_fileServer, SIGNAL(resetCommandReceived()));
    
    // Send a bogus path
    _fileManager->streamPath("bogus", QDir::temp());
    _multiSpy->waitForSignalByIndex(commandErrorSignalIndex, _ackTimerTimeoutMsecs);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
    _multiSpy->clearAllSignals();
    QCOMPARE(resetSpy.count(), 0);

    // Clean previous downloads
    for (size_t i=0; i<MockLinkFileServer::cFileTestCases; i++) {
        QString filePath = QDir::temp().absoluteFilePath(MockLinkFileServer::rgFileTestCases[i].filename);
        if (QFile::exists(filePath)) {
            Q_ASSERT(QFile::remove(filePath));
        }
    }
    
    // Run through the set of file test cases
    for (size_t i=0; i<MockLinkFileServer::cFileTestCases; i++) {
        const MockLinkFileServer::FileTestCase* testCase = &MockLinkFileServer::rgFileTestCases[i];
        
        // Run through the various failure modes for this test case
        for (size_t j=0; j<MockLinkFileServer::cFailureModes; j++) {
            qDebug() << "Testing successful download";
323
			
324 325 326 327 328
            // Run what should be a successful file download test case. No servers errors are being simulated.
            _fileServer->setErrorMode(MockLinkFileServer::errModeNone);
            _fileManager->streamPath(testCase->filename, QDir::temp());
            QTest::qWait(_ackTimerTimeoutMsecs); // Let the file manager timeout
            
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
329
            // This should be a successful download
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
            QCOMPARE(_multiSpy->checkOnlySignalByMask(commandCompleteSignalMask), true);
            _multiSpy->clearAllSignals();
            
            // We should get a single Reset command to close the session
            QCOMPARE(resetSpy.count(), 1);
            resetSpy.clear();
            
            // Validate file contents
            QString filePath = QDir::temp().absoluteFilePath(MockLinkFileServer::rgFileTestCases[i].filename);
            _validateFileContents(filePath, MockLinkFileServer::rgFileTestCases[i].length);
            MockLinkFileServer::ErrorMode_t errMode = MockLinkFileServer::rgFailureModes[j];
            
            qDebug() << "Testing failure mode:" << errMode;
            _fileServer->setErrorMode(errMode);
            
            _fileManager->downloadPath(testCase->filename, QDir::temp());
            QTest::qWait(_ackTimerTimeoutMsecs); // Let the file manager timeout
            
            if (errMode == MockLinkFileServer::errModeNakResponse) {
                // This will Nak the Open call which will fail the download, but not cause a Reset
                QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
                QCOMPARE(resetSpy.count(), 0);
            } else {
                if (testCase->packetCount == 1 && (errMode == MockLinkFileServer::errModeNoSecondResponse || errMode == MockLinkFileServer::errModeNakSecondResponse)) {
                    // The downloaded file fits within a single Ack response, hence there is no second Read issued.
                    // This should result in a successful download, followed by a Reset
                    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandCompleteSignalMask), true);
                    QCOMPARE(resetSpy.count(), 1);
                    
                    // Validate file contents
                    QString filePath = QDir::temp().absoluteFilePath(testCase->filename);
                    _validateFileContents(filePath, testCase->length);
                } else {
                    // Download should have failed, followed by a aReset
                    QCOMPARE(_multiSpy->checkOnlySignalByMask(commandErrorSignalMask), true);
                    QCOMPARE(resetSpy.count(), 1);
                }
            }

            // Cleanup for next iteration
            _multiSpy->clearAllSignals();
            resetSpy.clear();
            _fileServer->setErrorMode(MockLinkFileServer::errModeNone);
        }
    }
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
}

void FileManagerTest::_validateFileContents(const QString& filePath, uint8_t length)
{
	QFile file(filePath);
	
	// Make sure file size is correct
	QCOMPARE(file.size(), (qint64)length);
	
	// Read data
	QVERIFY(file.open(QIODevice::ReadOnly));
	QByteArray bytes = file.readAll();
	file.close();
	
	// Validate file contents:
	//      Repeating 0x00, 0x01 .. 0xFF until file is full
	for (uint8_t i=0; i<bytes.length(); i++) {
		QCOMPARE((uint8_t)bytes[i], (uint8_t)(i & 0xFF));
	}
}
395
#endif