File indexing completed on 2024-04-21 05:50:40
0001 #include "kgpgencrypt.h" 0002 0003 #include "common.h" 0004 #include "../transactions/kgpgencrypt.h" 0005 #include "../transactions/kgpgdecrypt.h" 0006 0007 #include <QLatin1Char> 0008 #include <QLatin1String> 0009 #include <QSignalSpy> 0010 #include <QStringList> 0011 #include <QTest> 0012 0013 void KGpgEncryptTest::init() 0014 { 0015 QVERIFY(resetGpgConf(m_tempdir)); 0016 } 0017 0018 static void checkEncryptedText(const KGpgEncrypt *encryption) 0019 { 0020 const QStringList elines = encryption->encryptedText(); 0021 QString encryptedText = elines.join(QLatin1Char('\n')); 0022 //Check if the encrypted text has a header and footer 0023 QCOMPARE(elines.first(), QStringLiteral("-----BEGIN PGP MESSAGE-----")); 0024 QCOMPARE(elines.last(), QStringLiteral("-----END PGP MESSAGE-----")); 0025 //Test if encrypted text contains "KGpg" 0026 QVERIFY(!elines.contains(QLatin1String("KGpg"))); 0027 QVERIFY(KGpgDecrypt::isEncryptedText(encryptedText, nullptr, nullptr)); 0028 int startPos = -1; 0029 QVERIFY(KGpgDecrypt::isEncryptedText(encryptedText, &startPos, nullptr)); 0030 QCOMPARE(startPos, 0); 0031 } 0032 0033 void KGpgEncryptTest::testAsciiArmoredEncryption() 0034 { 0035 //Add keys to keyring 0036 addGpgKey(m_tempdir, QLatin1String("keys/kgpgtest_BA7695F3C550DF14_pub.asc")); 0037 QString passphrase = readFile(QLatin1String("keys/kgpgtest_BA7695F3C550DF14.pass")); 0038 addGpgKey(m_tempdir, QLatin1String("keys/kgpgtest_BA7695F3C550DF14.asc"), passphrase); 0039 0040 //Encrypt text 0041 QString text = readFile(QLatin1String("keys/sample_text")); 0042 QStringList userIds(QLatin1String("Test KGpg")); 0043 KGpgEncrypt *encryption = new KGpgEncrypt( 0044 this, userIds, text, 0045 KGpgEncrypt::AsciiArmored | KGpgEncrypt::AllowUntrustedEncryption); 0046 QObject::connect(encryption, &KGpgEncrypt::done, 0047 [](int result) { QCOMPARE(result, static_cast<int>(KGpgTransaction::TS_OK)); }); 0048 QSignalSpy spy(encryption, &KGpgEncrypt::done); 0049 encryption->start(); 0050 QVERIFY(spy.wait(encryptionTestTimeout)); 0051 0052 checkEncryptedText(encryption); 0053 if (QTest::currentTestFailed()) 0054 return; 0055 QString encryptedText = encryption->encryptedText().join(QLatin1Char('\n')); 0056 0057 //Decrypt encrypted text 0058 KGpgDecrypt *decryption = new KGpgDecrypt(this, encryptedText); 0059 QObject::connect(decryption, &KGpgDecrypt::done, 0060 [](int result) { QCOMPARE(result, static_cast<int>(KGpgTransaction::TS_OK)); }); 0061 QSignalSpy spy2(decryption, &KGpgDecrypt::done); 0062 addPasswordArguments(decryption, passphrase); 0063 decryption->start(); 0064 QVERIFY(spy2.wait(decryptionTestTimeout)); 0065 0066 //Check if decrypted text is equal to original text 0067 QVERIFY(text.compare(decryption->decryptedText().join(QLatin1Char('\n')))); 0068 } 0069 0070 void KGpgEncryptTest::testHideKeyIdEncryption() 0071 { 0072 //Add keys to keyring 0073 addGpgKey(m_tempdir, QLatin1String("keys/kgpgtest_BA7695F3C550DF14_pub.asc")); 0074 QString passphrase = readFile(QLatin1String("keys/kgpgtest_BA7695F3C550DF14.pass")); 0075 addGpgKey(m_tempdir, QLatin1String("keys/kgpgtest_BA7695F3C550DF14.asc"), passphrase); 0076 0077 //Encrypt text 0078 QString text = readFile(QLatin1String("keys/sample_text")); 0079 QStringList userIds(QLatin1String("Test KGpg")); 0080 KGpgEncrypt *encryption = 0081 new KGpgEncrypt(this, userIds, text, 0082 KGpgEncrypt::AsciiArmored | KGpgEncrypt::HideKeyId | 0083 KGpgEncrypt::AllowUntrustedEncryption); 0084 QObject::connect(encryption, &KGpgEncrypt::done, 0085 [](int result) { QCOMPARE(result, static_cast<int>(KGpgTransaction::TS_OK)); }); 0086 QSignalSpy spy(encryption, &KGpgEncrypt::done); 0087 encryption->start(); 0088 QVERIFY(spy.wait(encryptionTestTimeout)); 0089 0090 checkEncryptedText(encryption); 0091 if (QTest::currentTestFailed()) 0092 return; 0093 QString encryptedText = encryption->encryptedText().join(QLatin1Char('\n')); 0094 //Check if encrypted text contains key Id 0095 QString keyId = QLatin1String("BA7695F3C550DF14"); 0096 QString log = encryption->getMessages().join(QLatin1Char('\n')); 0097 QVERIFY(!log.contains(keyId)); 0098 0099 //Decrypt encrypted text 0100 KGpgDecrypt *decryption = new KGpgDecrypt(this, encryptedText); 0101 QObject::connect(decryption, &KGpgDecrypt::done, 0102 [](int result) { QCOMPARE(result, static_cast<int>(KGpgTransaction::TS_OK)); }); 0103 QSignalSpy spy2(decryption, &KGpgDecrypt::done); 0104 addPasswordArguments(decryption, passphrase); 0105 decryption->start(); 0106 QVERIFY(spy2.wait(decryptionTestTimeout)); 0107 0108 //Check if decrypted text is equal to original text 0109 QVERIFY(text.compare(decryption->decryptedText().join(QLatin1Char('\n')))); 0110 } 0111 0112 void KGpgEncryptTest::testSymmetricEncryption() 0113 { 0114 QString passphrase = QLatin1String("symmetric key"); 0115 QString text = readFile(QLatin1String("keys/sample_text")); 0116 QStringList userIds; 0117 0118 //Encrypt text 0119 KGpgEncrypt *encryption = new KGpgEncrypt( 0120 this, userIds, text, 0121 KGpgEncrypt::AsciiArmored | KGpgEncrypt::AllowUntrustedEncryption); 0122 QObject::connect(encryption, &KGpgEncrypt::done, 0123 [](int result) { QCOMPARE(result, static_cast<int>(KGpgTransaction::TS_OK)); }); 0124 QSignalSpy spy(encryption, &KGpgEncrypt::done); 0125 addPasswordArguments(encryption, passphrase); 0126 encryption->start(); 0127 QVERIFY(spy.wait(encryptionTestTimeout)); 0128 0129 checkEncryptedText(encryption); 0130 if (QTest::currentTestFailed()) 0131 return; 0132 QString encryptedText = encryption->encryptedText().join(QLatin1Char('\n')); 0133 0134 //Decrypt encrypted text 0135 KGpgDecrypt *decryption = new KGpgDecrypt(this, encryptedText); 0136 QObject::connect(decryption, &KGpgDecrypt::done, 0137 [](int result) { QCOMPARE(result, static_cast<int>(KGpgTransaction::TS_OK)); }); 0138 QSignalSpy spy2(decryption, &KGpgDecrypt::done); 0139 addPasswordArguments(decryption, passphrase); 0140 decryption->start(); 0141 QVERIFY(spy2.wait(decryptionTestTimeout)); 0142 0143 //Check if decrypted text is equal to original text 0144 QVERIFY(text.compare(decryption->decryptedText().join(QLatin1Char('\n')))); 0145 } 0146 0147 QTEST_GUILESS_MAIN(KGpgEncryptTest)