void TestKeys::testCreateFileKey() { const QString dbName("testCreateFileKey database"); QBuffer keyBuffer; keyBuffer.open(QBuffer::ReadWrite); FileKey::create(&keyBuffer); keyBuffer.reset(); FileKey fileKey; QVERIFY(fileKey.load(&keyBuffer)); CompositeKey compositeKey; compositeKey.addKey(fileKey); Database* dbOrg = new Database(); dbOrg->setKey(compositeKey); dbOrg->metadata()->setName(dbName); QBuffer dbBuffer; dbBuffer.open(QBuffer::ReadWrite); KeePass2Writer writer; writer.writeDatabase(&dbBuffer, dbOrg); dbBuffer.reset(); delete dbOrg; KeePass2Reader reader; Database* dbRead = reader.readDatabase(&dbBuffer, compositeKey); QVERIFY(dbRead); QVERIFY(!reader.hasError()); QCOMPARE(dbRead->metadata()->name(), dbName); delete dbRead; }
void TestKeePass1Reader::reopenDatabase(Database* db, const QString& password, const QString& keyfileName) { QBuffer buffer; buffer.open(QIODevice::ReadWrite); KeePass2Writer writer; writer.writeDatabase(&buffer, db); QVERIFY(!writer.hasError()); QVERIFY(buffer.seek(0)); CompositeKey key; if (!password.isNull()) { key.addKey(PasswordKey(password)); } if (!keyfileName.isEmpty()) { FileKey fileKey; QVERIFY(fileKey.load(keyfileName)); key.addKey(fileKey); } KeePass2Reader reader; Database* newDb = reader.readDatabase(&buffer, key); QVERIFY(newDb); QVERIFY(!reader.hasError()); delete newDb; }
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 = m_dbOrg->rootGroup(); group->setUuid(Uuid::random()); group->setNotes("I'm a note!"); Entry* entry = new Entry(); entry->setPassword(QString::fromUtf8("\xc3\xa4\xa3\xb6\xc3\xbc\xe9\x9b\xbb\xe7\xb4\x85")); entry->setUuid(Uuid::random()); entry->attributes()->set("test", "protectedTest", true); QVERIFY(entry->attributes()->isProtected("test")); entry->attachments()->set("myattach.txt", QByteArray("this is an attachment")); entry->attachments()->set("aaa.txt", QByteArray("also an attachment")); entry->setGroup(group); Group* groupNew = new Group(); groupNew->setUuid(Uuid::random()); groupNew->setName("TESTGROUP"); groupNew->setNotes("I'm a sub group note!"); groupNew->setParent(group); 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.hasError()); QVERIFY(m_dbTest); }
bool Database::writeDatabase(QIODevice* device, QString* error) { Q_ASSERT(!m_data.isReadOnly); if (m_data.isReadOnly) { if (error) { *error = tr("File cannot be written as it is opened in read-only mode."); } return false; } QByteArray oldTransformedKey = m_data.transformedMasterKey; KeePass2Writer writer; setEmitModified(false); writer.writeDatabase(device, this); setEmitModified(true); if (writer.hasError()) { // the writer failed if (error) { *error = writer.errorString(); } return false; } Q_ASSERT(!m_data.transformedMasterKey.isEmpty()); Q_ASSERT(m_data.transformedMasterKey != oldTransformedKey); if (m_data.transformedMasterKey.isEmpty() || m_data.transformedMasterKey == oldTransformedKey) { if (error) { *error = tr("Key not transformed. This is a bug, please report it to the developers!"); } return false; } markAsClean(); return true; }