From 98d9dae087f693066781b53e3c9032d7ec6017ab Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Mon, 20 Jul 2015 21:50:39 +0200 Subject: [PATCH] Test if reset() and close() write only one final block. --- tests/TestHashedBlockStream.cpp | 15 +++++++++++++++ tests/TestHashedBlockStream.h | 1 + tests/TestSymmetricCipher.cpp | 19 +++++++++++++++++++ tests/TestSymmetricCipher.h | 1 + 4 files changed, 36 insertions(+) diff --git a/tests/TestHashedBlockStream.cpp b/tests/TestHashedBlockStream.cpp index eca63aff0..d5a0ceaf0 100644 --- a/tests/TestHashedBlockStream.cpp +++ b/tests/TestHashedBlockStream.cpp @@ -73,6 +73,21 @@ void TestHashedBlockStream::testWriteRead() buffer.buffer().clear(); } +void TestHashedBlockStream::testReset() +{ + QBuffer buffer; + QVERIFY(buffer.open(QIODevice::WriteOnly)); + + HashedBlockStream writer(&buffer, 16); + QVERIFY(writer.open(QIODevice::WriteOnly)); + QCOMPARE(writer.write(QByteArray(8, 'Z')), qint64(8)); + // test if reset() and close() write only one final block + QVERIFY(writer.reset()); + QVERIFY(writer.reset()); + writer.close(); + QCOMPARE(buffer.buffer().size(), 8 + (32 + 4 + 4) * 2); +} + void TestHashedBlockStream::testWriteFailure() { FailDevice failDevice(1500); diff --git a/tests/TestHashedBlockStream.h b/tests/TestHashedBlockStream.h index 96018d885..9aeac1411 100644 --- a/tests/TestHashedBlockStream.h +++ b/tests/TestHashedBlockStream.h @@ -27,6 +27,7 @@ class TestHashedBlockStream : public QObject private Q_SLOTS: void initTestCase(); void testWriteRead(); + void testReset(); void testWriteFailure(); }; diff --git a/tests/TestSymmetricCipher.cpp b/tests/TestSymmetricCipher.cpp index 55a1bbe37..578d25016 100644 --- a/tests/TestSymmetricCipher.cpp +++ b/tests/TestSymmetricCipher.cpp @@ -207,3 +207,22 @@ void TestSymmetricCipher::testPadding() QByteArray decrypted = streamDec.readAll(); QCOMPARE(decrypted, plainText); } + +void TestSymmetricCipher::testStreamReset() +{ + QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"); + QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f"); + + QBuffer buffer; + QVERIFY(buffer.open(QIODevice::WriteOnly)); + SymmetricCipherStream writer(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc, + SymmetricCipher::Encrypt); + QVERIFY(writer.init(key, iv)); + QVERIFY(writer.open(QIODevice::WriteOnly)); + QCOMPARE(writer.write(QByteArray(4, 'Z')), qint64(4)); + // test if reset() and close() write only one block + QVERIFY(writer.reset()); + QVERIFY(writer.reset()); + writer.close(); + QCOMPARE(buffer.buffer().size(), 16); +} diff --git a/tests/TestSymmetricCipher.h b/tests/TestSymmetricCipher.h index e90fe6001..1ac45793f 100644 --- a/tests/TestSymmetricCipher.h +++ b/tests/TestSymmetricCipher.h @@ -30,6 +30,7 @@ private Q_SLOTS: void testAes256CbcDecryption(); void testSalsa20(); void testPadding(); + void testStreamReset(); }; #endif // KEEPASSX_TESTSYMMETRICCIPHER_H