Deep copy subkeys when cloning CompositeKey.

This commit is contained in:
Felix Geyer 2011-11-13 14:52:43 +01:00
parent ac60f7ce70
commit 0014d50b59
5 changed files with 110 additions and 0 deletions

View File

@ -22,6 +22,15 @@
#include <QtCore/QtConcurrentRun> #include <QtCore/QtConcurrentRun>
CompositeKey::CompositeKey()
{
}
CompositeKey::CompositeKey(const CompositeKey& key)
{
*this = key;
}
CompositeKey::~CompositeKey() CompositeKey::~CompositeKey()
{ {
qDeleteAll(m_keys); qDeleteAll(m_keys);
@ -32,6 +41,14 @@ CompositeKey* CompositeKey::clone() const
return new CompositeKey(*this); 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 QByteArray CompositeKey::rawKey() const
{ {
@ -46,6 +63,9 @@ QByteArray CompositeKey::rawKey() const
QByteArray CompositeKey::transform(const QByteArray& seed, int rounds) const QByteArray CompositeKey::transform(const QByteArray& seed, int rounds) const
{ {
Q_ASSERT(seed.size() == 32);
Q_ASSERT(rounds > 0);
QByteArray key = rawKey(); QByteArray key = rawKey();
QFuture<QByteArray> future1 = QtConcurrent::run(transformKeyRaw, key.left(16), seed, rounds); QFuture<QByteArray> future1 = QtConcurrent::run(transformKeyRaw, key.left(16), seed, rounds);

View File

@ -25,8 +25,11 @@
class CompositeKey : public Key class CompositeKey : public Key
{ {
public: public:
CompositeKey();
CompositeKey(const CompositeKey& key);
~CompositeKey(); ~CompositeKey();
CompositeKey* clone() const; CompositeKey* clone() const;
CompositeKey& operator=(const CompositeKey& key);
QByteArray rawKey() const; QByteArray rawKey() const;
QByteArray transform(const QByteArray& seed, int rounds) const; QByteArray transform(const QByteArray& seed, int rounds) const;

View File

@ -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 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 testkeepass2reader SOURCES TestKeePass2Reader.cpp MOCS TestKeePass2Reader.h LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testkeepass2writer SOURCES TestKeePass2Writer.cpp MOCS TestKeePass2Writer.h LIBS ${TEST_LIBRARIES}) add_unit_test(NAME testkeepass2writer SOURCES TestKeePass2Writer.cpp MOCS TestKeePass2Writer.h LIBS ${TEST_LIBRARIES})

53
tests/TestKeys.cpp Normal file
View File

@ -0,0 +1,53 @@
/*
* Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "TestKeys.h"
#include <QtTest/QTest>
#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);

32
tests/TestKeys.h Normal file
View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_TESTKEYS_H
#define KEEPASSX_TESTKEYS_H
#include <QtCore/QObject>
class TestKeys : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testComposite();
};
#endif // KEEPASSX_TESTKEYS_H