2011-07-06 18:15:52 -04:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2012-01-01 16:04:27 -05:00
|
|
|
#include "tests.h"
|
2011-07-06 18:15:52 -04:00
|
|
|
#include "core/Database.h"
|
2011-07-08 07:57:02 -04:00
|
|
|
#include "core/Group.h"
|
2011-07-06 18:15:52 -04:00
|
|
|
#include "core/Metadata.h"
|
|
|
|
#include "crypto/Crypto.h"
|
|
|
|
#include "format/KeePass2Reader.h"
|
|
|
|
#include "format/KeePass2Writer.h"
|
|
|
|
#include "keys/PasswordKey.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");
|
2011-07-09 15:54:01 -04:00
|
|
|
Group* group = m_dbOrg->rootGroup();
|
2011-07-06 18:15:52 -04:00
|
|
|
group->setUuid(Uuid::random());
|
2012-04-11 11:54:27 -04:00
|
|
|
group->setNotes("I'm a note!");
|
2011-07-06 18:15:52 -04:00
|
|
|
Entry* entry = new Entry();
|
|
|
|
entry->setUuid(Uuid::random());
|
2012-04-14 09:38:20 -04:00
|
|
|
entry->attributes()->set("test", "protectedTest", true);
|
|
|
|
QVERIFY(entry->attributes()->isProtected("test"));
|
2012-04-21 10:45:46 -04:00
|
|
|
entry->attachments()->set("myattach.txt", QByteArray("this is an attachment"));
|
|
|
|
entry->attachments()->set("aaa.txt", QByteArray("also an attachment"));
|
2011-07-08 07:57:02 -04:00
|
|
|
entry->setGroup(group);
|
2011-12-27 09:47:06 -05:00
|
|
|
Group* groupNew = new Group();
|
|
|
|
groupNew->setUuid(Uuid::random());
|
|
|
|
groupNew->setName("TESTGROUP");
|
2012-04-11 11:54:27 -04:00
|
|
|
groupNew->setNotes("I'm a sub group note!");
|
2011-12-27 09:47:06 -05:00
|
|
|
groupNew->setParent(group);
|
2011-07-06 18:15:52 -04:00
|
|
|
|
|
|
|
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);
|
2012-01-06 14:03:13 -05:00
|
|
|
QVERIFY(!reader.hasError());
|
2011-07-06 18:15:52 -04:00
|
|
|
QVERIFY(m_dbTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass2Writer::testBasic()
|
|
|
|
{
|
|
|
|
QCOMPARE(m_dbTest->metadata()->name(), m_dbOrg->metadata()->name());
|
|
|
|
QVERIFY(m_dbTest->rootGroup());
|
2011-12-27 09:47:06 -05:00
|
|
|
QCOMPARE(m_dbTest->rootGroup()->children()[0]->name(), m_dbOrg->rootGroup()->children()[0]->name());
|
2012-04-11 11:54:27 -04:00
|
|
|
QCOMPARE(m_dbTest->rootGroup()->notes(), m_dbOrg->rootGroup()->notes());
|
|
|
|
QCOMPARE(m_dbTest->rootGroup()->children()[0]->notes(), m_dbOrg->rootGroup()->children()[0]->notes());
|
2011-07-06 18:15:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeePass2Writer::testProtectedAttributes()
|
|
|
|
{
|
|
|
|
QCOMPARE(m_dbTest->rootGroup()->entries().size(), 1);
|
|
|
|
Entry* entry = m_dbTest->rootGroup()->entries().at(0);
|
2012-04-14 09:38:20 -04:00
|
|
|
QCOMPARE(entry->attributes()->value("test"), QString("protectedTest"));
|
|
|
|
QCOMPARE(entry->attributes()->isProtected("test"), true);
|
2011-07-06 18:15:52 -04:00
|
|
|
}
|
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
void TestKeePass2Writer::testAttachments()
|
|
|
|
{
|
|
|
|
Entry* entry = m_dbTest->rootGroup()->entries().at(0);
|
|
|
|
QCOMPARE(entry->attachments()->keys().size(), 2);
|
|
|
|
QCOMPARE(entry->attachments()->value("myattach.txt"), QByteArray("this is an attachment"));
|
|
|
|
QCOMPARE(entry->attachments()->value("aaa.txt"), QByteArray("also an attachment"));
|
|
|
|
}
|
|
|
|
|
2012-01-01 16:04:27 -05:00
|
|
|
KEEPASSX_QTEST_CORE_MAIN(TestKeePass2Writer)
|