Skip to content

Commit

Permalink
Add functional test for merge with keys
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzk42 committed Dec 8, 2019
1 parent 0c7d9cd commit 68948f9
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
87 changes: 87 additions & 0 deletions tests/TestCli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,93 @@ void TestCli::testMerge()
QCOMPARE(m_stdoutFile->readAll(), QByteArray(""));
}

void TestCli::testMergeWithKeys() {
Create createCmd;
QVERIFY(!createCmd.name.isEmpty());
QVERIFY(createCmd.getDescriptionLine().contains(createCmd.name));

Merge mergeCmd;
QVERIFY(!mergeCmd.name.isEmpty());
QVERIFY(mergeCmd.getDescriptionLine().contains(mergeCmd.name));

Kdbx4Writer writer;
Kdbx4Reader reader;

QScopedPointer<QTemporaryDir> testDir(new QTemporaryDir());

QString sourceDatabaseFilename = testDir->path() + "/testSourceDatabase.kdbx";
QString sourceKeyfilePath = testDir->path() + "/testSourceKeyfile.txt";

QString targetDatabaseFilename = testDir->path() + "/testTargetDatabase.kdbx";
QString targetKeyfilePath = testDir->path() + "/testTargetKeyfile.txt";

qint64 pos = m_stdoutFile->pos();

Utils::Test::setNextPassword("a");
createCmd.execute({"create", sourceDatabaseFilename, "-k", sourceKeyfilePath});

Utils::Test::setNextPassword("b");
createCmd.execute({"create", targetDatabaseFilename, "-k", targetKeyfilePath});

Utils::Test::setNextPassword("a");
auto sourceDatabase =
QSharedPointer<Database>(Utils::unlockDatabase(sourceDatabaseFilename, true, sourceKeyfilePath, "", Utils::STDOUT));
QVERIFY(sourceDatabase);

Utils::Test::setNextPassword("b");
auto targetDatabase =
QSharedPointer<Database>(Utils::unlockDatabase(targetDatabaseFilename, true, targetKeyfilePath, "", Utils::STDOUT));
QVERIFY(targetDatabase);

auto * rootGroup = new Group();
auto * group = new Group();
group->setParent(rootGroup);
group->setName("Internet");

auto * entry = new Entry();
entry->setUuid(QUuid::createUuid());
entry->setTitle("Some Website");
entry->setPassword("secretsecretsecret");
group->addEntry(entry);

sourceDatabase->setRootGroup(rootGroup);

auto * otherRootGroup = new Group();
auto * otherGroup = new Group();
otherGroup->setParent(otherRootGroup);
otherGroup->setName("Internet");

auto * otherEntry = new Entry();
otherEntry->setUuid(QUuid::createUuid());
otherEntry->setTitle("Some Website 2");
otherEntry->setPassword("secretsecretsecret 2");
otherGroup->addEntry(otherEntry);

targetDatabase->setRootGroup(otherRootGroup);

QFile sourceDatabaseFile(sourceDatabaseFilename);
sourceDatabaseFile.open(QIODevice::WriteOnly);
QVERIFY(writer.writeDatabase(&sourceDatabaseFile, sourceDatabase.data()));
sourceDatabaseFile.flush();
sourceDatabaseFile.close();

QFile targetDatabaseFile(targetDatabaseFilename);
targetDatabaseFile.open(QIODevice::WriteOnly);
QVERIFY(writer.writeDatabase(&targetDatabaseFile, targetDatabase.data()));
targetDatabaseFile.flush();
targetDatabaseFile.close();

pos = m_stdoutFile->pos();
Utils::Test::setNextPassword("b");
Utils::Test::setNextPassword("a");
mergeCmd.execute({"merge", "-k", targetKeyfilePath, "--key-file-from", sourceKeyfilePath,
targetDatabaseFile.fileName(), sourceDatabaseFile.fileName()});

m_stdoutFile->seek(pos);
QList<QByteArray> lines = m_stdoutFile->readAll().split('\n');
QVERIFY(lines.contains(QString("Successfully merged %1 into %2.").arg(sourceDatabaseFile.fileName(), targetDatabaseFile.fileName()).toUtf8()));
}

void TestCli::testMove()
{
Move moveCmd;
Expand Down
1 change: 1 addition & 0 deletions tests/TestCli.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private slots:
void testList();
void testLocate();
void testMerge();
void testMergeWithKeys();
void testMove();
void testOpen();
void testRemove();
Expand Down

0 comments on commit 68948f9

Please sign in to comment.