FailDevice.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #include "FailDevice.h"
  18. FailDevice::FailDevice(int failAfter, QObject* parent)
  19. : QBuffer(parent)
  20. , m_failAfter(failAfter)
  21. , m_readCount(0)
  22. , m_writeCount(0)
  23. {
  24. }
  25. bool FailDevice::open(QIODevice::OpenMode openMode)
  26. {
  27. return QBuffer::open(openMode | QIODevice::Unbuffered);
  28. }
  29. qint64 FailDevice::readData(char* data, qint64 len)
  30. {
  31. if (m_readCount >= m_failAfter) {
  32. setErrorString("FAILDEVICE");
  33. return -1;
  34. } else {
  35. qint64 result = QBuffer::readData(data, len);
  36. if (result != -1) {
  37. m_readCount += result;
  38. }
  39. return result;
  40. }
  41. }
  42. qint64 FailDevice::writeData(const char* data, qint64 len)
  43. {
  44. if (m_writeCount >= m_failAfter) {
  45. setErrorString("FAILDEVICE");
  46. return -1;
  47. } else {
  48. qint64 result = QBuffer::writeData(data, len);
  49. if (result != -1) {
  50. m_writeCount += result;
  51. }
  52. return result;
  53. }
  54. }