2019-10-14 08:37:26 -04:00
|
|
|
/*
|
2020-10-08 22:48:45 -04:00
|
|
|
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
|
2019-10-14 08:37:26 -04:00
|
|
|
*
|
|
|
|
* 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 "TestCli.h"
|
|
|
|
|
|
|
|
#include "config-keepassx-tests.h"
|
|
|
|
#include "core/Bootstrap.h"
|
|
|
|
#include "core/Config.h"
|
2020-10-08 22:48:45 -04:00
|
|
|
#include "core/Entry.h"
|
2019-10-14 08:37:26 -04:00
|
|
|
#include "core/Global.h"
|
2020-10-08 22:48:45 -04:00
|
|
|
#include "core/Group.h"
|
|
|
|
#include "core/Metadata.h"
|
2019-10-14 08:37:26 -04:00
|
|
|
#include "core/Tools.h"
|
|
|
|
#include "crypto/Crypto.h"
|
2019-11-18 01:57:04 -05:00
|
|
|
#include "keys/drivers/YubiKey.h"
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
#include "cli/Add.h"
|
|
|
|
#include "cli/AddGroup.h"
|
|
|
|
#include "cli/Analyze.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"
|
|
|
|
#include "cli/Export.h"
|
|
|
|
#include "cli/Generate.h"
|
|
|
|
#include "cli/Help.h"
|
|
|
|
#include "cli/Import.h"
|
2020-01-26 23:44:31 -05:00
|
|
|
#include "cli/Info.h"
|
2019-10-14 08:37:26 -04:00
|
|
|
#include "cli/List.h"
|
|
|
|
#include "cli/Locate.h"
|
|
|
|
#include "cli/Merge.h"
|
|
|
|
#include "cli/Move.h"
|
|
|
|
#include "cli/Open.h"
|
|
|
|
#include "cli/Remove.h"
|
|
|
|
#include "cli/RemoveGroup.h"
|
|
|
|
#include "cli/Show.h"
|
|
|
|
#include "cli/Utils.h"
|
|
|
|
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QFuture>
|
2020-04-06 08:42:20 -04:00
|
|
|
#include <QSet>
|
|
|
|
#include <QSignalSpy>
|
|
|
|
#include <QTextStream>
|
2019-10-14 08:37:26 -04:00
|
|
|
#include <QtConcurrent>
|
|
|
|
|
|
|
|
QTEST_MAIN(TestCli)
|
|
|
|
|
|
|
|
QSharedPointer<Database> globalCurrentDatabase;
|
|
|
|
|
|
|
|
void TestCli::initTestCase()
|
|
|
|
{
|
|
|
|
QVERIFY(Crypto::init());
|
|
|
|
|
|
|
|
Config::createTempFileInstance();
|
2020-10-08 22:48:45 -04:00
|
|
|
Bootstrap::bootstrap();
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-12-13 23:23:25 -05:00
|
|
|
m_devNull.reset(new QFile());
|
2020-05-11 07:31:29 -04:00
|
|
|
#ifdef Q_OS_WIN
|
2020-12-13 23:23:25 -05:00
|
|
|
m_devNull->open(fopen("nul", "w"), QIODevice::WriteOnly);
|
2020-05-11 07:31:29 -04:00
|
|
|
#else
|
2020-12-13 23:23:25 -05:00
|
|
|
m_devNull->open(fopen("/dev/null", "w"), QIODevice::WriteOnly);
|
2020-05-11 07:31:29 -04:00
|
|
|
#endif
|
2020-12-13 23:23:25 -05:00
|
|
|
Utils::DEVNULL.setDevice(m_devNull.data());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::init()
|
|
|
|
{
|
2020-05-11 07:31:29 -04:00
|
|
|
const auto file = QString(KEEPASSX_TEST_DATA_DIR).append("/%1");
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
m_dbFile.reset(new TemporaryFile());
|
2020-05-11 07:31:29 -04:00
|
|
|
m_dbFile->copyFromFile(file.arg("NewDatabase.kdbx"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
m_dbFile2.reset(new TemporaryFile());
|
2020-05-11 07:31:29 -04:00
|
|
|
m_dbFile2->copyFromFile(file.arg("NewDatabase2.kdbx"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-07-25 00:09:36 -04:00
|
|
|
m_dbFileMulti.reset(new TemporaryFile());
|
|
|
|
m_dbFileMulti->copyFromFile(file.arg("NewDatabaseMulti.kdbx"));
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
m_xmlFile.reset(new TemporaryFile());
|
2020-05-11 07:31:29 -04:00
|
|
|
m_xmlFile->copyFromFile(file.arg("NewDatabase.xml"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
m_keyFileProtectedDbFile.reset(new TemporaryFile());
|
2020-05-11 07:31:29 -04:00
|
|
|
m_keyFileProtectedDbFile->copyFromFile(file.arg("KeyFileProtected.kdbx"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
m_keyFileProtectedNoPasswordDbFile.reset(new TemporaryFile());
|
2020-05-11 07:31:29 -04:00
|
|
|
m_keyFileProtectedNoPasswordDbFile->copyFromFile(file.arg("KeyFileProtectedNoPassword.kdbx"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
m_yubiKeyProtectedDbFile.reset(new TemporaryFile());
|
2020-05-11 07:31:29 -04:00
|
|
|
m_yubiKeyProtectedDbFile->copyFromFile(file.arg("YubiKeyProtectedPasswords.kdbx"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
m_stdout.reset(new QBuffer());
|
|
|
|
m_stdout->open(QIODevice::ReadWrite);
|
|
|
|
Utils::STDOUT.setDevice(m_stdout.data());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
m_stderr.reset(new QBuffer());
|
|
|
|
m_stderr->open(QIODevice::ReadWrite);
|
|
|
|
Utils::STDERR.setDevice(m_stderr.data());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
m_stdin.reset(new QBuffer());
|
|
|
|
m_stdin->open(QIODevice::ReadWrite);
|
|
|
|
Utils::STDIN.setDevice(m_stdin.data());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::cleanup()
|
|
|
|
{
|
|
|
|
m_dbFile.reset();
|
|
|
|
m_dbFile2.reset();
|
2020-07-25 00:09:36 -04:00
|
|
|
m_dbFileMulti.reset();
|
2019-10-20 18:23:11 -04:00
|
|
|
m_keyFileProtectedDbFile.reset();
|
|
|
|
m_keyFileProtectedNoPasswordDbFile.reset();
|
|
|
|
m_yubiKeyProtectedDbFile.reset();
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
Utils::STDOUT.setDevice(nullptr);
|
|
|
|
Utils::STDERR.setDevice(nullptr);
|
|
|
|
Utils::STDIN.setDevice(nullptr);
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:23:25 -05:00
|
|
|
void TestCli::cleanupTestCase()
|
|
|
|
{
|
|
|
|
m_devNull.reset();
|
|
|
|
}
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QSharedPointer<Database> TestCli::readDatabase(const QString& filename, const QString& pw, const QString& keyfile)
|
|
|
|
{
|
|
|
|
auto db = QSharedPointer<Database>::create();
|
|
|
|
auto key = QSharedPointer<CompositeKey>::create();
|
|
|
|
|
|
|
|
if (filename.isEmpty()) {
|
|
|
|
// Open the default test database
|
|
|
|
key->addKey(QSharedPointer<PasswordKey>::create("a"));
|
|
|
|
if (!db->open(m_dbFile->fileName(), key)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Open the specified database file using supplied credentials
|
|
|
|
key->addKey(QSharedPointer<PasswordKey>::create(pw));
|
|
|
|
if (!keyfile.isEmpty()) {
|
|
|
|
auto filekey = QSharedPointer<FileKey>::create();
|
|
|
|
filekey->load(keyfile);
|
|
|
|
key->addKey(filekey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!db->open(filename, key)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCli::execCmd(Command& cmd, const QStringList& args) const
|
|
|
|
{
|
|
|
|
// Move to end of stream
|
|
|
|
m_stdout->readAll();
|
|
|
|
m_stderr->readAll();
|
|
|
|
|
|
|
|
// Record stream position
|
|
|
|
auto outPos = m_stdout->pos();
|
|
|
|
auto errPos = m_stderr->pos();
|
|
|
|
|
|
|
|
// Execute command
|
|
|
|
int ret = cmd.execute(args);
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
// Move back to recorded position
|
|
|
|
m_stdout->seek(outPos);
|
|
|
|
m_stderr->seek(errPos);
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
// Skip over blank lines
|
|
|
|
QByteArray newline("\n");
|
|
|
|
while (m_stdout->peek(1) == newline) {
|
|
|
|
m_stdout->readLine();
|
|
|
|
}
|
|
|
|
while (m_stderr->peek(1) == newline) {
|
|
|
|
m_stdout->readLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
bool TestCli::isTotp(const QString& value)
|
2019-10-14 08:37:26 -04:00
|
|
|
{
|
2020-05-11 07:31:29 -04:00
|
|
|
static const QRegularExpression totp("^\\d{6}$");
|
|
|
|
return totp.match(value.trimmed()).hasMatch();
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
void TestCli::setInput(const QString& input)
|
2019-10-14 08:37:26 -04:00
|
|
|
{
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput(QStringList(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::setInput(const QStringList& input)
|
|
|
|
{
|
|
|
|
auto ba = input.join("\n").toLatin1();
|
|
|
|
// Always end in newline
|
|
|
|
if (!ba.endsWith("\n")) {
|
|
|
|
ba.append("\n");
|
|
|
|
}
|
|
|
|
auto pos = m_stdin->pos();
|
|
|
|
m_stdin->write(ba);
|
|
|
|
m_stdin->seek(pos);
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testBatchCommands()
|
|
|
|
{
|
|
|
|
Commands::setupCommands(false);
|
|
|
|
QVERIFY(Commands::getCommand("add"));
|
|
|
|
QVERIFY(Commands::getCommand("analyze"));
|
|
|
|
QVERIFY(Commands::getCommand("clip"));
|
|
|
|
QVERIFY(Commands::getCommand("close"));
|
2020-01-26 23:44:31 -05:00
|
|
|
QVERIFY(Commands::getCommand("db-create"));
|
|
|
|
QVERIFY(Commands::getCommand("db-info"));
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(Commands::getCommand("diceware"));
|
|
|
|
QVERIFY(Commands::getCommand("edit"));
|
|
|
|
QVERIFY(Commands::getCommand("estimate"));
|
|
|
|
QVERIFY(Commands::getCommand("export"));
|
|
|
|
QVERIFY(Commands::getCommand("generate"));
|
|
|
|
QVERIFY(Commands::getCommand("help"));
|
|
|
|
QVERIFY(Commands::getCommand("import"));
|
|
|
|
QVERIFY(Commands::getCommand("locate"));
|
|
|
|
QVERIFY(Commands::getCommand("ls"));
|
|
|
|
QVERIFY(Commands::getCommand("merge"));
|
|
|
|
QVERIFY(Commands::getCommand("mkdir"));
|
|
|
|
QVERIFY(Commands::getCommand("mv"));
|
|
|
|
QVERIFY(Commands::getCommand("open"));
|
|
|
|
QVERIFY(Commands::getCommand("rm"));
|
|
|
|
QVERIFY(Commands::getCommand("rmdir"));
|
|
|
|
QVERIFY(Commands::getCommand("show"));
|
|
|
|
QVERIFY(!Commands::getCommand("doesnotexist"));
|
2020-01-26 23:44:31 -05:00
|
|
|
QCOMPARE(Commands::getCommands().size(), 22);
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testInteractiveCommands()
|
|
|
|
{
|
|
|
|
Commands::setupCommands(true);
|
|
|
|
QVERIFY(Commands::getCommand("add"));
|
|
|
|
QVERIFY(Commands::getCommand("analyze"));
|
|
|
|
QVERIFY(Commands::getCommand("clip"));
|
|
|
|
QVERIFY(Commands::getCommand("close"));
|
2020-01-26 23:44:31 -05:00
|
|
|
QVERIFY(Commands::getCommand("db-create"));
|
|
|
|
QVERIFY(Commands::getCommand("db-info"));
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(Commands::getCommand("diceware"));
|
|
|
|
QVERIFY(Commands::getCommand("edit"));
|
|
|
|
QVERIFY(Commands::getCommand("estimate"));
|
|
|
|
QVERIFY(Commands::getCommand("exit"));
|
|
|
|
QVERIFY(Commands::getCommand("generate"));
|
|
|
|
QVERIFY(Commands::getCommand("help"));
|
|
|
|
QVERIFY(Commands::getCommand("locate"));
|
|
|
|
QVERIFY(Commands::getCommand("ls"));
|
|
|
|
QVERIFY(Commands::getCommand("merge"));
|
|
|
|
QVERIFY(Commands::getCommand("mkdir"));
|
|
|
|
QVERIFY(Commands::getCommand("mv"));
|
|
|
|
QVERIFY(Commands::getCommand("open"));
|
|
|
|
QVERIFY(Commands::getCommand("quit"));
|
|
|
|
QVERIFY(Commands::getCommand("rm"));
|
|
|
|
QVERIFY(Commands::getCommand("rmdir"));
|
|
|
|
QVERIFY(Commands::getCommand("show"));
|
|
|
|
QVERIFY(!Commands::getCommand("doesnotexist"));
|
2020-01-26 23:44:31 -05:00
|
|
|
QCOMPARE(Commands::getCommands().size(), 22);
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testAdd()
|
|
|
|
{
|
|
|
|
Add addCmd;
|
|
|
|
QVERIFY(!addCmd.name.isEmpty());
|
|
|
|
QVERIFY(addCmd.getDescriptionLine().contains(addCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(addCmd,
|
|
|
|
{"add",
|
|
|
|
"-u",
|
|
|
|
"newuser",
|
|
|
|
"--url",
|
|
|
|
"https://example.com/",
|
|
|
|
"-g",
|
|
|
|
"-L",
|
|
|
|
"20",
|
2020-06-22 10:46:33 -04:00
|
|
|
"-n",
|
|
|
|
"some notes",
|
2020-05-11 07:31:29 -04:00
|
|
|
m_dbFile->fileName(),
|
|
|
|
"/newuser-entry"});
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QVERIFY(m_stdout->readAll().contains("Successfully added entry newuser-entry."));
|
|
|
|
|
|
|
|
auto db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
auto* entry = db->rootGroup()->findEntryByPath("/newuser-entry");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->username(), QString("newuser"));
|
|
|
|
QCOMPARE(entry->url(), QString("https://example.com/"));
|
|
|
|
QCOMPARE(entry->password().size(), 20);
|
2020-06-22 10:46:33 -04:00
|
|
|
QCOMPARE(entry->notes(), QString("some notes"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Quiet option
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(addCmd, {"add", "-q", "-u", "newuser", "-g", "-L", "20", m_dbFile->fileName(), "/newentry-quiet"});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
entry = db->rootGroup()->findEntryByPath("/newentry-quiet");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->password().size(), 20);
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "newpassword"});
|
|
|
|
execCmd(addCmd,
|
|
|
|
{"add", "-u", "newuser2", "--url", "https://example.net/", "-p", m_dbFile->fileName(), "/newuser-entry2"});
|
|
|
|
QVERIFY(m_stdout->readAll().contains("Successfully added entry newuser-entry2."));
|
|
|
|
|
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
entry = db->rootGroup()->findEntryByPath("/newuser-entry2");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->username(), QString("newuser2"));
|
|
|
|
QCOMPARE(entry->url(), QString("https://example.net/"));
|
|
|
|
QCOMPARE(entry->password(), QString("newpassword"));
|
|
|
|
|
|
|
|
// Password generation options
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(addCmd, {"add", "-u", "newuser3", "-g", "-L", "34", m_dbFile->fileName(), "/newuser-entry3"});
|
|
|
|
QVERIFY(m_stdout->readAll().contains("Successfully added entry newuser-entry3."));
|
|
|
|
|
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
entry = db->rootGroup()->findEntryByPath("/newuser-entry3");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->username(), QString("newuser3"));
|
|
|
|
QCOMPARE(entry->password().size(), 34);
|
|
|
|
QRegularExpression defaultPasswordClassesRegex("^[a-zA-Z0-9]+$");
|
|
|
|
QVERIFY(defaultPasswordClassesRegex.match(entry->password()).hasMatch());
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(addCmd,
|
|
|
|
{"add",
|
|
|
|
"-u",
|
|
|
|
"newuser4",
|
|
|
|
"-g",
|
|
|
|
"-L",
|
|
|
|
"20",
|
|
|
|
"--every-group",
|
|
|
|
"-s",
|
|
|
|
"-n",
|
|
|
|
"-U",
|
|
|
|
"-l",
|
|
|
|
m_dbFile->fileName(),
|
|
|
|
"/newuser-entry4"});
|
|
|
|
QVERIFY(m_stdout->readAll().contains("Successfully added entry newuser-entry4."));
|
|
|
|
|
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
entry = db->rootGroup()->findEntryByPath("/newuser-entry4");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->username(), QString("newuser4"));
|
|
|
|
QCOMPARE(entry->password().size(), 20);
|
|
|
|
QVERIFY(!defaultPasswordClassesRegex.match(entry->password()).hasMatch());
|
2020-06-22 10:46:33 -04:00
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(addCmd, {"add", "-u", "newuser5", "-n", "test\\nnew line", m_dbFile->fileName(), "/newuser-entry5"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray(""));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully added entry newuser-entry5.\n"));
|
|
|
|
|
|
|
|
db = readDatabase();
|
|
|
|
entry = db->rootGroup()->findEntryByPath("/newuser-entry5");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->username(), QString("newuser5"));
|
|
|
|
QCOMPARE(entry->notes(), QString("test\nnew line"));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testAddGroup()
|
|
|
|
{
|
|
|
|
AddGroup addGroupCmd;
|
|
|
|
QVERIFY(!addGroupCmd.name.isEmpty());
|
|
|
|
QVERIFY(addGroupCmd.getDescriptionLine().contains(addGroupCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(addGroupCmd, {"mkdir", m_dbFile->fileName(), "/new_group"});
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully added group new_group.\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
auto db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
auto* group = db->rootGroup()->findGroupByPath("new_group");
|
|
|
|
QVERIFY(group);
|
|
|
|
QCOMPARE(group->name(), QString("new_group"));
|
|
|
|
|
|
|
|
// Trying to add the same group should fail.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(addGroupCmd, {"mkdir", m_dbFile->fileName(), "/new_group"});
|
|
|
|
QVERIFY(m_stderr->readAll().contains("Group /new_group already exists!"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Should be able to add groups down the tree.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(addGroupCmd, {"mkdir", m_dbFile->fileName(), "/new_group/newer_group"});
|
|
|
|
QVERIFY(m_stdout->readAll().contains("Successfully added group newer_group."));
|
|
|
|
|
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
group = db->rootGroup()->findGroupByPath("new_group/newer_group");
|
|
|
|
QVERIFY(group);
|
|
|
|
QCOMPARE(group->name(), QString("newer_group"));
|
|
|
|
|
|
|
|
// Should fail if the path is invalid.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(addGroupCmd, {"mkdir", m_dbFile->fileName(), "/invalid_group/newer_group"});
|
|
|
|
QVERIFY(m_stderr->readAll().contains("Group /invalid_group not found."));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Should fail to add the root group.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(addGroupCmd, {"mkdir", m_dbFile->fileName(), "/"});
|
|
|
|
QVERIFY(m_stderr->readAll().contains("Group / already exists!"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testAnalyze()
|
|
|
|
{
|
|
|
|
Analyze analyzeCmd;
|
|
|
|
QVERIFY(!analyzeCmd.name.isEmpty());
|
|
|
|
QVERIFY(analyzeCmd.getDescriptionLine().contains(analyzeCmd.name));
|
|
|
|
|
|
|
|
const QString hibpPath = QString(KEEPASSX_TEST_DATA_DIR).append("/hibp.txt");
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(analyzeCmd, {"analyze", "--hibp", hibpPath, m_dbFile->fileName()});
|
|
|
|
auto output = m_stdout->readAll();
|
|
|
|
QVERIFY(output.contains("Sample Entry"));
|
|
|
|
QVERIFY(output.contains("123"));
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testClip()
|
|
|
|
{
|
|
|
|
QClipboard* clipboard = QGuiApplication::clipboard();
|
|
|
|
clipboard->clear();
|
|
|
|
|
|
|
|
Clip clipCmd;
|
|
|
|
QVERIFY(!clipCmd.name.isEmpty());
|
|
|
|
QVERIFY(clipCmd.getDescriptionLine().contains(clipCmd.name));
|
|
|
|
|
|
|
|
// Password
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
QString errorOutput(m_stderr->readAll());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-04-09 08:02:13 -04:00
|
|
|
if (QProcessEnvironment::systemEnvironment().contains("WAYLAND_DISPLAY")) {
|
|
|
|
QSKIP("Clip test skipped due to QClipboard and Wayland issues");
|
|
|
|
}
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
if (errorOutput.contains("Unable to start program")
|
|
|
|
|| errorOutput.contains("No program defined for clipboard manipulation")) {
|
|
|
|
QSKIP("Clip test skipped due to missing clipboard tool");
|
|
|
|
}
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
2020-05-06 16:36:11 -04:00
|
|
|
QTRY_COMPARE(clipboard->text(), QString("Password"));
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Entry's \"Password\" attribute copied to the clipboard!\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Quiet option
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFile->fileName(), "/Sample Entry", "-q"});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2020-05-06 16:36:11 -04:00
|
|
|
QTRY_COMPARE(clipboard->text(), QString("Password"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-01-30 15:46:48 -05:00
|
|
|
// Username
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFile->fileName(), "/Sample Entry", "-a", "username"});
|
2020-05-06 16:36:11 -04:00
|
|
|
QTRY_COMPARE(clipboard->text(), QString("User Name"));
|
2020-01-30 15:46:48 -05:00
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
// TOTP
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFile->fileName(), "/Sample Entry", "--totp"});
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QTRY_VERIFY(isTotp(clipboard->text()));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Password with timeout
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
2019-10-14 08:37:26 -04:00
|
|
|
// clang-format off
|
|
|
|
QFuture<void> future = QtConcurrent::run(&clipCmd,
|
|
|
|
static_cast<int(Clip::*)(const QStringList&)>(&DatabaseCommand::execute),
|
|
|
|
QStringList{"clip", m_dbFile->fileName(), "/Sample Entry", "1"});
|
|
|
|
// clang-format on
|
|
|
|
|
2020-05-06 16:36:11 -04:00
|
|
|
QTRY_COMPARE(clipboard->text(), QString("Password"));
|
|
|
|
QTRY_COMPARE_WITH_TIMEOUT(clipboard->text(), QString(""), 2000);
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
future.waitForFinished();
|
|
|
|
|
|
|
|
// TOTP with timeout
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
2019-10-14 08:37:26 -04:00
|
|
|
future = QtConcurrent::run(&clipCmd,
|
|
|
|
static_cast<int (Clip::*)(const QStringList&)>(&DatabaseCommand::execute),
|
|
|
|
QStringList{"clip", m_dbFile->fileName(), "/Sample Entry", "1", "-t"});
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QTRY_VERIFY(isTotp(clipboard->text()));
|
2020-05-06 16:36:11 -04:00
|
|
|
QTRY_COMPARE_WITH_TIMEOUT(clipboard->text(), QString(""), 2000);
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
future.waitForFinished();
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFile->fileName(), "--totp", "/Sample Entry", "0"});
|
|
|
|
QVERIFY(m_stderr->readAll().contains("Invalid timeout value 0.\n"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFile->fileName(), "--totp", "/Sample Entry", "bleuh"});
|
|
|
|
QVERIFY(m_stderr->readAll().contains("Invalid timeout value bleuh.\n"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFile2->fileName(), "--totp", "/Sample Entry"});
|
|
|
|
QVERIFY(m_stderr->readAll().contains("Entry with path /Sample Entry has no TOTP set up.\n"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFile->fileName(), "-a", "TESTAttribute1", "/Sample Entry"});
|
2020-12-13 23:23:25 -05:00
|
|
|
QVERIFY(m_stderr->readAll().contains("ERROR: attribute TESTAttribute1 is ambiguous"));
|
2020-05-11 07:31:29 -04:00
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFile2->fileName(), "--attribute", "Username", "--totp", "/Sample Entry"});
|
|
|
|
QVERIFY(m_stderr->readAll().contains("ERROR: Please specify one of --attribute or --totp, not both.\n"));
|
2020-07-25 00:09:36 -04:00
|
|
|
|
|
|
|
// Best option
|
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFileMulti->fileName(), "Multi", "-b"});
|
|
|
|
QByteArray errorChoices = m_stderr->readAll();
|
|
|
|
QVERIFY(errorChoices.contains("Multi Entry 1"));
|
|
|
|
QVERIFY(errorChoices.contains("Multi Entry 2"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(clipCmd, {"clip", m_dbFileMulti->fileName(), "Entry 2", "-b"});
|
|
|
|
QTRY_COMPARE(clipboard->text(), QString("Password2"));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testCreate()
|
|
|
|
{
|
|
|
|
Create createCmd;
|
|
|
|
QVERIFY(!createCmd.name.isEmpty());
|
|
|
|
QVERIFY(createCmd.getDescriptionLine().contains(createCmd.name));
|
|
|
|
|
|
|
|
QScopedPointer<QTemporaryDir> testDir(new QTemporaryDir());
|
2020-03-18 21:51:36 -04:00
|
|
|
QString dbFilename;
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-03-18 21:51:36 -04:00
|
|
|
// Testing password option, password mismatch
|
|
|
|
dbFilename = testDir->path() + "/testCreate_pw.kdbx";
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "b"});
|
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p"});
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Error: Passwords do not match.\n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Failed to set database password.\n"));
|
2020-03-18 21:51:36 -04:00
|
|
|
|
|
|
|
// Testing password option
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "a"});
|
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p"});
|
|
|
|
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully created new database.\n"));
|
|
|
|
|
|
|
|
auto db = readDatabase(dbFilename, "a");
|
2020-03-18 21:51:36 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
|
|
|
|
// Testing with empty password (deny it)
|
|
|
|
dbFilename = testDir->path() + "/testCreate_blankpw.kdbx";
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"", "n"});
|
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p"});
|
|
|
|
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QVERIFY(m_stderr->readLine().contains("empty password"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Failed to set database password.\n"));
|
2020-03-18 21:51:36 -04:00
|
|
|
|
|
|
|
// Testing with empty password (accept it)
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"", "y"});
|
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p"});
|
|
|
|
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QVERIFY(m_stderr->readLine().contains("empty password"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully created new database.\n"));
|
|
|
|
|
|
|
|
db = readDatabase(dbFilename, "");
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
|
|
|
|
// Should refuse to create the database if it already exists.
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p"});
|
2019-10-14 08:37:26 -04:00
|
|
|
// Output should be empty when there is an error.
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2020-03-18 21:51:36 -04:00
|
|
|
QString errorMessage = QString("File " + dbFilename + " already exists.\n");
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stderr->readAll(), errorMessage.toUtf8());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-03-18 21:51:36 -04:00
|
|
|
// Should refuse to create without any key provided.
|
|
|
|
dbFilename = testDir->path() + "/testCreate_key.kdbx";
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(createCmd, {"db-create", dbFilename});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("No key is set. Aborting database creation.\n"));
|
2020-03-18 21:51:36 -04:00
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
// Testing with keyfile creation
|
2020-03-18 21:51:36 -04:00
|
|
|
dbFilename = testDir->path() + "/testCreate_key2.kdbx";
|
2019-10-20 18:23:11 -04:00
|
|
|
QString keyfilePath = testDir->path() + "/keyfile.txt";
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "a"});
|
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p", "-k", keyfilePath});
|
|
|
|
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully created new database.\n"));
|
|
|
|
|
|
|
|
db = readDatabase(dbFilename, "a", keyfilePath);
|
2020-03-18 21:51:36 -04:00
|
|
|
QVERIFY(db);
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Testing with existing keyfile
|
2020-03-18 21:51:36 -04:00
|
|
|
dbFilename = testDir->path() + "/testCreate_key3.kdbx";
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "a"});
|
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p", "-k", keyfilePath});
|
|
|
|
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully created new database.\n"));
|
|
|
|
|
|
|
|
db = readDatabase(dbFilename, "a", keyfilePath);
|
2020-03-18 21:51:36 -04:00
|
|
|
QVERIFY(db);
|
2020-01-06 21:00:39 -05:00
|
|
|
|
|
|
|
// Invalid decryption time (format).
|
2020-03-18 21:51:36 -04:00
|
|
|
dbFilename = testDir->path() + "/testCreate_time.kdbx";
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p", "-t", "NAN"});
|
2020-01-06 21:00:39 -05:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray("Invalid decryption time NAN.\n"));
|
2020-01-06 21:00:39 -05:00
|
|
|
|
|
|
|
// Invalid decryption time (range).
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p", "-t", "10"});
|
2020-01-06 21:00:39 -05:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QVERIFY(m_stderr->readAll().contains(QByteArray("Target decryption time must be between")));
|
2020-01-06 21:00:39 -05:00
|
|
|
|
|
|
|
int encryptionTime = 500;
|
|
|
|
// Custom encryption time
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "a"});
|
2020-01-06 21:00:39 -05:00
|
|
|
int epochBefore = QDateTime::currentMSecsSinceEpoch();
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(createCmd, {"db-create", dbFilename, "-p", "-t", QString::number(encryptionTime)});
|
2020-01-06 21:00:39 -05:00
|
|
|
// Removing 100ms to make sure we account for changes in computation time.
|
|
|
|
QVERIFY(QDateTime::currentMSecsSinceEpoch() > (epochBefore + encryptionTime - 100));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Benchmarking key derivation function for 500ms delay.\n"));
|
|
|
|
QVERIFY(m_stdout->readLine().contains(QByteArray("rounds for key derivation function.\n")));
|
2020-01-06 21:00:39 -05:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
db = readDatabase(dbFilename, "a");
|
2020-03-18 21:51:36 -04:00
|
|
|
QVERIFY(db);
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
2020-01-26 23:44:31 -05:00
|
|
|
void TestCli::testInfo()
|
|
|
|
{
|
|
|
|
Info infoCmd;
|
|
|
|
QVERIFY(!infoCmd.name.isEmpty());
|
|
|
|
QVERIFY(infoCmd.getDescriptionLine().contains(infoCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(infoCmd, {"db-info", m_dbFile->fileName()});
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QVERIFY(m_stdout->readLine().contains(QByteArray("UUID: ")));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Name: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Description: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Cipher: AES 256-bit\n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("KDF: AES (6000 rounds)\n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Recycle bin is enabled.\n"));
|
2020-01-26 23:44:31 -05:00
|
|
|
|
|
|
|
// Test with quiet option.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(infoCmd, {"db-info", "-q", m_dbFile->fileName()});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QVERIFY(m_stdout->readLine().contains(QByteArray("UUID: ")));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Name: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Description: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Cipher: AES 256-bit\n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("KDF: AES (6000 rounds)\n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Recycle bin is enabled.\n"));
|
2020-01-26 23:44:31 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
void TestCli::testDiceware()
|
|
|
|
{
|
|
|
|
Diceware dicewareCmd;
|
|
|
|
QVERIFY(!dicewareCmd.name.isEmpty());
|
|
|
|
QVERIFY(dicewareCmd.getDescriptionLine().contains(dicewareCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(dicewareCmd, {"diceware"});
|
|
|
|
QString passphrase(m_stdout->readLine());
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(!passphrase.isEmpty());
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(dicewareCmd, {"diceware", "-W", "2"});
|
|
|
|
passphrase = m_stdout->readLine();
|
2019-10-14 08:37:26 -04:00
|
|
|
QCOMPARE(passphrase.split(" ").size(), 2);
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(dicewareCmd, {"diceware", "-W", "10"});
|
|
|
|
passphrase = m_stdout->readLine();
|
2019-10-14 08:37:26 -04:00
|
|
|
QCOMPARE(passphrase.split(" ").size(), 10);
|
|
|
|
|
|
|
|
// Testing with invalid word count
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(dicewareCmd, {"diceware", "-W", "-10"});
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Invalid word count -10\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Testing with invalid word count format
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(dicewareCmd, {"diceware", "-W", "bleuh"});
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Invalid word count bleuh\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
TemporaryFile wordFile;
|
|
|
|
wordFile.open();
|
|
|
|
for (int i = 0; i < 4500; ++i) {
|
|
|
|
wordFile.write(QString("word" + QString::number(i) + "\n").toLatin1());
|
|
|
|
}
|
|
|
|
wordFile.close();
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(dicewareCmd, {"diceware", "-W", "11", "-w", wordFile.fileName()});
|
|
|
|
passphrase = m_stdout->readLine();
|
2019-10-14 08:37:26 -04:00
|
|
|
const auto words = passphrase.split(" ");
|
|
|
|
QCOMPARE(words.size(), 11);
|
|
|
|
QRegularExpression regex("^word\\d+$");
|
|
|
|
for (const auto& word : words) {
|
|
|
|
QVERIFY2(regex.match(word).hasMatch(), qPrintable("Word " + word + " was not on the word list"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryFile smallWordFile;
|
|
|
|
smallWordFile.open();
|
|
|
|
for (int i = 0; i < 50; ++i) {
|
|
|
|
smallWordFile.write(QString("word" + QString::number(i) + "\n").toLatin1());
|
|
|
|
}
|
|
|
|
smallWordFile.close();
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(dicewareCmd, {"diceware", "-W", "11", "-w", smallWordFile.fileName()});
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("The word list is too small (< 1000 items)\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testEdit()
|
|
|
|
{
|
|
|
|
Edit editCmd;
|
|
|
|
QVERIFY(!editCmd.name.isEmpty());
|
|
|
|
QVERIFY(editCmd.getDescriptionLine().contains(editCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(editCmd,
|
|
|
|
{"edit",
|
|
|
|
"-u",
|
|
|
|
"newuser",
|
|
|
|
"--url",
|
|
|
|
"https://otherurl.example.com/",
|
2020-06-22 10:46:33 -04:00
|
|
|
"-n",
|
|
|
|
"newnotes",
|
2020-05-11 07:31:29 -04:00
|
|
|
"-t",
|
|
|
|
"newtitle",
|
|
|
|
m_dbFile->fileName(),
|
|
|
|
"/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully edited entry newtitle.\n"));
|
|
|
|
|
|
|
|
auto db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
auto* entry = db->rootGroup()->findEntryByPath("/newtitle");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->username(), QString("newuser"));
|
|
|
|
QCOMPARE(entry->url(), QString("https://otherurl.example.com/"));
|
|
|
|
QCOMPARE(entry->password(), QString("Password"));
|
2020-06-22 10:46:33 -04:00
|
|
|
QCOMPARE(entry->notes(), QString("newnotes"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Quiet option
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(editCmd, {"edit", m_dbFile->fileName(), "-q", "-t", "newertitle", "/newtitle"});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(editCmd, {"edit", "-g", m_dbFile->fileName(), "/newertitle"});
|
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
entry = db->rootGroup()->findEntryByPath("/newertitle");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->username(), QString("newuser"));
|
|
|
|
QCOMPARE(entry->url(), QString("https://otherurl.example.com/"));
|
|
|
|
QVERIFY(!entry->password().isEmpty());
|
|
|
|
QVERIFY(entry->password() != QString("Password"));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(editCmd, {"edit", "-g", "-L", "34", "-t", "evennewertitle", m_dbFile->fileName(), "/newertitle"});
|
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
entry = db->rootGroup()->findEntryByPath("/evennewertitle");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->username(), QString("newuser"));
|
|
|
|
QCOMPARE(entry->url(), QString("https://otherurl.example.com/"));
|
|
|
|
QVERIFY(entry->password() != QString("Password"));
|
|
|
|
QCOMPARE(entry->password().size(), 34);
|
|
|
|
QRegularExpression defaultPasswordClassesRegex("^[a-zA-Z0-9]+$");
|
|
|
|
QVERIFY(defaultPasswordClassesRegex.match(entry->password()).hasMatch());
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(editCmd,
|
|
|
|
{"edit",
|
|
|
|
"-g",
|
|
|
|
"-L",
|
|
|
|
"20",
|
|
|
|
"--every-group",
|
|
|
|
"-s",
|
|
|
|
"-n",
|
|
|
|
"--upper",
|
|
|
|
"-l",
|
|
|
|
m_dbFile->fileName(),
|
|
|
|
"/evennewertitle"});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully edited entry evennewertitle.\n"));
|
|
|
|
|
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
entry = db->rootGroup()->findEntryByPath("/evennewertitle");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->password().size(), 20);
|
|
|
|
QVERIFY(!defaultPasswordClassesRegex.match(entry->password()).hasMatch());
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "newpassword"});
|
|
|
|
execCmd(editCmd, {"edit", "-p", m_dbFile->fileName(), "/evennewertitle"});
|
|
|
|
db = readDatabase();
|
|
|
|
QVERIFY(db);
|
2019-10-14 08:37:26 -04:00
|
|
|
entry = db->rootGroup()->findEntryByPath("/evennewertitle");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->password(), QString("newpassword"));
|
2020-06-22 10:46:33 -04:00
|
|
|
|
|
|
|
// with line break in notes
|
|
|
|
setInput("a");
|
|
|
|
execCmd(editCmd, {"edit", m_dbFile->fileName(), "-n", "testing\\nline breaks", "/evennewertitle"});
|
|
|
|
db = readDatabase();
|
|
|
|
entry = db->rootGroup()->findEntryByPath("/evennewertitle");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->notes(), QString("testing\nline breaks"));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testEstimate_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("input");
|
|
|
|
QTest::addColumn<QString>("length");
|
|
|
|
QTest::addColumn<QString>("entropy");
|
|
|
|
QTest::addColumn<QString>("log10");
|
|
|
|
QTest::addColumn<QStringList>("searchStrings");
|
|
|
|
|
|
|
|
QTest::newRow("Dictionary") << "password"
|
|
|
|
<< "8"
|
|
|
|
<< "1.0"
|
|
|
|
<< "0.3" << QStringList{"Type: Dictionary", "\tpassword"};
|
|
|
|
|
|
|
|
QTest::newRow("Spatial") << "zxcv"
|
|
|
|
<< "4"
|
|
|
|
<< "10.3"
|
|
|
|
<< "3.1" << QStringList{"Type: Spatial", "\tzxcv"};
|
|
|
|
|
|
|
|
QTest::newRow("Spatial(Rep)") << "sdfgsdfg"
|
|
|
|
<< "8"
|
|
|
|
<< "11.3"
|
|
|
|
<< "3.4" << QStringList{"Type: Spatial(Rep)", "\tsdfgsdfg"};
|
|
|
|
|
|
|
|
QTest::newRow("Dictionary / Sequence")
|
|
|
|
<< "password123"
|
|
|
|
<< "11"
|
|
|
|
<< "4.5"
|
|
|
|
<< "1.3" << QStringList{"Type: Dictionary", "Type: Sequence", "\tpassword", "\t123"};
|
|
|
|
|
|
|
|
QTest::newRow("Dict+Leet") << "p455w0rd"
|
|
|
|
<< "8"
|
|
|
|
<< "2.5"
|
|
|
|
<< "0.7" << QStringList{"Type: Dict+Leet", "\tp455w0rd"};
|
|
|
|
|
|
|
|
QTest::newRow("Dictionary(Rep)") << "hellohello"
|
|
|
|
<< "10"
|
|
|
|
<< "7.3"
|
|
|
|
<< "2.2" << QStringList{"Type: Dictionary(Rep)", "\thellohello"};
|
|
|
|
|
|
|
|
QTest::newRow("Sequence(Rep) / Dictionary")
|
|
|
|
<< "456456foobar"
|
|
|
|
<< "12"
|
|
|
|
<< "16.7"
|
|
|
|
<< "5.0" << QStringList{"Type: Sequence(Rep)", "Type: Dictionary", "\t456456", "\tfoobar"};
|
|
|
|
|
|
|
|
QTest::newRow("Bruteforce(Rep) / Bruteforce")
|
|
|
|
<< "xzxzy"
|
|
|
|
<< "5"
|
|
|
|
<< "16.1"
|
|
|
|
<< "4.8" << QStringList{"Type: Bruteforce(Rep)", "Type: Bruteforce", "\txzxz", "\ty"};
|
|
|
|
|
|
|
|
QTest::newRow("Dictionary / Date(Rep)")
|
|
|
|
<< "pass20182018"
|
|
|
|
<< "12"
|
|
|
|
<< "15.1"
|
|
|
|
<< "4.56" << QStringList{"Type: Dictionary", "Type: Date(Rep)", "\tpass", "\t20182018"};
|
|
|
|
|
|
|
|
QTest::newRow("Dictionary / Date / Bruteforce")
|
|
|
|
<< "mypass2018-2"
|
|
|
|
<< "12"
|
|
|
|
<< "32.9"
|
|
|
|
<< "9.9" << QStringList{"Type: Dictionary", "Type: Date", "Type: Bruteforce", "\tmypass", "\t2018", "\t-2"};
|
|
|
|
|
|
|
|
QTest::newRow("Strong Password") << "E*!%.Qw{t.X,&bafw)\"Q!ah$%;U/"
|
|
|
|
<< "28"
|
|
|
|
<< "165.7"
|
|
|
|
<< "49.8" << QStringList{"Type: Bruteforce", "\tE*"};
|
|
|
|
|
|
|
|
// TODO: detect passphrases and adjust entropy calculation accordingly (issue #2347)
|
|
|
|
QTest::newRow("Strong Passphrase")
|
|
|
|
<< "squint wooing resupply dangle isolation axis headsman"
|
|
|
|
<< "53"
|
|
|
|
<< "151.2"
|
|
|
|
<< "45.5"
|
|
|
|
<< QStringList{
|
|
|
|
"Type: Dictionary", "Type: Bruteforce", "Multi-word extra bits 22.0", "\tsquint", "\t ", "\twooing"};
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testEstimate()
|
|
|
|
{
|
|
|
|
QFETCH(QString, input);
|
|
|
|
QFETCH(QString, length);
|
|
|
|
QFETCH(QString, entropy);
|
|
|
|
QFETCH(QString, log10);
|
|
|
|
QFETCH(QStringList, searchStrings);
|
|
|
|
|
|
|
|
Estimate estimateCmd;
|
|
|
|
QVERIFY(!estimateCmd.name.isEmpty());
|
|
|
|
QVERIFY(estimateCmd.getDescriptionLine().contains(estimateCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput(input);
|
|
|
|
execCmd(estimateCmd, {"estimate", "-a"});
|
|
|
|
auto result = QString(m_stdout->readAll());
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(result.contains("Length " + length));
|
|
|
|
QVERIFY(result.contains("Entropy " + entropy));
|
|
|
|
QVERIFY(result.contains("Log10 " + log10));
|
|
|
|
for (const auto& string : asConst(searchStrings)) {
|
|
|
|
QVERIFY2(result.contains(string), qPrintable("String " + string + " missing"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testExport()
|
|
|
|
{
|
|
|
|
Export exportCmd;
|
|
|
|
QVERIFY(!exportCmd.name.isEmpty());
|
|
|
|
QVERIFY(exportCmd.getDescriptionLine().contains(exportCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(exportCmd, {"export", m_dbFile->fileName()});
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
TemporaryFile xmlOutput;
|
|
|
|
xmlOutput.open(QIODevice::WriteOnly);
|
|
|
|
xmlOutput.write(m_stdout->readAll());
|
|
|
|
xmlOutput.close();
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
QScopedPointer<Database> db(new Database());
|
2020-05-11 07:31:29 -04:00
|
|
|
QVERIFY(db->import(xmlOutput.fileName()));
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
auto* entry = db->rootGroup()->findEntryByPath("/Sample Entry");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->password(), QString("Password"));
|
|
|
|
|
|
|
|
// Quiet option
|
|
|
|
QScopedPointer<Database> dbQuiet(new Database());
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(exportCmd, {"export", "-f", "xml", "-q", m_dbFile->fileName()});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
|
|
|
|
xmlOutput.open(QIODevice::WriteOnly);
|
|
|
|
xmlOutput.write(m_stdout->readAll());
|
|
|
|
xmlOutput.close();
|
|
|
|
|
|
|
|
QVERIFY(db->import(xmlOutput.fileName()));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// CSV exporting
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(exportCmd, {"export", "-f", "csv", m_dbFile->fileName()});
|
|
|
|
QByteArray csvHeader = m_stdout->readLine();
|
2020-08-26 15:17:31 -04:00
|
|
|
QVERIFY(csvHeader.contains(QByteArray("\"Group\",\"Title\",\"Username\",\"Password\",\"URL\",\"Notes\"")));
|
2020-05-11 07:31:29 -04:00
|
|
|
QByteArray csvData = m_stdout->readAll();
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(csvData.contains(QByteArray(
|
2020-08-26 15:17:31 -04:00
|
|
|
"\"NewDatabase\",\"Sample Entry\",\"User Name\",\"Password\",\"http://www.somesite.com/\",\"Notes\"")));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// test invalid format
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(exportCmd, {"export", "-f", "yaml", m_dbFile->fileName()});
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Unsupported format yaml\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testGenerate_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QStringList>("parameters");
|
|
|
|
QTest::addColumn<QString>("pattern");
|
|
|
|
|
|
|
|
QTest::newRow("default") << QStringList{"generate"} << "^[^\r\n]+$";
|
|
|
|
QTest::newRow("length") << QStringList{"generate", "-L", "13"} << "^.{13}$";
|
|
|
|
QTest::newRow("lowercase") << QStringList{"generate", "-L", "14", "-l"} << "^[a-z]{14}$";
|
|
|
|
QTest::newRow("uppercase") << QStringList{"generate", "-L", "15", "--upper"} << "^[A-Z]{15}$";
|
|
|
|
QTest::newRow("numbers") << QStringList{"generate", "-L", "16", "-n"} << "^[0-9]{16}$";
|
|
|
|
QTest::newRow("special") << QStringList{"generate", "-L", "200", "-s"}
|
|
|
|
<< R"(^[\(\)\[\]\{\}\.\-*|\\,:;"'\/\_!+-<=>?#$%&^`@~]{200}$)";
|
|
|
|
QTest::newRow("special (exclude)") << QStringList{"generate", "-L", "200", "-s", "-x", "+.?@&"}
|
|
|
|
<< R"(^[\(\)\[\]\{\}\.\-*|\\,:;"'\/\_!-<=>#$%^`~]{200}$)";
|
|
|
|
QTest::newRow("extended") << QStringList{"generate", "-L", "50", "-e"}
|
|
|
|
<< R"(^[^a-zA-Z0-9\(\)\[\]\{\}\.\-\*\|\\,:;"'\/\_!+-<=>?#$%&^`@~]{50}$)";
|
|
|
|
QTest::newRow("numbers + lowercase + uppercase")
|
|
|
|
<< QStringList{"generate", "-L", "16", "-n", "--upper", "-l"} << "^[0-9a-zA-Z]{16}$";
|
|
|
|
QTest::newRow("numbers + lowercase + uppercase (exclude)")
|
|
|
|
<< QStringList{"generate", "-L", "500", "-n", "-U", "-l", "-x", "abcdefg0123@"} << "^[^abcdefg0123@]{500}$";
|
|
|
|
QTest::newRow("numbers + lowercase + uppercase (exclude similar)")
|
|
|
|
<< QStringList{"generate", "-L", "200", "-n", "-U", "-l", "--exclude-similar"} << "^[^l1IO0]{200}$";
|
|
|
|
QTest::newRow("uppercase + lowercase (every)")
|
|
|
|
<< QStringList{"generate", "-L", "2", "--upper", "-l", "--every-group"} << "^[a-z][A-Z]|[A-Z][a-z]$";
|
|
|
|
QTest::newRow("numbers + lowercase (every)")
|
|
|
|
<< QStringList{"generate", "-L", "2", "-n", "-l", "--every-group"} << "^[a-z][0-9]|[0-9][a-z]$";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testGenerate()
|
|
|
|
{
|
|
|
|
QFETCH(QStringList, parameters);
|
|
|
|
QFETCH(QString, pattern);
|
|
|
|
|
|
|
|
Generate generateCmd;
|
|
|
|
QVERIFY(!generateCmd.name.isEmpty());
|
|
|
|
QVERIFY(generateCmd.getDescriptionLine().contains(generateCmd.name));
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(generateCmd, parameters);
|
2019-10-14 08:37:26 -04:00
|
|
|
QRegularExpression regex(pattern);
|
2020-05-11 07:31:29 -04:00
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
QString password = QString::fromUtf8(m_stdout->readLine());
|
|
|
|
#else
|
|
|
|
QString password = QString::fromLatin1(m_stdout->readLine());
|
|
|
|
#endif
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY2(regex.match(password).hasMatch(),
|
|
|
|
qPrintable("Password " + password + " does not match pattern " + pattern));
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Testing with invalid password length
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(generateCmd, {"generate", "-L", "-10"});
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Invalid password length -10\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(generateCmd, {"generate", "-L", "0"});
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Invalid password length 0\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Testing with invalid word count format
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(generateCmd, {"generate", "-L", "bleuh"});
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Invalid password length bleuh\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testImport()
|
|
|
|
{
|
|
|
|
Import importCmd;
|
|
|
|
QVERIFY(!importCmd.name.isEmpty());
|
|
|
|
QVERIFY(importCmd.getDescriptionLine().contains(importCmd.name));
|
|
|
|
|
|
|
|
QScopedPointer<QTemporaryDir> testDir(new QTemporaryDir());
|
2020-05-11 07:31:29 -04:00
|
|
|
QString databaseFilename = testDir->path() + "/testImport1.kdbx";
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "a"});
|
2020-10-09 20:31:29 -04:00
|
|
|
execCmd(importCmd, {"import", m_xmlFile->fileName(), databaseFilename, "-p"});
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully imported database.\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
auto db = readDatabase(databaseFilename, "a");
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
auto* entry = db->rootGroup()->findEntryByPath("/Sample Entry 1");
|
|
|
|
QVERIFY(entry);
|
|
|
|
QCOMPARE(entry->username(), QString("User Name"));
|
|
|
|
|
|
|
|
// Should refuse to create the database if it already exists.
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(importCmd, {"import", m_xmlFile->fileName(), databaseFilename});
|
2019-10-14 08:37:26 -04:00
|
|
|
// Output should be empty when there is an error.
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
QString errorMessage = QString("File " + databaseFilename + " already exists.\n");
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stderr->readAll(), errorMessage.toUtf8());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-10-09 20:31:29 -04:00
|
|
|
// Testing import with non-existing keyfile
|
|
|
|
databaseFilename = testDir->path() + "/testImport2.kdbx";
|
|
|
|
QString keyfilePath = testDir->path() + "/keyfile.txt";
|
|
|
|
setInput({"a", "a"});
|
|
|
|
execCmd(importCmd, {"import", "-p", "-k", keyfilePath, m_xmlFile->fileName(), databaseFilename});
|
|
|
|
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully imported database.\n"));
|
|
|
|
|
|
|
|
db = readDatabase(databaseFilename, "a", keyfilePath);
|
|
|
|
QVERIFY(db);
|
|
|
|
|
|
|
|
// Testing import with existing keyfile
|
|
|
|
databaseFilename = testDir->path() + "/testImport3.kdbx";
|
|
|
|
setInput({"a", "a"});
|
|
|
|
execCmd(importCmd, {"import", "-p", "-k", keyfilePath, m_xmlFile->fileName(), databaseFilename});
|
|
|
|
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully imported database.\n"));
|
|
|
|
|
|
|
|
db = readDatabase(databaseFilename, "a", keyfilePath);
|
|
|
|
QVERIFY(db);
|
|
|
|
|
|
|
|
// Invalid decryption time (format).
|
|
|
|
databaseFilename = testDir->path() + "/testCreate_time.kdbx";
|
|
|
|
execCmd(importCmd, {"import", "-p", "-t", "NAN", m_xmlFile->fileName(), databaseFilename});
|
|
|
|
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray("Invalid decryption time NAN.\n"));
|
|
|
|
|
|
|
|
// Invalid decryption time (range).
|
|
|
|
execCmd(importCmd, {"import", "-p", "-t", "10", m_xmlFile->fileName(), databaseFilename});
|
|
|
|
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QVERIFY(m_stderr->readAll().contains(QByteArray("Target decryption time must be between")));
|
|
|
|
|
|
|
|
int encryptionTime = 500;
|
|
|
|
// Custom encryption time
|
|
|
|
setInput({"a", "a"});
|
|
|
|
int epochBefore = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
execCmd(importCmd,
|
|
|
|
{"import", "-p", "-t", QString::number(encryptionTime), m_xmlFile->fileName(), databaseFilename});
|
|
|
|
// Removing 100ms to make sure we account for changes in computation time.
|
|
|
|
QVERIFY(QDateTime::currentMSecsSinceEpoch() > (epochBefore + encryptionTime - 100));
|
|
|
|
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Benchmarking key derivation function for 500ms delay.\n"));
|
|
|
|
QVERIFY(m_stdout->readLine().contains(QByteArray("rounds for key derivation function.\n")));
|
|
|
|
|
|
|
|
db = readDatabase(databaseFilename, "a");
|
|
|
|
QVERIFY(db);
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
// Quiet option
|
|
|
|
QScopedPointer<QTemporaryDir> testDirQuiet(new QTemporaryDir());
|
2020-05-11 07:31:29 -04:00
|
|
|
QString databaseFilenameQuiet = testDirQuiet->path() + "/testImport2.kdbx";
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "a"});
|
2020-10-09 20:31:29 -04:00
|
|
|
execCmd(importCmd, {"import", "-p", "-q", m_xmlFile->fileName(), databaseFilenameQuiet});
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Enter password to encrypt database (optional): \n"));
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Repeat password: \n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
db = readDatabase(databaseFilenameQuiet, "a");
|
|
|
|
QVERIFY(db);
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testKeyFileOption()
|
|
|
|
{
|
|
|
|
List listCmd;
|
|
|
|
|
|
|
|
QString keyFilePath(QString(KEEPASSX_TEST_DATA_DIR).append("/KeyFileProtected.key"));
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", "-k", keyFilePath, m_keyFileProtectedDbFile->fileName()});
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("entry1\n"
|
|
|
|
"entry2\n"));
|
|
|
|
|
|
|
|
// Should raise an error with no key file.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", m_keyFileProtectedDbFile->fileName()});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QVERIFY(m_stderr->readAll().contains("Invalid credentials were provided"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Should raise an error if key file path is invalid.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", "-k", "invalidpath", m_keyFileProtectedDbFile->fileName()});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stderr->readAll().split(':').at(0), QByteArray("Failed to load key file invalidpath"));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testNoPasswordOption()
|
|
|
|
{
|
|
|
|
List listCmd;
|
|
|
|
|
|
|
|
QString keyFilePath(QString(KEEPASSX_TEST_DATA_DIR).append("/KeyFileProtectedNoPassword.key"));
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(listCmd, {"ls", "-k", keyFilePath, "--no-password", m_keyFileProtectedNoPasswordDbFile->fileName()});
|
|
|
|
// Expecting no password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("entry1\n"
|
|
|
|
"entry2\n"));
|
|
|
|
|
|
|
|
// Should raise an error with no key file.
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(listCmd, {"ls", "--no-password", m_keyFileProtectedNoPasswordDbFile->fileName()});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QVERIFY(m_stderr->readAll().contains("Invalid credentials were provided"));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testList()
|
|
|
|
{
|
|
|
|
List listCmd;
|
|
|
|
QVERIFY(!listCmd.name.isEmpty());
|
|
|
|
QVERIFY(listCmd.getDescriptionLine().contains(listCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", m_dbFile->fileName()});
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("Sample Entry\n"
|
|
|
|
"General/\n"
|
|
|
|
"Windows/\n"
|
|
|
|
"Network/\n"
|
|
|
|
"Internet/\n"
|
|
|
|
"eMail/\n"
|
|
|
|
"Homebanking/\n"));
|
|
|
|
|
|
|
|
// Quiet option
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", "-q", m_dbFile->fileName()});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("Sample Entry\n"
|
|
|
|
"General/\n"
|
|
|
|
"Windows/\n"
|
|
|
|
"Network/\n"
|
|
|
|
"Internet/\n"
|
|
|
|
"eMail/\n"
|
|
|
|
"Homebanking/\n"));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", "-R", m_dbFile->fileName()});
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("Sample Entry\n"
|
|
|
|
"General/\n"
|
|
|
|
" [empty]\n"
|
|
|
|
"Windows/\n"
|
|
|
|
" [empty]\n"
|
|
|
|
"Network/\n"
|
|
|
|
" [empty]\n"
|
|
|
|
"Internet/\n"
|
|
|
|
" [empty]\n"
|
|
|
|
"eMail/\n"
|
|
|
|
" [empty]\n"
|
|
|
|
"Homebanking/\n"
|
|
|
|
" Subgroup/\n"
|
|
|
|
" Subgroup Entry\n"));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", "-R", "-f", m_dbFile->fileName()});
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("Sample Entry\n"
|
|
|
|
"General/\n"
|
|
|
|
"General/[empty]\n"
|
|
|
|
"Windows/\n"
|
|
|
|
"Windows/[empty]\n"
|
|
|
|
"Network/\n"
|
|
|
|
"Network/[empty]\n"
|
|
|
|
"Internet/\n"
|
|
|
|
"Internet/[empty]\n"
|
|
|
|
"eMail/\n"
|
|
|
|
"eMail/[empty]\n"
|
|
|
|
"Homebanking/\n"
|
|
|
|
"Homebanking/Subgroup/\n"
|
|
|
|
"Homebanking/Subgroup/Subgroup Entry\n"));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", "-R", "-f", m_dbFile->fileName(), "/Homebanking"});
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("Subgroup/\n"
|
|
|
|
"Subgroup/Subgroup Entry\n"));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", m_dbFile->fileName(), "/General/"});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("[empty]\n"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", m_dbFile->fileName(), "/DoesNotExist/"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray("Cannot find group /DoesNotExist/.\n"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testLocate()
|
|
|
|
{
|
|
|
|
Locate locateCmd;
|
|
|
|
QVERIFY(!locateCmd.name.isEmpty());
|
|
|
|
QVERIFY(locateCmd.getDescriptionLine().contains(locateCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(locateCmd, {"locate", m_dbFile->fileName(), "Sample"});
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("/Sample Entry\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Quiet option
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(locateCmd, {"locate", m_dbFile->fileName(), "-q", "Sample"});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("/Sample Entry\n"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(locateCmd, {"locate", m_dbFile->fileName(), "Does Not Exist"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray("No results for that search term.\n"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// write a modified database
|
2020-05-11 07:31:29 -04:00
|
|
|
auto db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
auto* group = db->rootGroup()->findGroupByPath("/General/");
|
|
|
|
QVERIFY(group);
|
|
|
|
auto* entry = new Entry();
|
|
|
|
entry->setUuid(QUuid::createUuid());
|
|
|
|
entry->setTitle("New Entry");
|
|
|
|
group->addEntry(entry);
|
2020-05-11 07:31:29 -04:00
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
TemporaryFile tmpFile;
|
|
|
|
tmpFile.open();
|
|
|
|
tmpFile.close();
|
2020-05-11 07:31:29 -04:00
|
|
|
db->saveAs(tmpFile.fileName());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(locateCmd, {"locate", tmpFile.fileName(), "New"});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("/General/New Entry\n"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(locateCmd, {"locate", tmpFile.fileName(), "Entry"});
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("/Sample Entry\n/General/New Entry\n/Homebanking/Subgroup/Subgroup Entry\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testMerge()
|
|
|
|
{
|
|
|
|
Merge mergeCmd;
|
|
|
|
QVERIFY(!mergeCmd.name.isEmpty());
|
|
|
|
QVERIFY(mergeCmd.getDescriptionLine().contains(mergeCmd.name));
|
|
|
|
|
|
|
|
// load test database and save copies
|
2020-05-11 07:31:29 -04:00
|
|
|
auto db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
TemporaryFile targetFile1;
|
|
|
|
targetFile1.open();
|
|
|
|
targetFile1.close();
|
2020-05-11 07:31:29 -04:00
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
TemporaryFile targetFile2;
|
|
|
|
targetFile2.open();
|
|
|
|
targetFile2.close();
|
|
|
|
|
|
|
|
TemporaryFile targetFile3;
|
|
|
|
targetFile3.open();
|
2020-05-11 07:31:29 -04:00
|
|
|
targetFile3.close();
|
|
|
|
|
|
|
|
db->saveAs(targetFile1.fileName());
|
|
|
|
db->saveAs(targetFile2.fileName());
|
|
|
|
|
|
|
|
// save another copy with a different password
|
2019-10-14 08:37:26 -04:00
|
|
|
auto oldKey = db->key();
|
|
|
|
auto key = QSharedPointer<CompositeKey>::create();
|
|
|
|
key->addKey(QSharedPointer<PasswordKey>::create("b"));
|
|
|
|
db->setKey(key);
|
2020-05-11 07:31:29 -04:00
|
|
|
db->saveAs(targetFile3.fileName());
|
|
|
|
|
|
|
|
// Restore the original password
|
2019-10-14 08:37:26 -04:00
|
|
|
db->setKey(oldKey);
|
|
|
|
|
|
|
|
// then add a new entry to the in-memory database and save another copy
|
|
|
|
auto* entry = new Entry();
|
|
|
|
entry->setUuid(QUuid::createUuid());
|
|
|
|
entry->setTitle("Some Website");
|
|
|
|
entry->setPassword("secretsecretsecret");
|
|
|
|
auto* group = db->rootGroup()->findGroupByPath("/Internet/");
|
|
|
|
QVERIFY(group);
|
|
|
|
group->addEntry(entry);
|
2020-05-11 07:31:29 -04:00
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
TemporaryFile sourceFile;
|
|
|
|
sourceFile.open();
|
|
|
|
sourceFile.close();
|
2020-05-11 07:31:29 -04:00
|
|
|
db->saveAs(sourceFile.fileName());
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(mergeCmd, {"merge", "-s", targetFile1.fileName(), sourceFile.fileName()});
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QList<QByteArray> outLines1 = m_stdout->readAll().split('\n');
|
|
|
|
QVERIFY(outLines1.at(0).contains("Overwriting Internet"));
|
|
|
|
QVERIFY(outLines1.at(1).contains("Creating missing Some Website"));
|
2019-10-14 08:37:26 -04:00
|
|
|
QCOMPARE(outLines1.at(2),
|
|
|
|
QString("Successfully merged %1 into %2.").arg(sourceFile.fileName(), targetFile1.fileName()).toUtf8());
|
|
|
|
|
|
|
|
auto mergedDb = QSharedPointer<Database>::create();
|
2020-05-11 07:31:29 -04:00
|
|
|
QVERIFY(mergedDb->open(targetFile1.fileName(), oldKey));
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
auto* entry1 = mergedDb->rootGroup()->findEntryByPath("/Internet/Some Website");
|
|
|
|
QVERIFY(entry1);
|
|
|
|
QCOMPARE(entry1->title(), QString("Some Website"));
|
|
|
|
QCOMPARE(entry1->password(), QString("secretsecretsecret"));
|
|
|
|
|
|
|
|
// the dry run option should not modify the target database.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(mergeCmd, {"merge", "--dry-run", "-s", targetFile2.fileName(), sourceFile.fileName()});
|
|
|
|
QList<QByteArray> outLines2 = m_stdout->readAll().split('\n');
|
|
|
|
QVERIFY(outLines2.at(0).contains("Overwriting Internet"));
|
|
|
|
QVERIFY(outLines2.at(1).contains("Creating missing Some Website"));
|
2019-10-14 08:37:26 -04:00
|
|
|
QCOMPARE(outLines2.at(2), QByteArray("Database was not modified by merge operation."));
|
|
|
|
|
|
|
|
mergedDb = QSharedPointer<Database>::create();
|
2020-05-11 07:31:29 -04:00
|
|
|
QVERIFY(mergedDb->open(targetFile2.fileName(), oldKey));
|
2019-10-14 08:37:26 -04:00
|
|
|
entry1 = mergedDb->rootGroup()->findEntryByPath("/Internet/Some Website");
|
|
|
|
QVERIFY(!entry1);
|
|
|
|
|
|
|
|
// the dry run option can be used with the quiet option
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(mergeCmd, {"merge", "--dry-run", "-s", "-q", targetFile2.fileName(), sourceFile.fileName()});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
mergedDb = QSharedPointer<Database>::create();
|
2020-05-11 07:31:29 -04:00
|
|
|
QVERIFY(mergedDb->open(targetFile2.fileName(), oldKey));
|
2019-10-14 08:37:26 -04:00
|
|
|
entry1 = mergedDb->rootGroup()->findEntryByPath("/Internet/Some Website");
|
|
|
|
QVERIFY(!entry1);
|
|
|
|
|
|
|
|
// try again with different passwords for both files
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"b", "a"});
|
|
|
|
execCmd(mergeCmd, {"merge", targetFile3.fileName(), sourceFile.fileName()});
|
|
|
|
QList<QByteArray> outLines3 = m_stdout->readAll().split('\n');
|
2019-10-14 08:37:26 -04:00
|
|
|
QCOMPARE(outLines3.at(2),
|
|
|
|
QString("Successfully merged %1 into %2.").arg(sourceFile.fileName(), targetFile3.fileName()).toUtf8());
|
|
|
|
|
|
|
|
mergedDb = QSharedPointer<Database>::create();
|
2020-05-11 07:31:29 -04:00
|
|
|
QVERIFY(mergedDb->open(targetFile3.fileName(), key));
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
entry1 = mergedDb->rootGroup()->findEntryByPath("/Internet/Some Website");
|
|
|
|
QVERIFY(entry1);
|
|
|
|
QCOMPARE(entry1->title(), QString("Some Website"));
|
|
|
|
QCOMPARE(entry1->password(), QString("secretsecretsecret"));
|
|
|
|
|
|
|
|
// making sure that the message is different if the database was not
|
|
|
|
// modified by the merge operation.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(mergeCmd, {"merge", "-s", sourceFile.fileName(), sourceFile.fileName()});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Database was not modified by merge operation.\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Quiet option
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(mergeCmd, {"merge", "-q", "-s", sourceFile.fileName(), sourceFile.fileName()});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Quiet option without the -s option
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "a"});
|
|
|
|
execCmd(mergeCmd, {"merge", "-q", sourceFile.fileName(), sourceFile.fileName()});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
2019-12-21 09:46:01 -05:00
|
|
|
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));
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"a", "a"});
|
|
|
|
execCmd(createCmd, {"db-create", sourceDatabaseFilename, "-p", "-k", sourceKeyfilePath});
|
2019-12-21 09:46:01 -05:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput({"b", "b"});
|
|
|
|
execCmd(createCmd, {"db-create", targetDatabaseFilename, "-p", "-k", targetKeyfilePath});
|
2019-12-21 09:46:01 -05:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
auto sourceDatabase = readDatabase(sourceDatabaseFilename, "a", sourceKeyfilePath);
|
2019-12-21 09:46:01 -05:00
|
|
|
QVERIFY(sourceDatabase);
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
auto targetDatabase = readDatabase(targetDatabaseFilename, "b", targetKeyfilePath);
|
2019-12-21 09:46:01 -05:00
|
|
|
QVERIFY(targetDatabase);
|
|
|
|
|
|
|
|
auto* rootGroup = new Group();
|
|
|
|
rootGroup->setName("root");
|
|
|
|
rootGroup->setUuid(QUuid::createUuid());
|
|
|
|
auto* group = new Group();
|
|
|
|
group->setUuid(QUuid::createUuid());
|
|
|
|
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();
|
|
|
|
otherRootGroup->setName("root");
|
|
|
|
otherRootGroup->setUuid(QUuid::createUuid());
|
|
|
|
auto* otherGroup = new Group();
|
|
|
|
otherGroup->setUuid(QUuid::createUuid());
|
|
|
|
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);
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
sourceDatabase->saveAs(sourceDatabaseFilename);
|
|
|
|
targetDatabase->saveAs(targetDatabaseFilename);
|
|
|
|
|
|
|
|
setInput({"b", "a"});
|
|
|
|
execCmd(mergeCmd,
|
|
|
|
{"merge",
|
|
|
|
"-k",
|
|
|
|
targetKeyfilePath,
|
|
|
|
"--key-file-from",
|
|
|
|
sourceKeyfilePath,
|
|
|
|
targetDatabaseFilename,
|
|
|
|
sourceDatabaseFilename});
|
|
|
|
|
|
|
|
QList<QByteArray> lines = m_stdout->readAll().split('\n');
|
|
|
|
QVERIFY(lines.contains(
|
|
|
|
QString("Successfully merged %1 into %2.").arg(sourceDatabaseFilename, targetDatabaseFilename).toUtf8()));
|
2019-12-21 09:46:01 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
void TestCli::testMove()
|
|
|
|
{
|
|
|
|
Move moveCmd;
|
|
|
|
QVERIFY(!moveCmd.name.isEmpty());
|
|
|
|
QVERIFY(moveCmd.getDescriptionLine().contains(moveCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(moveCmd, {"mv", m_dbFile->fileName(), "invalid_entry_path", "invalid_group_path"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Could not find entry with path invalid_entry_path.\n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray());
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(moveCmd, {"mv", m_dbFile->fileName(), "Sample Entry", "invalid_group_path"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Could not find group with path invalid_group_path.\n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray());
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(moveCmd, {"mv", m_dbFile->fileName(), "Sample Entry", "General/"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully moved entry Sample Entry to group General/.\n"));
|
|
|
|
|
|
|
|
auto db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
auto* entry = db->rootGroup()->findEntryByPath("General/Sample Entry");
|
|
|
|
QVERIFY(entry);
|
|
|
|
|
|
|
|
// Test that not modified if the same group is destination.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(moveCmd, {"mv", m_dbFile->fileName(), "General/Sample Entry", "General/"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readLine(), QByteArray("Entry is already in group General/.\n"));
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// sanity check
|
2020-05-11 07:31:29 -04:00
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
entry = db->rootGroup()->findEntryByPath("General/Sample Entry");
|
|
|
|
QVERIFY(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testRemove()
|
|
|
|
{
|
|
|
|
Remove removeCmd;
|
|
|
|
QVERIFY(!removeCmd.name.isEmpty());
|
|
|
|
QVERIFY(removeCmd.getDescriptionLine().contains(removeCmd.name));
|
|
|
|
|
|
|
|
// load test database and save a copy with disabled recycle bin
|
2020-05-11 07:31:29 -04:00
|
|
|
auto db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(db);
|
|
|
|
TemporaryFile fileCopy;
|
|
|
|
fileCopy.open();
|
|
|
|
fileCopy.close();
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
db->metadata()->setRecycleBinEnabled(false);
|
|
|
|
db->saveAs(fileCopy.fileName());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// delete entry and verify
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeCmd, {"rm", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully recycled entry Sample Entry.\n"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
auto readBackDb = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(readBackDb);
|
|
|
|
QVERIFY(!readBackDb->rootGroup()->findEntryByPath("/Sample Entry"));
|
|
|
|
QVERIFY(readBackDb->rootGroup()->findEntryByPath(QString("/%1/Sample Entry").arg(Group::tr("Recycle Bin"))));
|
|
|
|
|
|
|
|
// try again, this time without recycle bin
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeCmd, {"rm", fileCopy.fileName(), "/Sample Entry"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully deleted entry Sample Entry.\n"));
|
|
|
|
|
|
|
|
readBackDb = readDatabase(fileCopy.fileName(), "a");
|
2019-10-14 08:37:26 -04:00
|
|
|
QVERIFY(readBackDb);
|
|
|
|
QVERIFY(!readBackDb->rootGroup()->findEntryByPath("/Sample Entry"));
|
|
|
|
QVERIFY(!readBackDb->rootGroup()->findEntryByPath(QString("/%1/Sample Entry").arg(Group::tr("Recycle Bin"))));
|
|
|
|
|
|
|
|
// finally, try deleting a non-existent entry
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeCmd, {"rm", fileCopy.fileName(), "/Sample Entry"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray("Entry /Sample Entry not found.\n"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// try deleting a directory, should fail
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeCmd, {"rm", fileCopy.fileName(), "/General"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray("Entry /General not found.\n"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testRemoveGroup()
|
|
|
|
{
|
|
|
|
RemoveGroup removeGroupCmd;
|
|
|
|
QVERIFY(!removeGroupCmd.name.isEmpty());
|
|
|
|
QVERIFY(removeGroupCmd.getDescriptionLine().contains(removeGroupCmd.name));
|
|
|
|
|
|
|
|
// try deleting a directory, should recycle it first.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeGroupCmd, {"rmdir", m_dbFile->fileName(), "/General"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully recycled group /General.\n"));
|
|
|
|
|
|
|
|
auto db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
auto* group = db->rootGroup()->findGroupByPath("General");
|
|
|
|
QVERIFY(!group);
|
|
|
|
|
|
|
|
// try deleting a directory again, should delete it permanently.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeGroupCmd, {"rmdir", m_dbFile->fileName(), "Recycle Bin/General"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully deleted group Recycle Bin/General.\n"));
|
|
|
|
|
|
|
|
db = readDatabase();
|
2019-10-14 08:37:26 -04:00
|
|
|
group = db->rootGroup()->findGroupByPath("Recycle Bin/General");
|
|
|
|
QVERIFY(!group);
|
|
|
|
|
|
|
|
// try deleting an invalid group, should fail.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeGroupCmd, {"rmdir", m_dbFile->fileName(), "invalid"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray("Group invalid not found.\n"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Should fail to remove the root group.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeGroupCmd, {"rmdir", m_dbFile->fileName(), "/"});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray("Cannot remove root group from database.\n"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testRemoveQuiet()
|
|
|
|
{
|
|
|
|
Remove removeCmd;
|
|
|
|
QVERIFY(!removeCmd.name.isEmpty());
|
|
|
|
QVERIFY(removeCmd.getDescriptionLine().contains(removeCmd.name));
|
|
|
|
|
|
|
|
// delete entry and verify
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeCmd, {"rm", "-q", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
auto db = readDatabase();
|
|
|
|
QVERIFY(db);
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QVERIFY(!db->rootGroup()->findEntryByPath("/Sample Entry"));
|
|
|
|
QVERIFY(db->rootGroup()->findEntryByPath(QString("/%1/Sample Entry").arg(Group::tr("Recycle Bin"))));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// remove the entry completely
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(removeCmd, {"rm", "-q", m_dbFile->fileName(), QString("/%1/Sample Entry").arg(Group::tr("Recycle Bin"))});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
|
|
|
|
db = readDatabase();
|
|
|
|
QVERIFY(!db->rootGroup()->findEntryByPath("/Sample Entry"));
|
|
|
|
QVERIFY(!db->rootGroup()->findEntryByPath(QString("/%1/Sample Entry").arg(Group::tr("Recycle Bin"))));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testShow()
|
|
|
|
{
|
|
|
|
Show showCmd;
|
|
|
|
QVERIFY(!showCmd.name.isEmpty());
|
|
|
|
QVERIFY(showCmd.getDescriptionLine().contains(showCmd.name));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
m_stderr->readLine(); // Skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("Title: Sample Entry\n"
|
|
|
|
"UserName: User Name\n"
|
2019-10-28 13:27:29 -04:00
|
|
|
"Password: PROTECTED\n"
|
2019-10-14 08:37:26 -04:00
|
|
|
"URL: http://www.somesite.com/\n"
|
|
|
|
"Notes: Notes\n"));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", "-s", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("Title: Sample Entry\n"
|
|
|
|
"UserName: User Name\n"
|
|
|
|
"Password: Password\n"
|
|
|
|
"URL: http://www.somesite.com/\n"
|
|
|
|
"Notes: Notes\n"));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", m_dbFile->fileName(), "-q", "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-28 13:27:29 -04:00
|
|
|
QByteArray("Title: Sample Entry\n"
|
|
|
|
"UserName: User Name\n"
|
|
|
|
"Password: PROTECTED\n"
|
|
|
|
"URL: http://www.somesite.com/\n"
|
|
|
|
"Notes: Notes\n"));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", "-a", "Title", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Sample Entry\n"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", "-a", "Password", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray("Password\n"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", "-a", "Title", "-a", "URL", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("Sample Entry\n"
|
|
|
|
"http://www.somesite.com/\n"));
|
|
|
|
|
2020-01-30 15:46:48 -05:00
|
|
|
// Test case insensitivity
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", "-a", "TITLE", "-a", "URL", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2020-01-30 15:46:48 -05:00
|
|
|
QByteArray("Sample Entry\n"
|
|
|
|
"http://www.somesite.com/\n"));
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", "-a", "DoesNotExist", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QVERIFY(m_stderr->readAll().contains("ERROR: unknown attribute DoesNotExist.\n"));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", "-t", m_dbFile->fileName(), "/Sample Entry"});
|
|
|
|
QVERIFY(isTotp(m_stdout->readAll()));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", "-a", "Title", m_dbFile->fileName(), "--totp", "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readLine(), QByteArray("Sample Entry\n"));
|
|
|
|
QVERIFY(isTotp(m_stdout->readAll()));
|
|
|
|
|
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", m_dbFile2->fileName(), "--totp", "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
|
|
|
QVERIFY(m_stderr->readAll().contains("Entry with path /Sample Entry has no TOTP set up.\n"));
|
2020-01-30 15:46:48 -05:00
|
|
|
|
|
|
|
// Show with ambiguous attributes
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(showCmd, {"show", m_dbFile->fileName(), "-a", "Testattribute1", "/Sample Entry"});
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2020-12-13 23:23:25 -05:00
|
|
|
QVERIFY(m_stderr->readAll().contains("ERROR: attribute Testattribute1 is ambiguous"));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testInvalidDbFiles()
|
|
|
|
{
|
|
|
|
Show showCmd;
|
|
|
|
QString nonExistentDbPath("/foo/bar/baz");
|
|
|
|
QString directoryName("/");
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(showCmd, {"show", nonExistentDbPath, "/Sample Entry"});
|
|
|
|
QCOMPARE(QString(m_stderr->readAll()),
|
2019-10-14 08:37:26 -04:00
|
|
|
QObject::tr("Failed to open database file %1: not found").arg(nonExistentDbPath) + "\n");
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(showCmd, {"show", directoryName, "whatever"});
|
|
|
|
QCOMPARE(QString(m_stderr->readAll()),
|
2019-10-14 08:37:26 -04:00
|
|
|
QObject::tr("Failed to open database file %1: not a plain file").arg(directoryName) + "\n");
|
|
|
|
|
|
|
|
// Create a write-only file and try to open it.
|
|
|
|
// QFileInfo.isReadable returns 'true' on Windows, even after the call to
|
|
|
|
// setPermissions(WriteOwner) and with NTFS permissions enabled, so this
|
|
|
|
// check doesn't work.
|
|
|
|
#if !defined(Q_OS_WIN)
|
|
|
|
QTemporaryFile tempFile;
|
|
|
|
QVERIFY(tempFile.open());
|
|
|
|
QString path = QFileInfo(tempFile).absoluteFilePath();
|
|
|
|
QVERIFY(tempFile.setPermissions(QFileDevice::WriteOwner));
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(showCmd, {"show", path, "some entry"});
|
|
|
|
QCOMPARE(QString(m_stderr->readAll()),
|
2019-10-14 08:37:26 -04:00
|
|
|
QObject::tr("Failed to open database file %1: not readable").arg(path) + "\n");
|
|
|
|
#endif // Q_OS_WIN
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Secret key for the YubiKey slot used by the unit test is
|
|
|
|
* 1c e3 0f d7 8d 20 dc fa 40 b5 0c 18 77 9a fb 0f 02 28 8d b7
|
2020-04-06 08:42:20 -04:00
|
|
|
* This secret can be on either slot but must be passive.
|
2019-10-14 08:37:26 -04:00
|
|
|
*/
|
|
|
|
void TestCli::testYubiKeyOption()
|
|
|
|
{
|
2020-04-06 08:42:20 -04:00
|
|
|
if (!YubiKey::instance()->isInitialized()) {
|
|
|
|
QSKIP("Unable to initialize YubiKey interface.");
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
2020-04-06 08:42:20 -04:00
|
|
|
YubiKey::instance()->findValidKeys();
|
|
|
|
|
|
|
|
// Wait for the hardware to respond
|
|
|
|
QSignalSpy detected(YubiKey::instance(), SIGNAL(detectComplete(bool)));
|
|
|
|
QTRY_VERIFY_WITH_TIMEOUT(detected.count() > 0, 2000);
|
|
|
|
|
|
|
|
auto keys = YubiKey::instance()->foundKeys();
|
|
|
|
if (keys.isEmpty()) {
|
|
|
|
QSKIP("No YubiKey devices were detected.");
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
2020-04-06 08:42:20 -04:00
|
|
|
bool wouldBlock = false;
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray challenge("CLITest");
|
|
|
|
QByteArray response;
|
|
|
|
QByteArray expected("\xA2\x3B\x94\x00\xBE\x47\x9A\x30\xA9\xEB\x50\x9B\x85\x56\x5B\x6B\x30\x25\xB4\x8E", 20);
|
2020-04-06 08:42:20 -04:00
|
|
|
|
|
|
|
// Find a key that as configured for this test
|
|
|
|
YubiKeySlot pKey(0, 0);
|
|
|
|
for (auto key : keys) {
|
|
|
|
if (YubiKey::instance()->testChallenge(key, &wouldBlock) && !wouldBlock) {
|
|
|
|
YubiKey::instance()->challenge(key, challenge, response);
|
|
|
|
if (response == expected) {
|
|
|
|
pKey = key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Tools::wait(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pKey.first == 0 && pKey.second == 0) {
|
|
|
|
QSKIP("No YubiKey is properly configured to perform this test.");
|
|
|
|
}
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
List listCmd;
|
|
|
|
Add addCmd;
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", "-y", "2", m_yubiKeyProtectedDbFile->fileName()});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll(), QByteArray());
|
|
|
|
QCOMPARE(m_stdout->readAll(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("entry1\n"
|
|
|
|
"entry2\n"));
|
|
|
|
|
|
|
|
// Should raise an error with no yubikey slot.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", m_yubiKeyProtectedDbFile->fileName()});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readLine(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("Error while reading the database: Invalid credentials were provided, please try again.\n"));
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stderr->readLine(),
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray("If this reoccurs, then your database file may be corrupt. (HMAC mismatch)\n"));
|
2020-05-11 07:31:29 -04:00
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Should raise an error if yubikey slot is not a string
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", "-y", "invalidslot", m_yubiKeyProtectedDbFile->fileName()});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll().split(':').at(0), QByteArray("Invalid YubiKey slot invalidslot\n"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
// Should raise an error if yubikey slot is invalid.
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(listCmd, {"ls", "-y", "3", m_yubiKeyProtectedDbFile->fileName()});
|
|
|
|
m_stderr->readLine(); // skip password prompt
|
|
|
|
QCOMPARE(m_stderr->readAll().split(':').at(0), QByteArray("Invalid YubiKey slot 3\n"));
|
|
|
|
QCOMPARE(m_stdout->readAll(), QByteArray());
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testCommandParsing_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("input");
|
|
|
|
QTest::addColumn<QStringList>("expectedOutput");
|
|
|
|
|
|
|
|
QTest::newRow("basic") << "hello world" << QStringList({"hello", "world"});
|
|
|
|
QTest::newRow("basic escaping") << "hello\\ world" << QStringList({"hello world"});
|
|
|
|
QTest::newRow("quoted string") << "\"hello world\"" << QStringList({"hello world"});
|
|
|
|
QTest::newRow("multiple params") << "show Passwords/Internet" << QStringList({"show", "Passwords/Internet"});
|
|
|
|
QTest::newRow("quoted string inside param")
|
|
|
|
<< R"(ls foo\ bar\ baz"quoted")" << QStringList({"ls", "foo bar baz\"quoted\""});
|
|
|
|
QTest::newRow("multiple whitespace") << "hello world" << QStringList({"hello", "world"});
|
|
|
|
QTest::newRow("single slash char") << "\\" << QStringList({"\\"});
|
|
|
|
QTest::newRow("double backslash entry name") << "show foo\\\\\\\\bar" << QStringList({"show", "foo\\\\bar"});
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testCommandParsing()
|
|
|
|
{
|
|
|
|
QFETCH(QString, input);
|
|
|
|
QFETCH(QStringList, expectedOutput);
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
QStringList result = Utils::splitCommandString(input);
|
|
|
|
QCOMPARE(result.size(), expectedOutput.size());
|
|
|
|
for (int i = 0; i < expectedOutput.size(); ++i) {
|
|
|
|
QCOMPARE(result[i], expectedOutput[i]);
|
|
|
|
}
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testOpen()
|
|
|
|
{
|
2020-05-11 07:31:29 -04:00
|
|
|
Open openCmd;
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
setInput("a");
|
|
|
|
execCmd(openCmd, {"open", m_dbFile->fileName()});
|
|
|
|
QVERIFY(openCmd.currentDatabase);
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
List listCmd;
|
2019-10-14 08:37:26 -04:00
|
|
|
// Set a current database, simulating interactive mode.
|
2020-05-11 07:31:29 -04:00
|
|
|
listCmd.currentDatabase = openCmd.currentDatabase;
|
|
|
|
execCmd(listCmd, {"ls"});
|
2019-10-14 08:37:26 -04:00
|
|
|
QByteArray expectedOutput("Sample Entry\n"
|
|
|
|
"General/\n"
|
|
|
|
"Windows/\n"
|
|
|
|
"Network/\n"
|
|
|
|
"Internet/\n"
|
|
|
|
"eMail/\n"
|
|
|
|
"Homebanking/\n");
|
2020-05-11 07:31:29 -04:00
|
|
|
QByteArray actualOutput = m_stdout->readAll();
|
2019-10-14 08:37:26 -04:00
|
|
|
actualOutput.truncate(expectedOutput.length());
|
|
|
|
QCOMPARE(actualOutput, expectedOutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCli::testHelp()
|
|
|
|
{
|
2020-05-11 07:31:29 -04:00
|
|
|
Help helpCmd;
|
2019-10-14 08:37:26 -04:00
|
|
|
Commands::setupCommands(false);
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
execCmd(helpCmd, {"help"});
|
|
|
|
QVERIFY(m_stdout->readAll().contains("Available commands"));
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
List listCmd;
|
|
|
|
execCmd(helpCmd, {"help", "ls"});
|
|
|
|
QVERIFY(m_stdout->readAll().contains(listCmd.description.toLatin1()));
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|