diff options
author | louib <louib@users.noreply.github.com> | 2018-12-20 07:10:46 +0300 |
---|---|---|
committer | Jonathan White <support@dmapps.us> | 2018-12-20 07:10:46 +0300 |
commit | 8ac9d0a131257b82728488592e1111c4546e051a (patch) | |
tree | b29fda9923f85af496047f2a116b4a0cfa3c329a /tests/TestCli.cpp | |
parent | a070f1bce7efcc5ce89aea533b181b0fa8c6dea0 (diff) |
Add create command to keepassxc-cli (#2540)
* Add tests for CLI::Create
Diffstat (limited to 'tests/TestCli.cpp')
-rw-r--r-- | tests/TestCli.cpp | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/tests/TestCli.cpp b/tests/TestCli.cpp index 430a3ba32..812e16166 100644 --- a/tests/TestCli.cpp +++ b/tests/TestCli.cpp @@ -34,6 +34,7 @@ #include "cli/Add.h" #include "cli/Clip.h" #include "cli/Command.h" +#include "cli/Create.h" #include "cli/Diceware.h" #include "cli/Edit.h" #include "cli/Estimate.h" @@ -137,9 +138,10 @@ QSharedPointer<Database> TestCli::readTestDatabase() const void TestCli::testCommand() { - QCOMPARE(Command::getCommands().size(), 12); + QCOMPARE(Command::getCommands().size(), 13); QVERIFY(Command::getCommand("add")); QVERIFY(Command::getCommand("clip")); + QVERIFY(Command::getCommand("create")); QVERIFY(Command::getCommand("diceware")); QVERIFY(Command::getCommand("edit")); QVERIFY(Command::getCommand("estimate")); @@ -274,7 +276,7 @@ void TestCli::testClip() // clang-format off QFuture<void> future = QtConcurrent::run(&clipCmd, &Clip::execute, QStringList{"clip", m_dbFile->fileName(), "/Sample Entry", "1"}); // clang-format on - + QTRY_COMPARE_WITH_TIMEOUT(clipboard->text(), QString("Password"), 500); QTRY_COMPARE_WITH_TIMEOUT(clipboard->text(), QString(""), 1500); @@ -296,6 +298,76 @@ void TestCli::testClip() QCOMPARE(m_stderrFile->readAll(), QByteArray("Entry with path /Sample Entry has no TOTP set up.\n")); } +void TestCli::testCreate() +{ + Create createCmd; + QVERIFY(!createCmd.name.isEmpty()); + QVERIFY(createCmd.getDescriptionLine().contains(createCmd.name)); + + QScopedPointer<QTemporaryDir> testDir(new QTemporaryDir()); + + QString databaseFilename = testDir->path() + "testCreate1.kdbx"; + // Password + Utils::Test::setNextPassword("a"); + createCmd.execute({"create", databaseFilename}); + + m_stderrFile->reset(); + m_stdoutFile->reset(); + + QCOMPARE(m_stdoutFile->readLine(), QByteArray("Insert password to encrypt database (Press enter to leave blank): \n")); + QCOMPARE(m_stdoutFile->readLine(), QByteArray("Successfully created new database.\n")); + + Utils::Test::setNextPassword("a"); + auto db = QSharedPointer<Database>(Utils::unlockDatabase(databaseFilename, "", Utils::DEVNULL)); + QVERIFY(db); + + // Should refuse to create the database if it already exists. + qint64 pos = m_stdoutFile->pos(); + qint64 errPos = m_stderrFile->pos(); + createCmd.execute({"create", databaseFilename}); + m_stdoutFile->seek(pos); + m_stderrFile->seek(errPos); + // Output should be empty when there is an error. + QCOMPARE(m_stdoutFile->readAll(), QByteArray("")); + QString errorMessage = QString("File " + databaseFilename + " already exists.\n"); + QCOMPARE(m_stderrFile->readAll(), errorMessage.toUtf8()); + + + // Testing with keyfile creation + QString databaseFilename2 = testDir->path() + "testCreate2.kdbx"; + QString keyfilePath = testDir->path() + "keyfile.txt"; + pos = m_stdoutFile->pos(); + errPos = m_stderrFile->pos(); + Utils::Test::setNextPassword("a"); + createCmd.execute({"create", databaseFilename2, "-k", keyfilePath}); + m_stdoutFile->seek(pos); + m_stderrFile->seek(errPos); + + QCOMPARE(m_stdoutFile->readLine(), QByteArray("Insert password to encrypt database (Press enter to leave blank): \n")); + QCOMPARE(m_stdoutFile->readLine(), QByteArray("Successfully created new database.\n")); + + Utils::Test::setNextPassword("a"); + auto db2 = QSharedPointer<Database>(Utils::unlockDatabase(databaseFilename2, keyfilePath, Utils::DEVNULL)); + QVERIFY(db2); + + + // Testing with existing keyfile + QString databaseFilename3 = testDir->path() + "testCreate3.kdbx"; + pos = m_stdoutFile->pos(); + errPos = m_stderrFile->pos(); + Utils::Test::setNextPassword("a"); + createCmd.execute({"create", databaseFilename3, "-k", keyfilePath}); + m_stdoutFile->seek(pos); + m_stderrFile->seek(errPos); + + QCOMPARE(m_stdoutFile->readLine(), QByteArray("Insert password to encrypt database (Press enter to leave blank): \n")); + QCOMPARE(m_stdoutFile->readLine(), QByteArray("Successfully created new database.\n")); + + Utils::Test::setNextPassword("a"); + auto db3 = QSharedPointer<Database>(Utils::unlockDatabase(databaseFilename3, keyfilePath, Utils::DEVNULL)); + QVERIFY(db3); +} + void TestCli::testDiceware() { Diceware dicewareCmd; |