AusweisApp2/test/helper/MockSocket.cpp

67 lines
1.2 KiB
C++
Raw Normal View History

2017-07-03 09:33:28 +02:00
/*!
2019-05-22 10:08:38 +02:00
* \copyright Copyright (c) 2016-2019 Governikus GmbH & Co. KG, Germany
2017-07-03 09:33:28 +02:00
*/
#include "MockSocket.h"
2017-12-20 14:54:05 +01:00
#include <cstring>
2017-07-03 09:33:28 +02:00
using namespace governikus;
MockSocket::MockSocket()
: QTcpSocket()
, mReadBuffer()
, mReaderBufferPosition(0)
, mReaderBufferChunk(-1)
, mWriteBuffer()
{
2017-12-20 14:54:05 +01:00
QTcpSocket::open(QIODevice::ReadWrite);
2017-07-03 09:33:28 +02:00
}
MockSocket::~MockSocket()
{
}
qint64 MockSocket::bytesAvailable() const
{
return mReadBuffer.size() - mReaderBufferPosition;
}
qint64 MockSocket::readData(char* pDestination, qint64 pMaxSize)
{
Q_ASSERT(pMaxSize <= INT_MAX);
int chunk = static_cast<int>(pMaxSize);
if (mReaderBufferChunk > -1 && mReaderBufferChunk < chunk)
{
chunk = mReaderBufferChunk;
}
QByteArray data = mReadBuffer.mid(mReaderBufferPosition, chunk);
int length = data.length();
if (length >= 0)
{
2017-12-20 14:54:05 +01:00
memcpy(pDestination, data.constData(), static_cast<size_t>(length));
2017-07-03 09:33:28 +02:00
mReaderBufferPosition += length;
}
return length;
}
qint64 MockSocket::writeData(const char* pData, qint64 pMaxSize)
{
const auto& data = QByteArray(pData, static_cast<int>(pMaxSize));
mWriteBuffer += data;
return data.size();
}
2019-09-30 17:22:19 +02:00
qint64 MockSocket::write(const QByteArray& pByteArray)
{
mWriteBuffer += pByteArray;
return QIODevice::write(pByteArray);
}