keepassxc
1/*
2* Copyright (C) 2015 Felix Geyer <debfx@fobos.de>
3*
4* This program is free software: you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation, either version 2 or (at your option)
7* version 3 of the License.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "FailDevice.h"19
20FailDevice::FailDevice(int failAfter, QObject* parent)21: QBuffer(parent)22, m_failAfter(failAfter)23, m_readCount(0)24, m_writeCount(0)25{
26}
27
28bool FailDevice::open(QIODevice::OpenMode openMode)29{
30return QBuffer::open(openMode | QIODevice::Unbuffered);31}
32
33qint64 FailDevice::readData(char* data, qint64 len)34{
35if (m_readCount >= m_failAfter) {36setErrorString("FAILDEVICE");37return -1;38} else {39qint64 result = QBuffer::readData(data, len);40if (result != -1) {41m_readCount += result;42}43
44return result;45}46}
47
48qint64 FailDevice::writeData(const char* data, qint64 len)49{
50if (m_writeCount >= m_failAfter) {51setErrorString("FAILDEVICE");52return -1;53} else {54qint64 result = QBuffer::writeData(data, len);55if (result != -1) {56m_writeCount += result;57}58
59return result;60}61}
62