Integrate KeePass2RandomStream into KeePass2 reader and writer classes.

This hopefully completes support for reading/writing kdbx <= 2.14 files.

Also fix a bug in KeePass2XmlWriter so it sets Protected="True" in the Value
tag instead of Key.
This commit is contained in:
Felix Geyer
2011-07-07 00:15:52 +02:00
parent 58e048be96
commit a299dd9715
12 changed files with 200 additions and 45 deletions

View File

@@ -105,6 +105,8 @@ add_unit_test(NAME testkeepass2xmlreader SOURCES TestKeePass2XmlReader.cpp MOCS
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 testgroupmodel SOURCES TestGroupModel.cpp MOCS TestGroupModel.h LIBS ${TEST_LIBRARIES} modeltest)
add_unit_test(NAME testentrymodel SOURCES TestEntryModel.cpp MOCS TestEntryModel.h LIBS ${TEST_LIBRARIES} modeltest)

BIN
tests/ProtectedStrings.kdbx Normal file

Binary file not shown.

View File

@@ -61,4 +61,33 @@ void TestKeePass2Reader::testCompressed()
delete reader;
}
void TestKeePass2Reader::testProtectedStrings()
{
QString filename = QString(KEEPASSX_TEST_DIR).append("/ProtectedStrings.kdbx");
CompositeKey key;
key.addKey(PasswordKey("masterpw"));
KeePass2Reader* reader = new KeePass2Reader();
Database* db = reader->readDatabase(filename, key);
QVERIFY(db);
QVERIFY(!reader->error());
QCOMPARE(db->metadata()->name(), QString("Protected Strings Test"));
Entry* entry = db->rootGroup()->entries().at(0);
QCOMPARE(entry->title(), QString("Sample Entry"));
QCOMPARE(entry->username(), QString("Protected User Name"));
QCOMPARE(entry->password(), QString("ProtectedPassword"));
QCOMPARE(entry->attributes().value("TestProtected"), QString("ABC"));
QCOMPARE(entry->attributes().value("TestUnprotected"), QString("DEF"));
QVERIFY(!db->metadata()->protectTitle());
QVERIFY(db->metadata()->protectUsername());
QVERIFY(db->metadata()->protectPassword());
QVERIFY(entry->isAttributeProtected("TestProtected"));
QVERIFY(!entry->isAttributeProtected("TestUnprotected"));
delete db;
delete reader;
}
QTEST_MAIN(TestKeePass2Reader);

View File

@@ -28,6 +28,7 @@ private Q_SLOTS:
void initTestCase();
void testNonAscii();
void testCompressed();
void testProtectedStrings();
};
#endif // KEEPASSX_TESTKEEPASS2READER_H

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2010 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 "TestKeePass2Writer.h"
#include <QtCore/QBuffer>
#include <QtTest/QTest>
#include "core/Database.h"
#include "core/Metadata.h"
#include "crypto/Crypto.h"
#include "format/KeePass2Reader.h"
#include "format/KeePass2Writer.h"
#include "keys/PasswordKey.h"
#include "format/KeePass2XmlWriter.h"
void TestKeePass2Writer::initTestCase()
{
Crypto::init();
CompositeKey key;
key.addKey(PasswordKey("test"));
m_dbOrg = new Database();
m_dbOrg->setKey(key);
m_dbOrg->metadata()->setName("TESTDB");
Group* group = new Group();
group->setUuid(Uuid::random());
group->setParent(m_dbOrg);
m_dbOrg->setRootGroup(group);
Entry* entry = new Entry();
entry->setUuid(Uuid::random());
entry->addAttribute("test", "protectedTest", true);
QVERIFY(entry->isAttributeProtected("test"));
group->addEntry(entry);
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
KeePass2Writer writer;
writer.writeDatabase(&buffer, m_dbOrg);
QVERIFY(!writer.error());
buffer.seek(0);
KeePass2Reader reader;
m_dbTest = reader.readDatabase(&buffer, key);
QVERIFY(!reader.error());
QVERIFY(m_dbTest);
}
void TestKeePass2Writer::testBasic()
{
QCOMPARE(m_dbTest->metadata()->name(), m_dbOrg->metadata()->name());
QVERIFY(m_dbTest->rootGroup());
}
void TestKeePass2Writer::testProtectedAttributes()
{
QCOMPARE(m_dbTest->rootGroup()->entries().size(), 1);
Entry* entry = m_dbTest->rootGroup()->entries().at(0);
QCOMPARE(entry->attributes().value("test"), QString("protectedTest"));
QCOMPARE(entry->isAttributeProtected("test"), true);
}
QTEST_MAIN(TestKeePass2Writer);

View File

@@ -0,0 +1,39 @@
/*
* Copyright (C) 2010 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_TESTKEEPASS2WRITER_H
#define KEEPASSX_TESTKEEPASS2WRITER_H
#include <QtCore/QObject>
class Database;
class TestKeePass2Writer : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testBasic();
void testProtectedAttributes();
private:
Database* m_dbOrg;
Database* m_dbTest;
};
#endif // KEEPASSX_TESTKEEPASS2WRITER_H