mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-05-02 14:46:07 -04:00
Adding --quiet option to the CLI. (#2507)
This commit is contained in:
parent
4e49de1afb
commit
fff0f11b33
21 changed files with 252 additions and 44 deletions
|
@ -161,6 +161,9 @@ void TestCli::testAdd()
|
|||
Utils::Test::setNextPassword("a");
|
||||
addCmd.execute({"add", "-u", "newuser", "--url", "https://example.com/", "-g", "-l", "20", m_dbFile->fileName(), "/newuser-entry"});
|
||||
m_stderrFile->reset();
|
||||
m_stdoutFile->reset();
|
||||
m_stdoutFile->readLine(); // skip password prompt
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray("Successfully added entry newuser-entry.\n"));
|
||||
|
||||
auto db = readTestDatabase();
|
||||
auto* entry = db->rootGroup()->findEntryByPath("/newuser-entry");
|
||||
|
@ -169,6 +172,16 @@ void TestCli::testAdd()
|
|||
QCOMPARE(entry->url(), QString("https://example.com/"));
|
||||
QCOMPARE(entry->password().size(), 20);
|
||||
|
||||
// Quiet option
|
||||
qint64 pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
addCmd.execute({"add", "-q", "-u", "newuser", "-g", "-l", "20", m_dbFile->fileName(), "/newentry-quiet"});
|
||||
m_stdoutFile->seek(pos);
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray(""));
|
||||
db = readTestDatabase();
|
||||
entry = db->rootGroup()->findEntryByPath("/newentry-quiet");
|
||||
QVERIFY(entry);
|
||||
|
||||
Utils::Test::setNextPassword("a");
|
||||
Utils::Test::setNextPassword("newpassword");
|
||||
addCmd.execute({"add", "-u", "newuser2", "--url", "https://example.net/", "-g", "-l", "20", "-p", m_dbFile->fileName(), "/newuser-entry2"});
|
||||
|
@ -181,7 +194,8 @@ void TestCli::testAdd()
|
|||
QCOMPARE(entry->password(), QString("newpassword"));
|
||||
}
|
||||
|
||||
bool isTOTP(const QString & value) {
|
||||
bool isTOTP(const QString& value)
|
||||
{
|
||||
QString val = value.trimmed();
|
||||
if (val.length() < 5 || val.length() > 6) {
|
||||
return false;
|
||||
|
@ -208,6 +222,7 @@ void TestCli::testClip()
|
|||
clipCmd.execute({"clip", m_dbFile->fileName(), "/Sample Entry"});
|
||||
|
||||
m_stderrFile->reset();
|
||||
m_stdoutFile->reset();
|
||||
QString errorOutput(m_stderrFile->readAll());
|
||||
|
||||
if (errorOutput.contains("Unable to start program")
|
||||
|
@ -215,6 +230,17 @@ void TestCli::testClip()
|
|||
QSKIP("Clip test skipped due to missing clipboard tool");
|
||||
}
|
||||
|
||||
QCOMPARE(clipboard->text(), QString("Password"));
|
||||
m_stdoutFile->readLine(); // skip prompt line
|
||||
QCOMPARE(m_stdoutFile->readLine(), QByteArray("Entry's password copied to the clipboard!\n"));
|
||||
|
||||
// Quiet option
|
||||
qint64 pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
clipCmd.execute({"clip", m_dbFile->fileName(), "/Sample Entry", "-q"});
|
||||
m_stdoutFile->seek(pos);
|
||||
// Output should be empty when quiet option is set.
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray(""));
|
||||
QCOMPARE(clipboard->text(), QString("Password"));
|
||||
|
||||
// TOTP
|
||||
|
@ -284,7 +310,7 @@ void TestCli::testDiceware()
|
|||
const auto words = passphrase.split(" ");
|
||||
QCOMPARE(words.size(), 11);
|
||||
QRegularExpression regex("^word\\d+$");
|
||||
for (const auto& word: words) {
|
||||
for (const auto& word : words) {
|
||||
QVERIFY2(regex.match(word).hasMatch(), qPrintable("Word " + word + " was not on the word list"));
|
||||
}
|
||||
}
|
||||
|
@ -297,6 +323,9 @@ void TestCli::testEdit()
|
|||
|
||||
Utils::Test::setNextPassword("a");
|
||||
editCmd.execute({"edit", "-u", "newuser", "--url", "https://otherurl.example.com/", "-t", "newtitle", m_dbFile->fileName(), "/Sample Entry"});
|
||||
m_stdoutFile->reset();
|
||||
m_stdoutFile->readLine(); // skip prompt line
|
||||
QCOMPARE(m_stdoutFile->readLine(), QByteArray("Successfully edited entry newtitle.\n"));
|
||||
|
||||
auto db = readTestDatabase();
|
||||
auto* entry = db->rootGroup()->findEntryByPath("/newtitle");
|
||||
|
@ -305,6 +334,13 @@ void TestCli::testEdit()
|
|||
QCOMPARE(entry->url(), QString("https://otherurl.example.com/"));
|
||||
QCOMPARE(entry->password(), QString("Password"));
|
||||
|
||||
// Quiet option
|
||||
qint64 pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
editCmd.execute({"edit", m_dbFile->fileName(), "-q", "-t", "newtitle", "/Sample Entry"});
|
||||
m_stdoutFile->seek(pos);
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray(""));
|
||||
|
||||
Utils::Test::setNextPassword("a");
|
||||
editCmd.execute({"edit", "-g", m_dbFile->fileName(), "/newtitle"});
|
||||
db = readTestDatabase();
|
||||
|
@ -430,7 +466,7 @@ void TestCli::testEstimate()
|
|||
QVERIFY(result.startsWith("Length " + length));
|
||||
QVERIFY(result.contains("Entropy " + entropy));
|
||||
QVERIFY(result.contains("Log10 " + log10));
|
||||
for (const auto& string: asConst(searchStrings)) {
|
||||
for (const auto& string : asConst(searchStrings)) {
|
||||
QVERIFY2(result.contains(string), qPrintable("String " + string + " missing"));
|
||||
}
|
||||
}
|
||||
|
@ -455,6 +491,18 @@ void TestCli::testExtract()
|
|||
auto* entry = db->rootGroup()->findEntryByPath("/Sample Entry");
|
||||
QVERIFY(entry);
|
||||
QCOMPARE(entry->password(), QString("Password"));
|
||||
|
||||
m_stdoutFile->reset();
|
||||
|
||||
// Quiet option
|
||||
QScopedPointer<Database> dbQuiet(new Database());
|
||||
qint64 pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
extractCmd.execute({"extract", "-q", m_dbFile->fileName()});
|
||||
m_stdoutFile->seek(pos);
|
||||
reader.readDatabase(m_stdoutFile.data(), dbQuiet.data());
|
||||
QVERIFY(!reader.hasError());
|
||||
QVERIFY(db.data());
|
||||
}
|
||||
|
||||
void TestCli::testGenerate_data()
|
||||
|
@ -533,8 +581,22 @@ void TestCli::testList()
|
|||
"eMail/\n"
|
||||
"Homebanking/\n"));
|
||||
|
||||
// Quiet option
|
||||
qint64 pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
listCmd.execute({"ls", "-q", m_dbFile->fileName()});
|
||||
m_stdoutFile->seek(pos);
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray("Sample Entry\n"
|
||||
"General/\n"
|
||||
"Windows/\n"
|
||||
"Network/\n"
|
||||
"Internet/\n"
|
||||
"eMail/\n"
|
||||
"Homebanking/\n"));
|
||||
|
||||
|
||||
pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
listCmd.execute({"ls", "-R", m_dbFile->fileName()});
|
||||
m_stdoutFile->seek(pos);
|
||||
m_stdoutFile->readLine(); // skip password prompt
|
||||
|
@ -581,8 +643,15 @@ void TestCli::testLocate()
|
|||
m_stdoutFile->readLine(); // skip password prompt
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray("/Sample Entry\n"));
|
||||
|
||||
// Quiet option
|
||||
qint64 pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
locateCmd.execute({"locate", m_dbFile->fileName(), "-q", "Sample"});
|
||||
m_stdoutFile->seek(pos);
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray("/Sample Entry\n"));
|
||||
|
||||
pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
locateCmd.execute({"locate", m_dbFile->fileName(), "Does Not Exist"});
|
||||
m_stdoutFile->seek(pos);
|
||||
m_stdoutFile->readLine(); // skip password prompt
|
||||
|
@ -709,6 +778,13 @@ void TestCli::testMerge()
|
|||
m_stdoutFile->seek(pos);
|
||||
m_stdoutFile->readLine();
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray("Database was not modified by merge operation.\n"));
|
||||
|
||||
// Quiet option
|
||||
pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
mergeCmd.execute({"merge", "-q", "-s", sourceFile.fileName(), sourceFile.fileName()});
|
||||
m_stdoutFile->seek(pos);
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray(""));
|
||||
}
|
||||
|
||||
void TestCli::testRemove()
|
||||
|
@ -779,6 +855,52 @@ void TestCli::testRemove()
|
|||
QCOMPARE(m_stderrFile->readAll(), QByteArray("Entry /Sample Entry not found.\n"));
|
||||
}
|
||||
|
||||
void TestCli::testRemoveQuiet()
|
||||
{
|
||||
Remove removeCmd;
|
||||
QVERIFY(!removeCmd.name.isEmpty());
|
||||
QVERIFY(removeCmd.getDescriptionLine().contains(removeCmd.name));
|
||||
|
||||
Kdbx3Reader reader;
|
||||
Kdbx3Writer writer;
|
||||
|
||||
qint64 pos = m_stdoutFile->pos();
|
||||
|
||||
// delete entry and verify
|
||||
Utils::Test::setNextPassword("a");
|
||||
removeCmd.execute({"rm", "-q", m_dbFile->fileName(), "/Sample Entry"});
|
||||
m_stdoutFile->seek(pos);
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray(""));
|
||||
|
||||
auto key = QSharedPointer<CompositeKey>::create();
|
||||
key->addKey(QSharedPointer<PasswordKey>::create("a"));
|
||||
QFile readBack(m_dbFile->fileName());
|
||||
readBack.open(QIODevice::ReadOnly);
|
||||
auto readBackDb = QSharedPointer<Database>::create();
|
||||
reader.readDatabase(&readBack, key, readBackDb.data());
|
||||
readBack.close();
|
||||
QVERIFY(readBackDb);
|
||||
QVERIFY(!readBackDb->rootGroup()->findEntryByPath("/Sample Entry"));
|
||||
QVERIFY(readBackDb->rootGroup()->findEntryByPath("/Recycle Bin/Sample Entry"));
|
||||
|
||||
pos = m_stdoutFile->pos();
|
||||
|
||||
// remove the entry completely
|
||||
Utils::Test::setNextPassword("a");
|
||||
removeCmd.execute({"rm", "-q", m_dbFile->fileName(), "/Recycle Bin/Sample Entry"});
|
||||
m_stdoutFile->seek(pos);
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray(""));
|
||||
|
||||
readBack.setFileName(m_dbFile->fileName());
|
||||
readBack.open(QIODevice::ReadOnly);
|
||||
readBackDb = QSharedPointer<Database>::create();
|
||||
reader.readDatabase(&readBack, key, readBackDb.data());
|
||||
readBack.close();
|
||||
QVERIFY(readBackDb);
|
||||
QVERIFY(!readBackDb->rootGroup()->findEntryByPath("/Sample Entry"));
|
||||
QVERIFY(!readBackDb->rootGroup()->findEntryByPath("/Recycle Bin/Sample Entry"));
|
||||
}
|
||||
|
||||
void TestCli::testShow()
|
||||
{
|
||||
Show showCmd;
|
||||
|
@ -797,6 +919,16 @@ void TestCli::testShow()
|
|||
|
||||
qint64 pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
showCmd.execute({"show", m_dbFile->fileName(), "-q", "/Sample Entry"});
|
||||
m_stdoutFile->seek(pos);
|
||||
QCOMPARE(m_stdoutFile->readAll(), QByteArray("Title: Sample Entry\n"
|
||||
"UserName: User Name\n"
|
||||
"Password: Password\n"
|
||||
"URL: http://www.somesite.com/\n"
|
||||
"Notes: Notes\n"));
|
||||
|
||||
pos = m_stdoutFile->pos();
|
||||
Utils::Test::setNextPassword("a");
|
||||
showCmd.execute({"show", "-a", "Title", m_dbFile->fileName(), "/Sample Entry"});
|
||||
m_stdoutFile->seek(pos);
|
||||
m_stdoutFile->readLine(); // skip password prompt
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue