Improve error reporing of layered streams.

This commit is contained in:
Felix Geyer 2015-05-09 20:11:20 +02:00
parent f6243675c9
commit cfffdae573
4 changed files with 22 additions and 15 deletions

View File

@ -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;
}

View File

@ -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 {

View File

@ -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));

View File

@ -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);
}