From 0014d50b596ec7603779ce210662c401773285a9 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sun, 13 Nov 2011 14:52:43 +0100 Subject: [PATCH] Deep copy subkeys when cloning CompositeKey. --- src/keys/CompositeKey.cpp | 20 +++++++++++++++ src/keys/CompositeKey.h | 3 +++ tests/CMakeLists.txt | 2 ++ tests/TestKeys.cpp | 53 +++++++++++++++++++++++++++++++++++++++ tests/TestKeys.h | 32 +++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 tests/TestKeys.cpp create mode 100644 tests/TestKeys.h diff --git a/src/keys/CompositeKey.cpp b/src/keys/CompositeKey.cpp index c5c50e110..fc72dd6a9 100644 --- a/src/keys/CompositeKey.cpp +++ b/src/keys/CompositeKey.cpp @@ -22,6 +22,15 @@ #include +CompositeKey::CompositeKey() +{ +} + +CompositeKey::CompositeKey(const CompositeKey& key) +{ + *this = key; +} + CompositeKey::~CompositeKey() { qDeleteAll(m_keys); @@ -32,6 +41,14 @@ CompositeKey* CompositeKey::clone() const return new CompositeKey(*this); } +CompositeKey& CompositeKey::operator=(const CompositeKey& key) +{ + Q_FOREACH (Key* subKey, key.m_keys) { + m_keys.append(subKey->clone()); + } + + return *this; +} QByteArray CompositeKey::rawKey() const { @@ -46,6 +63,9 @@ QByteArray CompositeKey::rawKey() const QByteArray CompositeKey::transform(const QByteArray& seed, int rounds) const { + Q_ASSERT(seed.size() == 32); + Q_ASSERT(rounds > 0); + QByteArray key = rawKey(); QFuture future1 = QtConcurrent::run(transformKeyRaw, key.left(16), seed, rounds); diff --git a/src/keys/CompositeKey.h b/src/keys/CompositeKey.h index 4a1d0172c..017ffbfdd 100644 --- a/src/keys/CompositeKey.h +++ b/src/keys/CompositeKey.h @@ -25,8 +25,11 @@ class CompositeKey : public Key { public: + CompositeKey(); + CompositeKey(const CompositeKey& key); ~CompositeKey(); CompositeKey* clone() const; + CompositeKey& operator=(const CompositeKey& key); QByteArray rawKey() const; QByteArray transform(const QByteArray& seed, int rounds) const; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e4cfeb03d..c878c9715 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -103,6 +103,8 @@ add_unit_test(NAME testgroup SOURCES TestGroup.cpp MOCS TestGroup.h LIBS ${TEST_ add_unit_test(NAME testkeepass2xmlreader SOURCES TestKeePass2XmlReader.cpp MOCS TestKeePass2XmlReader.h LIBS ${TEST_LIBRARIES}) +add_unit_test(NAME testkeys SOURCES TestKeys.cpp MOCS TestKeys.h LIBS ${TEST_LIBRARIES}) + add_unit_test(NAME testkeepass2reader SOURCES TestKeePass2Reader.cpp MOCS TestKeePass2Reader.h LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testkeepass2writer SOURCES TestKeePass2Writer.cpp MOCS TestKeePass2Writer.h LIBS ${TEST_LIBRARIES}) diff --git a/tests/TestKeys.cpp b/tests/TestKeys.cpp new file mode 100644 index 000000000..0698f8701 --- /dev/null +++ b/tests/TestKeys.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2011 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "TestKeys.h" + +#include + +#include "crypto/Crypto.h" +#include "keys/CompositeKey.h" +#include "keys/PasswordKey.h" + +void TestKeys::initTestCase() +{ + Crypto::init(); +} + +void TestKeys::testComposite() +{ + CompositeKey* compositeKey1 = new CompositeKey(); + PasswordKey* passwordKey1 = new PasswordKey(); + PasswordKey* passwordKey2 = new PasswordKey("test"); + + compositeKey1->addKey(*passwordKey1); + compositeKey1->addKey(*passwordKey2); + delete passwordKey1; + delete passwordKey2; + + QVERIFY(compositeKey1->transform(QByteArray(32, '\0'), 1).size() == 32); + + // make sure the subkeys are copied + CompositeKey* compositeKey2 = compositeKey1->clone(); + delete compositeKey1; + + QVERIFY(compositeKey2->transform(QByteArray(32, '\0'), 1).size() == 32); + + delete compositeKey2; +} + +QTEST_MAIN(TestKeys); diff --git a/tests/TestKeys.h b/tests/TestKeys.h new file mode 100644 index 000000000..d525c706a --- /dev/null +++ b/tests/TestKeys.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2011 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef KEEPASSX_TESTKEYS_H +#define KEEPASSX_TESTKEYS_H + +#include + +class TestKeys : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + void testComposite(); +}; + +#endif // KEEPASSX_TESTKEYS_H