66#include < QFileInfo>
77#include < QMessageBox>
88#include < QListWidget>
9+ #include < QComboBox>
910#include " mainwindow.h"
1011#include < QTimer>
1112#include < QWindow>
@@ -15,16 +16,37 @@ class TestOpenCryptUI : public QObject
1516 Q_OBJECT
1617
1718private slots:
19+ void initTestCase ();
1820 void testEncryptDecrypt ();
1921 void testEncryptDecryptWithKeyfile ();
22+ void testAllCiphersAndKDFs ();
23+ void cleanupTestCase ();
2024 void closeMessageBoxes ();
2125
2226private:
2327 QTimer *messageBoxTimer;
28+ MainWindow *mainWindow;
2429 QString createTestFile (const QString &content);
2530 QString createKeyfile (const QString &content);
31+ bool encryptAndDecrypt (const QString &cipher, const QString &kdf, bool useKeyfile);
2632};
2733
34+ void TestOpenCryptUI::initTestCase ()
35+ {
36+ mainWindow = new MainWindow ();
37+ mainWindow->show ();
38+
39+ messageBoxTimer = new QTimer (this );
40+ connect (messageBoxTimer, &QTimer::timeout, this , &TestOpenCryptUI::closeMessageBoxes);
41+ messageBoxTimer->start (1000 );
42+ }
43+
44+ void TestOpenCryptUI::cleanupTestCase ()
45+ {
46+ messageBoxTimer->stop ();
47+ delete mainWindow;
48+ }
49+
2850QString TestOpenCryptUI::createTestFile (const QString &content)
2951{
3052 QString testFilePath = QDir::currentPath () + " /test.txt" ;
@@ -57,13 +79,10 @@ QString TestOpenCryptUI::createKeyfile(const QString &content)
5779
5880void TestOpenCryptUI::testEncryptDecrypt ()
5981{
60- MainWindow mainWindow;
61- mainWindow.show ();
62-
63- QLineEdit *filePathInput = mainWindow.findChild <QLineEdit*>(" filePathLineEdit" );
64- QLineEdit *passwordInput = mainWindow.findChild <QLineEdit*>(" filePasswordLineEdit" );
65- QPushButton *encryptButton = mainWindow.findChild <QPushButton*>(" fileEncryptButton" );
66- QPushButton *decryptButton = mainWindow.findChild <QPushButton*>(" fileDecryptButton" );
82+ QLineEdit *filePathInput = mainWindow->findChild <QLineEdit*>(" filePathLineEdit" );
83+ QLineEdit *passwordInput = mainWindow->findChild <QLineEdit*>(" filePasswordLineEdit" );
84+ QPushButton *encryptButton = mainWindow->findChild <QPushButton*>(" fileEncryptButton" );
85+ QPushButton *decryptButton = mainWindow->findChild <QPushButton*>(" fileDecryptButton" );
6786
6887 QVERIFY (filePathInput);
6988 QVERIFY (passwordInput);
@@ -82,14 +101,8 @@ void TestOpenCryptUI::testEncryptDecrypt()
82101 out << " test" ;
83102 testFile.close ();
84103
85- // Log the file creation
86104 qDebug () << " Test file created with content 'test' at" << testFilePath;
87105
88- // Set up for message box handling
89- messageBoxTimer = new QTimer (this );
90- connect (messageBoxTimer, &QTimer::timeout, this , &TestOpenCryptUI::closeMessageBoxes);
91- messageBoxTimer->start (1000 );
92-
93106 filePathInput->setText (testFilePath);
94107 passwordInput->setText (" testpassword" );
95108 QTest::mouseClick (encryptButton, Qt::LeftButton);
@@ -98,7 +111,6 @@ void TestOpenCryptUI::testEncryptDecrypt()
98111
99112 QVERIFY (QFileInfo::exists (encryptedFilePath));
100113
101- // Log encrypted file content
102114 QFile encryptedFile (encryptedFilePath);
103115 QVERIFY (encryptedFile.open (QIODevice::ReadOnly));
104116 QByteArray encryptedContent = encryptedFile.readAll ();
@@ -113,7 +125,6 @@ void TestOpenCryptUI::testEncryptDecrypt()
113125
114126 QVERIFY (QFileInfo::exists (testFilePath));
115127
116- // Log decrypted file content
117128 QFile decryptedFile (testFilePath);
118129 QVERIFY (decryptedFile.open (QIODevice::ReadOnly | QIODevice::Text));
119130 QTextStream in (&decryptedFile);
@@ -123,20 +134,15 @@ void TestOpenCryptUI::testEncryptDecrypt()
123134
124135 QFile::remove (testFilePath);
125136 QFile::remove (encryptedFilePath);
126-
127- messageBoxTimer->stop ();
128137}
129138
130139void TestOpenCryptUI::testEncryptDecryptWithKeyfile ()
131140{
132- MainWindow mainWindow;
133- mainWindow.show ();
134-
135- QLineEdit *filePathInput = mainWindow.findChild <QLineEdit*>(" filePathLineEdit" );
136- QLineEdit *passwordInput = mainWindow.findChild <QLineEdit*>(" filePasswordLineEdit" );
137- QPushButton *encryptButton = mainWindow.findChild <QPushButton*>(" fileEncryptButton" );
138- QPushButton *decryptButton = mainWindow.findChild <QPushButton*>(" fileDecryptButton" );
139- QListWidget *keyfileListWidget = mainWindow.findChild <QListWidget*>(" fileKeyfileListWidget" );
141+ QLineEdit *filePathInput = mainWindow->findChild <QLineEdit*>(" filePathLineEdit" );
142+ QLineEdit *passwordInput = mainWindow->findChild <QLineEdit*>(" filePasswordLineEdit" );
143+ QPushButton *encryptButton = mainWindow->findChild <QPushButton*>(" fileEncryptButton" );
144+ QPushButton *decryptButton = mainWindow->findChild <QPushButton*>(" fileDecryptButton" );
145+ QListWidget *keyfileListWidget = mainWindow->findChild <QListWidget*>(" fileKeyfileListWidget" );
140146
141147 QVERIFY (filePathInput);
142148 QVERIFY (passwordInput);
@@ -148,11 +154,6 @@ void TestOpenCryptUI::testEncryptDecryptWithKeyfile()
148154 QString encryptedFilePath = testFilePath + " .enc" ;
149155 QString keyfilePath = createKeyfile (" secret key content" );
150156
151- // Set up for message box handling
152- messageBoxTimer = new QTimer (this );
153- connect (messageBoxTimer, &QTimer::timeout, this , &TestOpenCryptUI::closeMessageBoxes);
154- messageBoxTimer->start (1000 );
155-
156157 // Encryption with keyfile
157158 filePathInput->setText (testFilePath);
158159 passwordInput->setText (" testpassword" );
@@ -161,7 +162,6 @@ void TestOpenCryptUI::testEncryptDecryptWithKeyfile()
161162
162163 QTRY_VERIFY_WITH_TIMEOUT (QFileInfo::exists (encryptedFilePath), 15000 );
163164
164- // Log encrypted file content
165165 QFile encryptedFile (encryptedFilePath);
166166 QVERIFY (encryptedFile.open (QIODevice::ReadOnly));
167167 QByteArray encryptedContent = encryptedFile.readAll ();
@@ -171,25 +171,137 @@ void TestOpenCryptUI::testEncryptDecryptWithKeyfile()
171171 // Decryption with keyfile
172172 filePathInput->setText (encryptedFilePath);
173173 passwordInput->setText (" testpassword" );
174- // Keyfile should still be in the list
175174 QTest::mouseClick (decryptButton, Qt::LeftButton);
176175
177176 QTRY_VERIFY_WITH_TIMEOUT (QFileInfo::exists (testFilePath), 15000 );
178177
179- // Verify decrypted content
180178 QFile decryptedFile (testFilePath);
181179 QVERIFY (decryptedFile.open (QIODevice::ReadOnly | QIODevice::Text));
182180 QTextStream in (&decryptedFile);
183181 QString content = in.readAll ().trimmed ();
184182 qDebug () << " Decrypted file content:" << content;
185183 QCOMPARE (content, QString (" test with keyfile" ));
186184
187- // Clean up
188185 QFile::remove (testFilePath);
189186 QFile::remove (encryptedFilePath);
190187 QFile::remove (keyfilePath);
188+ }
191189
192- messageBoxTimer->stop ();
190+ bool TestOpenCryptUI::encryptAndDecrypt (const QString &cipher, const QString &kdf, bool useKeyfile)
191+ {
192+ qDebug () << " Starting encryptAndDecrypt test for" << cipher << " with" << kdf << (useKeyfile ? " and keyfile" : " " );
193+
194+ QLineEdit *filePathInput = mainWindow->findChild <QLineEdit*>(" filePathLineEdit" );
195+ QLineEdit *passwordInput = mainWindow->findChild <QLineEdit*>(" filePasswordLineEdit" );
196+ QPushButton *encryptButton = mainWindow->findChild <QPushButton*>(" fileEncryptButton" );
197+ QPushButton *decryptButton = mainWindow->findChild <QPushButton*>(" fileDecryptButton" );
198+ QComboBox *algorithmComboBox = mainWindow->findChild <QComboBox*>(" fileAlgorithmComboBox" );
199+ QComboBox *kdfComboBox = mainWindow->findChild <QComboBox*>(" kdfComboBox" );
200+ QListWidget *keyfileListWidget = mainWindow->findChild <QListWidget*>(" fileKeyfileListWidget" );
201+
202+ if (!filePathInput || !passwordInput || !encryptButton || !decryptButton ||
203+ !algorithmComboBox || !kdfComboBox || !keyfileListWidget) {
204+ qDebug () << " Failed to find all required UI elements" ;
205+ return false ;
206+ }
207+
208+ QString testContent = " Test content for " + cipher + " with " + kdf;
209+ QString testFilePath = createTestFile (testContent);
210+ if (testFilePath.isEmpty ()) {
211+ qDebug () << " Failed to create test file" ;
212+ return false ;
213+ }
214+
215+ QString encryptedFilePath = testFilePath + " .enc" ;
216+ QString keyfilePath;
217+
218+ if (useKeyfile) {
219+ keyfilePath = createKeyfile (" Secret key for " + cipher);
220+ if (keyfilePath.isEmpty ()) {
221+ qDebug () << " Failed to create keyfile" ;
222+ return false ;
223+ }
224+ keyfileListWidget->addItem (keyfilePath);
225+ qDebug () << " Keyfile created and added to list widget" ;
226+ } else {
227+ keyfileListWidget->clear ();
228+ qDebug () << " Keyfile list widget cleared" ;
229+ }
230+
231+ // Set up encryption parameters
232+ filePathInput->setText (testFilePath);
233+ passwordInput->setText (" testpassword" );
234+ algorithmComboBox->setCurrentText (cipher);
235+ kdfComboBox->setCurrentText (kdf);
236+ qDebug () << " Encryption parameters set up" ;
237+
238+ // Encrypt
239+ qDebug () << " Clicking encrypt button" ;
240+ QTest::mouseClick (encryptButton, Qt::LeftButton);
241+
242+ if (!QTest::qWaitFor ([&]() { return QFileInfo::exists (encryptedFilePath); }, 15000 )) {
243+ qDebug () << " Encryption failed or timed out for" << cipher << " with" << kdf;
244+ return false ;
245+ }
246+ qDebug () << " Encrypted file created:" << encryptedFilePath;
247+
248+ // Set up decryption parameters
249+ filePathInput->setText (encryptedFilePath);
250+ passwordInput->setText (" testpassword" );
251+ qDebug () << " Decryption parameters set up" ;
252+
253+ // Decrypt
254+ qDebug () << " Clicking decrypt button" ;
255+ QTest::mouseClick (decryptButton, Qt::LeftButton);
256+
257+ if (!QTest::qWaitFor ([&]() { return QFileInfo::exists (testFilePath); }, 15000 )) {
258+ qDebug () << " Decryption failed or timed out for" << cipher << " with" << kdf;
259+ return false ;
260+ }
261+ qDebug () << " Decrypted file created:" << testFilePath;
262+
263+ // Verify decrypted content
264+ QFile decryptedFile (testFilePath);
265+ if (!decryptedFile.open (QIODevice::ReadOnly | QIODevice::Text)) {
266+ qDebug () << " Failed to open decrypted file" ;
267+ return false ;
268+ }
269+ QTextStream in (&decryptedFile);
270+ QString decryptedContent = in.readAll ().trimmed ();
271+ decryptedFile.close ();
272+
273+ if (decryptedContent != testContent) {
274+ qDebug () << " Decrypted content does not match original for" << cipher << " with" << kdf;
275+ qDebug () << " Expected:" << testContent;
276+ qDebug () << " Actual:" << decryptedContent;
277+ return false ;
278+ }
279+ qDebug () << " Decrypted content matches original" ;
280+
281+ qDebug () << " Test for" << cipher << " with" << kdf << (useKeyfile ? " and keyfile" : " " ) << " PASSED" ;
282+
283+ // Clean up
284+ QFile::remove (testFilePath);
285+ QFile::remove (encryptedFilePath);
286+ if (useKeyfile) {
287+ QFile::remove (keyfilePath);
288+ }
289+ qDebug () << " Test files cleaned up" ;
290+
291+ return true ;
292+ }
293+
294+ void TestOpenCryptUI::testAllCiphersAndKDFs ()
295+ {
296+ QStringList ciphers = {" AES-256-GCM" , " ChaCha20-Poly1305" , " AES-256-CTR" , " AES-256-CBC" };
297+ QStringList kdfs = {" Argon2" , " Scrypt" , " PBKDF2" };
298+
299+ for (const QString &cipher : ciphers) {
300+ for (const QString &kdf : kdfs) {
301+ QVERIFY2 (encryptAndDecrypt (cipher, kdf, false ), qPrintable (QString (" Failed for %1 with %2 without keyfile" ).arg (cipher, kdf)));
302+ QVERIFY2 (encryptAndDecrypt (cipher, kdf, true ), qPrintable (QString (" Failed for %1 with %2 with keyfile" ).arg (cipher, kdf)));
303+ }
304+ }
193305}
194306
195307void TestOpenCryptUI::closeMessageBoxes ()
0 commit comments