| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
|
| 3 |
#include "test_utils.h" |
| 4 |
#include "tests.h" |
| 5 |
|
| 6 |
void CoreTests::expireEncryptionPassword() |
| 7 |
{ |
| 8 |
#ifdef WITH_QCA_ENCRYPTION |
| 9 |
const QString tab1 = testTab(1); |
| 10 |
const QString tab2 = testTab(2); |
| 11 |
const Args args1 = Args("tab") << tab1 << "separator" << " "; |
| 12 |
const Args args2 = Args("tab") << tab2 << "separator" << " "; |
| 13 |
|
| 14 |
RUN("config" << "encrypt_tabs" << "true", "true\n"); |
| 15 |
|
| 16 |
RUN(args1 << "add" << "A1", ""); |
| 17 |
RUN(args2 << "add" << "B1", ""); |
| 18 |
|
| 19 |
// Setting an expiration should not cause any expiration yet |
| 20 |
RUN("config" << "expire_encrypted_tab_seconds" << "1", "1\n"); |
| 21 |
KEYS(clipboardBrowserId); |
| 22 |
QTest::qWait(1500); |
| 23 |
KEYS(clipboardBrowserId); |
| 24 |
|
| 25 |
RUN("config" << "expire_encrypted_tab_seconds" << "2", "2\n"); |
| 26 |
const int waitToExpireMs = 2500; |
| 27 |
KEYS(clipboardBrowserId); |
| 28 |
|
| 29 |
TEST( m_test->stopServer() ); |
| 30 |
m_test->setEnv("COPYQ_PASSWORD", ""); |
| 31 |
TEST( m_test->startServer() ); |
| 32 |
|
| 33 |
// Start expiration timer from manual password entry. |
| 34 |
RUN_MULTIPLE( |
| 35 |
[&]{ RUN("show" << tab1, ""); }, |
| 36 |
[&]{ KEYS(passwordEntryCurrentId << ":TEST123" << "ENTER"); } |
| 37 |
); |
| 38 |
|
| 39 |
RUN_MULTIPLE( |
| 40 |
[&]{ KEYS(clipboardBrowserId); }, |
| 41 |
[&]{ RUN("selectedTab", tab1 + "\n"); }, |
| 42 |
[&]{ RUN(args1 << "read" << "0", "A1"); }, |
| 43 |
[&]{ RUN(args2 << "read" << "0", "B1"); } |
| 44 |
); |
| 45 |
|
| 46 |
RUN("show" << tab2, ""); |
| 47 |
RUN("show" << tab1, ""); |
| 48 |
RUN("show" << tab2, ""); |
| 49 |
|
| 50 |
KEYS(clipboardBrowserId); |
| 51 |
QTest::qWait(waitToExpireMs); |
| 52 |
KEYS(clipboardBrowserId); |
| 53 |
|
| 54 |
RUN_MULTIPLE( |
| 55 |
[&]{ KEYS(passwordEntryCurrentId << ":TEST123" << "ENTER"); }, |
| 56 |
[&]{ RUN(args1 << "read" << "0", "A1"); } |
| 57 |
); |
| 58 |
KEYS(clipboardBrowserId); |
| 59 |
RUN("selectedTab", tab2 + "\n"); |
| 60 |
|
| 61 |
RUN("show" << tab1, ""); |
| 62 |
RUN_MULTIPLE( |
| 63 |
[&]{ RUN("selectedTab", tab1 + "\n"); }, |
| 64 |
[&]{ KEYS(clipboardBrowserId); }, |
| 65 |
[&]{ RUN(args1 << "read" << "0", "A1"); } |
| 66 |
); |
| 67 |
|
| 68 |
RUN("show" << tab2, ""); |
| 69 |
RUN(args2 << "read" << "0", "B1"); |
| 70 |
|
| 71 |
// Expire again: active tab should stay unlocked. |
| 72 |
KEYS(clipboardBrowserId); |
| 73 |
QTest::qWait(waitToExpireMs); |
| 74 |
KEYS(clipboardBrowserId); |
| 75 |
|
| 76 |
RUN("show" << tab2, ""); |
| 77 |
RUN(args2 << "read" << "0", "B1"); |
| 78 |
|
| 79 |
// Switching to the other expired tab should prompt again. |
| 80 |
RUN_MULTIPLE( |
| 81 |
[&]{ KEYS(passwordEntryCurrentId << ":TEST123" << "ENTER"); }, |
| 82 |
[&]{ RUN("show" << tab1, ""); } |
| 83 |
); |
| 84 |
RUN(args1 << "read" << "0", "A1"); |
| 85 |
KEYS(clipboardBrowserId); |
| 86 |
|
| 87 |
// Avoid asking password for a new tab (if prompted recently) |
| 88 |
const QString tab3 = testTab(3); |
| 89 |
RUN("show" << tab3, ""); |
| 90 |
|
| 91 |
KEYS(clipboardBrowserId); |
| 92 |
QTest::qWait(waitToExpireMs); |
| 93 |
KEYS(clipboardBrowserId); |
| 94 |
|
| 95 |
// Read multiple expired tabs items, wait for password prompt once |
| 96 |
RUN_MULTIPLE( |
| 97 |
[&]{ RUN(args1 << "read" << "0", "A1"); }, |
| 98 |
[&]{ RUN(args2 << "read" << "0", "B1"); }, |
| 99 |
[&]{ QTest::qWait(200); KEYS(passwordEntryCurrentId << ":TEST123" << "ENTER"); } |
| 100 |
); |
| 101 |
|
| 102 |
KEYS(clipboardBrowserId); |
| 103 |
QTest::qWait(waitToExpireMs); |
| 104 |
KEYS(clipboardBrowserId); |
| 105 |
|
| 106 |
// Expired tabs should require password, even if the configuration changed |
| 107 |
RUN("config" << "expire_encrypted_tab_seconds" << "0", "0\n"); |
| 108 |
RUN_MULTIPLE( |
| 109 |
[&]{ KEYS(passwordEntryCurrentId << ":TEST123" << "ENTER"); }, |
| 110 |
[&]{ RUN("show" << tab1, ""); } |
| 111 |
); |
| 112 |
KEYS(clipboardBrowserId); |
| 113 |
#else |
| 114 |
SKIP("Encryption support not built-in"); |
| 115 |
#endif |
| 116 |
} |
| 117 |
|
| 118 |
void CoreTests::expireEncryptionPasswordOnConfigChange() |
| 119 |
{ |
| 120 |
// Expired tabs should require password, |
| 121 |
// even if the expiration was disabled afterwards |
| 122 |
#ifdef WITH_QCA_ENCRYPTION |
| 123 |
const QString tab1 = testTab(1); |
| 124 |
const Args args1{"tab", tab1}; |
| 125 |
RUN(args1 << "add" << "A1", ""); |
| 126 |
|
| 127 |
RUN("config" << "encrypt_tabs" << "true", "true\n"); |
| 128 |
|
| 129 |
// Setting an expiration should not cause any expiration yet |
| 130 |
RUN("config" << "expire_encrypted_tab_seconds" << "1", "1\n"); |
| 131 |
KEYS(clipboardBrowserId); |
| 132 |
QTest::qWait(1500); |
| 133 |
KEYS(clipboardBrowserId); |
| 134 |
|
| 135 |
RUN("config" << "expire_encrypted_tab_seconds" << "0", "0\n"); |
| 136 |
RUN_MULTIPLE( |
| 137 |
[&]{ KEYS(passwordEntryCurrentId << ":TEST123" << "ENTER"); }, |
| 138 |
[&]{ RUN("show" << tab1, ""); } |
| 139 |
); |
| 140 |
KEYS(clipboardBrowserId); |
| 141 |
RUN(args1 << "read" << "0", "A1"); |
| 142 |
#else |
| 143 |
SKIP("Encryption support not built-in"); |
| 144 |
#endif |
| 145 |
} |