mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-10-01 01:26:01 -04:00
Additional database file checks in cli/Utils.unlockDatabase
Avoids prompting the user for a password if unlocking is likely to fail due to some problem with the database file (i.e. not found, not a file, not readable). Add unit tests.
This commit is contained in:
parent
344198bc2a
commit
547c246e88
@ -24,6 +24,7 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QProcess>
|
||||
#include <QScopedPointer>
|
||||
|
||||
@ -108,6 +109,22 @@ namespace Utils
|
||||
TextStream out(outputDescriptor);
|
||||
TextStream err(errorDescriptor);
|
||||
|
||||
QFileInfo dbFileInfo(databaseFilename);
|
||||
if (dbFileInfo.canonicalFilePath().isEmpty()) {
|
||||
err << QObject::tr("Failed to open database file %1: not found").arg(databaseFilename) << endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!dbFileInfo.isFile()) {
|
||||
err << QObject::tr("Failed to open database file %1: not a plain file").arg(databaseFilename) << endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!dbFileInfo.isReadable()) {
|
||||
err << QObject::tr("Failed to open database file %1: not readable").arg(databaseFilename) << endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
if (isPasswordProtected) {
|
||||
out << QObject::tr("Insert password to unlock %1: ").arg(databaseFilename) << flush;
|
||||
QString line = Utils::getPassword(outputDescriptor);
|
||||
|
@ -1409,3 +1409,38 @@ void TestCli::testShow()
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray(""));
|
||||
QCOMPARE(m_stderrFile->readAll(), QByteArray("Entry with path /Sample Entry has no TOTP set up.\n"));
|
||||
}
|
||||
|
||||
void TestCli::testInvalidDbFiles()
|
||||
{
|
||||
Show showCmd;
|
||||
QString nonExistentDbPath("/foo/bar/baz");
|
||||
QString directoryName("/");
|
||||
|
||||
qint64 pos = m_stderrFile->pos();
|
||||
showCmd.execute({"show", nonExistentDbPath, "-q", "/Sample Entry"});
|
||||
m_stderrFile->seek(pos);
|
||||
QCOMPARE(QString(m_stderrFile->readAll()),
|
||||
QObject::tr("Failed to open database file %1: not found").arg(nonExistentDbPath) + "\n");
|
||||
|
||||
pos = m_stderrFile->pos();
|
||||
showCmd.execute({"show", directoryName, "-q", "whatever"});
|
||||
m_stderrFile->seek(pos);
|
||||
QCOMPARE(QString(m_stderrFile->readAll()),
|
||||
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));
|
||||
pos = m_stderrFile->pos();
|
||||
showCmd.execute({"show", path, "some entry"});
|
||||
m_stderrFile->seek(pos);
|
||||
QCOMPARE(QString(m_stderrFile->readAll()),
|
||||
QObject::tr("Failed to open database file %1: not readable").arg(path) + "\n");
|
||||
#endif // Q_OS_WIN
|
||||
}
|
||||
|
@ -63,6 +63,7 @@ private slots:
|
||||
void testRemove();
|
||||
void testRemoveQuiet();
|
||||
void testShow();
|
||||
void testInvalidDbFiles();
|
||||
|
||||
private:
|
||||
QByteArray m_dbData;
|
||||
|
Loading…
Reference in New Issue
Block a user