| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
|
| 3 |
#include "itemencryptedtests.h" |
| 4 |
|
| 5 |
#include "tests/test_utils.h" |
| 6 |
|
| 7 |
ItemEncryptedTests::ItemEncryptedTests(const TestInterfacePtr &test, QObject *parent) |
| 8 |
: QObject(parent) |
| 9 |
, m_test(test) |
| 10 |
{ |
| 11 |
setProperty("CopyQ_test_id", QStringLiteral("itemencrypted")); |
| 12 |
} |
| 13 |
|
| 14 |
void ItemEncryptedTests::initTestCase() |
| 15 |
{ |
| 16 |
SKIP_ON_ENV("COPYQ_TESTS_SKIP_ITEMENCRYPT"); |
| 17 |
|
| 18 |
TEST(m_test->initTestCase()); |
| 19 |
} |
| 20 |
|
| 21 |
void ItemEncryptedTests::cleanupTestCase() |
| 22 |
{ |
| 23 |
TEST(m_test->cleanupTestCase()); |
| 24 |
} |
| 25 |
|
| 26 |
void ItemEncryptedTests::init() |
| 27 |
{ |
| 28 |
TEST(m_test->init()); |
| 29 |
|
| 30 |
QVERIFY(isGpgInstalled()); |
| 31 |
} |
| 32 |
|
| 33 |
void ItemEncryptedTests::cleanup() |
| 34 |
{ |
| 35 |
TEST( m_test->cleanup() ); |
| 36 |
} |
| 37 |
|
| 38 |
void ItemEncryptedTests::encryptDecryptData() |
| 39 |
{ |
| 40 |
RUN("plugins.itemencrypted.generateTestKeys()", "\n"); |
| 41 |
|
| 42 |
// Test gpg errors first. |
| 43 |
RUN("plugins.itemencrypted.encrypt(input());print('')", ""); |
| 44 |
|
| 45 |
const QByteArray input("\x00\x01\x02\x03\x04", 5); |
| 46 |
QByteArray stdoutActual; |
| 47 |
|
| 48 |
// Encrypted data differs. |
| 49 |
QCOMPARE( m_test->run(Args("-e") << "plugins.itemencrypted.encrypt(input())", &stdoutActual, nullptr, input), 0 ); |
| 50 |
QVERIFY(!stdoutActual.isEmpty()); |
| 51 |
QVERIFY(stdoutActual != input); |
| 52 |
|
| 53 |
QCOMPARE( m_test->run(Args("-e") << "plugins.itemencrypted.decrypt(plugins.itemencrypted.encrypt(input()))", &stdoutActual, nullptr, input), 0 ); |
| 54 |
QCOMPARE(stdoutActual, input); |
| 55 |
} |
| 56 |
|
| 57 |
void ItemEncryptedTests::encryptDecryptItems() |
| 58 |
{ |
| 59 |
#ifdef Q_OS_MAC |
| 60 |
SKIP("Ctrl+L shortcut doesn't seem work on OS X"); |
| 61 |
#endif |
| 62 |
|
| 63 |
RUN("plugins.itemencrypted.generateTestKeys()", "\n"); |
| 64 |
|
| 65 |
// Load commands from the plugin generating keys. |
| 66 |
KEYS("Ctrl+P" << "ENTER"); |
| 67 |
RUN("commands().length", "4\n"); |
| 68 |
|
| 69 |
const auto tab = testTab(1); |
| 70 |
RUN("setCurrentTab" << tab, ""); |
| 71 |
RUN("tab" << tab << "add" << "TEST", ""); |
| 72 |
RUN("tab" << tab << "read" << "0", "TEST"); |
| 73 |
|
| 74 |
// Encrypt. |
| 75 |
KEYS("Ctrl+L"); |
| 76 |
WAIT_ON_OUTPUT("tab" << tab << "read" << "?" << "0", "application/x-copyq-encrypted\n"); |
| 77 |
|
| 78 |
// Decrypt and check text. |
| 79 |
KEYS("Ctrl+L"); |
| 80 |
WAIT_ON_OUTPUT("tab" << tab << "read" << "?" << "0", "text/plain\n"); |
| 81 |
RUN("tab" << tab << "read" << "0", "TEST"); |
| 82 |
|
| 83 |
// Encrypt and add note. |
| 84 |
KEYS("Ctrl+L"); |
| 85 |
WAIT_ON_OUTPUT("tab" << tab << "read" << "?" << "0", "application/x-copyq-encrypted\n"); |
| 86 |
KEYS("Shift+F2" << ":NOTE" << "F2"); |
| 87 |
RUN("tab" << tab << "read" << "application/x-copyq-item-notes" << "0", "NOTE"); |
| 88 |
|
| 89 |
// Decrypt and check note and text. |
| 90 |
KEYS("Ctrl+L"); |
| 91 |
WAIT_ON_OUTPUT("tab" << tab << "read" << "0", "TEST"); |
| 92 |
RUN("tab" << tab << "read" << "application/x-copyq-item-notes" << "0", "NOTE"); |
| 93 |
} |
| 94 |
|
| 95 |
bool ItemEncryptedTests::isGpgInstalled() const |
| 96 |
{ |
| 97 |
QByteArray actualStdout; |
| 98 |
const auto exitCode = m_test->run(Args("-e") << "plugins.itemencrypted.isGpgInstalled()", &actualStdout); |
| 99 |
Q_ASSERT(exitCode == 0); |
| 100 |
Q_ASSERT(actualStdout == "true\n" || actualStdout == "false\n"); |
| 101 |
return actualStdout == "true\n"; |
| 102 |
} |