Skip to content
MockLinkFTP.cc 17.6 KiB
Newer Older
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
 * (c) 2009-2020 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.
 *
 ****************************************************************************/

DonLakeFlyer's avatar
 
DonLakeFlyer committed
#include "MockLinkFTP.h"
#include "MockLink.h"

DonLakeFlyer's avatar
 
DonLakeFlyer committed
const MockLinkFTP::ErrorMode_t MockLinkFTP::rgFailureModes[] = {
    MockLinkFTP::errModeNoResponse,
    MockLinkFTP::errModeNakResponse,
    MockLinkFTP::errModeNoSecondResponse,
    MockLinkFTP::errModeNakSecondResponse,
    MockLinkFTP::errModeBadSequence,
DonLakeFlyer's avatar
 
DonLakeFlyer committed
const size_t MockLinkFTP::cFailureModes = sizeof(MockLinkFTP::rgFailureModes) / sizeof(MockLinkFTP::rgFailureModes[0]);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
const MockLinkFTP::FileTestCase MockLinkFTP::rgFileTestCases[MockLinkFTP::cFileTestCases] = {
    // File fits one Read Ack packet, partially filling data
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    { "partial.qgc",    sizeof(((MavlinkFTP::Request*)0)->data) - 1,     1,    false},
    // File fits one Read Ack packet, exactly filling all data
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    { "exact.qgc",      sizeof(((MavlinkFTP::Request*)0)->data),         1,    true },
    // File is larger than a single Read Ack packets, requires multiple Reads
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    { "multi.qgc",      sizeof(((MavlinkFTP::Request*)0)->data) + 1,     2,    false },
DonLakeFlyer's avatar
 
DonLakeFlyer committed
MockLinkFTP::MockLinkFTP(uint8_t systemIdServer, uint8_t componentIdServer, MockLink* mockLink)
    : _systemIdServer   (systemIdServer)
    , _componentIdServer(componentIdServer)
    , _mockLink         (mockLink)
    srand(0); // make sure unit tests are deterministic
}

DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::ensureNullTemination(MavlinkFTP::Request* request)
{
    if (request->hdr.size < sizeof(request->data)) {
        request->data[request->hdr.size] = '\0';
    } else {
        request->data[sizeof(request->data)-1] = '\0';
    }
/// @brief Handles List command requests. Only supports root folder paths.
///         File list returned is set using the setFileList method.
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_listCommand(uint8_t senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request, uint16_t seqNumber)
    // FIXME: Does not support directories that span multiple packets
    
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    MavlinkFTP::Request  ackResponse;
Don Gagne's avatar
Don Gagne committed
    QString                     path;
    uint16_t                    outgoingSeqNumber = _nextSeqNumber(seqNumber);
Don Gagne's avatar
Don Gagne committed

    ensureNullTemination(request);

    // We only support root path
    path = (char *)&request->data[0];
    if (!path.isEmpty() && path != "/") {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrFail, outgoingSeqNumber, MavlinkFTP::kCmdListDirectory);
        return;
    }
    
    // Offset requested is past the end of the list
    if (request->hdr.offset > (uint32_t)_fileList.size()) {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrEOF, outgoingSeqNumber, MavlinkFTP::kCmdListDirectory);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    ackResponse.hdr.opcode = MavlinkFTP::kRspAck;
    ackResponse.hdr.req_opcode = MavlinkFTP::kCmdListDirectory;
    ackResponse.hdr.session = 0;
    ackResponse.hdr.offset = request->hdr.offset;
    ackResponse.hdr.size = 0;
    if (request->hdr.offset == 0) {
        // Requesting first batch of file names
        Q_ASSERT(_fileList.size());
        char *bufPtr = (char *)&ackResponse.data[0];
        for (int i=0; i<_fileList.size(); i++) {
            strcpy(bufPtr, _fileList[i].toStdString().c_str());
Don Gagne's avatar
Don Gagne committed
            uint8_t cchFilename = static_cast<uint8_t>(strlen(bufPtr));
            Q_ASSERT(cchFilename);
            ackResponse.hdr.size += cchFilename + 1;
            bufPtr += cchFilename + 1;
        }
        _sendResponse(senderSystemId, senderComponentId, &ackResponse, outgoingSeqNumber);
    } else if (_errMode == errModeNakSecondResponse) {
        // Nak error all subsequent requests
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrFail, outgoingSeqNumber, MavlinkFTP::kCmdListDirectory);
        return;
    } else if (_errMode == errModeNoSecondResponse) {
        // No response for all subsequent requests
        return;
        // FIXME: Does not support directories that span multiple packets
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrEOF, outgoingSeqNumber, MavlinkFTP::kCmdListDirectory);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_openCommand(uint8_t senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request, uint16_t seqNumber)
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    MavlinkFTP::Request response;
    QString             path;
    uint16_t            outgoingSeqNumber = _nextSeqNumber(seqNumber);
    QString             tmpFilename;
    ensureNullTemination(request);

    size_t cchPath = strnlen((char *)request->data, sizeof(request->data));
    Q_ASSERT(cchPath != sizeof(request->data));
    Q_UNUSED(cchPath); // Fix initialized-but-not-referenced warning on release builds
    path = (char *)request->data;
DonLakeFlyer's avatar
 
DonLakeFlyer committed

    _currentFile.close();
    
    // Check path against one of our known test cases
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    for (const FileTestCase& testCase: rgFileTestCases) {
        if (path == testCase.filename) {
            tmpFilename = _createTestCaseTempFile(testCase);
DonLakeFlyer's avatar
 
DonLakeFlyer committed

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    if (tmpFilename.isEmpty()) {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        if (path == "/version.json") {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
            tmpFilename = ":MockLink/Version.MetaData.json";
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        }
    }

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    if (!tmpFilename.isEmpty()) {
        _currentFile.setFileName(tmpFilename);
        if (!_currentFile.open(QIODevice::ReadOnly)) {
            _sendNakErrno(senderSystemId, senderComponentId, _currentFile.error(), outgoingSeqNumber, MavlinkFTP::kCmdOpenFileRO);
            return;
        }
    } else {
        _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrFailFileNotFound, outgoingSeqNumber, MavlinkFTP::kCmdOpenFileRO);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    response.hdr.opcode     = MavlinkFTP::kRspAck;
    response.hdr.req_opcode = MavlinkFTP::kCmdOpenFileRO;
    response.hdr.session    = _sessionId;
    // Data contains file length
    response.hdr.size = sizeof(uint32_t);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    response.openFileLength = _currentFile.size();
    _sendResponse(senderSystemId, senderComponentId, &response, outgoingSeqNumber);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_readCommand(uint8_t senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request, uint16_t seqNumber)
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    MavlinkFTP::Request	response;
    uint16_t			outgoingSeqNumber = _nextSeqNumber(seqNumber);

    if (request->hdr.session != _sessionId) {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrInvalidSession, outgoingSeqNumber, MavlinkFTP::kCmdReadFile);
    uint32_t readOffset = request->hdr.offset;  // offset into file for reading
DonLakeFlyer's avatar
 
DonLakeFlyer committed

    if (readOffset != 0) {
        // If we get here it means the client is requesting additional data past the first request
        if (_errMode == errModeNakSecondResponse) {
            // Nak error all subsequent requests
DonLakeFlyer's avatar
 
DonLakeFlyer committed
            _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrFail, outgoingSeqNumber, MavlinkFTP::kCmdReadFile);
            return;
        } else if (_errMode == errModeNoSecondResponse) {
            // No rsponse for all subsequent requests
            return;
        }
    }
    
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    if (readOffset >= _currentFile.size()) {
        _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrEOF, outgoingSeqNumber, MavlinkFTP::kCmdReadFile);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    uint8_t cBytesToRead = (uint8_t)qMin((qint64)sizeof(response.data), _currentFile.size() - readOffset);
    _currentFile.seek(readOffset);
    QByteArray bytes = _currentFile.read(cBytesToRead);
    memcpy(response.data, bytes.constData(), cBytesToRead);
    // We should always have written something, otherwise there is something wrong with the code above
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    Q_ASSERT(cBytesToRead);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    response.hdr.session    = _sessionId;
    response.hdr.size       = cBytesToRead;
    response.hdr.offset     = request->hdr.offset;
    response.hdr.opcode     = MavlinkFTP::kRspAck;
    response.hdr.req_opcode = MavlinkFTP::kCmdReadFile;
    _sendResponse(senderSystemId, senderComponentId, &response, outgoingSeqNumber);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_burstReadCommand(uint8_t senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request, uint16_t seqNumber)
{
    uint16_t                outgoingSeqNumber = _nextSeqNumber(seqNumber);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    MavlinkFTP::Request    response;

    if (request->hdr.session != _sessionId) {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrFail, outgoingSeqNumber, MavlinkFTP::kCmdBurstReadFile);
        return;
    }
    
    uint32_t readOffset = 0;	// offset into file for reading
    uint32_t ackOffset = 0;     // offset for ack
    uint8_t cDataAck;           // number of bytes in ack
    
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    while (readOffset < _currentFile.size()) {
        cDataAck = 0;
        
        if (readOffset != 0) {
            // If we get here it means the client is requesting additional data past the first request
            if (_errMode == errModeNakSecondResponse) {
                // Nak error all subsequent requests
DonLakeFlyer's avatar
 
DonLakeFlyer committed
                _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrFail, outgoingSeqNumber, MavlinkFTP::kCmdBurstReadFile);
                return;
            } else if (_errMode == errModeNoSecondResponse) {
                // No response for all subsequent requests
                return;
            }
        }
        
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        uint8_t cBytesToRead = (uint8_t)qMin((qint64)sizeof(response.data), _currentFile.size());
        _currentFile.seek(readOffset);
        QByteArray bytes = _currentFile.read(cBytesToRead);
        memcpy(response.data, bytes.constData(), cBytesToRead);

        // We should always have written something, otherwise there is something wrong with the code above
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        Q_ASSERT(cBytesToRead);
        
        response.hdr.session = _sessionId;
        response.hdr.size = cDataAck;
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        response.hdr.offset = cBytesToRead;
        response.hdr.opcode = MavlinkFTP::kRspAck;
        response.hdr.req_opcode = MavlinkFTP::kCmdBurstReadFile;
        _sendResponse(senderSystemId, senderComponentId, &response, outgoingSeqNumber);
        
        outgoingSeqNumber = _nextSeqNumber(outgoingSeqNumber);
        ackOffset += cDataAck;
    }
	
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrEOF, outgoingSeqNumber, MavlinkFTP::kCmdBurstReadFile);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_terminateCommand(uint8_t senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request, uint16_t seqNumber)
    uint16_t outgoingSeqNumber = _nextSeqNumber(seqNumber);

    if (request->hdr.session != _sessionId) {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        _sendNak(senderSystemId, senderComponentId, MavlinkFTP::kErrInvalidSession, outgoingSeqNumber, MavlinkFTP::kCmdTerminateSession);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    _sendAck(senderSystemId, senderComponentId, outgoingSeqNumber, MavlinkFTP::kCmdTerminateSession);
    emit terminateCommandReceived();
}

DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_resetCommand(uint8_t senderSystemId, uint8_t senderComponentId, uint16_t seqNumber)
    uint16_t outgoingSeqNumber = _nextSeqNumber(seqNumber);
    
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    _sendAck(senderSystemId, senderComponentId, outgoingSeqNumber, MavlinkFTP::kCmdResetSessions);
    
    emit resetCommandReceived();
}
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::handleFTPMessage(const mavlink_message_t& message)
{
    if (message.msgid != MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL) {
        return;
    }
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    MavlinkFTP::Request  ackResponse;
    mavlink_file_transfer_protocol_t requestFTP;
    mavlink_msg_file_transfer_protocol_decode(&message, &requestFTP);
    
    if (requestFTP.target_system != _systemIdServer) {
        return;
    }
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    MavlinkFTP::Request* request = (MavlinkFTP::Request*)&requestFTP.payload[0];
	if (_randomDropsEnabled) {
	    if (rand() % 3 == 0) {
	        qDebug() << "FileServer: Random drop of incoming packet";
	        return;
	    }
	}

	if (_lastReplyValid && request->hdr.seqNumber + 1 == _lastReplySequence) {
	    // this is the same request as the one we replied to last. It means the (n)ack got lost, and the GCS
	    // resent the request
	    qDebug() << "FileServer: resending response";
	    _mockLink->respondWithMavlinkMessage(_lastReply);
	    return;
	}

    uint16_t incomingSeqNumber = request->hdr.seqNumber;
    uint16_t outgoingSeqNumber = _nextSeqNumber(incomingSeqNumber);
    
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    if (request->hdr.opcode != MavlinkFTP::kCmdResetSessions && request->hdr.opcode != MavlinkFTP::kCmdTerminateSession) {
        if (_errMode == errModeNoResponse) {
            // Don't respond to any requests, this shold cause the client to eventually timeout waiting for the ack
            return;
        } else if (_errMode == errModeNakResponse) {
            // Nak all requests, the actual error send back doesn't really matter as long as it's an error
DonLakeFlyer's avatar
 
DonLakeFlyer committed
            _sendNak(message.sysid, message.compid, MavlinkFTP::kErrFail, outgoingSeqNumber, (MavlinkFTP::OpCode_t)request->hdr.opcode);
            return;
        }
Don Gagne's avatar
Don Gagne committed
    switch (request->hdr.opcode) {
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        case MavlinkFTP::kCmdNone:
            // ignored, always acked
DonLakeFlyer's avatar
 
DonLakeFlyer committed
            ackResponse.hdr.opcode = MavlinkFTP::kRspAck;
Don Gagne's avatar
Don Gagne committed
            ackResponse.hdr.session = 0;
            ackResponse.hdr.size = 0;
            _sendResponse(message.sysid, message.compid, &ackResponse, outgoingSeqNumber);
Don Gagne's avatar
Don Gagne committed
            break;

DonLakeFlyer's avatar
 
DonLakeFlyer committed
        case MavlinkFTP::kCmdListDirectory:
            _listCommand(message.sysid, message.compid, request, incomingSeqNumber);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        case MavlinkFTP::kCmdOpenFileRO:
            _openCommand(message.sysid, message.compid, request, incomingSeqNumber);
Don Gagne's avatar
Don Gagne committed

DonLakeFlyer's avatar
 
DonLakeFlyer committed
        case MavlinkFTP::kCmdReadFile:
            _readCommand(message.sysid, message.compid, request, incomingSeqNumber);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        case MavlinkFTP::kCmdBurstReadFile:
            _burstReadCommand(message.sysid, message.compid, request, incomingSeqNumber);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        case MavlinkFTP::kCmdTerminateSession:
            _terminateCommand(message.sysid, message.compid, request, incomingSeqNumber);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
        case MavlinkFTP::kCmdResetSessions:
            _resetCommand(message.sysid, message.compid, incomingSeqNumber);
            break;
            
        default:
            // nack for all NYI opcodes
DonLakeFlyer's avatar
 
DonLakeFlyer committed
            _sendNak(message.sysid, message.compid, MavlinkFTP::kErrUnknownCommand, outgoingSeqNumber, (MavlinkFTP::OpCode_t)request->hdr.opcode);
Don Gagne's avatar
Don Gagne committed

/// @brief Sends an Ack
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_sendAck(uint8_t targetSystemId, uint8_t targetComponentId, uint16_t seqNumber, MavlinkFTP::OpCode_t reqOpcode)
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    MavlinkFTP::Request ackResponse;
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    ackResponse.hdr.opcode = MavlinkFTP::kRspAck;
	ackResponse.hdr.req_opcode = reqOpcode;
    ackResponse.hdr.session = 0;
    ackResponse.hdr.size = 0;
    
    _sendResponse(targetSystemId, targetComponentId, &ackResponse, seqNumber);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_sendNak(uint8_t targetSystemId, uint8_t targetComponentId, MavlinkFTP::ErrorCode_t error, uint16_t seqNumber, MavlinkFTP::OpCode_t reqOpcode)
Don Gagne's avatar
Don Gagne committed
{
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    MavlinkFTP::Request nakResponse;
Don Gagne's avatar
Don Gagne committed

DonLakeFlyer's avatar
 
DonLakeFlyer committed
    nakResponse.hdr.opcode      = MavlinkFTP::kRspNak;
    nakResponse.hdr.req_opcode  = reqOpcode;
    nakResponse.hdr.session     = 0;
    nakResponse.hdr.size        = 1;
    nakResponse.data[0]         = error;
Don Gagne's avatar
Don Gagne committed
    
    _sendResponse(targetSystemId, targetComponentId, &nakResponse, seqNumber);
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_sendNakErrno(uint8_t targetSystemId, uint8_t targetComponentId, uint8_t nakErrno, uint16_t seqNumber, MavlinkFTP::OpCode_t reqOpcode)
{
    MavlinkFTP::Request nakResponse;

    nakResponse.hdr.opcode      = MavlinkFTP::kRspNak;
    nakResponse.hdr.req_opcode  = reqOpcode;
    nakResponse.hdr.session     = 0;
    nakResponse.hdr.size        = 2;
    nakResponse.data[0]         = MavlinkFTP::kErrFailErrno;
    nakResponse.data[1]         = nakErrno;

    _sendResponse(targetSystemId, targetComponentId, &nakResponse, seqNumber);
}

/// @brief Emits a Request through the messageReceived signal.
DonLakeFlyer's avatar
 
DonLakeFlyer committed
void MockLinkFTP::_sendResponse(uint8_t targetSystemId, uint8_t targetComponentId, MavlinkFTP::Request* request, uint16_t seqNumber)
Don Gagne's avatar
Don Gagne committed
{
    request->hdr.seqNumber = seqNumber;
    _lastReplySequence = seqNumber;
    _lastReplyValid = true;
DonLakeFlyer's avatar
 
DonLakeFlyer committed
    mavlink_msg_file_transfer_protocol_pack_chan(_systemIdServer,               // System ID
                                                 _componentIdServer,            // Component ID
                                                 _mockLink->mavlinkChannel(),
DonLakeFlyer's avatar
 
DonLakeFlyer committed
                                                 &_lastReply,                   // Mavlink Message to pack into
                                                 0,                             // Target network
                                                 targetSystemId,
                                                 targetComponentId,
DonLakeFlyer's avatar
 
DonLakeFlyer committed
                                                 (uint8_t*)request);            // Payload

	if (_randomDropsEnabled) {
	    if (rand() % 3 == 0) {
	        qDebug() << "FileServer: Random drop of outgoing packet";
	        return;
	    }
	}
Don Gagne's avatar
Don Gagne committed
    
    _mockLink->respondWithMavlinkMessage(_lastReply);
Lorenz Meier's avatar
Lorenz Meier committed
}

/// @brief Generates the next sequence number given an incoming sequence number. Handles generating
/// bad sequence numbers when errModeBadSequence is set.
DonLakeFlyer's avatar
 
DonLakeFlyer committed
uint16_t MockLinkFTP::_nextSeqNumber(uint16_t seqNumber)
{
    uint16_t outgoingSeqNumber = seqNumber + 1;
    
    if (_errMode == errModeBadSequence) {
        outgoingSeqNumber++;
    }
    return outgoingSeqNumber;
}
DonLakeFlyer's avatar
 
DonLakeFlyer committed

QString MockLinkFTP::_createTestCaseTempFile(const FileTestCase& testCase)
{
    QGCTemporaryFile tmpFile("MockLinkFTPTestCase");
    tmpFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
    for (int i=0; i<testCase.length; i++) {
        tmpFile.write(QByteArray(1, i % 255));
    }
    tmpFile.close();
    return tmpFile.fileName();
}