|
| 1 | +// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | +#include <QApplication> |
| 4 | +#include <QCheckBox> |
| 5 | +#include <QDialogButtonBox> |
| 6 | +#include <QLineEdit> |
| 7 | +#include <QPlainTextEdit> |
| 8 | +#include <QPushButton> |
| 9 | +#include <QtTest> |
| 10 | + |
| 11 | +#include "../../../src/keygendialog.h" |
| 12 | + |
| 13 | +/** |
| 14 | + * @class tst_keygendialog |
| 15 | + * @brief Widget-level tests for KeygenDialog. |
| 16 | + * |
| 17 | + * KeygenDialog is the modal that fronts GPG key-pair generation. These tests |
| 18 | + * cover construction and the input-driven UI state transitions; they don't |
| 19 | + * drive a full key generation (that needs a real gpg + several seconds of |
| 20 | + * entropy gathering). |
| 21 | + * |
| 22 | + * The dialog is normally parented to a ConfigDialog; tests pass nullptr to |
| 23 | + * sidestep ConfigDialog construction entirely. The protected `done()` slot |
| 24 | + * isn't exercised here for the same reason (it dispatches into |
| 25 | + * dialog->genKey(), which dereferences the ConfigDialog parent). |
| 26 | + */ |
| 27 | +class tst_keygendialog : public QObject { |
| 28 | + Q_OBJECT |
| 29 | + |
| 30 | +private Q_SLOTS: |
| 31 | + void constructionLoadsNonEmptyTemplate(); |
| 32 | + void expertCheckboxTogglesTemplateEditor(); |
| 33 | + void nameTextUpdatesNameRealLine(); |
| 34 | + void emailTextUpdatesNameEmailLine(); |
| 35 | + void matchingPassphrasesEnableButtonBox(); |
| 36 | + void mismatchedPassphrasesDisableButtonBox(); |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * @brief The default GPG batch template gets loaded into the editor on |
| 41 | + * construction, regardless of whether ed25519 is supported. |
| 42 | + */ |
| 43 | +void tst_keygendialog::constructionLoadsNonEmptyTemplate() { |
| 44 | + KeygenDialog dialog(nullptr); |
| 45 | + auto *editor = |
| 46 | + dialog.findChild<QPlainTextEdit *>(QStringLiteral("plainTextEdit")); |
| 47 | + QVERIFY2(editor != nullptr, "plainTextEdit widget must exist"); |
| 48 | + const QString tpl = editor->toPlainText(); |
| 49 | + QVERIFY2(!tpl.isEmpty(), "default key template must be loaded"); |
| 50 | + // Both the ed25519 and RSA fallback templates contain Name-Real and |
| 51 | + // Name-Email placeholders we rely on in the other tests. |
| 52 | + QVERIFY2(tpl.contains(QStringLiteral("Name-Real:")), |
| 53 | + "template must contain a Name-Real: line"); |
| 54 | + QVERIFY2(tpl.contains(QStringLiteral("Name-Email:")), |
| 55 | + "template must contain a Name-Email: line"); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @brief Toggling the "Expert" checkbox enables/disables direct editing of |
| 60 | + * the GPG batch template. |
| 61 | + */ |
| 62 | +void tst_keygendialog::expertCheckboxTogglesTemplateEditor() { |
| 63 | + KeygenDialog dialog(nullptr); |
| 64 | + auto *checkBox = dialog.findChild<QCheckBox *>(QStringLiteral("checkBox")); |
| 65 | + auto *editor = |
| 66 | + dialog.findChild<QPlainTextEdit *>(QStringLiteral("plainTextEdit")); |
| 67 | + QVERIFY2(checkBox != nullptr, "checkBox widget must exist"); |
| 68 | + QVERIFY2(editor != nullptr, "plainTextEdit widget must exist"); |
| 69 | + |
| 70 | + // Default state: checkbox unchecked, editor read-only / disabled. |
| 71 | + checkBox->setChecked(false); |
| 72 | + QVERIFY2(editor->isReadOnly(), "editor should start read-only"); |
| 73 | + QVERIFY2(!editor->isEnabled(), "editor should start disabled"); |
| 74 | + |
| 75 | + checkBox->setChecked(true); |
| 76 | + QVERIFY2(!editor->isReadOnly(), "expert mode should drop read-only"); |
| 77 | + QVERIFY2(editor->isEnabled(), "expert mode should enable editor"); |
| 78 | + |
| 79 | + checkBox->setChecked(false); |
| 80 | + QVERIFY2(editor->isReadOnly(), "unchecking expert restores read-only"); |
| 81 | + QVERIFY2(!editor->isEnabled(), "unchecking expert disables editor"); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief Typing in the Name field replaces the Name-Real: line in the |
| 86 | + * template. |
| 87 | + */ |
| 88 | +void tst_keygendialog::nameTextUpdatesNameRealLine() { |
| 89 | + KeygenDialog dialog(nullptr); |
| 90 | + auto *nameEdit = dialog.findChild<QLineEdit *>(QStringLiteral("name")); |
| 91 | + auto *editor = |
| 92 | + dialog.findChild<QPlainTextEdit *>(QStringLiteral("plainTextEdit")); |
| 93 | + QVERIFY2(nameEdit != nullptr, "name widget must exist"); |
| 94 | + QVERIFY2(editor != nullptr, "plainTextEdit widget must exist"); |
| 95 | + |
| 96 | + // The slot fires on textChanged(); setText() is enough to trigger it. |
| 97 | + nameEdit->setText(QStringLiteral("QtPass Tester")); |
| 98 | + QVERIFY2(editor->toPlainText().contains( |
| 99 | + QStringLiteral("Name-Real: QtPass Tester")), |
| 100 | + "template should reflect typed name"); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @brief Typing in the Email field replaces the Name-Email: line. |
| 105 | + */ |
| 106 | +void tst_keygendialog::emailTextUpdatesNameEmailLine() { |
| 107 | + KeygenDialog dialog(nullptr); |
| 108 | + auto *emailEdit = dialog.findChild<QLineEdit *>(QStringLiteral("email")); |
| 109 | + auto *editor = |
| 110 | + dialog.findChild<QPlainTextEdit *>(QStringLiteral("plainTextEdit")); |
| 111 | + QVERIFY2(emailEdit != nullptr, "email widget must exist"); |
| 112 | + QVERIFY2(editor != nullptr, "plainTextEdit widget must exist"); |
| 113 | + |
| 114 | + emailEdit->setText(QStringLiteral("tester@qtpass.example")); |
| 115 | + QVERIFY2(editor->toPlainText().contains( |
| 116 | + QStringLiteral("Name-Email: tester@qtpass.example")), |
| 117 | + "template should reflect typed email"); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Matching passphrases in both passphrase fields enable the |
| 122 | + * DialogButtonBox so OK can be clicked. |
| 123 | + */ |
| 124 | +void tst_keygendialog::matchingPassphrasesEnableButtonBox() { |
| 125 | + KeygenDialog dialog(nullptr); |
| 126 | + auto *pp1 = dialog.findChild<QLineEdit *>(QStringLiteral("passphrase1")); |
| 127 | + auto *pp2 = dialog.findChild<QLineEdit *>(QStringLiteral("passphrase2")); |
| 128 | + auto *buttonBox = |
| 129 | + dialog.findChild<QDialogButtonBox *>(QStringLiteral("buttonBox")); |
| 130 | + QVERIFY2(pp1 != nullptr, "passphrase1 widget must exist"); |
| 131 | + QVERIFY2(pp2 != nullptr, "passphrase2 widget must exist"); |
| 132 | + QVERIFY2(buttonBox != nullptr, "buttonBox widget must exist"); |
| 133 | + |
| 134 | + pp1->setText(QStringLiteral("testkey123")); |
| 135 | + pp2->setText(QStringLiteral("testkey123")); |
| 136 | + QVERIFY2(buttonBox->isEnabled(), "matching passphrases enable OK"); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * @brief Mismatched passphrases disable the DialogButtonBox. |
| 141 | + */ |
| 142 | +void tst_keygendialog::mismatchedPassphrasesDisableButtonBox() { |
| 143 | + KeygenDialog dialog(nullptr); |
| 144 | + auto *pp1 = dialog.findChild<QLineEdit *>(QStringLiteral("passphrase1")); |
| 145 | + auto *pp2 = dialog.findChild<QLineEdit *>(QStringLiteral("passphrase2")); |
| 146 | + auto *buttonBox = |
| 147 | + dialog.findChild<QDialogButtonBox *>(QStringLiteral("buttonBox")); |
| 148 | + QVERIFY2(pp1 != nullptr, "passphrase1 widget must exist"); |
| 149 | + QVERIFY2(pp2 != nullptr, "passphrase2 widget must exist"); |
| 150 | + QVERIFY2(buttonBox != nullptr, "buttonBox widget must exist"); |
| 151 | + |
| 152 | + pp1->setText(QStringLiteral("testkey123")); |
| 153 | + pp2->setText(QStringLiteral("testkey456")); |
| 154 | + QVERIFY2(!buttonBox->isEnabled(), "mismatched passphrases disable OK"); |
| 155 | +} |
| 156 | + |
| 157 | +QTEST_MAIN(tst_keygendialog) |
| 158 | +#include "tst_keygendialog.moc" |
0 commit comments