mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-04-04 04:57:39 +03:00
[CLI] Add a db-edit command (#8400)
This commit is contained in:
parent
b1e7c34b82
commit
db98f114f9
16 changed files with 529 additions and 52 deletions
|
@ -34,7 +34,9 @@
|
|||
#include "cli/AttachmentImport.h"
|
||||
#include "cli/AttachmentRemove.h"
|
||||
#include "cli/Clip.h"
|
||||
#include "cli/Create.h"
|
||||
#include "cli/DatabaseCreate.h"
|
||||
#include "cli/DatabaseEdit.h"
|
||||
#include "cli/DatabaseInfo.h"
|
||||
#include "cli/Diceware.h"
|
||||
#include "cli/Edit.h"
|
||||
#include "cli/Estimate.h"
|
||||
|
@ -42,7 +44,6 @@
|
|||
#include "cli/Generate.h"
|
||||
#include "cli/Help.h"
|
||||
#include "cli/Import.h"
|
||||
#include "cli/Info.h"
|
||||
#include "cli/List.h"
|
||||
#include "cli/Merge.h"
|
||||
#include "cli/Move.h"
|
||||
|
@ -242,7 +243,7 @@ void TestCli::testBatchCommands()
|
|||
QVERIFY(Commands::getCommand("show"));
|
||||
QVERIFY(Commands::getCommand("search"));
|
||||
QVERIFY(!Commands::getCommand("doesnotexist"));
|
||||
QCOMPARE(Commands::getCommands().size(), 25);
|
||||
QCOMPARE(Commands::getCommands().size(), 26);
|
||||
}
|
||||
|
||||
void TestCli::testInteractiveCommands()
|
||||
|
@ -274,7 +275,7 @@ void TestCli::testInteractiveCommands()
|
|||
QVERIFY(Commands::getCommand("show"));
|
||||
QVERIFY(Commands::getCommand("search"));
|
||||
QVERIFY(!Commands::getCommand("doesnotexist"));
|
||||
QCOMPARE(Commands::getCommands().size(), 25);
|
||||
QCOMPARE(Commands::getCommands().size(), 26);
|
||||
}
|
||||
|
||||
void TestCli::testAdd()
|
||||
|
@ -732,7 +733,7 @@ void TestCli::testClip()
|
|||
|
||||
void TestCli::testCreate()
|
||||
{
|
||||
Create createCmd;
|
||||
DatabaseCreate createCmd;
|
||||
QVERIFY(!createCmd.name.isEmpty());
|
||||
QVERIFY(createCmd.getDescriptionLine().contains(createCmd.name));
|
||||
|
||||
|
@ -848,9 +849,147 @@ void TestCli::testCreate()
|
|||
QVERIFY(db);
|
||||
}
|
||||
|
||||
void TestCli::testDatabaseEdit()
|
||||
{
|
||||
TemporaryFile firstKeyFile;
|
||||
firstKeyFile.open();
|
||||
firstKeyFile.write(QString("keyFilePassword").toLatin1());
|
||||
firstKeyFile.close();
|
||||
|
||||
TemporaryFile secondKeyFile;
|
||||
secondKeyFile.open();
|
||||
secondKeyFile.write(QString("newKeyFilePassword").toLatin1());
|
||||
secondKeyFile.close();
|
||||
|
||||
QScopedPointer<QTemporaryDir> testDir(new QTemporaryDir());
|
||||
|
||||
DatabaseCreate createCmd;
|
||||
DatabaseEdit editCmd;
|
||||
QVERIFY(!editCmd.name.isEmpty());
|
||||
QVERIFY(editCmd.getDescriptionLine().contains(editCmd.name));
|
||||
|
||||
QString dbFilename;
|
||||
dbFilename = testDir->path() + "/testDatabaseEdit.kdbx";
|
||||
|
||||
// Creating a database for testing
|
||||
setInput({"a", "a"});
|
||||
execCmd(createCmd, {"db-create", dbFilename, "-p"});
|
||||
QCOMPARE(m_stdout->readLine(), QByteArray("Successfully created new database.\n"));
|
||||
|
||||
// Sanity check.
|
||||
auto db = readDatabase(dbFilename, "a");
|
||||
QVERIFY(!db.isNull());
|
||||
|
||||
setInput("a");
|
||||
execCmd(editCmd, {"db-edit", dbFilename, "-p", "--unset-password"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray(""));
|
||||
m_stderr->readLine();
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray("Cannot use p and unset-password at the same time.\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(editCmd, {"db-edit", dbFilename, "--set-key-file", "/key/file/path", "--unset-key-file"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray(""));
|
||||
// Skipping the password prompt.
|
||||
m_stderr->readLine();
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray("Cannot use set-key-file and unset-key-file at the same time.\n"));
|
||||
|
||||
// Sanity check.
|
||||
db = readDatabase(dbFilename, "a");
|
||||
QVERIFY(!db.isNull());
|
||||
|
||||
setInput({"a", "b", "b"});
|
||||
execCmd(editCmd, {"db-edit", dbFilename, "-p"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully edited the database.\n"));
|
||||
|
||||
// Sanity check
|
||||
db = readDatabase(dbFilename, "b");
|
||||
QVERIFY(!db.isNull());
|
||||
|
||||
setInput("b");
|
||||
execCmd(editCmd, {"db-edit", dbFilename, "--set-key-file", firstKeyFile.fileName()});
|
||||
// Skipping the password prompt.
|
||||
m_stderr->readLine();
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray(""));
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully edited the database.\n"));
|
||||
|
||||
// Sanity check
|
||||
db = readDatabase(dbFilename, "b");
|
||||
QVERIFY(db.isNull());
|
||||
db = readDatabase(dbFilename, "b", firstKeyFile.fileName());
|
||||
QVERIFY(!db.isNull());
|
||||
|
||||
setInput("b");
|
||||
execCmd(editCmd,
|
||||
{"db-edit", dbFilename, "-k", firstKeyFile.fileName(), "--set-key-file", secondKeyFile.fileName()});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully edited the database.\n"));
|
||||
|
||||
// Sanity check
|
||||
db = readDatabase(dbFilename, "b", firstKeyFile.fileName());
|
||||
QVERIFY(db.isNull());
|
||||
db = readDatabase(dbFilename, "b", secondKeyFile.fileName());
|
||||
QVERIFY(!db.isNull());
|
||||
|
||||
setInput("b");
|
||||
execCmd(editCmd, {"db-edit", dbFilename, "-k", secondKeyFile.fileName(), "--unset-password"});
|
||||
// Skipping the password prompt.
|
||||
m_stderr->readLine();
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray(""));
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully edited the database.\n"));
|
||||
|
||||
execCmd(editCmd,
|
||||
{"db-edit",
|
||||
dbFilename,
|
||||
"--no-password",
|
||||
"-k",
|
||||
secondKeyFile.fileName(),
|
||||
"--set-key-file",
|
||||
firstKeyFile.fileName()});
|
||||
// Skipping the password prompt.
|
||||
m_stderr->readLine();
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray(""));
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully edited the database.\n"));
|
||||
|
||||
setInput({"b", "b"});
|
||||
execCmd(editCmd, {"db-edit", dbFilename, "-k", firstKeyFile.fileName(), "--no-password", "--set-password"});
|
||||
// Skipping over the password setting prompts.
|
||||
m_stderr->readLine();
|
||||
m_stderr->readLine();
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray(""));
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully edited the database.\n"));
|
||||
|
||||
setInput("b");
|
||||
execCmd(editCmd, {"db-edit", dbFilename, "-k", firstKeyFile.fileName(), "--unset-key-file"});
|
||||
// Skipping the password prompt.
|
||||
m_stderr->readLine();
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray(""));
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("Successfully edited the database.\n"));
|
||||
|
||||
// Sanity check
|
||||
db = readDatabase(dbFilename, "b", firstKeyFile.fileName());
|
||||
QVERIFY(db.isNull());
|
||||
db = readDatabase(dbFilename, "b");
|
||||
QVERIFY(!db.isNull());
|
||||
|
||||
// Trying to remove the key file when there is none set should
|
||||
// raise an error.
|
||||
setInput("b");
|
||||
execCmd(editCmd, {"db-edit", dbFilename, "-p", "--unset-key-file"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray(""));
|
||||
m_stderr->readLine();
|
||||
QCOMPARE(m_stderr->readLine(), QByteArray("Cannot remove file key: The database does not have a file key.\n"));
|
||||
QCOMPARE(m_stderr->readLine(), QByteArray("Could not change the database key.\n"));
|
||||
|
||||
setInput("b");
|
||||
execCmd(editCmd, {"db-edit", dbFilename, "--unset-password"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray(""));
|
||||
// Skipping the password prompt.
|
||||
m_stderr->readLine();
|
||||
QCOMPARE(m_stderr->readLine(), QByteArray("Cannot remove all the keys from a database.\n"));
|
||||
}
|
||||
|
||||
void TestCli::testInfo()
|
||||
{
|
||||
Info infoCmd;
|
||||
DatabaseInfo infoCmd;
|
||||
QVERIFY(!infoCmd.name.isEmpty());
|
||||
QVERIFY(infoCmd.getDescriptionLine().contains(infoCmd.name));
|
||||
|
||||
|
@ -1613,7 +1752,7 @@ void TestCli::testMerge()
|
|||
|
||||
void TestCli::testMergeWithKeys()
|
||||
{
|
||||
Create createCmd;
|
||||
DatabaseCreate createCmd;
|
||||
QVERIFY(!createCmd.name.isEmpty());
|
||||
QVERIFY(createCmd.getDescriptionLine().contains(createCmd.name));
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ private slots:
|
|||
void testCommandParsing_data();
|
||||
void testCommandParsing();
|
||||
void testCreate();
|
||||
void testDatabaseEdit();
|
||||
void testDiceware();
|
||||
void testEdit();
|
||||
void testEstimate_data();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue