From cfffdae5734c99c6882ac8bc8647ef0c6a7afa75 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sat, 9 May 2015 20:11:20 +0200 Subject: [PATCH] Improve error reporing of layered streams. --- src/streams/HashedBlockStream.cpp | 6 ++++++ src/streams/SymmetricCipherStream.cpp | 3 ++- tests/TestHashedBlockStream.cpp | 12 ++++++------ tests/TestSymmetricCipher.cpp | 16 ++++++++-------- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/streams/HashedBlockStream.cpp b/src/streams/HashedBlockStream.cpp index 39e16de2e..cd43dc0bf 100644 --- a/src/streams/HashedBlockStream.cpp +++ b/src/streams/HashedBlockStream.cpp @@ -150,6 +150,7 @@ bool HashedBlockStream::readHashedBlock() if (m_blockSize == 0) { if (hash.count('\0') != 32) { m_error = true; + setErrorString("Invalid hash of final block."); return false; } @@ -166,6 +167,7 @@ bool HashedBlockStream::readHashedBlock() if (hash != CryptoHash::hash(m_buffer, CryptoHash::Sha256)) { m_error = true; + setErrorString("Mismatch between hash and data."); return false; } @@ -213,6 +215,7 @@ bool HashedBlockStream::writeHashedBlock() { if (!Endian::writeInt32(m_blockIndex, m_baseDevice, ByteOrder)) { m_error = true; + setErrorString(m_baseDevice->errorString()); return false; } m_blockIndex++; @@ -227,17 +230,20 @@ bool HashedBlockStream::writeHashedBlock() if (m_baseDevice->write(hash) != hash.size()) { m_error = true; + setErrorString(m_baseDevice->errorString()); return false; } if (!Endian::writeInt32(m_buffer.size(), m_baseDevice, ByteOrder)) { m_error = true; + setErrorString(m_baseDevice->errorString()); return false; } if (!m_buffer.isEmpty()) { if (m_baseDevice->write(m_buffer) != m_buffer.size()) { m_error = true; + setErrorString(m_baseDevice->errorString()); return false; } diff --git a/src/streams/SymmetricCipherStream.cpp b/src/streams/SymmetricCipherStream.cpp index 48ceb8f4d..28525b39f 100644 --- a/src/streams/SymmetricCipherStream.cpp +++ b/src/streams/SymmetricCipherStream.cpp @@ -147,6 +147,7 @@ bool SymmetricCipherStream::readBlock() else if (padLength > m_cipher->blockSize()) { // invalid padding m_error = true; + setErrorString("Invalid padding."); return false; } else { @@ -217,7 +218,7 @@ bool SymmetricCipherStream::writeBlock(bool lastBlock) if (m_baseDevice->write(m_buffer) != m_buffer.size()) { m_error = true; - // TODO: copy error string + setErrorString(m_cipher->errorString()); return false; } else { diff --git a/tests/TestHashedBlockStream.cpp b/tests/TestHashedBlockStream.cpp index 09179fef2..2a37d0aa4 100644 --- a/tests/TestHashedBlockStream.cpp +++ b/tests/TestHashedBlockStream.cpp @@ -36,15 +36,15 @@ void TestHashedBlockStream::testWriteRead() QByteArray data = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"); QBuffer buffer; - buffer.open(QIODevice::ReadWrite); + QVERIFY(buffer.open(QIODevice::ReadWrite)); HashedBlockStream writer(&buffer, 16); - writer.open(QIODevice::WriteOnly); + QVERIFY(writer.open(QIODevice::WriteOnly)); HashedBlockStream reader(&buffer); - reader.open(QIODevice::ReadOnly); + QVERIFY(reader.open(QIODevice::ReadOnly)); - writer.write(data.left(16)); + QCOMPARE(writer.write(data.left(16)), qint64(16)); QVERIFY(writer.reset()); buffer.reset(); QCOMPARE(reader.read(17), data.left(16)); @@ -52,7 +52,7 @@ void TestHashedBlockStream::testWriteRead() buffer.reset(); buffer.buffer().clear(); - writer.write(data.left(10)); + QCOMPARE(writer.write(data.left(10)), qint64(10)); QVERIFY(writer.reset()); buffer.reset(); QCOMPARE(reader.read(5), data.left(5)); @@ -62,7 +62,7 @@ void TestHashedBlockStream::testWriteRead() buffer.reset(); buffer.buffer().clear(); - writer.write(data.left(20)); + QCOMPARE(writer.write(data.left(20)), qint64(20)); QVERIFY(writer.reset()); buffer.reset(); QCOMPARE(reader.read(20), data.left(20)); diff --git a/tests/TestSymmetricCipher.cpp b/tests/TestSymmetricCipher.cpp index ec9badd90..55a1bbe37 100644 --- a/tests/TestSymmetricCipher.cpp +++ b/tests/TestSymmetricCipher.cpp @@ -57,12 +57,12 @@ void TestSymmetricCipher::testAes256CbcEncryption() SymmetricCipher::Encrypt); QVERIFY(stream.init(key, iv)); buffer.open(QIODevice::WriteOnly); - stream.open(QIODevice::WriteOnly); + QVERIFY(stream.open(QIODevice::WriteOnly)); QVERIFY(stream.reset()); buffer.reset(); buffer.buffer().clear(); - stream.write(plainText.left(16)); + QCOMPARE(stream.write(plainText.left(16)), qint64(16)); QCOMPARE(buffer.data(), cipherText.left(16)); QVERIFY(stream.reset()); // make sure padding is written @@ -70,13 +70,13 @@ void TestSymmetricCipher::testAes256CbcEncryption() buffer.reset(); buffer.buffer().clear(); - stream.write(plainText.left(10)); + QCOMPARE(stream.write(plainText.left(10)), qint64(10)); QVERIFY(buffer.data().isEmpty()); QVERIFY(stream.reset()); buffer.reset(); buffer.buffer().clear(); - stream.write(plainText.left(10)); + QCOMPARE(stream.write(plainText.left(10)), qint64(10)); stream.close(); QCOMPARE(buffer.data().size(), 16); } @@ -106,20 +106,20 @@ void TestSymmetricCipher::testAes256CbcDecryption() SymmetricCipher::Decrypt); QVERIFY(stream.init(key, iv)); buffer.open(QIODevice::ReadOnly); - stream.open(QIODevice::ReadOnly); + QVERIFY(stream.open(QIODevice::ReadOnly)); QCOMPARE(stream.read(10), plainText.left(10)); buffer.reset(); - stream.reset(); + QVERIFY(stream.reset()); QCOMPARE(stream.read(20), plainText.left(20)); buffer.reset(); - stream.reset(); + QVERIFY(stream.reset()); QCOMPARE(stream.read(16), plainText.left(16)); buffer.reset(); - stream.reset(); + QVERIFY(stream.reset()); QCOMPARE(stream.read(100), plainText); }